de2be3ea by 苗菁

feat: 单点登录token

1 parent 86f1e19d
...@@ -77,7 +77,7 @@ ...@@ -77,7 +77,7 @@
77 if (process.env.NODE_ENV === 'development') { 77 if (process.env.NODE_ENV === 'development') {
78 localStorage.removeItem('token') 78 localStorage.removeItem('token')
79 } else { 79 } else {
80 Cookies.remove('token') 80 Cookies.remove('ACCESS_TOKEN')
81 } 81 }
82 if (window._config.casEnable) { 82 if (window._config.casEnable) {
83 window.location.href = window._config.casBaseURL + '/logout?service=' + encodeURIComponent(window.location.href); 83 window.location.href = window._config.casBaseURL + '/logout?service=' + encodeURIComponent(window.location.href);
......
...@@ -8,99 +8,101 @@ import axios from 'axios' ...@@ -8,99 +8,101 @@ import axios from 'axios'
8 import router from './router' 8 import router from './router'
9 import store from './store' 9 import store from './store'
10 import Cookies from 'js-cookie' 10 import Cookies from 'js-cookie'
11 import { getMenuInfo } from '@/api/user' 11 import {getMenuInfo} from '@/api/user'
12 import { getUrlParam } from '@/utils/operation' 12 import {getUrlParam} from '@/utils/operation'
13 import NProgress from 'nprogress' // progress bar 13 import NProgress from 'nprogress' // progress bar
14 import 'nprogress/nprogress.css' // progress bar style 14 import 'nprogress/nprogress.css' // progress bar style
15 import getPageTitle from '@/utils/get-page-title' 15 import getPageTitle from '@/utils/get-page-title'
16 NProgress.configure({ showSpinner: false }) // NProgress Configuration 16
17 NProgress.configure({showSpinner: false}) // NProgress Configuration
17 18
18 router.beforeEach(async (to, from, next) => { 19 router.beforeEach(async (to, from, next) => {
19 Vue.prototype.$currentRoute = to 20 Vue.prototype.$currentRoute = to
20 NProgress.start() 21 NProgress.start()
21 document.title = getPageTitle(to.meta.title) 22 document.title = getPageTitle(to.meta.title)
22 let hasAddDict = store.state.dict.addDict 23 let hasAddDict = store.state.dict.addDict
23 let hasAddRoute = store.state.permission.addRoutes 24 let hasAddRoute = store.state.permission.addRoutes
24 // cas操作 25 // cas操作
25 const token = localStorage.getItem("token") || Cookies.get('token'); 26 const token = localStorage.getItem("token") || Cookies.get('token');
26 if (to.path === '/login') { 27 if (to.path === '/login') {
27 if (token) { 28 if (token) {
28 next('/') 29 next('/')
29 } else { 30 } else {
30 next() 31 next()
31 }
32 return
33 } 32 }
34 if (window._config.casEnable === true) { 33 return
35 let locationUrl = window.location.protocol + '//' + window.location.host + window.location.pathname; 34 }
36 if (!token) { 35 if (window._config.casEnable === true) {
37 let ticket = getUrlParam('ticket'); 36 let locationUrl = window.location.protocol + '//' + window.location.host + window.location.pathname;
38 if (ticket) { 37 if (!token) {
39 axios.get(Vue.prototype.BASE_API.ip + "/management/cas/validate", { 38 let ticket = getUrlParam('ticket');
40 params: { 39 if (ticket) {
41 'ticket': ticket, 40 axios.get(Vue.prototype.BASE_API.ip + "/management/cas/validate", {
42 'service': locationUrl 41 params: {
43 } 42 'ticket': ticket,
44 }).then(async (res) => { 43 'service': locationUrl
45 if (process.env.NODE_ENV === 'development') { 44 }
46 localStorage.setItem('token', res.data.content.accessToken) 45 }).then(async (res) => {
47 } else { 46 if (process.env.NODE_ENV === 'development') {
48 Cookies.set('token', res.data.content.accessToken) 47 localStorage.setItem('token', res.data.content.accessToken)
49 } 48 } else {
50 window.location.href = localStorage.getItem('location') 49 Cookies.set('ACCESS_TOKEN', res.data.content.accessToken)
50 }
51 window.location.href = localStorage.getItem('location')
51 52
52 }).catch(e => { 53 }).catch(e => {
53 console.log(e) 54 console.log(e)
54 }) 55 })
55 } else { 56 } else {
56 localStorage.setItem("location", window.location.href) 57 localStorage.setItem("location", window.location.href)
57 window.location.href = window._config.casBaseURL + '/login?service=' + encodeURIComponent(locationUrl); 58 window.location.href = window._config.casBaseURL + '/login?service=' + encodeURIComponent(locationUrl);
58 } 59 }
59 } else {
60 permission()
61 }
62 async function permission () {
63 if (!hasAddDict) {
64 store.dispatch('dict/generateDic')
65 }
66 if (hasAddRoute) {
67 next()
68 // next({ ...to, replace: true })
69 } else {
70 const { result: getMenuData } = await getMenuInfo()
71 const accessRoutes = await store.dispatch('permission/generateRoutes', getMenuData)
72 // 获取用户信息
73 await store.dispatch('user/getUserInfo')
74 router.addRoutes([...accessRoutes, { path: '*', redirect: '/404', hidden: true }])
75 const routeTo = Cookies.get('routerTo')
76 if (routeTo && routeTo !== '/') {
77 next({ ...to, replace: true })
78 } else {
79 next('/home')
80 }
81 }
82 }
83 } else { 60 } else {
84 if (!token) { 61 permission()
85 const redirectData = { 62 }
86 path: '/login', 63
87 replace: true, 64 async function permission() {
88 } 65 if (!hasAddDict) {
89 if (to.path) { 66 store.dispatch('dict/generateDic')
90 redirectData.query = { 67 }
91 ...redirectData.query, 68 if (hasAddRoute) {
92 redirect: to.path,
93 };
94 }
95 next(redirectData)
96 return
97 }
98 next() 69 next()
70 // next({ ...to, replace: true })
71 } else {
72 const {result: getMenuData} = await getMenuInfo()
73 const accessRoutes = await store.dispatch('permission/generateRoutes', getMenuData)
74 // 获取用户信息
75 await store.dispatch('user/getUserInfo')
76 router.addRoutes([...accessRoutes, {path: '*', redirect: '/404', hidden: true}])
77 const routeTo = Cookies.get('routerTo')
78 if (routeTo && routeTo !== '/') {
79 next({...to, replace: true})
80 } else {
81 next('/home')
82 }
83 }
84 }
85 } else {
86 if (!token) {
87 const redirectData = {
88 path: '/login',
89 replace: true,
90 }
91 if (to.path) {
92 redirectData.query = {
93 ...redirectData.query,
94 redirect: to.path,
95 };
96 }
97 next(redirectData)
98 return
99 } 99 }
100 NProgress.done() 100 next()
101 }
102 NProgress.done()
101 }) 103 })
102 router.afterEach(to => { 104 router.afterEach(to => {
103 // 解决刷新页面报404问题 105 // 解决刷新页面报404问题
104 Cookies.set("routerTo", to.fullPath) 106 Cookies.set("routerTo", to.fullPath)
105 NProgress.done() 107 NProgress.done()
106 }) 108 })
......
...@@ -34,14 +34,6 @@ service.interceptors.request.use( ...@@ -34,14 +34,6 @@ service.interceptors.request.use(
34 } else { 34 } else {
35 config.headers.delete('Authorization') 35 config.headers.delete('Authorization')
36 } 36 }
37 } else {
38 const token = Cookies.get('token')
39 // 添加请求头
40 if (token) {
41 config.headers['Authorization'] = 'Bearer ' + token
42 } else {
43 config.headers.delete('Authorization')
44 }
45 } 37 }
46 return config 38 return config
47 }, 39 },
...@@ -78,7 +70,7 @@ service.interceptors.response.use( ...@@ -78,7 +70,7 @@ service.interceptors.response.use(
78 if (process.env.NODE_ENV === 'development') { 70 if (process.env.NODE_ENV === 'development') {
79 localStorage.removeItem('token') 71 localStorage.removeItem('token')
80 } else { 72 } else {
81 Cookies.remove('token') 73 Cookies.remove('ACCESS_TOKEN')
82 } 74 }
83 if (window._config.casEnable) { 75 if (window._config.casEnable) {
84 window.location.href = window._config.casBaseURL + '/logout?service=' + encodeURIComponent(locationUrl); 76 window.location.href = window._config.casBaseURL + '/logout?service=' + encodeURIComponent(locationUrl);
...@@ -92,7 +84,6 @@ service.interceptors.response.use( ...@@ -92,7 +84,6 @@ service.interceptors.response.use(
92 return false 84 return false
93 } 85 }
94 } 86 }
95
96 } else { 87 } else {
97 // 对响应错误做点什么 88 // 对响应错误做点什么
98 Message({ 89 Message({
......