emis-app/common/appUpdate/index.js
2024-06-16 01:56:52 +08:00

106 lines
2.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { confirmModal, showLoading, hideLoading, showToast } from "@/common/untool"
export const getSystemInfo = ()=> {
let info = uni.getSystemInfoSync()
console.log('getSystemInfo=====', info)
return info
}
export const getCurrentAppInfo = ()=> {
return new Promise((resolve, reject)=> {
// 保存 app 版本信息
// #ifdef APP-PLUS
plus.runtime.getProperty(plus.runtime.appid, (widgetInfo)=> {
console.log('plus.runtime.appid=========', plus.runtime.appid);
console.log('widgetInfo=========', widgetInfo);
widgetInfo._appVersion = `${widgetInfo.version}.${widgetInfo.versionCode}`
resolve(widgetInfo)
});
// #endif
})
}
const installNewApk = (url)=> {
return new Promise((resolve, reject)=> {
let downInstance = uni.downloadFile({
url: url,
success: (downloadResult) => {
hideLoading()
if (downloadResult.statusCode === 200) {
showLoading('正在安装...')
plus.runtime.install(downloadResult.tempFilePath, {
force: true
}, function() {
hideLoading()
console.log('install success...');
plus.runtime.restart();
resolve()
}, function(e) {
hideLoading()
console.error('install fail...');
reject()
});
} else {
reject()
}
},
fail: (err)=> {
console.log('download file err...', err);
hideLoading()
}
});
downInstance.onProgressUpdate((res)=> {
let { totalBytesExpectedToWrite, totalBytesWritten } = res
let percent = (totalBytesWritten / totalBytesExpectedToWrite) * 100
showLoading(`下载进度:${parseInt(percent)}%`)
})
})
}
const getLastAppInfo = async (curAppInfo)=> {
return {
version: '1.0.1',
versionCode: 101,
apkUrl: '',
wgtUrl: '',
// whiteDeviceIds: ['F9E778C90D0E9A8DA9D95AA97B00A3FB'],
}
}
const compareInfo = (curAppInfo, lastAppInfo)=> {
let hasNewVersion = false
let url = ''
// 强制更新app
if(lastAppInfo.version && curAppInfo.version != lastAppInfo.version) {
hasNewVersion = true
url = lastAppInfo.apkUrl
}
// 热更新
if(!url && lastAppInfo.versionCode && curAppInfo.versionCode != lastAppInfo.versionCode) {
hasNewVersion = true
url = lastAppInfo.wgtUrl
}
// 判断是否走白名单更新
if(lastAppInfo.whiteDeviceIds?.length > 0) {
let sysInfo = getSystemInfo()
hasNewVersion = lastAppInfo.whiteDeviceIds.indexOf(sysInfo?.deviceId) > -1
}
return {
hasNewVersion,
url,
lastAppInfo
}
}
export const checkUpdate = async ()=> {
let curAppInfo = await getCurrentAppInfo()
let lastAppInfo = await getLastAppInfo(curAppInfo)
let cmpRes = await compareInfo(curAppInfo, lastAppInfo)
if(cmpRes.hasNewVersion) {
let fmBl = confirmModal(`app最新版本:${cmpRes.lastAppInfo.version}.${cmpRes.lastAppInfo.versionCode}, 是否更新?`)
if(fmBl) {
await installNewApk(cmpRes.url)
}
}
}