针对uniapp版本更新的问题,网上搜了很多不错的,但是总是有些问题不能成功,经过在其他博主的基础上进行了修改,试了试可以实现。
基本思想为:在数据库里先存一个版本号version,在客户端通过plus.runtime.version获取当前app版本信息,(将数据库的版本设置成大于当前版本信息),当数据库里先存一个版本号version大于当前版本的时候,更新,将更新的地址放到服务器上即可。
App.vue的代码如下:
mounted() {
let that = this;
that.CheckUpdate();
},
methods: {
//版本更新
CheckUpdate: function() {
var _this = this;
//获取当前app版本信息
var version = plus.runtime.version;
//console.log(version);
uni.request({
//获取服务器的版本信息
url: '服务器端存的版本号请求地址',
data: {},
success: (res) => {
//console.log(res.data[0].version)
//console.log("现在的版本"+version+"数据库版本"+res.data[0].version+"进入查找app版本");
if (res.data[0].version > version) {
plus.nativeUI.confirm("检测到有新版本,是否更新", function(e) {
if (e.index == 0) {
if (plus.networkinfo.getCurrentType() != 3) {
plus.nativeUI.confirm("检测到您目前非Wifi连接,是否继续更新", function(e) {
if (e.index == 0) {
_this.downWgt();
} else {
}
}, "", )["取消", "确定"]
return;
}
_this.downWgt(); //下载文件
} else {
//plus.runtime.quit();//安卓控制不更新退出应用
}
}, "", ["立即更新", "以后再说"]);
//dtask.start();
}
},
fail: () => {
uni.showToast({
title: '调用请求失败',
mask: false,
duration: 5000,
icon: "none"
});
},
complete: () => {}
});
},
downWgt() {
// uni.showLoading({
// title: '更新中……'
// })
plus.nativeUI.showWaiting("正在更新,请耐心等待...");
uni.downloadFile({ //执行下载
url: '新的app的下载地址', //下载地址
success: downloadResult => { //下载成功
uni.hideLoading();
if (downloadResult.statusCode == 200) {
plus.runtime.install( //安装
downloadResult.tempFilePath, {
force: true
},
);
}
}
});
},
},