|
|
|
import router from './router'
|
|
|
|
import store from './store'
|
|
|
|
import { Message } from 'element-ui'
|
|
|
|
import NProgress from 'nprogress'
|
|
|
|
import 'nprogress/nprogress.css'
|
|
|
|
import { getToken, setToken } from '@/utils/auth'
|
|
|
|
import { isPathMatch } from '@/utils/validate'
|
|
|
|
import { isRelogin } from '@/utils/request'
|
|
|
|
import { extractPrefix } from '@/utils/common'
|
|
|
|
|
|
|
|
// 政务登录接口
|
|
|
|
import { governmentGetInfo } from '@/api/login/index'
|
|
|
|
|
|
|
|
NProgress.configure({ showSpinner: false })
|
|
|
|
|
|
|
|
const whiteList = ['/login', '/register']
|
|
|
|
|
|
|
|
const isWhiteList = (path) => {
|
|
|
|
return whiteList.some(pattern => isPathMatch(pattern, path))
|
|
|
|
}
|
|
|
|
|
|
|
|
router.beforeEach((to, from, next) => {
|
|
|
|
if (!extractPrefix(to.path)) {
|
|
|
|
store.commit("SET_CRUMBS", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
NProgress.start()
|
|
|
|
|
|
|
|
const { userToken, signature, timespan } = to.query
|
|
|
|
console.log(userToken, signature, timespan, '参数')
|
|
|
|
|
|
|
|
|
|
|
|
if (userToken && signature && timespan) {
|
|
|
|
// 政务登录
|
|
|
|
governmentGetInfo({
|
|
|
|
userToken,
|
|
|
|
signature,
|
|
|
|
timespan
|
|
|
|
}).then(res => {
|
|
|
|
const token = res.token
|
|
|
|
if (token) {
|
|
|
|
setToken(token)
|
|
|
|
localStorage.setItem('otherToken', userToken)
|
|
|
|
|
|
|
|
const newQuery = { ...to.query }
|
|
|
|
delete newQuery.userToken
|
|
|
|
delete newQuery.signature
|
|
|
|
delete newQuery.timespan
|
|
|
|
const newPath = to.path + '?' + new URLSearchParams(newQuery).toString()
|
|
|
|
window.history.replaceState({}, '', newPath)
|
|
|
|
|
|
|
|
if (store.getters.roles.length === 0) {
|
|
|
|
isRelogin.show = true
|
|
|
|
store.dispatch('GetInfo').then(() => {
|
|
|
|
isRelogin.show = false
|
|
|
|
store.dispatch('GenerateRoutes').then(accessRoutes => {
|
|
|
|
router.addRoutes(accessRoutes)
|
|
|
|
next({ ...to, replace: true })
|
|
|
|
})
|
|
|
|
}).catch(err => {
|
|
|
|
store.dispatch('LogOut').then(() => {
|
|
|
|
Message.error(err)
|
|
|
|
next({ path: '/' })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).catch(err => {
|
|
|
|
console.error('政务系统登录失败:', err)
|
|
|
|
Message.error('政务系统登录失败,请重新登录')
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//原来的登录逻辑
|
|
|
|
const token = getToken()
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
to.meta.title && store.dispatch('settings/setTitle', to.meta.title)
|
|
|
|
if (to.path === '/login') {
|
|
|
|
next({ path: '/' })
|
|
|
|
NProgress.done()
|
|
|
|
} else if (isWhiteList(to.path)) {
|
|
|
|
next()
|
|
|
|
} else {
|
|
|
|
if (store.getters.roles.length === 0) {
|
|
|
|
isRelogin.show = true
|
|
|
|
store.dispatch('GetInfo').then(() => {
|
|
|
|
isRelogin.show = false
|
|
|
|
store.dispatch('GenerateRoutes').then(accessRoutes => {
|
|
|
|
router.addRoutes(accessRoutes)
|
|
|
|
next({ ...to, replace: true })
|
|
|
|
})
|
|
|
|
}).catch(err => {
|
|
|
|
store.dispatch('LogOut').then(() => {
|
|
|
|
Message.error(err)
|
|
|
|
next({ path: '/' })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (isWhiteList(to.path)) {
|
|
|
|
next()
|
|
|
|
} else {
|
|
|
|
next(`/login?redirect=${encodeURIComponent(to.fullPath)}`)
|
|
|
|
NProgress.done()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
router.afterEach(() => {
|
|
|
|
NProgress.done()
|
|
|
|
})
|