permission.js
2.55 KB
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
import NProgress from "nprogress"
import router from "./index"
import store from "../store"
//  获取角色信息,根据用户权限动态加载路由
router.beforeEach((to, from, next) => {
  NProgress.start()
  if (store.getters.token) {
    // if (to.path === "/login") {
    next()
    // } else {
    // if (!store.getters.info.permissions) {
    // (async function getAddRouters() {
    //   // 省略 axios 请求代码 通过 token 向后台请求用户权限等信息,这里用假数据赋值
    //   const userInfo = {
    //     permissions: [
    //       {
    //         role: "superAdmin",
    //         roleName: "超级管理员"
    //       }
    //     ],
    //     name: "李老师",
    //     // avatar: data.avatar ? data.avatar : "",
    //     // uid: data.id,
    //     authorityRouter: ["examinationPaper",
    //       "examinationPaperAdd",
    //       "examinationPaperEdit",
    //       "examinationPaperRecycle", "ask",
    //       "askAnalysis",
    //       "test",
    //       "testAnalysis", "portrait"],
    //   }
    //   await store.commit("setInfo", { ...userInfo })
    //   await store.commit("setRouters", userInfo.authorityRouter)
    //   let newAddRouters = store.getters.addRouters
    //   newAddRouters.forEach(res => {
    //     router.addRoute(res)
    //   })
    //   router.addRoute({
    //     path: "*",
    //     redirect: "/404",
    //     hidden: true,
    //     children: []
    //   })
    //   next({ path: to.fullPath })
    // }())
    // } else {
    // next()
    // }
    // }
  } else {
    if (store.getters.info && store.getters.addRouters) {
      console.log(store.getters.addRouters)
      store.commit("setToken", "isLogin");
      store.commit(
        "setRouters",
        store.getters.info.authorityRouter
      );
      let newAddRouters = store.getters.addRouters;
      newAddRouters.forEach((res) => {
        router.addRoute(res);
      });
      router.addRoute({
        path: "*",
        redirect: "/404",
        hidden: true,
        children: [],
      });
      next({ path: to.fullPath })
    } else {
      if (store.getters.code) {
        next()
      } else {
        if (to.path === "/login") {
          next()
        } else {
          next({ path: "/login" })
        }
      }
    }
  }
})
router.afterEach(() => {
  NProgress.done()
})
router.onError((error) => {
  const pattern = /Loading chunk (\d)+ failed/g;
  const isChunkLoadFailed = error.message.match(pattern);
  const targetPath = router.history.pending.fullPath;
  if (isChunkLoadFailed) { router.replace(targetPath); }
});