From 58d9f460b2f8c34430285115e2557d18333c5cab Mon Sep 17 00:00:00 2001 From: Codex Assistant <codex@example.com> Date: 星期三, 08 十月 2025 14:16:55 +0800 Subject: [PATCH] feat: 修复Player实体phone字段数据冗余问题并优化小程序报名逻辑 --- web/src/utils/auth.ts | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 44 insertions(+), 0 deletions(-) diff --git a/web/src/utils/auth.ts b/web/src/utils/auth.ts index a55e90c..3cb965e 100644 --- a/web/src/utils/auth.ts +++ b/web/src/utils/auth.ts @@ -75,6 +75,50 @@ return !!(token && userInfo) } +/** + * 瑙f瀽骞跺垽鏂璊WT鏄惁杩囨湡 + * 瑙勫垯锛氳嫢token涓虹┖鎴栨棤娉曡В鏋恊xp锛岃涓鸿繃鏈燂紱exp鍗曚綅涓虹 + */ +export function isTokenExpired(token?: string | null): boolean { + if (!token) return true + try { + const parts = token.split('.') + if (parts.length !== 3) return true + const payloadJson = JSON.parse(decodeBase64Url(parts[1])) + const exp = payloadJson?.exp + if (!exp || typeof exp !== 'number') return true + const now = Math.floor(Date.now() / 1000) + return exp <= now + } catch (e) { + console.error('瑙f瀽JWT澶辫触:', e) + return true + } +} + +/** + * Base64Url 瑙g爜 + */ +function decodeBase64Url(input: string): string { + // 鏇挎崲URL瀹夊叏瀛楃骞惰ˉ榻�'=' + let base64 = input.replace(/-/g, '+').replace(/_/g, '/') + const pad = base64.length % 4 + if (pad) { + base64 += '='.repeat(4 - pad) + } + const decoded = atob(base64) + // 澶勭悊UTF-8 + try { + return decodeURIComponent( + decoded + .split('') + .map(c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)) + .join('') + ) + } catch { + return decoded + } +} + // 娓呴櫎鎵�鏈夎璇佹暟鎹� export function clearAuth(): void { removeToken() -- Gitblit v1.8.0