const ROUTE_START = '/screen'; const Routes = { 'patient_file': /* 建档页 */ '/register', 'tongueface_upload': /*拍照页*/ '/camera', 'tongueface_upload_result': /* [虚拟] 拍照结果页 */ '/camera/result', 'tongueface_analysis': /* 问卷页 */ '/questionnaire', 'tongueface_analysis_result': /* 舌面象分析报告页 */ '/report/analysis', 'health_analysis': /* 健康报告页 */ '/report', 'pulse_upload': /* 脉诊页 */ '/pulse', 'pulse_upload_result': /* 脉诊结果页 */ '/pulse/result', 'alcohol_upload_result': /* 酒精结果页 */ '/alcohol/result', 'screen': ROUTE_START, } as const; const analysisFlowMap = new Map([ ['/alcohol/result', 'alcohol'], ]); const ANALYSIS_EXTEND_FLOW = 'ANALYSIS_EXTEND_FLOW'; export type FlowKey = (typeof Routes)[keyof typeof Routes]; export interface Flow { route: FlowKey; title?: string; optional?: boolean; } export type FlowRoute = { next: Flow; prev: Flow | null; value: Flow }; export class FlowMap extends Map { private readonly string: string = ''; constructor(options: string[]) { super(); this.string = options.join('->'); const length = options.length; if (length === 0) return; // 修正虚拟路由 const k1 = 'tongueface_upload'; const k2 = 'tongueface_upload_result'; if ( !options.includes(k2) && options[ length - 1 ] === k1 ) options.push(k2); options.unshift(ROUTE_START); options.push('screen?返回首页'); const analysisFlowSet = new Set(); for ( let i = 1; i < options.length; i++ ) { const path = options[i]; const optional = path.endsWith('?'); const [name, title] = path.split('?'); const route = (options[i] = getPath(name)); this.set(options[i - 1], { route, optional, title: title || void 0}); if (analysisFlowMap.has(route)) analysisFlowSet.add(analysisFlowMap.get(route)!); } sessionStorage.setItem(ANALYSIS_EXTEND_FLOW, Array.from(analysisFlowSet).filter(Boolean).join(',')); } parse(key: FlowKey): FlowRoute { const next = this.get(key); if (!next) throw { message: `[路由] 配置异常无法解析正确路径,请联系管理员! (${this})` }; if (key === ROUTE_START) return { next, prev: null, value: { route: '/screen', optional: false, title: '' } }; const keys = [...this.keys()]; const values = [...this.values()]; const v = values.findIndex((flow) => flow.route === key); const k = values.findIndex((flow) => flow.route === keys[v]); return { next, prev: values[k], value: values[v] }; } toString(): string { return this.string || `未配置路由`; } } export function getPath(value?: string): FlowKey { return Routes[value as keyof typeof Routes]; } export function getAnalysisExtendFlowValue(key = ANALYSIS_EXTEND_FLOW) { return sessionStorage.getItem(key) || '' }