1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- 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'
- import dialogExtend from '@/plugins/dialogExtend'
- 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('entUpdatePassword') === 'needChange') fullScreen('entUpdatePassword')
- // 强制填写个人信息
- else if (localStorage.getItem('necessaryInfoReady') === 'fddeaddc47868b' && tokenIndex === 2) dialogExtend('necessaryInfoDialog')
- // 企业登录免费职位广告提示
- else if (localStorage.getItem('positionAd')) {
- localStorage.setItem('positionAd', '')
- dialogExtend('positionAd')
- }
- // 企业信息完成度提示
- else if (localStorage.getItem('checkEnterpriseBaseInfoFalseHref') && tokenIndex === 1) {
- if (to.path !== '/recruit/enterprise/position/add') { // 除了点击企业登录免费职位广告提示跳转路由不提示
- const href = localStorage.getItem('checkEnterpriseBaseInfoFalseHref')
- localStorage.setItem('checkEnterpriseBaseInfoFalseHref', '')
- localStorage.setItem('entUpdatePassword', '')
- if (to.path !== href) {
- setTimeout(() => {
- Confirm('系统提示', '企业信息设置未完善,是否前往完善?').then(() => {
- window.location.href = href
- })
- }, 4000)
- }
- }
- }
- if (to.fullPath === '/login') {
- next({ path: '/recruitHome' })
- }
- // 获取字典信息
- // const dictStore = useDictStore()
- // dictStore.getDictTypeData()
- next()
- } 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()
- })
- // router.onError(error => {
- // const fetchResourcesErrors = ['Failed to fetch dynamically imported module', 'Importing a module script failed']
- // if (fetchResourcesErrors.some((item) => error?.message && error.message?.includes(item))) {
- // window.location.reload()
- // }
- // });
|