import router from './router' import { useNProgress } from '@/hooks/web/useNProgress' import { useTitle } from '@/hooks/web/useTitle' import { getToken, getIsEnterprise } from '@/utils/auth' import { useDictStore } from '@/store/dict' import { useUserStore } from '@/store/user' import Confirm from '@/plugins/confirm' const { start, done } = useNProgress() // loginType:1.enterprise: 企业路由 // 2.personal: 个人路由 // 3.common: 没有限制访问权限 // 3.personalCommon: 无需登录也能访问的页面 // 路由守卫 router.beforeEach(async (to, from, next) => { localStorage.setItem('routerTest', to.path) // 本地环境保存代码热更新会导致路径缺失问题 const tokenIndex = getIsEnterprise() ? 1: 2 start() // loadStart() if (to.path === '/enterpriseVerification') { const res = JSON.parse(localStorage.getItem('emailLoginInfo') || "false") const obj = res ? { ...res, type: 'emailLogin' } : {} useUserStore().changeRole(obj) next() } else if (getToken(tokenIndex)) { // 企业信息完成度提示 if (localStorage.getItem('checkEnterpriseBaseInfoFalseHref')) { const href = localStorage.getItem('checkEnterpriseBaseInfoFalseHref') localStorage.setItem('checkEnterpriseBaseInfoFalseHref', '') setTimeout(() => { Confirm('系统提示', '企业信息设置未完善,是否前往完善?').then(() => { window.location.href = href }) }, 4000); } if (to.path === '/login') { next({ path: '/recruitHome' }) } else { // 获取字典信息 const dictStore = useDictStore() dictStore.getDictTypeData() next() // const type = localStorage.getItem('loginType') // // 判断企业路由和个人路由,防止互串 // if (!type) { removeToken(); next(`/login?redirect=${to.fullPath}`) } // else if (to.meta?.loginType === 'common' || to.meta?.loginType === 'personalCommon') next() // // else if (type === 'enterprise' && to.meta?.loginType === 'personalCommon') next({ path: `/${type}` }) // 企业端不能访问任何个人端路由 // // else if (type === 'personal' && to.meta?.loginType === 'personalCommon') next() // else if (to.meta?.loginType === type) next() // else next({ path: `/${type}` }) } } else { if (to.meta?.loginType === 'personalCommon' || to.meta?.loginType === 'common') { // 路由不重定向 next() } else if (to.meta?.loginType === 'enterprise') { // 没有企业token->去个人首页 next({ path: '/recruitHome' }) } else { next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页 } } }) router.afterEach((to) => { useTitle(to?.meta?.title) done() // 结束Progress // loadDone() })