schangxiang@126.com
2025-09-19 9be9c3784b2881a3fa25e93ae2033dc2803c0ed0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import router from "@/router/index-user.js";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import { userInfoCookie } from "@/utils/auth";
import store from "@/store";
 
NProgress.configure({
  showSpinner: false
}); // NProgress Configuration
 
const whiteList = [
  "/login",
  "/reg",
  "/forget-pwd",
  "/auth-redirect",
  "/print",
  "/inbound/purchase/print-barcode",
  // 网站
  "/index",
  "/upload-card-id",
  "/contact-us",
  "/business",
  "/store-search",
  "/way-search",
  "/newsinfo"
]; // no redirect whitelist
 
/* *
 * 查找菜单是否有权限
 * @param routes asyncRouterMap
 * @param path
 */
function findRouter(routes, path, parentPath) {
  let isExist = false;
 
  routes.forEach(route => {
    var currentPath = route.path;
    if (currentPath.indexOf("/") !== 0) {
      currentPath = parentPath + "/" + currentPath;
    }
    if (currentPath.toLowerCase() === path.toLowerCase()) {
      isExist = true;
      return true;
    } else {
      if (route.children) {
        if (findRouter(route.children, path, currentPath)) {
          isExist = true;
        }
      }
    }
  });
 
  return isExist;
}
 
/* *
 * 校验是否存在白名单中
 * @param path
 */
function checkWhite(path) {
  var isOK = false;
  whiteList.forEach(item => {
    if (path.toLowerCase().indexOf(item) >= 0) isOK = true;
  });
  return isOK;
}
 
router.beforeEach((to, from, next) => {
  NProgress.start(); // start progress bar
  if (checkWhite(to.path)) {
    // 在免登录白名单,直接进入
    const userInfo = store.getters.userInfo;
    if (!userInfo || !userInfo.accessToken) {
      store.dispatch("GetUserInfo").then(res => {
        next();
        NProgress.done();
        return;
      });
    } else {
      next();
      NProgress.done();
    }
    return;
  }
  var userInfo = userInfoCookie.getUserInfo();
  if (userInfo && userInfo.accessToken) {
    // determine if there has token
    /* has token */
    if (to.path === "/login") {
      next();
      NProgress.done(); // if current page is dashboard will not trigger    afterEach hook, so manually handle it
    } else {
      const userInfo = store.getters.userInfo;
      // var addRouters = store.getters.routers;
      if (!userInfo || !userInfo.accessToken) {
        store.dispatch("GetUserInfo").then(res => {
          if (!res.menuList) {
            next(`/login?redirect=${to.path}`); // 否则全部重定向到登录页
            NProgress.done(); // if current page is login will not trigger afterEach hook, so manually handle it
            return;
          }
          // 生成菜单
          store.dispatch("GenerateUserRoutes", res.menuList).then(() => {
            // 根据roles权限生成可访问的路由表
            /* var routers = store.getters.routers;
            if (!routers.length) {
              Message.error('没有设置任何权限!');
              next(`/login?redirect=${to.path}`) // 全部重定向到登录页
            }
            router.addRoutes(routers); // 动态添加可访问路由表 */
            next({ ...to, replace: true });
          });
        });
      } else {
        var routes = store.getters.permission_routers;
        const existRoute = findRouter(routes, to.path, "");
 
        if (existRoute) {
          next();
        } else {
          next({
            path: "/401",
            replace: true,
            query: {
              noGoBack: true
            }
          });
        }
      }
      NProgress.done(); // finish progress bar
    }
  } else {
    next(`/login?redirect=${to.path}`); // 否则全部重定向到登录页
    NProgress.done(); // if current page is login will not trigger afterEach hook, so manually handle it
  }
});
 
router.afterEach(() => {
  NProgress.done(); // finish progress bar
});