feat: 单点登录token
Showing
3 changed files
with
12 additions
and
19 deletions
| ... | @@ -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,12 +8,13 @@ import axios from 'axios' | ... | @@ -8,12 +8,13 @@ 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 |
| ... | @@ -45,7 +46,7 @@ router.beforeEach(async (to, from, next) => { | ... | @@ -45,7 +46,7 @@ router.beforeEach(async (to, from, next) => { |
| 45 | if (process.env.NODE_ENV === 'development') { | 46 | if (process.env.NODE_ENV === 'development') { |
| 46 | localStorage.setItem('token', res.data.content.accessToken) | 47 | localStorage.setItem('token', res.data.content.accessToken) |
| 47 | } else { | 48 | } else { |
| 48 | Cookies.set('token', res.data.content.accessToken) | 49 | Cookies.set('ACCESS_TOKEN', res.data.content.accessToken) |
| 49 | } | 50 | } |
| 50 | window.location.href = localStorage.getItem('location') | 51 | window.location.href = localStorage.getItem('location') |
| 51 | 52 | ||
| ... | @@ -59,7 +60,8 @@ router.beforeEach(async (to, from, next) => { | ... | @@ -59,7 +60,8 @@ router.beforeEach(async (to, from, next) => { |
| 59 | } else { | 60 | } else { |
| 60 | permission() | 61 | permission() |
| 61 | } | 62 | } |
| 62 | async function permission () { | 63 | |
| 64 | async function permission() { | ||
| 63 | if (!hasAddDict) { | 65 | if (!hasAddDict) { |
| 64 | store.dispatch('dict/generateDic') | 66 | store.dispatch('dict/generateDic') |
| 65 | } | 67 | } |
| ... | @@ -67,14 +69,14 @@ router.beforeEach(async (to, from, next) => { | ... | @@ -67,14 +69,14 @@ router.beforeEach(async (to, from, next) => { |
| 67 | next() | 69 | next() |
| 68 | // next({ ...to, replace: true }) | 70 | // next({ ...to, replace: true }) |
| 69 | } else { | 71 | } else { |
| 70 | const { result: getMenuData } = await getMenuInfo() | 72 | const {result: getMenuData} = await getMenuInfo() |
| 71 | const accessRoutes = await store.dispatch('permission/generateRoutes', getMenuData) | 73 | const accessRoutes = await store.dispatch('permission/generateRoutes', getMenuData) |
| 72 | // 获取用户信息 | 74 | // 获取用户信息 |
| 73 | await store.dispatch('user/getUserInfo') | 75 | await store.dispatch('user/getUserInfo') |
| 74 | router.addRoutes([...accessRoutes, { path: '*', redirect: '/404', hidden: true }]) | 76 | router.addRoutes([...accessRoutes, {path: '*', redirect: '/404', hidden: true}]) |
| 75 | const routeTo = Cookies.get('routerTo') | 77 | const routeTo = Cookies.get('routerTo') |
| 76 | if (routeTo && routeTo !== '/') { | 78 | if (routeTo && routeTo !== '/') { |
| 77 | next({ ...to, replace: true }) | 79 | next({...to, replace: true}) |
| 78 | } else { | 80 | } else { |
| 79 | next('/home') | 81 | next('/home') |
| 80 | } | 82 | } | ... | ... |
| ... | @@ -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({ | ... | ... |
-
Please register or sign in to post a comment