import router from './router' import store from './store' import { SET_LOGIN_USER_INFO } from '@/store/modules/user' import { setToken, getToken } from '@/utils/auth' router.beforeEach(async(to, from, next) => { // 获取当前token const hasToken = getToken() // 跳转到登录页 if (to.name === 'Login') { // 请求logo await store.dispatch('dict/getLogo', to.params.name.toLowerCase() === 'user') next() return } if (to.name === 'Download') { console.log('======>正在跳转至下载页面') return next() } // 如果需要跳转到C端首页 if (to.name === 'UserHome') { // 如果当前有token to.query.token && setToken(to.query.token) // 开始派发时间 store.commit(`user/${SET_LOGIN_USER_INFO}`, { vin: to.query.vin || '', token: to.query.token, type: 'C', phone: to.query.phone }) // 请求logo await store.dispatch('dict/getLogo', true) next() return } if (from.path === '/') { // 如果当前没有token,则直接跳转登录页 if (!hasToken) { const metaType = to.meta.type || 'business' // 请求logo await store.dispatch('dict/getLogo', { isCustomer: metaType.toLowerCase() === 'user' }) return next({ name: 'Login', params: { name: metaType }, query: { ...(to.query || {}) } }) } else { // 请求logo和字典项 await Promise.all([ store.dispatch('dict/getLogo', { isCustomer: store.getters.loginUser.type === 'C' }), store.dispatch('dict/initDict') ]) } } next() })