Blame view

src/router/permission.js 3.84 KB
c1b532ad   梁保满   权限配置,路由基础设置
1
  import NProgress from "nprogress"
c1b532ad   梁保满   权限配置,路由基础设置
2
3
  import router from "./index"
  import store from "../store"
c1b532ad   梁保满   权限配置,路由基础设置
4
5
6
7
8
  
  //  获取角色信息,根据用户权限动态加载路由
  router.beforeEach((to, from, next) => {
    NProgress.start()
    if (store.getters.token) {
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
      // 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"],
29f8fb90   梁保满   刷新浏览器重新动态添加路由
32
  
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
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
      //   }
      //   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) {
c1b532ad   梁保满   权限配置,路由基础设置
74
          next()
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
75
76
77
78
79
80
        } else {
          if (to.path === "/login") {
            next()
          } else {
            next({ path: "/login" })
          }
29f8fb90   梁保满   刷新浏览器重新动态添加路由
81
        }
c1b532ad   梁保满   权限配置,路由基础设置
82
      }
c1b532ad   梁保满   权限配置,路由基础设置
83
84
85
86
87
88
    }
  })
  
  router.afterEach(() => {
    NProgress.done()
  })
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
89
90
91
92
93
94
95
  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); }
  });
  
c1b532ad   梁保满   权限配置,路由基础设置
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
  
  
  // //  真实使用 
  // import fetchUser from "@/api/apis/user"
  //
  // router.beforeEach((to, from, next) => {
  //   if (store.getters.token && store.getters.token !== "undefined") {
  //     // store.dispatch('setToken', store.getters.token)
  //     if (to.path === "/login") {
  //       next({path: "/"})
  //     } else {
  //       if (!store.getters.info) {
  //         (async function getAddRouters () {
  //           fetchUser.fetchGetUserInfo().then(async function (response) {
  //             await store.dispatch("setInfo", response.data)
              // await store.dispatch("newRoutes", store.getters.info.authorityRouter)
  //             await router.addRoutes(store.getters.addRouters)
  //             next({path: "/index"})
  //           }).catch(function (error) {
  //             console.log(error)
  //           })
  //         }())
  //       } else {
  //         let is404 = to.matched.some(record => {
  //           console.log(record)
  //           if (record.meta.role) {
  //             return store.getters.info.authorityRouter === -1
  //           }
  //         })
  //         if (is404) {
  //           next({path: "/404"})
  //           return false
  //         }
  //         next()
  //       }
  //     }
  //   } else {
  //     if (to.path === "/login") {
  //       next()
  //     }
  //     next({path: "/login"})
  //   }
  // })