index.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import type { RouteRecordRaw } from 'vue-router';
  2. import type { App } from 'vue';
  3. import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router';
  4. import { basicRoutes } from './routes';
  5. // let userName = unref(userStore.getUserInfo).username;
  6. // 白名单应该包含基本静态路由
  7. const WHITE_NAME_LIST: string[] = [];
  8. const getRouteNames = (array: any[]) =>
  9. array.forEach((item) => {
  10. WHITE_NAME_LIST.push(item.name);
  11. getRouteNames(item.children || []);
  12. });
  13. getRouteNames(basicRoutes);
  14. // app router
  15. export const router = createRouter({
  16. history: createWebHistory(import.meta.env.VITE_PUBLIC_PATH),
  17. routes: basicRoutes as unknown as RouteRecordRaw[],
  18. strict: true,
  19. scrollBehavior: () => ({ left: 0, top: 0 }),
  20. });
  21. // TODO 【QQYUN-4517】【表单设计器】记录分享路由守卫测试
  22. // 存储当前页面的browseId(用于关联离开/进入日志)
  23. // const currentBrowseId = '';
  24. // router.beforeEach(async (to, from, next) => {
  25. // if (to.path === '/sys/log/addBrowseLog') {
  26. // // const url = '/sys/log/addBrowseLog';
  27. // // const currentPath = to.fullPath;
  28. // // // 生成时间戳函数
  29. // // const formatTimestamp = () => {
  30. // // const date = new Date();
  31. // // return [
  32. // // date.getFullYear(),
  33. // // String(date.getMonth() + 1).padStart(2, '0'),
  34. // // String(date.getDate()).padStart(2, '0'),
  35. // // String(date.getHours()).padStart(2, '0'),
  36. // // String(date.getMinutes()).padStart(2, '0'),
  37. // // String(date.getSeconds()).padStart(2, '0'),
  38. // // String(date.getMilliseconds()).padStart(3, '0'),
  39. // // ].join('');
  40. // // };
  41. // // // 1. 如果存在上一个页面的browseId,发送离开日志
  42. // // if (currentBrowseId && from.fullPath !== '/') {
  43. // // try {
  44. // // await defHttp.post({
  45. // // url,
  46. // // params: {
  47. // // browseId: currentBrowseId,
  48. // // isEnd: true,
  49. // // method: from.fullPath,
  50. // // },
  51. // // });
  52. // // console.log('离开页面日志记录成功');
  53. // // } catch (e) {
  54. // // console.error('离开页面日志记录失败:', e);
  55. // // }
  56. // // }
  57. // // // 2. 记录新页面进入日志
  58. // // currentBrowseId = formatTimestamp();
  59. // // try {
  60. // // await defHttp.post({
  61. // // url,
  62. // // params: {
  63. // // browseId: currentBrowseId,
  64. // // isEnd: false,
  65. // // method: to.fullPath,
  66. // // },
  67. // // });
  68. // // console.log('进入页面日志记录成功');
  69. // // } catch (e) {
  70. // // console.error('进入页面日志记录失败:', e);
  71. // // }
  72. // }
  73. // next();
  74. // });
  75. // reset router
  76. export function resetRouter() {
  77. router.getRoutes().forEach((route) => {
  78. const { name } = route;
  79. if (name && !WHITE_NAME_LIST.includes(name as string)) {
  80. router.hasRoute(name) && router.removeRoute(name);
  81. }
  82. });
  83. }
  84. // config router
  85. export function setupRouter(app: App<Element>) {
  86. app.use(router);
  87. }