需要后端返回一个接口,接口里有最新版本号和下载地址,通过对比当前版本号和最新版本号来控制下载和安装最新版本
//获取当前版本
AndroidCheckUpdate() {const systemInfo = uni.getSystemInfoSync();this.version = Number(systemInfo.appVersion.split('.').join(''))home.getVersionNew({}).then(res => {console.log(res,'res')var versions = Number(res.data.version.split('.').join(''))||Number(version)if(versions>this.version){uni.showModal({title:'版本提示',content:'检查到当前版本不是最新版本,是否更新为最新版本?',success:(e)=> {if(e.confirm){console.log(e)this.pullapk(res.data.fileUrl)//下载更新}}})}}).catch(e => {uni.showToast({title:e.msg,icon:'none'})return})},
pullapk(url){console.log(url)uni.showLoading({ title: "下载中" });uni.downloadFile({url: url, //下载地址success: (res) => {console.log(res, "下载成功");if (res.statusCode === 200) {console.log("下载成功");uni.hideLoading();uni.showToast({ title: "下载成功", icon: "success" });uni.saveFile({tempFilePath: res.tempFilePath,success: function (res) {uni.openDocument({filePath: res.savedFilePath,success: function (res) {console.log(res, "打开安装包");},});},fail: (err) => {console.log(err, "打开安装包-失败");},});}},fail(e) {uni.hideLoading();uni.showToast({ title: "下载失败,请检查网络", icon: "none" });console.log(e)},})
},