主要是拿本地的版本號(hào)和后端返回的版本號(hào)做對(duì)比(在每次修改完代碼以后蕉堰,要打個(gè)更新包或者整包給后端喔)
<template>
<view>
<sxPopup ref="appUpdate" @closeMaks="closeMaks">
<view class="update-main">
<image src="/static/images/update_close.png" class="update-close" @tap="hide"></image>
<view class="update-content">
最新安裝包已準(zhǔn)備就緒恭請(qǐng)主人體驗(yàn)嘗
</view>
<view class="update-btn btn-solid" @tap="updateApp">
立即體驗(yàn)
</view>
</view>
</sxPopup>
</view>
</template>
在data中定義
version:"", // 當(dāng)前版本
serverVersion:"", // 服務(wù)器版本
clientType:'', // 客戶端類型
updateInfo:{}, // 更新信息
curDownSize:0, // 當(dāng)前下載大小
在props中
autoChangeUpdate:{ // 是否自動(dòng)檢測(cè)更新
type:Boolean,
default:false
}
mounted() {
_this = this;
this.changeUpdate()
},
在methods中
show(){
//顯示更新彈窗
this.$refs.appUpdate.open()
},
hide(){
//隱藏更新彈窗
uni.showTabBar();
this.$refs.appUpdate.close()
},
changeUpdate(){ // 檢測(cè)更新
//如果父組件選擇自動(dòng)更新
if(this.autoChangeUpdate){
this.getServerVersion()
}
},
isUpdate(curVersion,serVersion){ // 判斷是否需要更新
let [serArray,curArray] = [serVersion.split("."),curVersion.split(".")]
if(parseInt(serArray[0]) > parseInt(curArray[0])){
return true
}else if(parseInt(serArray[1]) > parseInt(curArray[1])){
return true
}else if(parseInt(serArray[1]) >= parseInt(curArray[1]) && parseInt(serArray[2]) > parseInt(curArray[2])){
return true
}else{
return false
}
},
computedVersion(version){ // 計(jì)算版本
let array = version.split(".");
let sum = 0;
sum += array[0]*10
sum += array[1]+array[2]
return sum;
},
getServerVersion(){ // 獲取服務(wù)器版本
if(!this.autoChangeUpdate){
uni.showLoading({
title:"檢測(cè)更新"
})
}
this.$ajax("index/apiUpdateVersion",{}).then(res=>{
uni.hideLoading()
if(res.code == 200){
this.updateInfo = res.data
plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
// 獲取當(dāng)前app版本
_this.version = wgtinfo.version;
console.log(uni.getSystemInfoSync().platform)
// 判斷手機(jī)類型
switch (uni.getSystemInfoSync().platform) {
case 'android':
if (_this.isUpdate(_this.version,res.data.android_version)) {
_this.serverVersion = res.data.android_version;
_this.clientType = 'android'
_this.show()
uni.hideTabBar();
}else{
if(!_this.autoChangeUpdate){
_this.$toast("當(dāng)前已是最新版本")
}
}
break;
case 'ios':
if (_this.isUpdate(_this.version,res.data.ios_version)) {
_this.serverVersion = res.data.ios_version;
_this.clientType = 'ios'
_this.show()
uni.hideTabBar();
}else{
if(!_this.autoChangeUpdate){
_this.$toast("當(dāng)前已是最新版本")
}
}
break;
}
// _this.$emit('isUpdata',false)
});
}else{
this.$toast(res.data.msg)
}
})
},
updateApp(){ // 更新app
let version = this.version.split('.');
let updateVersion = this.serverVersion.split('.');
this.hide()
// 大更新
if (parseInt(updateVersion[0]) > parseInt(version[0])) {
if(_this.clientType == 'android'){
console.log(_this.clientType)
console.log(this.updateInfo.android_url)
this.downApp(this.updateInfo.android_url,'bigUpdate');
}else{
plus.runtime.openURL(this.updateInfo.ios_url)
}
} else if (parseInt(updateVersion.join('.').replace(/\./g, '')) > parseInt(version.join('.').replace(/\./g, ''))) { //小更新
// 熱更新
this.downApp(this.updateInfo.hot_update,'thermalRenewal');
}
},
downApp(url,updateType){
// 文件大小
let fileSize = '';
if(updateType == 'bigUpdate'){
fileSize = this.updateInfo.android_url_file_size;
}else{
fileSize = this.updateInfo.hot_update_file_size;
}
fileSize = parseFloat(fileSize) * 1024 * 1024
console.log(fileSize)
let waiting = plus.nativeUI.showWaiting('下載中...');
// 創(chuàng)建下載任務(wù)
let dtask = plus.downloader.createDownload(url,{
filename: '_doc/update/' // 文件下載保存路徑
},function(d, status) {
console.log(status,5555)
if (status == 200) {
console.log(status,'.............')
waiting.setTitle('安裝中...');
// 下載成功
plus.runtime.install(d.filename, {}, function() {
plus.nativeUI.closeWaiting();
plus.nativeUI.alert('更新完成饮睬!', function() {
uni.showTabBar();
// 清除所有下載的包
plus.downloader.clear(-1);
// 熱更新自動(dòng)安裝
if(updateType == 'thermalRenewal'){
plus.runtime.restart();
}
});
}, function(e) {
plus.nativeUI.closeWaiting();
plus.nativeUI.alert('安裝失敗[' + e.code + ']:' + e.message);
});
} else {
//下載失敗
plus.nativeUI.alert('下載失敗!');
setTimeout(()=>{
plus.nativeUI.closeWaiting();
},1000)
if(updateType == 'bigUpdate'){
plus.runtime.openURL(url) //打開(kāi)網(wǎng)頁(yè)手動(dòng)下載
}
}
})
// 監(jiān)聽(tīng)下載狀態(tài)
// dtask.addEventListener("statechanged", function(download, status) {
// // console.log(`監(jiān)聽(tīng):下載狀態(tài)${status}`)
// if (status == 200) {
// let i = download.downloadedSize
// i *= 100 / fileSize;
// console.log(`監(jiān)聽(tīng):下載大小${i}`)
// if(!isNaN(i)){
// i = parseInt(i)
// waiting.setTitle('已下載 ' + i + "%");
// // waiting.setTitle(i);
// }
// }else if(status == 404){
// plus.nativeUI.closeWaiting();
// plus.runtime.toast("下載地址錯(cuò)誤")
// }
// }, true);
dtask.start();
}