1,在页面首页判断是否为android,获取manifest.json配置的版本号
onLoad() { let that = this //#ifdef APP-PLUS uni.getSystemInfo({ //获取系统信息 success: (res) => { if (res.platform == "android") { //检测当前平台,如果是安卓则启动安卓更新 plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) { //获取配置信息 if (wgtinfo.name == "NMFS-Uni") { that.version = wgtinfo.version //当前版本号 that.AndroidCheckUpdate(); } }); } } }) //#endif },
2.获取最新的版本号,与本身版本号比较
methods: { AndroidCheckUpdate: function() { //版本更新 var _this = this;
uni.request({
url: 'http://xxxx.com/version.txt',
method: 'GET',
data: {},
success: res => {
var server_version = res.version;
_this.checkVersionToLoadUpdate(server_version, _this.version);
}
},
checkVersionToLoadUpdate: function(server_version, curr_version) { //版本更新 if (server_version > curr_version) { if (plus.networkinfo.getCurrentType() != 3) {//获取手机设备的相关信息,判断是否在Wifi状态。 uni.showToast({ title: '有新的版本发布,检测到您目前非Wifi连接,为节约您的流量,程序已停止自动更新,将在您连接WIFI之后重新检测更新', mask: true, duration: 5000, icon: "none" }); return; } else { uni.showModal({ title: "版本更新", content: '有新的版本发布,检测到您当前为Wifi连接,是否立即进行新版本下载?', confirmText: '立即更新', cancelText: '稍后进行', success: function(res) { if (res.confirm) { var downloadApkUrl = 'http://cwwy.caiyunduan.com/apk/mfs.apk ';//apk下载的地址 var dtask = plus.downloader.createDownload(downloadApkUrl, {},function(d, status) {//新建下载任务 if (status == 200) {//当下载完成 plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), {}, {}, function(error) {//安装应用 uni.showToast({ title: '安装失败', duration: 1500, icon: 'none' }); }) } else { uni.showToast({ title: '更新失败', duration: 1500, icon: 'none' }); } }) dtask.start(); var prg = 0; var showLoading = plus.nativeUI.showWaiting("正在下载"); dtask.addEventListener('statechanged', function(task, status) {//添加下载任务事件监听器 // 给下载任务设置一个监听 并根据状态 做操作 switch (task.state) { case 1: showLoading.setTitle("正在下载"); break; case 2: showLoading.setTitle("已连接到服务器"); break; case 3: prg = parseInt(//下载的进度 (parseFloat(task.downloadedSize) / parseFloat(task.totalSize)) * 100 ); showLoading.setTitle("版本更新,正在下载" + prg + "% "); break; case 4: plus.nativeUI.closeWaiting();//关闭系统提示框 //下载完成 break; } }); } } }) } } },
}
注意!
plus对象只有在手机端,真机调试才有。不然会报 plus is not defined