优化
This commit is contained in:
+61
-13
@@ -1,3 +1,5 @@
|
||||
import appManifest from '@/manifest.json'
|
||||
|
||||
const updateEndpoint = String(import.meta.env.VITE_PDA_UPDATE_URL || '').trim()
|
||||
|
||||
let checking = false
|
||||
@@ -15,20 +17,40 @@ function getRuntimeInfo() {
|
||||
return
|
||||
}
|
||||
|
||||
// 标准真机基座的版本属于 HBuilderX;调试更新按当前工程版本模拟。
|
||||
const debugBase = import.meta.env.DEV || uni.getAppBaseInfo().packageName === 'io.dcloud.HBuilder'
|
||||
const runtime = {
|
||||
appid: String((debugBase ? appManifest.appid : plus.runtime.appid) || ''),
|
||||
appVersion: String((debugBase ? appManifest.versionName : plus.runtime.version) || ''),
|
||||
appVersionCode: Number(debugBase ? appManifest.versionCode : plus.runtime.versionCode)
|
||||
}
|
||||
if (!runtime.appid || !runtime.appVersion || !Number.isInteger(runtime.appVersionCode) || runtime.appVersionCode < 1) {
|
||||
reject(new Error('无法读取当前应用版本,请重新运行 App'))
|
||||
return
|
||||
}
|
||||
if (debugBase) {
|
||||
resolve({ ...runtime, wgtVersion: runtime.appVersion })
|
||||
return
|
||||
}
|
||||
|
||||
// 正式 APK 始终保留原生基座版本码,WGT 升级只改变资源版本。
|
||||
plus.runtime.getProperty(plus.runtime.appid, (properties) => {
|
||||
resolve({
|
||||
appid: plus.runtime.appid,
|
||||
appVersion: String(plus.runtime.version || ''),
|
||||
appVersionCode: Number(plus.runtime.versionCode || 0),
|
||||
wgtVersion: String(properties?.version || plus.runtime.version || '')
|
||||
})
|
||||
resolve({ ...runtime, wgtVersion: String(properties?.version || runtime.appVersion) })
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function normalizeUpdate(payload, runtime) {
|
||||
const value = payload && typeof payload === 'object' ? payload : {}
|
||||
if (value.update !== true) return null
|
||||
if ('code' in value && String(value.code) !== '200') {
|
||||
const error = new Error(String(value.message || '检查更新失败,请稍后重试'))
|
||||
error.code = value.code
|
||||
error.requestId = value.requestId
|
||||
error.traceId = value.traceId
|
||||
throw error
|
||||
}
|
||||
if (typeof value.update !== 'boolean') throw new Error('更新服务返回了无效的版本信息')
|
||||
if (!value.update) return null
|
||||
|
||||
if (value.appid !== runtime.appid) throw new Error('更新包与当前应用不匹配')
|
||||
const packageType = String(value.packageType || '').toLowerCase()
|
||||
@@ -110,20 +132,45 @@ function downloadUpdate(update) {
|
||||
})
|
||||
}
|
||||
|
||||
function prepareUpdateFile(filePath, update) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const fail = (cause) => {
|
||||
const error = new Error('更新包文件准备失败,请重新下载')
|
||||
error.stage = 'prepare'
|
||||
error.code = cause?.code
|
||||
reject(error)
|
||||
}
|
||||
// 下载接口以 /package 结尾,临时文件可能没有安装器要求的扩展名。
|
||||
const filename = `overseas-pda-${update.versionName}-${Date.now()}.${update.packageType}`
|
||||
plus.io.resolveLocalFileSystemURL(filePath, (entry) => {
|
||||
entry.getParent((parent) => {
|
||||
entry.moveTo(parent, filename, (file) => resolve(file.toLocalURL()), fail)
|
||||
}, fail)
|
||||
}, fail)
|
||||
})
|
||||
}
|
||||
|
||||
function installUpdate(filePath) {
|
||||
return new Promise((resolve, reject) => {
|
||||
plus.runtime.install(
|
||||
filePath,
|
||||
{ force: false },
|
||||
resolve,
|
||||
() => reject(new Error('更新包安装失败,请重新下载'))
|
||||
(cause) => {
|
||||
const code = cause?.code == null ? '' : `(${cause.code})`
|
||||
const reason = String(cause?.message || '请重新下载后重试')
|
||||
const error = new Error(`更新包安装失败${code}:${reason}`)
|
||||
error.stage = 'install'
|
||||
error.code = cause?.code
|
||||
reject(error)
|
||||
}
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
function showFailure(error, mandatory) {
|
||||
const message = error?.message || '检查更新失败,请稍后重试'
|
||||
if (!mandatory) {
|
||||
if (!mandatory && error?.stage !== 'install' && error?.stage !== 'prepare') {
|
||||
uni.showToast({ title: message, icon: 'none', duration: 3000 })
|
||||
return Promise.resolve(false)
|
||||
}
|
||||
@@ -132,9 +179,9 @@ function showFailure(error, mandatory) {
|
||||
uni.showModal({
|
||||
title: '更新失败',
|
||||
content: message,
|
||||
confirmText: '重新尝试',
|
||||
confirmText: mandatory ? '重新尝试' : '知道了',
|
||||
showCancel: false,
|
||||
complete: () => resolve(true)
|
||||
complete: () => resolve(mandatory)
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -145,7 +192,8 @@ async function runUpdate(update) {
|
||||
|
||||
try {
|
||||
const filePath = await downloadUpdate(update)
|
||||
await installUpdate(filePath)
|
||||
const installPath = await prepareUpdateFile(filePath, update)
|
||||
await installUpdate(installPath)
|
||||
if (update.packageType === 'wgt') {
|
||||
uni.showModal({
|
||||
title: '更新完成',
|
||||
@@ -184,7 +232,7 @@ export async function checkForAppUpdate({ manual = false } = {}) {
|
||||
})
|
||||
if (response.statusCode !== 200) throw new Error('更新服务暂时不可用')
|
||||
|
||||
const update = normalizeUpdate(response.data?.data ?? response.data, runtime)
|
||||
const update = normalizeUpdate(response.data, runtime)
|
||||
if (update && isNewerUpdate(update, runtime)) {
|
||||
await runUpdate(update)
|
||||
} else if (manual) {
|
||||
|
||||
Reference in New Issue
Block a user