Uniapp app在线升级版本
提示:本文章主要实现了检测当前版本是否为最新版本,弹窗可选择更新版本
|
一、判断版本是否是最新版
1.通过获取线上的版本号和app的版本号进行对比 查看是不是最新版 — app版本号小于线上版本号则不是最新版 提示更新
二、实现步骤
1.线上新建文件 smartAppversion.json
用于存储版本号和更新网址
代码如下(示例):
{
"code": 0,
"msg": "success",
"version": "153", // 线上版本号
"url": "http://172.24.241.145:8080/download/hunan.wgt" // 更新下载地址
}
2. 检测是否最新版本
代码如下(示例):
<view class="user-info" >
<view class="uni-user" @click="handleCheckVersion">
<view class="uni-user-title">检测更新</view>
</view>
</view>
data() {
return {
innerVer: null,
version: null,
isCheckVersion: false
}
}
handleCheckVersion() {
let _this = this;
// #ifdef APP-PLUS
_this.isCheckVersion = true;
plus.screen.lockOrientation('portrait-primary') // 竖屏锁定
plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
_this.innerVer = widgetInfo.version;
_this.version = widgetInfo.versionCode;
uni.request({
url: 'http://172.24.241.145:8080/download/smartAppversion.json?_t='+ new Date().getTime(), //版本检测
data: {},
header: {},
success: (result) => {
if (result.data.code === 0) {
setTimeout(() => {
_this.isCheckVersion = false;
}, 1000);
if (result.data.version - _this.version > 0) { // 如果最新版本大于现在已经安装的App的版本
uni.showModal({
title: "更新提示",
content: "发现新版本,请确认下载更新?",
success: (res) => {
if (res.confirm) {
uni.showLoading({
title: '下载更新包中...'
});
uni.downloadFile({
url: result.data.url,
success: (downloadResult) => {
uni.hideLoading();
if (downloadResult.statusCode === 200) {
plus.runtime.install(downloadResult.tempFilePath, {
force: true
}, function() {
console.log('App安装成功!');
uni.showModal({
title: 'App安装成功!',
showCancel: false
})
plus.runtime.restart();
}, function(e) {
console.log('App安装失败!');
})
}
}
});
}
}
})
} else {
uni.showModal({
title: '当前已是最新版本',
showCancel: false
})
}
}
}
})
})
// #endif
}
3. 更新包安装
菜单 – 发行 – 原生App-制作应用wgt包–确定
打包完成后把smartAppversion.json 文件中的url换成对应的wgt包即可