本功能结合一个大佬的功能,进行修改,此本也感谢大佬指点;
import config from "./config.js"
var UpdateVersion = function (){
this.version = config.version
this.updateApi =config.download
this.runEnv=config.runEnv
}
UpdateVersion.prototype = {
//版本号对比, curV 大于 reqV返回true
compare: function(curV, reqV){
if(curV && reqV){
//将两个版本号拆成数字
var arr1 = curV.split('.'),
arr2 = reqV.split('.');
var minLength = Math.min(arr1.length, arr2.length),
position = 0,
diff = 0;
//依次比较版本号每一位大小,当对比得出结果后跳出循环
while(position < minLength && ((diff=parseInt(arr1[position]) - parseInt(arr2[position])) == 0)){
position++;
}
diff = (diff!=0) ? diff : (arr1.length-arr2.length);
return diff > 0;
}else{
console.log('版本号不能为空');
return false;
}
},
//热更新检测
getNewData: function(type){
var _this = this // plus.runtime.appid获取版本号的;
if(plus && plus.runtime.appid == 'HBuilder' || true){
uni.request({
url: _this.updateApi+"/version.json",
method: 'GET',
success: (resR)=>{
var res = resR.data
if(_this.runEnv!==res.runEnv){
plus.nativeUI.toast("环境错误:当前APP环境为开发环境,无法更新")
}else{
if(_this.compare(res.version, _this.version)){
getApp().globalData.ifUpdate = true
//弹出更新提示
//version 最新版的版本号
//url 最新版的热更新地址
//force_update 是否强制更新
//runEnv 版本类型
var downUrl =_this.updateApi+"/"+ res.url
if(!/\.wgt$/.test(res.url)){
_this.alertUpdateTips(downUrl, res.force_update,true)
}else{
_this.alertUpdateTips(downUrl, res.force_update)
}
}else{
if(type){
plus.nativeUI.toast("当前已经是最新版本")
}
getApp().globalData.ifUpdate = false
}
}
}
})
}else{
console.log('当前环境不检测版本')
if(type){
uni.showToast({
title: "当前已经是最新版本",
icon: 'none'
})
}
}
},
//弹出更新提示
alertUpdateTips: function(url, forced_update, isDownloadPack){
var _this = this
if(forced_update == '1'){
plus.nativeUI.alert('已检测到新版本,该版本有重大更新,请立即更新!', function(){
isDownloadPack ? (_this.updatePackage(url)) : (_this.downloadPack(url))
}, "更新提示", "立即更新");
}else{
plus.nativeUI.confirm("!已检测到新版本,是否立即更新!", function(e){
if(e.index == 0){
//下载安装包
isDownloadPack ? (_this.updatePackage(url)) : (_this.downloadPack(url))
}
}, "更新提示", ["确定", "取消"])
}
},
//下载安装包
downloadPack: function(url){
var _this = this
var w = plus.nativeUI.showWaiting('下载更新中,请勿关闭');
var totalSize = 0
var dtask = plus.downloader.createDownload(url, { method: 'GET', filename: '_downloads/'},function(d,status){
plus.nativeUI.closeWaiting();
if(status == 200){
_this.installPack(d.filename);
}else{//下载失败
plus.nativeUI.closeWaiting();
plus.nativeUI.toast('下载失败');
}
});
dtask.addEventListener("statechanged", function(download, status){
if(download.state == 2){
totalSize = download.totalSize
totalSize = Math.round(parseInt(totalSize) / (1024 * 1024 / 100)) / 100
}
if(download.state == 3 && download.totalSize){
//已下载 download.downloadedSize
//任务大小 download.totalSize
totalSize = download.totalSize
totalSize = Math.round(parseInt(totalSize) / (1024 * 1024 / 100)) / 100
var ds = Math.round(parseInt(download.downloadedSize) / (1024 * 1024 / 100)) / 100
if(ds && totalSize){
w.setTitle(`更新进度:${ds}M/${totalSize}M`);
}
}
}, false);
dtask.start();
},
//安装
installPack: function(path){
var _this = this
plus.nativeUI.showWaiting('安装更新文件中...');
plus.runtime.install(path,{},function(){
plus.nativeUI.closeWaiting();
_this.tipReload()
},function(e){
plus.nativeUI.closeWaiting();
plus.nativeUI.toast( '安装失败')
});
},
//提示重启
tipReload: function(){
if(plus.os.name == 'Android'){
plus.nativeUI.alert('最新版安装完成,请立即重启', function(e){
if(e.index == 0){
plus.runtime.restart();
}
}, '重启提示', '立即重启')
}else{
plus.nativeUI.alert('更新成功,重启APP后即可使用最新版!');
}
},
//整包更新
updatePackage (url){
getApp().globalData.ifUpdate = false
if(/\.apk$/.test(url)){
this.downloadPack(url)
}else{
plus.runtime.openURL(url)
}
}
}
export default UpdateVersion
/*
import UpdateVersion from './utils/update.js'
//#ifdef APP-PLUS
var v = new UpdateVersion()
v.getNewData()
//#endif
*/
main.js
import Vue from 'vue'
import App from './App'
import UpdateVersion from "base/update.js"
//#ifdef APP-PLUS
const v = new UpdateVersion()
v.getNewData()
//#endif
App.mpType = 'app'
const app = new Vue({
...App
})
app.$mount()