思路:
app:后端给个接口前端,前端传版本号给后端,后端返回一个是否升级状态以及安装包的地址。
pc端:一个列表接口(查看所有版本的),还有输入版本号、上传apk保存的接口。
//可以在登录后进行判断
<u-modal v-model="showUpdate" title="检测到有新版本" :content="content" :async-close="true" @confirm="confirm"
confirm-text="开始下载">
<view v-if="percent !== 0">
<u-line-progress :striped="true" :percent="percent" :striped-active="true"
type="primary"></u-line-progress>
</view>
</u-modal>
export default {
data() {
return {
appInformation: {
version: ''//保存版本号
},
appAddress: '',//保存后端返回的地址
showUpdate: false,//是否更新
content: null,
percent: 0,//显示进度条
}
},
onLoad: function () {
this.updateApp()
},
methods: {
updateApp() {
plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
// console.log(JSON.stringify(wgtinfo)); //客户端详细数据
// console.log(wgtinfo.version); //应用版本号
this.appInformation.version = wgtinfo.version
this.$http.post("接口", this.appInformation).then(res => {
console.log('返回是否升级版本', res.data);
if (res.data.code == 200) {
this.appAddress = res.data.message//这里是下载apk的整个路径
if (res.data.message !== '已经是最新版本') {
this.content = '请升级app到最新版本!'
this.showUpdate = true
}
}
})
})
},
//点击确定,下载安装包
confirm() {
this.content = '下载APP版本中,请勿关闭App!'
const downloadTask = uni.downloadFile({
// 下载资源包
url: this.appAddress,
success: (downloadResult) => {
console.log('下载资源包', downloadResult)
if (downloadResult.statusCode === 200) {
plus.runtime.install(
downloadResult.tempFilePath,
{
// 安装资源包
force: false
},
function () {
plus.runtime.restart() // 重启APP
},
function (e) { }
)
}
}
})
downloadTask.onProgressUpdate((res) => {
const { progress } = res
this.percent = progress//这里是进度条
})
},
}
}
pc 端页面,后端做逻辑处理