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); } }); // // 真实使用 // 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"}) // } // })