92 lines
3.9 KiB
JavaScript
92 lines
3.9 KiB
JavaScript
![]() |
import Vue from 'vue'
|
|||
|
import router from './router'
|
|||
|
import store from './store'
|
|||
|
import NProgress from 'nprogress' // progress bar
|
|||
|
import 'nprogress/nprogress.css' // progress bar style
|
|||
|
import notification from 'ant-design-vue/es/notification'
|
|||
|
import { ACCESS_TOKEN,INDEX_MAIN_PAGE_PATH } from '@/store/mutation-types'
|
|||
|
import { generateIndexRouter } from "@/utils/util"
|
|||
|
|
|||
|
NProgress.configure({ showSpinner: false }) // NProgress Configuration
|
|||
|
|
|||
|
const whiteList = ['/user/login', '/user/register', '/user/register-result','/user/alteration'] // no redirect whitelist
|
|||
|
|
|||
|
router.beforeEach((to, from, next) => {
|
|||
|
console.log('最开始',to, from, next);
|
|||
|
NProgress.start() // start progress bar
|
|||
|
|
|||
|
console.log(`🚀 -----------------------------------------------------------------------------------`);
|
|||
|
console.log(`🚀 ~ file: permission.js ~ line 18 ~ router.beforeEach ~ ACCESS_TOKEN`, Vue.ls.get(ACCESS_TOKEN),to.path);
|
|||
|
console.log(`🚀 -----------------------------------------------------------------------------------`);
|
|||
|
if (Vue.ls.get(ACCESS_TOKEN)) {
|
|||
|
console.log("有token--");
|
|||
|
/* has token */
|
|||
|
if (to.path === '/user/login') {
|
|||
|
next({ path: INDEX_MAIN_PAGE_PATH })
|
|||
|
NProgress.done()
|
|||
|
} else {
|
|||
|
console.log('看看有没有权限列表,store.getters.permissionList.length ->',store.getters.permissionList.length);
|
|||
|
if (store.getters.permissionList.length === 0) {
|
|||
|
console.log('准备查询权限菜单');
|
|||
|
store.dispatch('GetPermissionList').then(res => {
|
|||
|
const menuData = res.result.menu;
|
|||
|
console.log('查询菜单权限成功!',res);
|
|||
|
if (menuData === null || menuData === "" || menuData === undefined) {
|
|||
|
return;
|
|||
|
}
|
|||
|
console.log('返回的内容不为空,牛逼奥,铁子,generateIndexRouter =>',generateIndexRouter,menuData);
|
|||
|
let constRoutes = [];
|
|||
|
// 添加主界面路由
|
|||
|
constRoutes = generateIndexRouter(menuData);
|
|||
|
console.log('生成首页 constRoutes =>',constRoutes);
|
|||
|
console.log('生成的路由存进去');
|
|||
|
store.dispatch('UpdateAppRouter', { constRoutes }).then(() => {
|
|||
|
console.log('新的路由已经存进去了');
|
|||
|
// 根据roles权限生成可访问的路由表
|
|||
|
// 动态添加可访问路由表
|
|||
|
router.addRoutes(store.getters.addRouters)
|
|||
|
const redirect = decodeURIComponent(from.query.redirect || to.path)
|
|||
|
if (to.path === redirect) {
|
|||
|
console.log('原地跳redirect',redirect);
|
|||
|
// hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
|
|||
|
next({ ...to, replace: true })
|
|||
|
} else {
|
|||
|
// 跳转到目的路由
|
|||
|
console.log('跳转到这里,redirect =>',redirect);
|
|||
|
next({ path: redirect })
|
|||
|
}
|
|||
|
})
|
|||
|
}).catch((e) => {
|
|||
|
/* notification.error({
|
|||
|
message: '系统提示',
|
|||
|
description: '请求用户信息失败,请重试!'
|
|||
|
})*/
|
|||
|
console.log('查询菜单出现问题,',e);
|
|||
|
store.dispatch('Logout').then(() => {
|
|||
|
console.log('退出登录后');
|
|||
|
next({ path: '/user/login', query: { redirect: to.fullPath } })
|
|||
|
})
|
|||
|
})
|
|||
|
} else {
|
|||
|
next()
|
|||
|
}
|
|||
|
}
|
|||
|
} else {
|
|||
|
console.log("无token--");
|
|||
|
if (whiteList.indexOf(to.path) !== -1) {
|
|||
|
// 在免登录白名单,直接进入
|
|||
|
console.log("在白名单中,放行--");
|
|||
|
next()
|
|||
|
} else {
|
|||
|
console.log("不再白名单中,拦住,登录去--");
|
|||
|
next({ path: '/user/login', query: { redirect: to.fullPath } })
|
|||
|
NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
|
|||
|
}
|
|||
|
}
|
|||
|
})
|
|||
|
|
|||
|
router.afterEach((to, from, failure) => {
|
|||
|
console.log('结束了',to, from, failure);
|
|||
|
NProgress.done() // finish progress bar
|
|||
|
})
|