flow.model.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. const ROUTE_START = '/screen';
  2. const Routes = {
  3. 'patient_file': /* 建档页 */ '/register',
  4. 'tongueface_upload': /*拍照页*/ '/camera',
  5. 'tongueface_upload_result': /* [虚拟] 拍照结果页 */ '/camera/result',
  6. 'tongueface_analysis': /* 问卷页 */ '/questionnaire',
  7. 'tongueface_analysis_result': /* 舌面象分析报告页 */ '/report/analysis',
  8. 'health_analysis': /* 健康报告页 */ '/report',
  9. 'pulse_upload': /* 脉诊页 */ '/pulse',
  10. 'pulse_upload_result': /* 脉诊结果页 */ '/pulse/result',
  11. 'alcohol_upload_result': /* 酒精结果页 */ '/alcohol/result',
  12. 'screen': ROUTE_START,
  13. } as const;
  14. const analysisFlowMap = new Map<FlowKey, string>([
  15. ['/alcohol/result', 'alcohol'],
  16. ]);
  17. const ANALYSIS_EXTEND_FLOW = 'ANALYSIS_EXTEND_FLOW';
  18. export type FlowKey = (typeof Routes)[keyof typeof Routes];
  19. export interface Flow {
  20. route: FlowKey;
  21. title?: string;
  22. optional?: boolean;
  23. }
  24. export type FlowRoute = { next: Flow; prev: Flow | null; value: Flow };
  25. export class FlowMap extends Map<FlowKey, Flow> {
  26. private readonly string: string = '';
  27. constructor(options: string[]) {
  28. super();
  29. this.string = options.join('->');
  30. const length = options.length;
  31. if (length === 0) return;
  32. // 修正虚拟路由
  33. const k1 = 'tongueface_upload';
  34. const k2 = 'tongueface_upload_result';
  35. if ( !options.includes(k2) && options[ length - 1 ] === k1 ) options.push(k2);
  36. options.unshift(ROUTE_START);
  37. options.push('screen?返回首页');
  38. const analysisFlowSet = new Set<string>();
  39. for ( let i = 1; i < options.length; i++ ) {
  40. const path = options[i];
  41. const optional = path.endsWith('?');
  42. const [name, title] = path.split('?');
  43. const route = (options[i] = getPath(name));
  44. this.set(<FlowKey>options[i - 1], { route, optional, title: title || void 0});
  45. if (analysisFlowMap.has(route)) analysisFlowSet.add(analysisFlowMap.get(route)!);
  46. }
  47. sessionStorage.setItem(ANALYSIS_EXTEND_FLOW, Array.from(analysisFlowSet).filter(Boolean).join(','));
  48. }
  49. parse(key: FlowKey): FlowRoute {
  50. const next = this.get(key);
  51. if (!next) throw { message: `[路由] 配置异常无法解析正确路径,请联系管理员! (${this})` };
  52. if (key === ROUTE_START) return { next, prev: null, value: { route: '/screen', optional: false, title: '' } };
  53. const keys = [...this.keys()];
  54. const values = [...this.values()];
  55. const v = values.findIndex((flow) => flow.route === key);
  56. const k = values.findIndex((flow) => flow.route === keys[v]);
  57. return { next, prev: values[k], value: values[v] };
  58. }
  59. toString(): string {
  60. return this.string || `未配置路由`;
  61. }
  62. }
  63. export function getPath(value?: string): FlowKey {
  64. return Routes[value as keyof typeof Routes];
  65. }
  66. export function getAnalysisExtendFlowValue(key = ANALYSIS_EXTEND_FLOW) {
  67. return sessionStorage.getItem(key) || ''
  68. }