nalinv-mobile/common/updateApp.js
2024-11-01 16:11:55 +08:00

105 lines
3.1 KiB
JavaScript

import config from '@/common/config.js'
const updateApp = {
updateAppVersion: function(vm,flag) {
let _this = vm;
// #ifdef APP-PLUS
plus.runtime.getProperty(plus.runtime.appid, function (appInfo) {
//获取系统
let osType = plus.os.name.toUpperCase()
if (osType === 'ISO') {
return _this.$u.toast('暂不支持苹果在线更新')
}
//获取本地应用ID
let appID = appInfo.appid
let version = appInfo.version
//校验是否需要更新
_this.$u.api.checkUpdate({
osType,
appID,
version
}).then((response) => {
if (!response || response.code != 0 || response.data === null) {
if(flag){
return _this.$u.toast('系统暂无更新,当前版本:' + version)
}else{
return
}
} else {
//拼接 下载地址
let url = config.baseUrl + '/iep-app/app-release-management/download/';
if (response.data.packetType === '原生APP安装包') {
url = url + 'apk/' + response.data.handle + '.apk'
}
if (response.data.packetType === 'WGT资源包') {
url = url + 'wgt/' + response.data.handle + '.wgt'
}
//判断是否强制更新
let showCancel = true
if (response.data.forceUpdate === 'true') {
showCancel = false
}
//弹出框更新提示
uni.showModal({
title: "更新提示",
content: response.data.content,
showCancel: showCancel,
confirmText: '更新',
success: (res) => {
if (res.confirm) {
console.log(url)
plus.nativeUI.showWaiting("正在下载.....");
try {
const downloadTask = uni.downloadFile({
url: url,
header: {
Authorization: 'Bearer ' + _this.vuex_token,
"TENANT-ID": _this.vuex_tenantId
},
success: (res) => {
if (res && res.statusCode === 200) {
plus.runtime.install(plus.io.convertLocalFileSystemURL(res.tempFilePath), {},
function () {
plus.nativeUI.alert("更新完成,需要重新启应用!", function () {
plus.runtime.restart();
plus.nativeUI.closeWaiting();
})
}, function (error) {
console.log(error)
plus.nativeUI.closeWaiting();
plus.nativeUI.alert("更新文件失败[" + error.code + "]:" + error.message);
}
);
} else {
plus.nativeUI.closeWaiting();
plus.nativeUI.alert("Download failed:" + res.statusCode);
}
}
});
downloadTask.onProgressUpdate((res) => {
console.log('下载进度' + res.progress);
console.log('已经下载的数据长度' + res.totalBytesWritten);
console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
});
} catch (error) {
console.log(error)
plus.nativeUI.closeWaiting();
}
}
}
})
}
}).catch((e) => {
plus.nativeUI.closeWaiting();
_this.$u.toast(e)
})
});
// #endif
}
}
export default updateApp;