pad-app/upDate.js

87 lines
3.9 KiB
JavaScript
Raw Permalink Normal View History

2024-04-19 16:21:28 +08:00
import Vue from 'vue'
// 获取当前版本号
function getLocalVersion() {
return new Promise((resolve, reject) => {
plus.runtime.getProperty(plus.runtime.appid, function (widgetInfo) {
const version = widgetInfo.version
resolve(version)
})
}).catch(err => {
console.log(err);
})
}
export function downloadPackage() {
return new Promise((resolve, reject) => {
// 相关接口
let token = uni.getStorageSync('loginObj')
let version
const reqDTO = {
obligateData1: 'pad'
}
uni.request({
url: `${Vue.prototype.$local}/api/version/getMsg`,
header: {
'Content-Type': 'application/json', //自定义请求头信息
'Authorization': `Bearer ${token.access_token}`
},
data: reqDTO,
method: 'POST', //请求方式,必须为大写
success: (res) => {
if (res.statusCode == 200 && res.data.data != null) {
getLocalVersion().then(resD => {
if (res.data.data.versionNum != resD) {
uni.showModal({
title: '检测到有版本更新!',
content: '请升级app到最新版本',
cancelText: '暂不升级',
showCancel: false,
confirmText: '立即升级',
success: resDD => {
if (resDD.confirm) {
const task = plus.downloader.createDownload(res.data.data.url);
task.addEventListener('statechanged', (download, status) => {
console.log(download,status);
if (status == 200) {
const {
downloadedSize,
totalSize,
filename,
state,
} = download;
const p = (downloadedSize / totalSize).toFixed(2) * 100;
const isComplete = state === 4;
console.log(['下载中' + p, 'status=' + status, '是否下载完成' + (isComplete)].join(''))
// onProgress && onProgress(p);
if (isComplete) {
console.log('下载完成,开始安装')
plus.runtime.install(filename, {}, () => {
console.log('安装成功')
}, () => {
console.log('安装失败')
});
}
}
}, false);
task.start();
}
}
})
}
})
}
},
fail: function (err) {
uni.showModal({
title: '提示',
showCancel: false,
content: '请求超时,请退出重新进入!'
})
}
})
resolve(version)
}).catch(err => {
console.log(err);
})
}