Elements-SY

modify

1 -import router from "./router"; 1 +import router from './router'
2 -import store from "./store"; 2 +import store from './store'
3 -import { Message } from "element-ui"; 3 +import { Message } from 'element-ui'
4 -import { getToken, removeToken } from "@/utils/auth"; 4 +import { getToken, setToken, removeToken } from '@/utils/auth'
5 +import settings from "@/settings"
6 +
7 +
8 +const whiteList = ['/login', '/auth-redirect', '/bind', '/register']
5 9
6 -const whiteList = ["/login", "/auth-redirect", "/bind", "/register"];
7 router.beforeEach((to, from, next) => { 10 router.beforeEach((to, from, next) => {
8 if (getToken()) { 11 if (getToken()) {
9 /* has token*/ 12 /* has token*/
10 - if (to.path === "/login") { 13 + if (to.path === '/login') {
11 - next({ path: "/" }); 14 + next({ path: '/' })
12 } else { 15 } else {
13 if (store.getters.roles.length === 0) { 16 if (store.getters.roles.length === 0) {
14 // 判断当前用户是否已拉取完user_info信息 17 // 判断当前用户是否已拉取完user_info信息
15 - store 18 + store.dispatch('GetInfo').then(() => {
16 - .dispatch("GetInfo") 19 + store.dispatch('GetTableHeaders').then(() => {
17 - .then(() => { 20 + store.dispatch('GenerateRoutes').then(accessRoutes => {
18 - //获取用户表头
19 - store.dispatch("GetTableHeaders").then(() => {
20 // 根据roles权限生成可访问的路由表 21 // 根据roles权限生成可访问的路由表
21 - store.dispatch("GenerateRoutes").then((accessRoutes) => { 22 + router.addRoutes(accessRoutes) // 动态添加可访问路由表
22 - router.addRoutes(accessRoutes); // 动态添加可访问路由表 23 + next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
23 - next({ ...to, replace: true }); // hack方法 确保addRoutes已完成 24 + })
24 - }); 25 + })
25 - }); 26 + }).catch(err => {
27 + Message.error(err)
28 + // store.dispatch('LogOut').then(() => {
29 + // // next({ path: '/' })
30 + // window.location.href = settings.loginURL;//wsso系统跳转
31 + // })
26 }) 32 })
27 - .catch((err) => {});
28 } else { 33 } else {
29 - next(); 34 + next()
30 } 35 }
31 } 36 }
32 } else { 37 } else {
33 - removeToken(); 38 + removeToken()
34 // 没有token 39 // 没有token
40 + //wsso登录时对后端重定向带来的token进行记录
41 + let token = location.search.split("token=")[1]
42 + if (token != undefined && token != '' && token != null) {
43 + setToken(token)
44 + next({ path: '/' })
45 + } else {
35 if (whiteList.indexOf(to.path) !== -1) { 46 if (whiteList.indexOf(to.path) !== -1) {
36 // 在免登录白名单,直接进入 47 // 在免登录白名单,直接进入
37 - next(); 48 + next()
38 } else { 49 } else {
39 - next(`/login?redirect=${to.fullPath}`); // 否则全部重定向到登录页 50 + window.location.href = settings.loginURL;//wsso系统跳转
51 + }
40 } 52 }
41 } 53 }
42 -}); 54 +})
43 55
44 -router.afterEach(() => {}); 56 +router.afterEach(() => {
57 +})
......