56 lines
1.8 KiB
JavaScript
56 lines
1.8 KiB
JavaScript
import Vue from 'vue'
|
|
import router from './router'
|
|
import store from './store'
|
|
import { ACCESS_TOKEN, UI_CACHE_DB_DICT_DATA, USER_INFO, USER_NAME } from '@/store/mutation-types'
|
|
import { welcome } from '@/utils/util'
|
|
import { getSSOUserInfo } from './api/api'
|
|
|
|
const whiteList = ['/user/login'] // no redirect whitelist
|
|
|
|
router.beforeEach(async (to, from, next) => {
|
|
// 单点登录
|
|
if (to.query.code) {
|
|
const { result } = await getSSOUserInfo({ code: to.query.code })
|
|
const userInfo = result.userInfo
|
|
Vue.ls.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)
|
|
Vue.ls.set(USER_NAME, userInfo.username, 7 * 24 * 60 * 60 * 1000)
|
|
Vue.ls.set(USER_INFO, userInfo, 7 * 24 * 60 * 60 * 1000)
|
|
Vue.ls.set(UI_CACHE_DB_DICT_DATA, result.sysAllDictItems, 7 * 24 * 60 * 60 * 1000)
|
|
store.commit('SET_TOKEN', result.token)
|
|
store.commit('SET_INFO', userInfo)
|
|
store.commit('SET_NAME', { username: userInfo.username, realname: userInfo.realname, welcome: welcome() })
|
|
store.commit('SET_AVATAR', userInfo.avatar)
|
|
}
|
|
if (Vue.ls.get(ACCESS_TOKEN)) {
|
|
/* has token */
|
|
if (to.path === '/user/login') {
|
|
next('/')
|
|
} else {
|
|
if (to.query.taskName) {
|
|
to.meta.title = to.query.taskName
|
|
}
|
|
next()
|
|
}
|
|
} else {
|
|
if (whiteList.indexOf(to.path) !== -1) {
|
|
// 在免登录白名单,直接进入
|
|
if (to.path === '/user/login' && to.path !== process.env.VUE_APP_LOGIN_PAGE) {
|
|
window.location.replace(process.env.VUE_APP_LOGIN_PAGE)
|
|
return
|
|
}
|
|
next()
|
|
} else {
|
|
const path = '/user/login'
|
|
if (path === '/user/login' && path !== process.env.VUE_APP_LOGIN_PAGE) {
|
|
window.location.replace(process.env.VUE_APP_LOGIN_PAGE)
|
|
return
|
|
}
|
|
next({ path: path, query: { redirect: to.fullPath } })
|
|
}
|
|
}
|
|
})
|
|
|
|
router.afterEach(() => {
|
|
|
|
})
|