星油H5
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.

47 lines
1.2 KiB

2 years ago
import router from './router'
import useStore from './store/piniaIndex'
2 years ago
import { obtainUrlPathParameterTarget } from '@/utils/index.js'
import { getToken, removeToken } from '@/utils/auth'
import { Notify } from 'vant'
router.beforeEach(async (to, from, next) => {
2 years ago
//pinia挂载顺序问题异步处理路由拦截
setTimeout(() => {
let store = useStore() //获取状态
let token = getToken() //获取cookie
// removeToken()
2 years ago
// store.getPosition()
2 years ago
if (!token) {
2 years ago
let paramsTarget = obtainUrlPathParameterTarget(location.href)
console.log('paramsTarget', paramsTarget)
sessionStorage.setItem('paramsTarget', JSON.stringify(paramsTarget))
store.login(paramsTarget).then(() => {
2 years ago
next()
})
return
}
2 years ago
if (!store.user) {
store.getUser()
}
//检测到没有token直接登录
// if (!token) {
// if (typeof data == 'object' && data?.accessToken) {
// store.login(data)
// }
// }
//拦截 如果不是去首页 还没有token直接去首页
if (to.path !== '/' && !token) {
Notify('请检查登陆状态')
next({ path: '/' })
} else {
next()
}
}, 0)
})
2 years ago
router.afterEach(() => {})