1、版本更新写成mixin函数,getUpdateApk.js
export default {
data () {
return {
appInfo: {
appid: plus.runtime.appid,
version: plus.runtime.version
}
}
},
methods: {
compareVersionAndUpdate (wgtUrl) {
console.log(wgtUrl)
this.updateapp2(wgtUrl)
},
updateapp2 (wgtUrl) {
let that = this
uni.showModal({
title: '版本更新',
content: `确认要更新到${that.appInfo.appid}${that.appInfo.version}`,
confirmText: "更新",
showCancel: false,
success: function (e) {
if (e.confirm) {
if (plus.os.name.toLowerCase() == 'ios') {
plus.runtime.openURL(wgtUrl)
} else {
var dtask = plus.downloader.createDownload(wgtUrl, {}, function (d, status) {
uni.showToast({
title: '下载完成',
mask: false,
duration: 1000
});
if (status == 200) {
plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), {}, e => e, function (error) {
uni.showToast({
title: '安装失败-01',
mask: false,
duration: 1500
});
})
} else {
uni.showToast({
title: '更新失败-02',
mask: false,
duration: 1500
});
}
});
try {
dtask.start()
var prg = 0;
var showLoading = plus.nativeUI.showWaiting("正在下载", { "back": "none" });
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;
}
});
} catch (err) {
plus.nativeUI.closeWaiting();
uni.showToast({
title: '更新失败-03',
mask: false,
duration: 1500
});
}
}
} else {
}
}
});
},
updateapp (wgtUrl) {
uni.showLoading({
title: '更新中……'
})
uni.downloadFile({
url: wgtUrl,
success: downloadResult => {
if (downloadResult.statusCode === 200) {
uni.showModal({
title: '',
content: '更新成功,确定现在重启吗?',
confirmText: '重启',
confirmColor: '#EE8F57',
success: function (res) {
if (res.confirm) {
plus.runtime.install(
downloadResult.tempFilePath, {
force: true
},
function () {
plus.runtime.restart();
},
function (e) {
}
);
}
}
});
}
},
complete: () => {
uni.hideLoading();
}
});
}
}
}
2、vuex中actions
actions: {
compareVersionGetUrl ({ commit, state }, params) {
return new Promise((resolve, reject) => {
setTimeout(() => {
let version = '2.0.0'
console.log(version != params.version)
if (version != params.version) {
resolve({ url: `http:.../__UNI__E3CD4E2_0216140622.apk`, version: '2.0.0' })
} else {
reject({ url: null, msg: '已经是最新版本' })
}
}, 500)
})
}
}
3、在关于系统的页面中使用如下
<template>
<view class="app-container">
<view class="nav">
<view style="display:flex;justify-content:center;margin:50rpx 0;">
<view style="width:400rpx">
<u-button type="primary"
@click="checkIsLatest">检查更新</u-button>
</view>
</view>
</view>
</view>
</template>
<script>
import updateApp from '@/common/utils/getUpdateApk'
export default {
mixins: [updateApp],
data () {
return {
version: plus.runtime.version,
titleName: '关于应用',
}
},
methods: {
checkIsLatest () {
this.$store.dispatch("compareVersionGetUrl", { version: '1.0.0' }).then(res => {
this.compareVersionAndUpdate(res.url)
})
}
}
}
</script>