All checks were successful
Build and Deploy Front / deploy (push) Successful in 4m3s
新增单号规则截取模式与本地扫描引擎;登录图形验证码;吉祥物空态;开发态从 Router 自动回退 PC/H5 刷新直链。 Co-authored-by: Cursor <cursoragent@cursor.com>
123 lines
3.9 KiB
TypeScript
123 lines
3.9 KiB
TypeScript
/**
|
||
* 开发态 SPA 直链/刷新回退:从 Vue Router 配置自动收集页面路径。
|
||
* 新增页面只要写进 router(newModules 或 index.ts),无需再改 vite 白名单。
|
||
*
|
||
* 匹配策略:
|
||
* - 精确匹配页面 path(避免 /system/approval/definition/list 等「页下挂 API」被误回退)
|
||
* - 含 :param 的路由按前缀匹配(如 /h5/admin/orders/:id)
|
||
* - `/h5` 整树一律 SPA(H5 无同前缀后端 API,刷新/深链必须回退;含未登记的新 H5 子页)
|
||
*/
|
||
import fs from 'node:fs';
|
||
import path from 'node:path';
|
||
|
||
export type DevSpaRouteIndex = {
|
||
exact: Set<string>;
|
||
prefixes: Set<string>;
|
||
/** 调试/日志用 */
|
||
sourceFiles: string[];
|
||
};
|
||
|
||
const SKIP_PATHS = new Set(['', ':pathMatch(.*)*', '/:pathMatch(.*)*']);
|
||
|
||
function stripComments(src: string): string {
|
||
return src.replace(/\/\*[\s\S]*?\*\//g, '').replace(/\/\/.*$/gm, '');
|
||
}
|
||
|
||
function normalizePath(p: string): string {
|
||
if (!p || p === '/') return '/';
|
||
return `/${p}`.replace(/\/+/g, '/').replace(/\/$/, '') || '/';
|
||
}
|
||
|
||
function addRoutePath(
|
||
fullRaw: string,
|
||
exact: Set<string>,
|
||
prefixes: Set<string>,
|
||
): void {
|
||
if (SKIP_PATHS.has(fullRaw) || fullRaw.includes('pathMatch')) return;
|
||
const full = normalizePath(fullRaw);
|
||
if (full.includes(':')) {
|
||
const prefix = normalizePath(full.split('/:')[0] || full);
|
||
if (prefix && prefix !== '/') {
|
||
prefixes.add(prefix);
|
||
exact.add(prefix);
|
||
}
|
||
return;
|
||
}
|
||
exact.add(full);
|
||
}
|
||
|
||
/** 本仓库路由写法:一个绝对根 path + 若干相对 children(children 可自带 a/b)。 */
|
||
function collectPathsFromSource(
|
||
src: string,
|
||
exact: Set<string>,
|
||
prefixes: Set<string>,
|
||
): void {
|
||
const code = stripComments(src);
|
||
const re = /path:\s*['"]([^'"]+)['"]/g;
|
||
let currentRoot: string | null = null;
|
||
let m: RegExpExecArray | null;
|
||
while ((m = re.exec(code))) {
|
||
const p = m[1];
|
||
if (p.startsWith('/')) {
|
||
currentRoot = normalizePath(p);
|
||
addRoutePath(currentRoot, exact, prefixes);
|
||
continue;
|
||
}
|
||
if (!currentRoot) continue;
|
||
addRoutePath(`${currentRoot}/${p}`, exact, prefixes);
|
||
}
|
||
}
|
||
|
||
export function loadDevSpaRouteIndex(projectRoot: string): DevSpaRouteIndex {
|
||
const exact = new Set<string>(['/', '/login', '/admin']);
|
||
const prefixes = new Set<string>();
|
||
const sourceFiles: string[] = [];
|
||
|
||
const modulesDir = path.join(projectRoot, 'src/router/routes/newModules');
|
||
if (fs.existsSync(modulesDir)) {
|
||
for (const file of fs.readdirSync(modulesDir)) {
|
||
if (!file.endsWith('.ts')) continue;
|
||
const full = path.join(modulesDir, file);
|
||
sourceFiles.push(full);
|
||
collectPathsFromSource(fs.readFileSync(full, 'utf8'), exact, prefixes);
|
||
}
|
||
}
|
||
|
||
const indexFile = path.join(projectRoot, 'src/router/index.ts');
|
||
if (fs.existsSync(indexFile)) {
|
||
sourceFiles.push(indexFile);
|
||
collectPathsFromSource(fs.readFileSync(indexFile, 'utf8'), exact, prefixes);
|
||
}
|
||
|
||
return { exact, prefixes, sourceFiles };
|
||
}
|
||
|
||
/** 无同前缀 API、整树可安全回退的前端命名空间 */
|
||
const SPA_NAMESPACE_PREFIXES = ['/h5'] as const;
|
||
|
||
export function createDevSpaPathMatcher(projectRoot: string) {
|
||
const index = loadDevSpaRouteIndex(projectRoot);
|
||
// 保证 H5 根在 exact 里,便于排查
|
||
index.exact.add('/h5');
|
||
index.exact.add('/h5/login');
|
||
index.exact.add('/h5/admin');
|
||
index.exact.add('/h5/admin/login');
|
||
|
||
const isSpaPath = (urlOrPath: string): boolean => {
|
||
const raw = (urlOrPath || '').split('?')[0] || '';
|
||
const p = normalizePath(raw);
|
||
|
||
// H5:整树回退(客户中心 + 管理端 ops,含深链 /h5/admin/orders/123)
|
||
for (const ns of SPA_NAMESPACE_PREFIXES) {
|
||
if (p === ns || p.startsWith(`${ns}/`)) return true;
|
||
}
|
||
|
||
if (index.exact.has(p)) return true;
|
||
for (const prefix of index.prefixes) {
|
||
if (p === prefix || p.startsWith(`${prefix}/`)) return true;
|
||
}
|
||
return false;
|
||
};
|
||
return { index, isSpaPath };
|
||
}
|