23 lines
715 B
TypeScript
23 lines
715 B
TypeScript
/** 从微信扫码结果解析核销 token(32 位 hex 或带 token 参数的 URL) */
|
||
export function parseRedeemTokenFromScan(raw: string): string | null {
|
||
const trimmed = raw.trim();
|
||
if (!trimmed) return null;
|
||
|
||
if (/^[a-f0-9]{32}$/i.test(trimmed)) {
|
||
return trimmed.toLowerCase();
|
||
}
|
||
|
||
try {
|
||
const url = trimmed.startsWith('http') ? new URL(trimmed) : new URL(trimmed, 'https://local.invalid');
|
||
const fromQuery = url.searchParams.get('token');
|
||
if (fromQuery && /^[a-f0-9]{32}$/i.test(fromQuery)) {
|
||
return fromQuery.toLowerCase();
|
||
}
|
||
} catch {
|
||
/* not a URL */
|
||
}
|
||
|
||
const hexMatch = trimmed.match(/[a-f0-9]{32}/i);
|
||
return hexMatch ? hexMatch[0].toLowerCase() : null;
|
||
}
|