1.小程序检测,更新
//在app.vue中
<script>
Update(){
const updateManager = uni.getUpdateManager();
updateManager.onCheckForUpdate(function (res) {
// 请求完新版本信息的回调
});
updateManager.onUpdateReady(function (res) {
uni.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
success(res) {
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate();
}
}
});
});
updateManager.onUpdateFailed(function (res) {
// 新的版本下载失败
});
}
}
</script>
//生命周期中调用检测
onShow(){
this.Update()
}
2.app检测、更新
视图构造
data中的定义
/**检查app版本*/
//目前在nvue页面定义的,不排除vue页面
updateApp() {
// #ifdef APP-NVUE
plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
//定义接口比对版本号
getAppUpdate({
type: 1
}).then(res => {
if (res.resultCode == '000000') {
if (res.data) {
this.appInform = res.data;
if (res.data.appVersion != wgtinfo.version) {
this.showUpdate = true;
}
}
}
}).catch(error => {
console.log("0000000000000000", error)
})
})
// #endif
},```
```cpp
//showLoading控制组件是否显示的
updateStart(type) {
let _this = this;
if (type == 2) return _this.showUpdate = false;
if (type == 1) {
// _this.showUpdate=false;
if (_this.showLoading) return;
uni.showLoading({
title: '更新中,请稍后...'
})
_this.showLoading = true;
// plus.runtime.openURL(this.appInform.appUrl);
var dtask = plus.downloader.createDownload(_this.appInform.appUrl, {}, function(d, status) {
uni.hideLoading();
_this.showLoading = false;
// 下载完成
if (status == 200) {
plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), {}, {}, function(
error) {
uni.showToast({
title: '安装失败',
duration: 1500
});
})
} else {
uni.showToast({
title: '更新失败',
duration: 1500
});
}
});
dtask.start();
}
},```