index.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import type { Router } from 'vue-router';
  2. import { Modal, notification } from 'ant-design-vue';
  3. import { AxiosCanceler } from '/@/utils/http/axios/axiosCancel';
  4. import { createProgressGuard } from './progressGuard';
  5. import { createPermissionGuard } from './permissionGuard';
  6. import { createPageLoadingGuard } from './pageLoadingGuard';
  7. import { useSetting } from '/@/hooks/core/useSetting';
  8. import { getIsOpenTab, setCurrentTo } from '/@/utils/helper/routeHelper';
  9. import { setTitle } from '/@/utils/browser';
  10. const { projectSetting, globSetting } = useSetting();
  11. export function createGuard(router: Router) {
  12. const { openNProgress, closeMessageOnSwitch, removeAllHttpPending } = projectSetting;
  13. let axiosCanceler: AxiosCanceler | null;
  14. if (removeAllHttpPending) {
  15. axiosCanceler = new AxiosCanceler();
  16. }
  17. router.beforeEach(async (to) => {
  18. const isOpen = getIsOpenTab(to.fullPath);
  19. to.meta.inTab = isOpen;
  20. try {
  21. if (closeMessageOnSwitch) {
  22. Modal.destroyAll();
  23. notification.destroy();
  24. }
  25. // TODO Some special interfaces require long connections
  26. // Switching the route will delete the previous request
  27. removeAllHttpPending && axiosCanceler!.removeAllPending();
  28. } catch (error) {
  29. console.warn('basic guard error:' + error);
  30. }
  31. setCurrentTo(to);
  32. return true;
  33. });
  34. router.afterEach((to) => {
  35. // change html title
  36. setTimeout(() => {
  37. setTitle(to.meta.title, globSetting.title);
  38. }, 0);
  39. });
  40. openNProgress && createProgressGuard(router);
  41. createPermissionGuard(router);
  42. createPageLoadingGuard(router);
  43. }