Blame view

src/router/guard/index.ts 1.47 KB
陈文彬 authored
1
2
3
4
5
6
7
8
import type { Router } from 'vue-router';

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