Blame view

src/router/guard/index.ts 1.55 KB
陈文彬 authored
1
2
3
import type { Router } from 'vue-router';

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

  router.afterEach((to) => {
    // change html title
vben authored
42
    setTitle(to.meta.title, globSetting.title);
vben authored
43
44
  });
45
  openNProgress && createProgressGuard(router);
陈文彬 authored
46
47
48
  createPermissionGuard(router);
  createPageLoadingGuard(router);
}