feat(mini-user): show version on mine page and force exit when below min (v3.4.13)
Display v3.4.x next to brand on mine page; below minClientVersion prompt update via UpdateManager or exit mini program after acknowledge. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -4,6 +4,8 @@ import { fetchClientConfig } from './pay-wechat';
|
||||
/** 与 package.json version 同步,供服务端 minClientVersion 比对 */
|
||||
export const APP_VERSION = '3.4.13';
|
||||
|
||||
export const APP_VERSION_LABEL = `v${APP_VERSION}`;
|
||||
|
||||
function parseSemver(v: string): number[] {
|
||||
return v.split('.').map((n) => parseInt(n, 10) || 0);
|
||||
}
|
||||
@@ -20,17 +22,76 @@ export function compareSemver(a: string, b: string): number {
|
||||
return 0;
|
||||
}
|
||||
|
||||
function exitMiniProgramIfSupported() {
|
||||
if (process.env.TARO_ENV !== 'weapp') return;
|
||||
if (typeof Taro.exitMiniProgram !== 'function') return;
|
||||
void Taro.exitMiniProgram({});
|
||||
}
|
||||
|
||||
/** 微信 CDN 有新包时引导 applyUpdate;返回是否已触发更新流程 */
|
||||
function tryWeappForceUpdate(min: string): Promise<boolean> {
|
||||
if (process.env.TARO_ENV !== 'weapp' || typeof Taro.getUpdateManager !== 'function') {
|
||||
return Promise.resolve(false);
|
||||
}
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const manager = Taro.getUpdateManager();
|
||||
let settled = false;
|
||||
const finish = (updated: boolean) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
resolve(updated);
|
||||
};
|
||||
|
||||
manager.onCheckForUpdate((res) => {
|
||||
if (!res.hasUpdate) {
|
||||
finish(false);
|
||||
return;
|
||||
}
|
||||
manager.onUpdateReady(() => {
|
||||
void Taro.showModal({
|
||||
title: '版本过低',
|
||||
content: `当前 ${APP_VERSION_LABEL} 低于要求版本 v${min},已检测到新版本,请立即更新后继续使用。`,
|
||||
showCancel: false,
|
||||
confirmText: '立即更新',
|
||||
success: (r) => {
|
||||
if (r.confirm) {
|
||||
manager.applyUpdate();
|
||||
finish(true);
|
||||
} else {
|
||||
finish(false);
|
||||
}
|
||||
},
|
||||
fail: () => finish(false),
|
||||
});
|
||||
});
|
||||
manager.onUpdateFailed(() => finish(false));
|
||||
});
|
||||
|
||||
setTimeout(() => finish(false), 4000);
|
||||
});
|
||||
}
|
||||
|
||||
async function enforceMinClientVersion(min: string) {
|
||||
const updated = await tryWeappForceUpdate(min);
|
||||
if (updated) return;
|
||||
|
||||
await Taro.showModal({
|
||||
title: '版本过低',
|
||||
content: `当前 ${APP_VERSION_LABEL} 低于要求版本 v${min},请更新至最新版本后继续使用。`,
|
||||
showCancel: false,
|
||||
confirmText: '我知道了',
|
||||
});
|
||||
|
||||
exitMiniProgramIfSupported();
|
||||
}
|
||||
|
||||
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: '我知道了',
|
||||
});
|
||||
await enforceMinClientVersion(min);
|
||||
}
|
||||
} catch {
|
||||
/* 配置拉取失败不阻塞启动 */
|
||||
|
||||
@@ -23,6 +23,7 @@ import {
|
||||
} from '../../lib/mini-wechat-profile';
|
||||
import { isLoggedIn, logout, request, toast, type UserProfile } from '../../lib/api';
|
||||
import { isWechatEnv } from '../../lib/weixin';
|
||||
import { APP_VERSION_LABEL } from '../../lib/client-version';
|
||||
import { maskPhone } from '../../lib/phone';
|
||||
import {
|
||||
DEFAULT_SHARE_DESC,
|
||||
@@ -499,7 +500,7 @@ export default function MinePage() {
|
||||
</View>
|
||||
|
||||
<View className="mine-footer">
|
||||
<Text className="mine-version">杜康好客</Text>
|
||||
<Text className="mine-version">杜康好客 {APP_VERSION_LABEL}</Text>
|
||||
<Text className="mine-logout" onClick={() => logout()}>
|
||||
退出登录
|
||||
</Text>
|
||||
|
||||
Reference in New Issue
Block a user