feat(升级中心): 实现备用更新方案并优化版本检查逻辑
This commit is contained in:
@@ -1,31 +1,124 @@
|
||||
export default function() {
|
||||
// #ifdef APP-PLUS
|
||||
return new Promise((resolve, reject) => {
|
||||
plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
|
||||
let data = {
|
||||
action: 'checkVersion',
|
||||
appid: plus.runtime.appid,
|
||||
appVersion: plus.runtime.version,
|
||||
wgtVersion: widgetInfo.version
|
||||
}
|
||||
uniCloud.callFunction({
|
||||
name: 'uni-upgrade-center',
|
||||
data,
|
||||
success: (e) => {
|
||||
resolve(e)
|
||||
},
|
||||
fail: (error) => {
|
||||
reject(error)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
// #endif
|
||||
// #ifndef APP-PLUS
|
||||
return new Promise((resolve, reject) => {
|
||||
reject({
|
||||
message: '请在App中使用'
|
||||
})
|
||||
})
|
||||
// #endif
|
||||
}
|
||||
import { requestUpdatePackage } from '@/api/modules/sys'
|
||||
import { getMaxVersion } from './tools'
|
||||
import { getFileExtension } from '@/utils'
|
||||
|
||||
/**
|
||||
* 统一检查更新入口
|
||||
*/
|
||||
export default async function checkUpdate() {
|
||||
// #ifdef APP-PLUS
|
||||
try {
|
||||
const upgradeData = await getUpgradeCheckData()
|
||||
|
||||
// 优先调用 uni-upgrade-center(3 秒超时)
|
||||
const result = await withTimeout(
|
||||
callUpgradeCenter(upgradeData),
|
||||
3000
|
||||
)
|
||||
|
||||
console.log('检查版本更新成功:', result)
|
||||
return result
|
||||
} catch (err) {
|
||||
console.warn('uniCloud更新方案失败,启用备用方案:', err?.message)
|
||||
return await useBackupUpdate()
|
||||
}
|
||||
// #endif
|
||||
|
||||
// #ifndef APP-PLUS
|
||||
throw { message: '请在 App 中使用' }
|
||||
// #endif
|
||||
}
|
||||
|
||||
/**
|
||||
* 备用更新方案
|
||||
*/
|
||||
async function useBackupUpdate() {
|
||||
const currentVersion = await getCurrentVersion()
|
||||
|
||||
if (!currentVersion) {
|
||||
throw { message: '获取当前版本号失败' }
|
||||
}
|
||||
|
||||
console.log('当前版本号:', currentVersion)
|
||||
|
||||
const res = await requestUpdatePackage('10', currentVersion)
|
||||
|
||||
if (!res || !res.updateUrl) {
|
||||
throw { message: '没有匹配的更新包,当前版本无需升级,如您确定有更新,请卸载本版本后前往应用市场重新安装' }
|
||||
}
|
||||
|
||||
// 将服务器返回的更新信息转换为 uni-upgrade-center 格式
|
||||
return {
|
||||
result: {
|
||||
...res,
|
||||
url: res.updateUrl,
|
||||
platform: ['Android', 'Ios'],
|
||||
type: getFileExtension(res.updateUrl),
|
||||
is_mandatory: true,
|
||||
is_backup_update: true,
|
||||
title: "更新",
|
||||
contents: "当前版本已经弃用,请立即更新",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* uni-upgrade-center 调用 Promise 化
|
||||
*/
|
||||
function callUpgradeCenter(data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ export default function() {
|
||||
if (!e.result) return;
|
||||
const {
|
||||
code,
|
||||
is_backup_update, // 是否备用更新
|
||||
message,
|
||||
is_silently, // 是否静默更新
|
||||
url, // 安装包下载地址
|
||||
@@ -17,17 +18,20 @@ export default function() {
|
||||
type // 安装包类型
|
||||
} = e.result;
|
||||
|
||||
// 此处逻辑仅为实例,可自行编写
|
||||
if (code > 0) {
|
||||
// 腾讯云和阿里云下载链接不同,需要处理一下,阿里云会原样返回
|
||||
const hasUpdate = code > 0 || is_backup_update
|
||||
|
||||
// 如果不是备用更新,需要处理下载链接
|
||||
if (!is_backup_update) {
|
||||
const {
|
||||
fileList
|
||||
} = await uniCloud.getTempFileURL({
|
||||
fileList: [url]
|
||||
});
|
||||
if (fileList[0].tempFileURL)
|
||||
e.result.url = fileList[0].tempFileURL;
|
||||
|
||||
e.result.url = fileList[0].tempFileURL;
|
||||
}
|
||||
|
||||
// 此处逻辑仅为实例,可自行编写
|
||||
if (hasUpdate) {
|
||||
resolve(e)
|
||||
|
||||
// 静默更新,只有wgt有
|
||||
|
||||
32
uni_modules/uni-upgrade-center-app/utils/tools.js
Normal file
32
uni_modules/uni-upgrade-center-app/utils/tools.js
Normal file
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* 比较版本号
|
||||
* @param {string} v1 - 版本号1
|
||||
* @param {string} v2 - 版本号2
|
||||
* @returns {number} - 1表示v1大于v2,-1表示v1小于v2,0表示相等
|
||||
*/
|
||||
export function compareVersion(v1, v2) {
|
||||
const arr1 = v1.split('.').map(Number)
|
||||
const arr2 = v2.split('.').map(Number)
|
||||
const maxLen = Math.max(arr1.length, arr2.length)
|
||||
|
||||
for (let i = 0; i < maxLen; i++) {
|
||||
const n1 = arr1[i] ?? 0
|
||||
const n2 = arr2[i] ?? 0
|
||||
|
||||
if (n1 > n2) return 1
|
||||
if (n1 < n2) return -1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取较大版本号
|
||||
* @param {string} v1 - 版本号1
|
||||
* @param {string} v2 - 版本号2
|
||||
* @returns {string} - 较大的版本号
|
||||
*/
|
||||
export function getMaxVersion(v1, v2) {
|
||||
return compareVersion(v1, v2) >= 0 ? v1 : v2
|
||||
}
|
||||
Reference in New Issue
Block a user