代碼
//#ifdef APP-PLUS
// 版本升級 this.curVersion 定義到data里面
checkUpdate(){
// 獲取manifest.json 的信息 版本信息
plus.runtime.getProperty( plus.runtime.appid, ( wgtinfo ) => {
this.curVersion = wgtinfo.version; //應(yīng)用版本名稱
this.curVersionCode = wgtinfo.versionCode; //應(yīng)用版本號
console.log( "this.curVersion:" + this.curVersion );
} );
// 更新版本接口
var server = "接口";
var that = this;
uni.request({
url: server, //請求更新地址
data: that.curVersion,
success(res) {
if (res.statusCode == 200 && that.curVersion < res.data.version) {
uni.showModal({
title: '版本更新' + res.data.version,
content: res.data.content,
confirmText: "更新",
success: function(e) {
if (e.confirm) {
if (plus.os.name.toLowerCase() == 'ios') {
// 跳轉(zhuǎn)到下載頁面
plus.runtime.openURL(res.data.apk)
} else {
// res.data.downloadUrl返回的安裝包
// url 最新的apk == res.data.apk接口返回的數(shù)據(jù)
const url = res.data.apk
// 新建下載任務(wù)
var dtask = plus.downloader.createDownload(url, {},function(d,status){
if(status == 200){ //下載完成
plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), {},
function(success) {
uni.showToast({
title: '安裝成功',
mask: false,
duration: 1500
});
}, function(error) {
uni.showToast({
title: '安裝失敗',
mask: false,
duration: 1500
});
})
}else{
uni.showToast({
title: '更新失敗',
mask: false,
duration: 1500
});
}
})
// 開始下載
dtask.start()
}
} else {
//取消
uni.showToast({
title: '請求失敗',
mask: false,
duration: 1500
});
}
}
});
} else {
uni.showModal({
title: '提示',
content: '已是最新版本',
showCancel: false
});
}
},
})
},
補(bǔ)充
應(yīng)用版本名稱和版本號的位置

image.png