1c4b986924
Co-authored-by: Cursor <cursoragent@cursor.com>
71 lines
2.0 KiB
TypeScript
71 lines
2.0 KiB
TypeScript
import Taro from '@tarojs/taro';
|
|
import { fetchClientConfig } from './pay-wechat';
|
|
|
|
/** 与 package.json version 同步,供服务端 minClientVersion 比对 */
|
|
export const APP_VERSION = '3.4.13';
|
|
|
|
function parseSemver(v: string): number[] {
|
|
return v.split('.').map((n) => parseInt(n, 10) || 0);
|
|
}
|
|
|
|
export function compareSemver(a: string, b: string): number {
|
|
const pa = parseSemver(a);
|
|
const pb = parseSemver(b);
|
|
const len = Math.max(pa.length, pb.length);
|
|
for (let i = 0; i < len; i += 1) {
|
|
const da = pa[i] ?? 0;
|
|
const db = pb[i] ?? 0;
|
|
if (da !== db) return da - db;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
export async function checkClientVersionGate() {
|
|
try {
|
|
const config = await fetchClientConfig();
|
|
const min = config.minClientVersion?.trim();
|
|
if (min && compareSemver(APP_VERSION, min) < 0) {
|
|
await Taro.showModal({
|
|
title: '版本过低',
|
|
content: '当前小程序版本过低,请更新至最新版本后继续使用。',
|
|
showCancel: false,
|
|
confirmText: '我知道了',
|
|
});
|
|
}
|
|
} catch {
|
|
/* 配置拉取失败不阻塞启动 */
|
|
}
|
|
}
|
|
|
|
export function setupWeappUpdateManager() {
|
|
if (process.env.TARO_ENV !== 'weapp') return;
|
|
if (typeof Taro.getUpdateManager !== 'function') return;
|
|
|
|
const manager = Taro.getUpdateManager();
|
|
manager.onCheckForUpdate((res) => {
|
|
if (!res.hasUpdate) return;
|
|
manager.onUpdateReady(() => {
|
|
void Taro.showModal({
|
|
title: '更新提示',
|
|
content: '新版本已准备好,是否重启应用?',
|
|
confirmText: '立即更新',
|
|
success: (r) => {
|
|
if (r.confirm) manager.applyUpdate();
|
|
},
|
|
});
|
|
});
|
|
manager.onUpdateFailed(() => {
|
|
void Taro.showModal({
|
|
title: '更新失败',
|
|
content: '新版本下载失败,请删除小程序后重新搜索打开。',
|
|
showCancel: false,
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
export function initClientVersionChecks() {
|
|
setupWeappUpdateManager();
|
|
void checkClientVersionGate();
|
|
}
|