Files
taimed-international-app/uni_modules/uni-upgrade-center-app/utils/call-check-version.js

154 lines
3.7 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 { requestUpdatePackage, checkProblemVersion } from '@/api/modules/sys'
import { getMaxVersion } from './tools'
import { getFileExtension } from '@/utils'
/**
* 统一检查更新入口
*/
export default async function checkUpdate() {
// #ifdef APP-PLUS
try {
/** ① 先判断是否问题版本 */
const currentVersion = await getCurrentVersion()
if (!currentVersion) {
throw new Error('获取当前版本号失败')
}
const problemRes = await checkProblemVersion('10', currentVersion)
if (!!problemRes?.updateUrl) {
console.warn('命中问题版本,使用修复更新包更新')
return handleBackupResponse(problemRes)
}
/** ② 非问题版本,再走 uni-upgrade-center3 秒超时) */
console.log('当前客户端无问题继续uniCloud版本检测')
const upgradeData = await getUpgradeCheckData()
const result = await withTimeout(
callUpgradeCenter(upgradeData),
3000
)
console.log('uniCloud 检测成功:', result)
return result
} catch (err) {
/** ③ uniCloud 异常兜底 */
throw '更新检测失败, 如需升级请卸载后重新安装'
// console.warn('uniCloud 更新检测失败,启用备用方案')
// return await useBackupUpdate()
}
// #endif
// #ifndef APP-PLUS
throw { message: '请在 App 中使用' }
// #endif
}
/**
* 处理备用更新响应
*/
function handleBackupResponse(res) {
return {
result: {
url: res.updateUrl,
platform: ['Android', 'Ios'],
type: getFileExtension(res.updateUrl),
is_mandatory: true,
is_backup_update: true,
title: "更新",
contents: "当前版本已经弃用,请立即更新",
...res,
}
}
}
/**
* 备用更新方案
*/
async function useBackupUpdate() {
const currentVersion = await getCurrentVersion()
if (!currentVersion) {
throw { message: '获取当前版本号失败' }
}
try {
const res = await requestUpdatePackage('10', currentVersion)
if (!res || !res.updateUrl) {
throw { message: '没有匹配的更新包,当前版本无需升级,如您确定有更新,请卸载本版本后前往应用市场重新安装' }
}
// 将服务器返回的更新信息转换为 uni-upgrade-center 格式
return handleBackupResponse(res)
} catch (err) {
console.warn('备用更新检测失败:', err)
throw err.errMsg || err.message || '备用更新检测失败'
}
}
/**
* uni-upgrade-center 调用 Promise 化
*/
function callUpgradeCenter(data) {
return new Promise((resolve, reject) => {
if (!uniCloud?.callFunction) {
reject(new Error('uniCloud 未初始化'))
return
}
uniCloud.callFunction({
name: 'uni-upgrade-center',
data,
success: resolve,
fail: reject
})
})
}
/**
* 超时控制
*/
function withTimeout(promise, timeout) {
return Promise.race([
promise,
new Promise((_, reject) =>
setTimeout(() => reject(new Error('更新检测超时')), timeout)
)
])
}
/**
* 获取 upgrade-center 所需参数
*/
function getUpgradeCheckData() {
return new Promise((resolve, reject) => {
plus.runtime.getProperty(
plus.runtime.appid,
(widgetInfo) => {
resolve({
action: 'checkVersion',
appid: plus.runtime.appid,
appVersion: plus.runtime.version,
wgtVersion: widgetInfo.version
})
}
)
})
}
/**
* 获取当前客户端版本app / wgt 取最大)
*/
export async function getCurrentVersion() {
// #ifdef APP-PLUS
const widgetInfo = await new Promise(resolve => {
plus.runtime.getProperty(plus.runtime.appid, resolve)
})
return getMaxVersion(
plus.runtime.version,
widgetInfo.version
)
// #endif
}