This commit is contained in:
aihe 2024-09-02 15:55:35 +08:00
parent b1574ff8ca
commit 65e7250cca

View File

@ -1,19 +1,24 @@
import { confirmModal, showLoading, hideLoading, showToast } from "@/common/untool" import {
confirmModal,
showLoading,
hideLoading,
showToast
} from "@/common/untool"
import * as apiService from "@/common/api" import * as apiService from "@/common/api"
export const getSystemInfo = ()=> { export const getSystemInfo = () => {
let info = uni.getSystemInfoSync() let info = uni.getSystemInfoSync()
console.log('getSystemInfo=====', info) console.log('getSystemInfo=====', info)
return info return info
} }
export const getCurrentAppInfo = ()=> { export const getCurrentAppInfo = () => {
return new Promise((resolve, reject)=> { return new Promise((resolve, reject) => {
// 保存 app 版本信息 // 保存 app 版本信息
// #ifdef APP-PLUS // #ifdef APP-PLUS
plus.runtime.getProperty(plus.runtime.appid, (widgetInfo)=> { plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
console.log('plus.runtime.appid=========', plus.runtime.appid); console.log('plus.runtime.appid=========', plus.runtime.appid);
console.log('widgetInfo=========', widgetInfo); console.log('widgetInfo=========', widgetInfo);
widgetInfo._appVersion = `${widgetInfo.version}.${widgetInfo.versionCode}` widgetInfo._appVersion = `${widgetInfo.version}.${widgetInfo.versionCode}`
resolve(widgetInfo) resolve(widgetInfo)
}); });
@ -26,52 +31,66 @@ export const getCurrentAppInfo = ()=> {
}) })
} }
const installNewApk = (url)=> { const installNewApk = (url) => {
console.log("installNewApk url===", url) console.log("installNewApk url===", url)
return new Promise((resolve, reject)=> { return new Promise((resolve, reject) => {
showLoading('正在下载资源文件,请稍等...') showLoading('正在下载资源文件,请稍等...')
let downInstance = uni.downloadFile({ let downInstance = uni.downloadFile({
url: url, url: url,
success: (downloadResult) => { success: (downloadResult) => {
console.log("===downloadResult===", downloadResult) console.log("===downloadResult===", downloadResult)
hideLoading() hideLoading()
if (downloadResult.statusCode === 200) { if (downloadResult.statusCode === 200) {
showLoading('正在安装...') showLoading('正在安装...')
plus.runtime.install(downloadResult.tempFilePath, { // #ifdef APP-PLUS
force: true plus.runtime.install(downloadResult.tempFilePath, {
}, function() { force: true
}, function() {
hideLoading() hideLoading()
console.log('install success...'); console.log('install success...');
plus.runtime.restart(); plus.runtime.restart();
resolve() resolve()
}, function(e) { }, function(e) {
hideLoading() hideLoading()
console.error('install fail...'); console.error('install fail...');
reject() reject()
}); });
} else { // #endif
} else {
showToast(`文件下载失败, 错误码:${downloadResult.statusCode}`) showToast(`文件下载失败, 错误码:${downloadResult.statusCode}`)
reject() reject()
} }
}, },
fail: (err)=> { fail: (err) => {
console.log('download file err...', err); console.log('download file err...', err);
showToast("文件下载失败") showToast("文件下载失败")
hideLoading() hideLoading()
} }
}); });
downInstance.onProgressUpdate((res)=> { downInstance.onProgressUpdate((res) => {
let { totalBytesExpectedToWrite, totalBytesWritten } = res let {
totalBytesExpectedToWrite,
totalBytesWritten
} = res
let percent = (totalBytesWritten / totalBytesExpectedToWrite) * 100 let percent = (totalBytesWritten / totalBytesExpectedToWrite) * 100
// showLoading(`下载进度:${parseInt(percent)}%`) // showLoading(`下载进度:${parseInt(percent)}%`)
}) })
}) })
} }
const getLastAppInfo = async (curAppInfo)=> { const getLastAppInfo = async (curAppInfo) => {
let { version, versionCode } = curAppInfo let {
let { deviceId } = getSystemInfo() version,
let res = await apiService.getLastAppVersion({ version, versionCode, deviceId }) versionCode
} = curAppInfo
let {
deviceId
} = getSystemInfo()
let res = await apiService.getLastAppVersion({
version,
versionCode,
deviceId
})
console.log("getLastAppInfo res====", res) console.log("getLastAppInfo res====", res)
// res.data = { // res.data = {
// version: '1.0.0', // version: '1.0.0',
@ -84,21 +103,21 @@ const getLastAppInfo = async (curAppInfo)=> {
return res.data return res.data
} }
const compareInfo = (curAppInfo, lastAppInfo)=> { const compareInfo = (curAppInfo, lastAppInfo) => {
let hasNewVersion = false let hasNewVersion = false
let url = '' let url = ''
// 强制更新app // 强制更新app
if(lastAppInfo.version && curAppInfo.version != lastAppInfo.version) { if (lastAppInfo.version && curAppInfo.version != lastAppInfo.version) {
hasNewVersion = true hasNewVersion = true
url = lastAppInfo.apkUrl url = lastAppInfo.apkUrl
} }
// 热更新 // 热更新
if(!url && lastAppInfo.versionCode && curAppInfo.versionCode != lastAppInfo.versionCode) { if (!url && lastAppInfo.versionCode && curAppInfo.versionCode != lastAppInfo.versionCode) {
hasNewVersion = true hasNewVersion = true
url = lastAppInfo.wgtUrl url = lastAppInfo.wgtUrl
} }
// 判断是否走白名单更新 // 判断是否走白名单更新
if(url && lastAppInfo.whiteDeviceIds?.length > 0) { if (url && lastAppInfo.whiteDeviceIds?.length > 0) {
let sysInfo = getSystemInfo() let sysInfo = getSystemInfo()
hasNewVersion = lastAppInfo.whiteDeviceIds.indexOf(sysInfo?.deviceId) > -1 hasNewVersion = lastAppInfo.whiteDeviceIds.indexOf(sysInfo?.deviceId) > -1
} }
@ -110,7 +129,7 @@ const compareInfo = (curAppInfo, lastAppInfo)=> {
} }
} }
export const getCompareVersion = async()=> { export const getCompareVersion = async () => {
let curAppInfo = await getCurrentAppInfo() let curAppInfo = await getCurrentAppInfo()
let lastAppInfo = await getLastAppInfo(curAppInfo) let lastAppInfo = await getLastAppInfo(curAppInfo)
let cmpRes = await compareInfo(curAppInfo, lastAppInfo) let cmpRes = await compareInfo(curAppInfo, lastAppInfo)
@ -118,13 +137,40 @@ export const getCompareVersion = async()=> {
return cmpRes return cmpRes
} }
export const checkUpdate = async ()=> { export const checkUpdate = async () => {
// app升级检测
// #ifdef APP-PLUS
let cmpRes = await getCompareVersion() let cmpRes = await getCompareVersion()
if(cmpRes.hasNewVersion) { if (cmpRes.hasNewVersion) {
let fmBl = await confirmModal(`app最新版本:${cmpRes.lastAppInfo.version}.${cmpRes.lastAppInfo.versionCode}, 是否更新?`) let fmBl = await confirmModal(
if(fmBl) { `app最新版本:${cmpRes.lastAppInfo.version}.${cmpRes.lastAppInfo.versionCode}, 是否更新?`)
if (fmBl) {
await installNewApk(cmpRes.url) await installNewApk(cmpRes.url)
} }
} }
return cmpRes return cmpRes
// #endif
//微信小程序检测
// #ifdef MP-WEIXIN
if (wx.canIUse('getUpdateManager')) { //判断当前微信版本是否支持版本更新
const updateManager = wx.getUpdateManager();
updateManager.onCheckForUpdate(async function(res) {
if (res.hasUpdate) { // 请求完新版本信息的回调
updateManager.onUpdateReady(async function() {
let cbl = await confirmModal('新版本已经准备好,是否重启应用?')
if (cbl) { // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate()
}
});
updateManager.onUpdateFailed(function() {
showToast('新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~')
})
}
})
} else {
showToast('当前微信版本过低,无法使用新功能,请升级到最新微信版本后重试。')
}
// #endif
} }