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.
Gyyq-Upstairs/src/permission.js

143 lines
4.0 KiB

import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
1 month ago
import { getToken, setToken } from '@/utils/auth'
import { isPathMatch } from '@/utils/validate'
import { isRelogin } from '@/utils/request'
1 month ago
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) => {
1 month ago
if (!extractPrefix(to.path)) {
store.commit("SET_CRUMBS", false);
2 months ago
}
NProgress.start()
1 month ago
//政务登录
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 => {
const token = res.token
if (token) {
1 month ago
setToken(token)
1 month ago
localStorage.setItem('otherToken', userToken)
1 month ago
1 month ago
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 => {
1 month ago
router.addRoutes(accessRoutes)
next({ ...to, replace: true })
})
}).catch(err => {
1 month ago
store.dispatch('LogOut').then(() => {
Message.error(err)
next({ path: '/' })
})
1 month ago
})
} else {
next()
}
}
} else {
if (isWhiteList(to.path)) {
next()
} else {
1 month ago
next(`/login?redirect=${encodeURIComponent(to.fullPath)}`)
NProgress.done()
}
}
})
router.afterEach(() => {
NProgress.done()
1 month ago
})