// 检查是否更新版本
updateVersion() {
// 热更新不会更新app的版本号
// 这里用一个常量定义(每次修改版本号将设置里面的版本号手动更新)
let realVersion = '1';
//plus.runtime.getProperty(),获取本机安装的版本,具体使用请参照如下链接
// https: //www.html5plus.org/doc/zh_cn/runtime.html#plus.runtime.WidgetInfo
plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
let currentVersion = widgetInfo.version.replace(/\./g, ""); //获取当前版本号
sendVersion().then(res => {
const code = res.data.code;
if (code === 100) {
let newVersion = res.data.data.id;
let downloadUrl = res.data.data.downloadUrl;
if (realVersion < newVersion) {
uni.showModal({
title: '已发现新版本',
content: '确认更新?',
success: function(res) {
if (res.confirm) {
uni.downloadFile({
url: downloadUrl,
success: (downloadResult) => {
plus.runtime.install(
downloadResult
.tempFilePath, {
force: true
},
function() {
// 应用热重启,重新启动进入首页。
plus.runtime.restart();
uni.showModal({
title: '安装成功!',
content: '已重新进入应用',
success: function(res) {
if (res.confirm) {
console.log('用户点击确定');
} else if(res.cancel) {
console.log('用户点击取消');
}
}
});
// uni.hideToast();
},
function(e) {
uni.showToast({
title: '安装失败',
icon: 'none'
})
})
},
fail: (err) => {
console.log(err)
uni.showToast({
title: '下载失败',
icon: 'none'
})
}
})
} else if (res.cancel) {
uni.showToast({
title: '已取消',
icon: 'none'
})
}
}
})
}
} else {
uni.showToast({
title: res.data.msg,
icon: 'none'
})
}
}).catch(err => {
console.log(err)
})
})
},
uni 热更新
最新推荐文章于 2024-12-24 16:20:28 发布