/** * 一次性补资料链接的路径解析。 * * 公开链接有两种形态: * - `https://<域名>/supplement/`:输入平台订单号; * - `https://<域名>/supplement/<平台订单号>`:直接打开对应订单的补资料页。 * 与后台 HashRouter 完全隔离。 */ export function isCollectMaterialLinkPath(pathname: unknown): boolean { return splitPathSegments(pathname)[0] === 'supplement' } /** * 只认 `/supplement/<一段 code>`: * - `/supplement/abc` 命中,结尾多余斜杠容忍; * - `/supplement`、`/supplement/a/b` 不命中(由入口页/页面自己展示无效)。 */ export function resolveCollectMaterialLinkCode(pathname: unknown): string { const segments = splitPathSegments(pathname) if (segments.length !== 2 || segments[0] !== 'supplement') return '' return decodePathSegment(segments[1]) } /** 统一拼补资料链接;origin 结尾多余斜杠会被去掉。 */ export function buildCollectMaterialLinkUrl(origin: unknown, code: unknown): string { const base = String(origin ?? '') .trim() .replace(/\/+$/, '') const normalized = String(code ?? '').trim() if (!base || !normalized) return '' return `${base}/supplement/${encodeURIComponent(normalized)}` } /** * 入口页输入归一化:去空格;如果用户粘贴了完整 `/supplement/<订单号>` 链接, * 自动提取最后一段,避免把整条 URL 当成订单号。 */ export function normalizeOrderNoInput(value: unknown): string { const raw = String(value ?? '').trim() if (!raw) return '' const marker = '/supplement/' const index = raw.indexOf(marker) if (index < 0) return raw const segment = raw .slice(index + marker.length) .split(/[?#]/)[0] .replace(/\/+$/, '') .trim() return decodePathSegment(segment) } function splitPathSegments(pathname: unknown): string[] { const normalized = String(pathname ?? '').trim() if (!normalized.startsWith('/')) return [] return normalized.split(/[?#]/)[0].replace(/\/+$/, '').split('/').filter(Boolean) } function decodePathSegment(segment: string): string { try { return decodeURIComponent(segment) } catch { return segment } }