1. First you need to get the version of the current app
const systemInfo = uni.getSystemInfoSync();
// 应用程序版本号
// #ifdef APP
me.version = systemInfo.appWgtVersion;
// #endif
// #ifdef H5
me.version = systemInfo.appVersion;
// #endif
2. Obtain the app version saved by the server
3. Click the button to verify the version number
// 检查更新按钮
checkUpdate () {
const me = this
const compare = me.compareVersion(me.version, me.webVersion)
if (compare < 0) {
me.$refs.version.open()
} else {
me.$showMessage('已是最新版本')
}
},
// 比较版本号
compareVersion(v1, v2) {
const arr1 = v1.split('.')
const arr2 = v2.split('.')
if (arr1.length === arr2.length) {
for(let i = 0; i < arr1.length; i++) {
let ver1 = parseInt(arr1[i] || '0')
let ver2 = parseInt(arr2[i] || '0')
if (ver1 > ver2) {
return 1
} else if (ver1 < ver2) {
return -1
}
}
return 0
} else {
this.$showMessage('版本号的长度不一致,请联系管理员解决')
return 0
}
},
4. If the version number is not the latest, open a prompt pop-up window.
5. Click to download when updating and display the progress (pop-up window display).
You can get the download progress using the officially provided onProgressUpdate.
<uni-popup ref="process" type="center">
<view class="popup-content">
<progress :percent="progress" border-radius="5"></progress>
</view>
<view style="text-align: center;color: #fff;padding: 10px;">
{
{ '下载中: ' + progress + '%'}}
</view>
</uni-popup>
// 更新
dialogConfirm () {
this.$refs.version.close()
var downloadTask = uni.downloadFile({
url: base_url + '/UploadFile/base.apk',
success: (res) => {
this.$refs.process.close()
this.progress = 0
if (res.statusCode === 200) {
uni.openDocument({
filePath: res.tempFilePath,
success() {
}
})
}
}
})
downloadTask.onProgressUpdate(res => {
this.$refs.process.open()
this.progress = res.progress
})
}
After the download is completed, close the pop-up window, use the obtained temporary address to open the file with uni.openDocument, and you can install it.
Record it here~