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 fullScreen from '@/plugins/fullScreen' import dialogExtend from '@/plugins/dialogExtend' import { useMallStore } from '@/store/mall' import { useEnterpriseStore } from '@/store/enterprise' const { start, done } = useNProgress() let isRefresh = true const ENTERPRISE_PATH = '/recruit/enterprise' const TEACHER_PATH = '/recruit/teacher' // 路由守卫 router.beforeEach(async (to, from, next) => { start() // 个人端 404 处理 if (to.path === '/404') { next() return } const isEnterprise = to.path.includes(ENTERPRISE_PATH) if (!isEnterprise && to.path !== '/enterpriseVerification' && !hasRoute(to.path)) { next('/404') } const isTeacher = to.path.includes(TEACHER_PATH) const tokenIndex = isEnterprise ? 1 : isTeacher ? 3 : 2 // 1:企业 2:个人 3:教师 // 获取商城装修模版 const mallStore = useMallStore() const enterpriseStore = useEnterpriseStore() if (!localStorage.getItem('mallTemplate')) { await mallStore.getMallDiyTemplate() } localStorage.setItem('routerTest', to.path) // 本地环境保存代码热更新会导致路径缺失问题 if (to.path === '/enterpriseVerification') { const res = JSON.parse(localStorage.getItem('emailLoginInfo') || "false") const obj = res ? { ...res, type: 'emailLogin' } : {} // 清除路由表 enterpriseStore.clearEnterpriseMenu() isRefresh = true useUserStore().changeRole(obj) next() return } if (getToken(tokenIndex)) { if (isEnterprise) { // 获取企业路由 if (!enterpriseStore.enterpriseMenu || !enterpriseStore.enterpriseMenu.length ) { const { menus } = await enterpriseStore.getEnterpriseMenu() enterpriseStore.saveEnterpriseMenu(menus) } // 渲染路由 if (isRefresh) { isRefresh = false const routes = enterpriseStore.assignEnterpriseMenu(enterpriseStore.enterpriseMenu) // 有角色但可访问的菜单权限则提示联系管理员分配菜单权限 if (!routes || !routes.length) { next('/permissionPrompt') return } routes.forEach(route => { router.addRoute(route) }) // 判断是否存在路由 if (to.path !== ENTERPRISE_PATH && !hasRoute(to.path)) { next('/404') return } // 强制修改密码 if (localStorage.getItem('entUpdatePassword') === 'needChange') fullScreen('entUpdatePassword') // 企业登录-招聘会广告 else if (hasRoute('/recruit/enterprise/jobFair/index') && !localStorage.getItem('jobFairAd') && tokenIndex === 1) { localStorage.setItem('jobFairAd', 'hasBeenShow') dialogExtend('jobFairAd') } // 企业登录-免费职位广告提示 else if (hasRoute('/recruit/enterprise/position/index') && localStorage.getItem('positionAd') && tokenIndex === 1) { 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.path === ENTERPRISE_PATH) { next(findPath(routes)) function findPath (nodes, root = '') { const first = nodes[0] const path = root + first.path if (!first.children || !first.children.length) { return path } return findPath(first.children, path + '/') } } next({ ...to, replace: true }) return } // 判断是否存在路由 if (!hasRoute(to.path)) { next('/404') return } } // if (isTeacher) {} // 强制填写个人信息 fddeaddc47868b/ready if (localStorage.getItem('chooseRole') === 'showChooseRole' && to.path !== '/register/selectedPersonRole' && tokenIndex === 2) next('/register/selectedPersonRole') else if (localStorage.getItem('necessaryInfoReady') === 'fddeaddc47868b' && tokenIndex === 2 && localStorage.getItem('chooseRole') !== 'showChooseRole') dialogExtend('necessaryInfoDialog') if (to.fullPath === '/login') { next('/recruitHome') return } next() return } if (to.meta?.commonPage) { // 公共页面,路由不重定向 next() return } next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页 }) router.afterEach((to) => { useTitle(to?.meta?.title) done() // 结束Progress // loadDone() }) function hasRoute (path) { const routes = router.getRoutes() return routeFlattening(routes).some(_path => { if (_path.includes(':')) { const change = path.split('/') const _change = _path.split('/') if (change.length !== _change.length) { return false } const res = _change.reduce((e, v, i) => { if (v.includes(':')) { e.push(true) return e } e.push(change[i] === v) return e }, []) return res.every(e => e) } // 检查常规路由或包含参数的路由 return _path === path }) } /** * @param {Array} routes * @returns {Array} * 路由扁平化 抽离children字段 */ function routeFlattening (routes) { return routes.reduce((prev, cur) => { prev.push(cur.path) if (cur.children && cur.children.length) { prev.push(...routeFlattening(cur.children)) } return prev }, []) } // 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() // } // });