|
@@ -12,6 +12,7 @@ import { useEnterpriseStore } from '@/store/enterprise'
|
|
|
const { start, done } = useNProgress()
|
|
|
|
|
|
let isRefresh = true
|
|
|
+const ENTERPRISE_PATH = '/recruit/enterprise'
|
|
|
|
|
|
// loginType:1.enterprise: 企业路由
|
|
|
// 2.personal: 个人路由
|
|
@@ -19,6 +20,18 @@ let isRefresh = true
|
|
|
// 3.personalCommon: 无需登录也能访问的页面
|
|
|
// 路由守卫
|
|
|
router.beforeEach(async (to, from, next) => {
|
|
|
+ start()
|
|
|
+ // 个人端 404 处理
|
|
|
+ // debugger
|
|
|
+ if (to.path === '/404') {
|
|
|
+ next()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ const isEnterprise = to.path.includes(ENTERPRISE_PATH)
|
|
|
+ if (!isEnterprise && to.path !== '/enterpriseVerification' && !hasRoute(to.path)) {
|
|
|
+ next('/404')
|
|
|
+ }
|
|
|
+ localStorage.setItem('routerTest', to.path) // 本地环境保存代码热更新会导致路径缺失问题
|
|
|
// 获取商城装修模版
|
|
|
const mallStore = useMallStore()
|
|
|
const enterpriseStore = useEnterpriseStore()
|
|
@@ -27,8 +40,6 @@ router.beforeEach(async (to, from, next) => {
|
|
|
}
|
|
|
|
|
|
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' } : {}
|
|
@@ -41,23 +52,48 @@ router.beforeEach(async (to, from, next) => {
|
|
|
return
|
|
|
}
|
|
|
if (getToken(tokenIndex)) {
|
|
|
- if (tokenIndex === 1) { // 获取企业路由
|
|
|
+ if (isEnterprise) {
|
|
|
+ // 获取企业路由
|
|
|
if (!enterpriseStore.enterpriseMenu || !enterpriseStore.enterpriseMenu.length ) {
|
|
|
const { menus } = await enterpriseStore.getEnterpriseMenu()
|
|
|
enterpriseStore.saveEnterpriseMenu(menus)
|
|
|
}
|
|
|
+ // 渲染路由
|
|
|
if (isRefresh) {
|
|
|
- try {
|
|
|
- isRefresh = false
|
|
|
- const routes = enterpriseStore.assignEnterpriseMenu(enterpriseStore.enterpriseMenu)
|
|
|
- routes.forEach(route => {
|
|
|
- router.addRoute(route)
|
|
|
- })
|
|
|
- next({ ...to, replace: true })
|
|
|
- } catch(error) {
|
|
|
- console.log(error)
|
|
|
+ isRefresh = false
|
|
|
+ const routes = enterpriseStore.assignEnterpriseMenu(enterpriseStore.enterpriseMenu)
|
|
|
+ routes.forEach(route => {
|
|
|
+ router.addRoute(route)
|
|
|
+ })
|
|
|
+ // 判断是否存在路由
|
|
|
+ if (to.path !== ENTERPRISE_PATH && !hasRoute(to.path)) {
|
|
|
+ next('/404')
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if (to.path === ENTERPRISE_PATH) {
|
|
|
+ next(findPath(routes))
|
|
|
+ // const firstPath = routes[0]
|
|
|
+ // if (!firstPath.children) {
|
|
|
+ // next(firstPath.path)
|
|
|
+ // } else {
|
|
|
+ // // 查找二级路由
|
|
|
+ // next(routes[0].children[0].path)
|
|
|
+ // }
|
|
|
+ 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 + '/')
|
|
|
+ }
|
|
|
}
|
|
|
- // debugger
|
|
|
+ next({ ...to, replace: true })
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 判断是否存在路由
|
|
|
+ if (!hasRoute(to.path)) {
|
|
|
+ next('/404')
|
|
|
return
|
|
|
}
|
|
|
}
|
|
@@ -86,19 +122,21 @@ router.beforeEach(async (to, from, next) => {
|
|
|
}
|
|
|
}
|
|
|
if (to.fullPath === '/login') {
|
|
|
- next({ path: '/recruitHome' })
|
|
|
+ next('/recruitHome')
|
|
|
+ return
|
|
|
}
|
|
|
// 获取字典信息
|
|
|
// const dictStore = useDictStore()
|
|
|
// dictStore.getDictTypeData()
|
|
|
next()
|
|
|
+ return
|
|
|
}
|
|
|
if (to.meta?.loginType === 'personalCommon' || to.meta?.loginType === 'common') { // 路由不重定向
|
|
|
next()
|
|
|
return
|
|
|
}
|
|
|
if (to.meta?.loginType === 'enterprise') { // 没有企业token->去个人首页
|
|
|
- next({ path: '/recruitHome' })
|
|
|
+ next('/recruitHome')
|
|
|
return
|
|
|
}
|
|
|
next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
|
|
@@ -110,6 +148,48 @@ router.afterEach((to) => {
|
|
|
// loadDone()
|
|
|
})
|
|
|
|
|
|
+
|
|
|
+function hasRoute (path) {
|
|
|
+ const routes = router.getRoutes()
|
|
|
+ // console.log(routes.filter(item => item.path.includes('/recruit/enterprise')))
|
|
|
+ // debugger
|
|
|
+ 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))) {
|