1、app.vue: app加載時候,獲取app版本號
//獲取版本號 plus.runtime.getProperty(plus.runtime.appid,(wgtinfo)=>{ uni.setStorage({ key: 'wgtinfoVersionCode', data: wgtinfo.versionCode }) })
2、index:首頁頁面加載時,獲取后端返回的app版本號
getSysVersion() { const _this = this;
//獲取緩存中,當前app的版本號 uni.getStorage({ key: 'wgtinfoVersionCode', success: (res) => { this.wgtinfoVersionCode = res.data; uni.request({ url: this.$url + '/sysNotice/querySysVersion', //后端接口 method: 'post', sslVerify: false,//不驗證ssl證書 success: res => { console.log(res.data.data, this.wgtinfoVersionCode); if(res.statusCode == 200) {
//后端返回的app版本和當前app版本比較 if(Number(res.data.data) != Number(this.wgtinfoVersionCode)) { // this.showModal = true; uni.showModal({ title: "發現新版本", content: "確認下載更新", success: (res) => { if (res.confirm == true) {//當用戶確定更新,執行更新 _this.appdownLoad(); } } }) } } }, fail: err => { console.log(err) this.$api.msg('請求錯誤!'); this.logining = false; }, }); } }) },
3、執行下載app:
appdownLoad(){ var that = this; uni.showLoading({ title: '安裝包下載中……' }) const downloadTask = uni.downloadFile({ url: 'http://XXXXXXXXXXXX.apk', //服務器 app 的下載地址 success: (downloadResult) => { uni.hideLoading(); if (downloadResult.statusCode === 200) { uni.showModal({ title: '', content: '下載成功,是否允許安裝新版本?', confirmText: '確定', success: function(res) { if (res.confirm == true) { plus.runtime.install(//安裝 downloadResult.tempFilePath, { force: true }, function(res) { utils.showToast('更新成功,重啟中'); plus.runtime.restart(); } ); } } }); } } }); },
附圖: