route.d.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. declare namespace AuthRoute {
  2. /** 根路由路径 */
  3. type RootRoutePath = '/';
  4. /** 捕获无效路由的路由路径 */
  5. type NotFoundRoutePath = '/:pathMatch(.*)*';
  6. type RootRouteKey = PageRoute.RootRouteKey;
  7. type NotFoundRouteKey = PageRoute.NotFoundRouteKey;
  8. type RouteKey = PageRoute.RouteKey;
  9. type LastDegreeRouteKey = PageRoute.LastDegreeRouteKey;
  10. type AllRouteKey = RouteKey | RootRouteKey | NotFoundRouteKey;
  11. /** 路由路径 */
  12. type RoutePath<K extends AllRouteKey = AllRouteKey> = AuthRouteUtils.GetRoutePath<K>;
  13. /**
  14. * 路由的组件
  15. * - basic - 基础布局,具有公共部分的布局
  16. * - blank - 空白布局
  17. * - multi - 多级路由布局(三级路由或三级以上时,除第一级路由和最后一级路由,其余的采用该布局)
  18. * - self - 作为子路由,使用自身的布局(作为最后一级路由,没有子路由)
  19. */
  20. type RouteComponentType = 'basic' | 'blank' | 'multi' | 'self';
  21. /** 路由描述 */
  22. interface RouteMeta<K extends AuthRoute.RoutePath> {
  23. /** 路由标题(可用来作document.title或者菜单的名称) */
  24. title: string;
  25. /** 用来支持多国语言 如果i18nTitle和title同时存在优先使用i18nTitle */
  26. i18nTitle?: string;
  27. /** 路由的动态路径(需要动态路径的页面需要将path添加进范型参数) */
  28. dynamicPath?: AuthRouteUtils.GetDynamicPath<K>;
  29. /** 作为单级路由的父级路由布局组件 */
  30. singleLayout?: Extract<RouteComponentType, 'basic' | 'blank'>;
  31. /** 需要登录权限 */
  32. requiresAuth?: boolean;
  33. /**
  34. * 哪些类型的用户有权限才能访问的路由(空的话则表示不需要权限)
  35. * @description 后端动态路由数据不需要该属性,直接由后端根据用户角色返回对应权限的路由数据
  36. */
  37. permissions?: Auth.RoleType[];
  38. /** 缓存页面 */
  39. keepAlive?: boolean;
  40. /** 菜单和面包屑对应的图标 */
  41. icon?: string;
  42. /** 使用本地svg作为的菜单和面包屑对应的图标(assets/svg-icon文件夹的的svg文件名) */
  43. localIcon?: string;
  44. /** 是否在菜单中隐藏(一些列表、表格的详情页面需要通过参数跳转,所以不能显示在菜单中) */
  45. hide?: boolean;
  46. /** 外链链接 */
  47. href?: string;
  48. /** 是否支持多个tab页签(默认一个,即相同name的路由会被替换) */
  49. multiTab?: boolean;
  50. /** 路由顺序,可用于菜单的排序 */
  51. order?: number;
  52. /** 当前路由需要选中的菜单项(用于跳转至不在左侧菜单显示的路由且需要高亮某个菜单的情况) */
  53. activeMenu?: RouteKey;
  54. /** 表示是否是多级路由的中间级路由(用于转换路由数据时筛选多级路由的标识,定义路由时不用填写) */
  55. multi?: boolean;
  56. /** 是否固定在tab卡不可关闭 */
  57. affix?: boolean;
  58. }
  59. type Route<K extends AllRouteKey = AllRouteKey> = K extends AllRouteKey
  60. ? {
  61. /** 路由名称(路由唯一标识) */
  62. name: K;
  63. /** 路由路径 */
  64. path: AuthRouteUtils.GetRoutePath<K>;
  65. /** 路由重定向 */
  66. redirect?: AuthRouteUtils.GetRoutePath;
  67. /**
  68. * 路由组件
  69. * - basic: 基础布局,具有公共部分的布局
  70. * - blank: 空白布局
  71. * - multi: 多级路由布局(三级路由或三级以上时,除第一级路由和最后一级路由,其余的采用该布局)
  72. * - self: 作为子路由,使用自身的布局(作为最后一级路由,没有子路由)
  73. */
  74. component?: RouteComponentType;
  75. /** 子路由 */
  76. children?: Route[];
  77. /** 路由描述 */
  78. meta: RouteMeta<RoutePath<K>>;
  79. } & Omit<import('vue-router').RouteRecordRaw, 'name' | 'path' | 'redirect' | 'component' | 'children' | 'meta'>
  80. : never;
  81. /** 前端导入的路由模块 */
  82. type RouteModule = Record<string, { default: Route }>;
  83. }
  84. declare namespace AuthRouteUtils {
  85. /** 路由key层级分割符 */
  86. type RouteKeySplitMark = '_';
  87. /** 路由path层级分割符 */
  88. type RoutePathSplitMark = '/';
  89. /** 空白字符串 */
  90. type BlankString = '';
  91. /** key转换成path */
  92. type KeyToPath<K extends string> = K extends `${infer _Left}${RouteKeySplitMark}${RouteKeySplitMark}${infer _Right}`
  93. ? never
  94. : K extends `${infer Left}${RouteKeySplitMark}${infer Right}`
  95. ? Left extends BlankString
  96. ? never
  97. : Right extends BlankString
  98. ? never
  99. : KeyToPath<`${Left}${RoutePathSplitMark}${Right}`>
  100. : `${RoutePathSplitMark}${K}`;
  101. /** 根据路由key获取路由路径 */
  102. type GetRoutePath<K extends AuthRoute.AllRouteKey = AuthRoute.AllRouteKey> = K extends AuthRoute.AllRouteKey
  103. ? K extends AuthRoute.RootRouteKey
  104. ? AuthRoute.RootRoutePath
  105. : K extends AuthRoute.NotFoundRouteKey
  106. ? AuthRoute.NotFoundRoutePath
  107. : KeyToPath<K>
  108. : never;
  109. /** 获取一级路由(有子路由的一级路由和没有子路由的路由) */
  110. type GetFirstDegreeRouteKey<K extends AuthRoute.RouteKey = AuthRoute.RouteKey> =
  111. K extends `${infer _Left}${RouteKeySplitMark}${infer _Right}` ? never : K;
  112. /** 获取有子路由的一级路由 */
  113. type GetFirstDegreeRouteKeyWithChildren<K extends AuthRoute.RouteKey = AuthRoute.RouteKey> =
  114. K extends `${infer Left}${RouteKeySplitMark}${infer _Right}` ? Left : never;
  115. /** 单级路由的key (单级路由需要添加一个父级路由用于应用布局组件) */
  116. type SingleRouteKey = Exclude<
  117. GetFirstDegreeRouteKey,
  118. GetFirstDegreeRouteKeyWithChildren | AuthRoute.RootRouteKey | AuthRoute.NotFoundRouteKey
  119. >;
  120. /** 单独路由父级路由key */
  121. type SingleRouteParentKey = `${SingleRouteKey}-parent`;
  122. /** 单独路由父级路由path */
  123. type SingleRouteParentPath = KeyToPath<SingleRouteParentKey>;
  124. /** 获取路由动态路径 */
  125. type GetDynamicPath<P extends AuthRoute.RoutePath> =
  126. | `${P}/:${string}`
  127. | `${P}/:${string}(${string})`
  128. | `${P}/:${string}(${string})?`;
  129. }