2024-06-15 17:56:52 +00:00
|
|
|
|
import { confirmModal, showLoading, hideLoading, showToast } from "@/common/untool"
|
2024-06-16 03:54:33 +00:00
|
|
|
|
import * as apiService from "@/common/api"
|
2024-06-15 17:56:52 +00:00
|
|
|
|
|
|
|
|
|
|
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)
|
2024-06-16 04:50:00 +00:00
|
|
|
|
});
|
2024-06-15 17:56:52 +00:00
|
|
|
|
// #endif
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const installNewApk = (url)=> {
|
2024-06-15 18:31:52 +00:00
|
|
|
|
console.log("installNewApk url===", url)
|
2024-06-15 17:56:52 +00:00
|
|
|
|
return new Promise((resolve, reject)=> {
|
2024-06-16 01:48:22 +00:00
|
|
|
|
showLoading('正在下载资源文件,请稍等...')
|
2024-06-15 17:56:52 +00:00
|
|
|
|
let downInstance = uni.downloadFile({
|
|
|
|
|
|
url: url,
|
|
|
|
|
|
success: (downloadResult) => {
|
2024-06-15 18:31:52 +00:00
|
|
|
|
console.log("===downloadResult===", downloadResult)
|
2024-06-15 17:56:52 +00:00
|
|
|
|
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 {
|
2024-06-15 18:31:52 +00:00
|
|
|
|
showToast(`文件下载失败, 错误码:${downloadResult.statusCode}`)
|
2024-06-15 17:56:52 +00:00
|
|
|
|
reject()
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
fail: (err)=> {
|
2024-06-15 18:31:52 +00:00
|
|
|
|
console.log('download file err...', err);
|
|
|
|
|
|
showToast("文件下载失败")
|
2024-06-15 17:56:52 +00:00
|
|
|
|
hideLoading()
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
downInstance.onProgressUpdate((res)=> {
|
|
|
|
|
|
let { totalBytesExpectedToWrite, totalBytesWritten } = res
|
|
|
|
|
|
let percent = (totalBytesWritten / totalBytesExpectedToWrite) * 100
|
2024-06-16 01:48:22 +00:00
|
|
|
|
// showLoading(`下载进度:${parseInt(percent)}%`)
|
|
|
|
|
|
})
|
2024-06-15 17:56:52 +00:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const getLastAppInfo = async (curAppInfo)=> {
|
2024-06-16 03:54:33 +00:00
|
|
|
|
let { version, versionCode } = curAppInfo
|
|
|
|
|
|
let { deviceId } = getSystemInfo()
|
|
|
|
|
|
let res = await apiService.getLastAppVersion({ version, versionCode, deviceId })
|
|
|
|
|
|
console.log("getLastAppInfo res====", res)
|
|
|
|
|
|
// res.data = {
|
|
|
|
|
|
// version: '1.0.0',
|
|
|
|
|
|
// versionCode: 100,
|
|
|
|
|
|
// apkUrl: 'http://192.168.1.7:5173/static/t.apk',
|
|
|
|
|
|
// wgtUrl: 'http://192.168.1.7:5173/static/t.wgt',
|
|
|
|
|
|
// whiteDeviceIds: ['F9E778C90D0E9A8DA9D95AA97B00A3FB'],
|
|
|
|
|
|
// remark: '',
|
|
|
|
|
|
// }
|
|
|
|
|
|
return res.data
|
2024-06-15 17:56:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
// 判断是否走白名单更新
|
2024-06-16 04:50:00 +00:00
|
|
|
|
if(url && lastAppInfo.whiteDeviceIds?.length > 0) {
|
2024-06-15 17:56:52 +00:00
|
|
|
|
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)
|
2024-06-15 18:31:52 +00:00
|
|
|
|
console.log("checkUpdate cmpRes ====", cmpRes)
|
2024-06-16 04:50:00 +00:00
|
|
|
|
if(cmpRes.hasNewVersion && cmpRes.url) {
|
2024-06-15 18:31:52 +00:00
|
|
|
|
let fmBl = await confirmModal(`app最新版本:${cmpRes.lastAppInfo.version}.${cmpRes.lastAppInfo.versionCode}, 是否更新?`)
|
2024-06-15 17:56:52 +00:00
|
|
|
|
if(fmBl) {
|
|
|
|
|
|
await installNewApk(cmpRes.url)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-06-16 03:54:33 +00:00
|
|
|
|
return cmpRes
|
2024-06-15 17:56:52 +00:00
|
|
|
|
}
|