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

View File

@ -1,4 +1,9 @@
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 = () => {
@ -37,6 +42,7 @@ const installNewApk = (url)=> {
hideLoading() hideLoading()
if (downloadResult.statusCode === 200) { if (downloadResult.statusCode === 200) {
showLoading('正在安装...') showLoading('正在安装...')
// #ifdef APP-PLUS
plus.runtime.install(downloadResult.tempFilePath, { plus.runtime.install(downloadResult.tempFilePath, {
force: true force: true
}, function() { }, function() {
@ -49,6 +55,7 @@ const installNewApk = (url)=> {
console.error('install fail...'); console.error('install fail...');
reject() reject()
}); });
// #endif
} else { } else {
showToast(`文件下载失败, 错误码:${downloadResult.statusCode}`) showToast(`文件下载失败, 错误码:${downloadResult.statusCode}`)
reject() reject()
@ -61,7 +68,10 @@ const installNewApk = (url)=> {
} }
}); });
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)}%`)
}) })
@ -69,9 +79,18 @@ const installNewApk = (url)=> {
} }
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',
@ -119,12 +138,39 @@ export const getCompareVersion = async()=> {
} }
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(
`app最新版本:${cmpRes.lastAppInfo.version}.${cmpRes.lastAppInfo.versionCode}, 是否更新?`)
if (fmBl) { 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
} }