index.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import type { RouteRecordRaw } from 'vue-router';
  2. import type { App } from 'vue';
  3. import { createRouter } from 'vue-router';
  4. import { basicRoutes } from './routes';
  5. import { createRouteHistory } from './helper/routeHelper';
  6. import { useMessage } from '@jeesite/core/hooks/web/useMessage';
  7. import { useGlobSetting } from '@jeesite/core/hooks/setting';
  8. import { useTabs } from '@jeesite/core/hooks/web/useTabs';
  9. import { useGo } from '@jeesite/core/hooks/web/usePage';
  10. import { initFramePage } from '@jeesite/core/layouts/iframe/useFrameKeepAlive';
  11. import { encryptByMd5 } from '@jeesite/core/utils/cipher';
  12. // import qs from 'qs';
  13. // 白名单应该包含基本静态路由
  14. const WHITE_NAME_LIST: string[] = [];
  15. const getRouteNames = (array: any[]) =>
  16. array.forEach((item) => {
  17. WHITE_NAME_LIST.push(item.name);
  18. getRouteNames(item.children || []);
  19. });
  20. getRouteNames(basicRoutes);
  21. // app router
  22. export const router = createRouter({
  23. history: createRouteHistory(),
  24. routes: basicRoutes as unknown as RouteRecordRaw[],
  25. strict: true,
  26. scrollBehavior: () => ({ left: 0, top: 0 }),
  27. });
  28. // reset router
  29. export function resetRouter() {
  30. router.getRoutes().forEach((route) => {
  31. const { name } = route;
  32. if (name && !WHITE_NAME_LIST.includes(name as string)) {
  33. router.hasRoute(name) && router.removeRoute(name);
  34. }
  35. });
  36. }
  37. function initTabPage() {
  38. const { showMessage, showMessageModal } = useMessage();
  39. const { ctxAdminPath } = useGlobSetting();
  40. const addFramePage = initFramePage();
  41. const go = useGo(router);
  42. window['tabPage'] = Object.assign(window['tabPage'] || {}, {
  43. addTabPage: async function (_$this: any, title: string, url: string) {
  44. if (url && !String(url).startsWith(ctxAdminPath)) {
  45. await go(url);
  46. return;
  47. }
  48. const idx = url.indexOf('?');
  49. const path = (idx == -1 ? url : url.substring(0, idx)).replace(ctxAdminPath, '');
  50. // const paramStr = idx == -1 ? '' : url.substring(idx + 1);
  51. // const params = (paramStr && paramStr != '' ? qs.parse(paramStr) : {});
  52. const name = encryptByMd5(url).substring(5, 15);
  53. const route = {
  54. meta: { frameSrc: url, title },
  55. path: path + '/' + name,
  56. name: 'JeeSite' + name,
  57. url,
  58. };
  59. addFramePage(route);
  60. await go(route.path);
  61. },
  62. getCurrentTabPage: function (currentTabCallback: Fn) {
  63. const route = router.currentRoute.value;
  64. const frame = document.querySelector(`.jeesite-iframe-page .${route.name as string}`);
  65. if (frame && typeof currentTabCallback == 'function') {
  66. try {
  67. currentTabCallback(frame['contentWindow']);
  68. } catch (e) {
  69. console.error(e);
  70. }
  71. }
  72. return frame;
  73. },
  74. getPrevTabPage: async function (preTabCallback: Fn, isCloseCurrentTab = false) {
  75. const { tabStore, closeCurrent } = useTabs(router);
  76. const index = tabStore.getTabList.findIndex((item) => item.path === router.currentRoute.value.path);
  77. if (index > 1) {
  78. const rotue = tabStore.getTabList[index - 1];
  79. const frame = document.querySelector(`.jeesite-iframe-page .${rotue.name as string}`);
  80. if (frame && typeof preTabCallback == 'function') {
  81. try {
  82. preTabCallback(frame['contentWindow']);
  83. } catch (e) {
  84. console.error(e);
  85. }
  86. }
  87. }
  88. if (isCloseCurrentTab) {
  89. setTimeout(closeCurrent, 0);
  90. }
  91. },
  92. closeCurrentTabPage: async function (preTabCallback: Fn) {
  93. await this.getPrevTabPage(preTabCallback, true);
  94. },
  95. });
  96. window['toastr'] = Object.assign(window['toastr'] || {}, {
  97. options: { positionClass: {}, timeOut: undefined },
  98. showMessage: function (msg: string, _title?: string, type = 'info') {
  99. if (this.options?.positionClass == 'toast-top-full-width') {
  100. return showMessage('posfull:' + msg, type, this.options?.timeOut);
  101. }
  102. return showMessage(msg, type, this.options?.timeOut);
  103. },
  104. error: function (msg: string, title?: string) {
  105. this.showMessage(msg, title, 'error');
  106. },
  107. warning: function (msg: string, title?: string) {
  108. this.showMessage(msg, title, 'warning');
  109. },
  110. success: function (msg: string, title?: string) {
  111. this.showMessage(msg, title, 'success');
  112. },
  113. info: function (msg: string, title?: string) {
  114. this.showMessage(msg, title, 'info');
  115. },
  116. });
  117. }
  118. // config router
  119. export function setupRouter(app: App<Element>) {
  120. app.use(router);
  121. initTabPage();
  122. }