Blame view

src/router/guard/index.ts 1.77 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
vben authored
9
import { useGlobSetting, useProjectSetting } from '/@/hooks/setting';
vben authored
10
vben authored
11
import { getIsOpenTab, getRoute } from '/@/router/helper/routeHelper';
vben authored
12
import { setTitle } from '/@/utils/browser';
vben authored
13
import { AxiosCanceler } from '/@/utils/http/axios/axiosCancel';
陈文彬 authored
14
15
import { tabStore } from '/@/store/modules/tab';
vben authored
16
import { useI18n } from '/@/hooks/web/useI18n';
vben authored
17
import { REDIRECT_NAME } from '/@/router/constant';
18
19
const { closeMessageOnSwitch, removeAllHttpPending } = useProjectSetting();
vben authored
20
const globSetting = useGlobSetting();
vben authored
21
陈文彬 authored
22
export function createGuard(router: Router) {
vben authored
23
  let axiosCanceler: Nullable<AxiosCanceler>;
24
25
26
  if (removeAllHttpPending) {
    axiosCanceler = new AxiosCanceler();
  }
vben authored
27
28

  createPageLoadingGuard(router);
29
  router.beforeEach(async (to) => {
30
    // Determine whether the tab has been opened
vben authored
31
    const isOpen = getIsOpenTab(to.fullPath);
32
    to.meta.inTab = isOpen;
33
34

    // Notify routing changes
vben authored
35
    tabStore.commitLastChangeRouteState(getRoute(to));
36
陈文彬 authored
37
    try {
38
39
40
41
      if (closeMessageOnSwitch) {
        Modal.destroyAll();
        notification.destroy();
      }
陈文彬 authored
42
      // Switching the route will delete the previous request
43
      removeAllHttpPending && axiosCanceler!.removeAllPending();
陈文彬 authored
44
45
46
    } catch (error) {
      console.warn('basic guard error:' + error);
    }
vben authored
47
    return true;
陈文彬 authored
48
  });
vben authored
49
50

  router.afterEach((to) => {
vben authored
51
    const { t } = useI18n();
vben authored
52
    // change html title
vben authored
53
    to.name !== REDIRECT_NAME && setTitle(t(to.meta.title), globSetting.title);
vben authored
54
  });
vben authored
55
  createProgressGuard(router);
陈文彬 authored
56
57
  createPermissionGuard(router);
}