import router from './router' import useStore from './store/piniaIndex' import { obtainUrlPathParameterTarget } from '@/utils/index.js' import { getToken, removeToken } from '@/utils/auth' import { Notify } from 'vant' router.beforeEach(async (to, from, next) => { //pinia挂载顺序问题异步处理路由拦截 setTimeout(() => { let store = useStore() //获取状态 let token = getToken() //获取cookie // removeToken() // store.getPosition() if (!token) { let paramsTarget = obtainUrlPathParameterTarget(location.href) console.log('paramsTarget', paramsTarget) sessionStorage.setItem('paramsTarget', JSON.stringify(paramsTarget)) store.login(paramsTarget).then(() => { next() }) return } 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) }) router.afterEach(() => {})