You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
4.1 KiB
145 lines
4.1 KiB
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 url = window.location.href
|
|
|
|
// 检查是否包含政务平台返回的关键参数
|
|
const hasUserToken = url.includes('userToken=')
|
|
const hasSignature = url.includes('signature=')
|
|
const hasTimespan = url.includes('timespan=')
|
|
|
|
if (hasUserToken && hasSignature && hasTimespan) {
|
|
const regUserToken = /[?&]userToken=([^&#]+)/
|
|
const regSignature = /[?&]signature=([^&#]+)/
|
|
const regTimespan = /[?&]timespan=([^&#]+)/
|
|
|
|
const userTokenMatch = url.match(regUserToken)
|
|
const signatureMatch = url.match(regSignature)
|
|
const timespanMatch = url.match(regTimespan)
|
|
|
|
const userToken = userTokenMatch ? userTokenMatch[1] : null
|
|
const signature = signatureMatch ? signatureMatch[1] : null
|
|
const timespan = timespanMatch ? timespanMatch[1] : null
|
|
|
|
if (userToken && signature && timespan) {
|
|
// 调用政务系统登录接口
|
|
governmentGetInfo({
|
|
userToken,
|
|
signature,
|
|
timespan
|
|
}).then(res => {
|
|
// 假设 res.token 是返回的 token
|
|
const token = res.token
|
|
if (token) {
|
|
setToken(token) // 存储 token
|
|
localStorage.setItem('otherToken', userToken)
|
|
|
|
// 清除 URL 中的敏感参数
|
|
const cleanUrl = url
|
|
.replace(regUserToken, '')
|
|
.replace(regSignature, '')
|
|
.replace(regTimespan, '')
|
|
.replace(/^&/, '?')
|
|
|
|
window.history.replaceState({}, '', cleanUrl)
|
|
|
|
// 继续路由守卫流程
|
|
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('政务系统登录失败,请重新登录')
|
|
})
|
|
} else {
|
|
Message.error('缺少必要的登录参数')
|
|
next(`/login?redirect=${encodeURIComponent(to.fullPath)}`)
|
|
}
|
|
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()
|
|
}) |