tj
2025-06-05 bba272999cc546f65781bf3d20245a3f819af67f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { createRouter, createWebHashHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
 
import appConfig from '@/config/index'
import Util from '@/lin/util/util'
import autoJump from '@/lin/util/auto-jump'
import store from '../store'
import routes from './route'
 
// 判断是否需要登录访问, 配置位于 config 文件夹
let isLoginRequired = routeName => {
  // 首次执行时缓存配置
  let { notLoginRoute } = appConfig
  const notLoginMark = {}
 
  // 构建标记对象
  if (Array.isArray(notLoginRoute)) {
    for (let i = 0; i < notLoginRoute.length; i += 1) {
      notLoginMark[notLoginRoute[i].toString()] = true
    }
  }
 
  notLoginRoute = null // 释放内存
 
  // 重写初始化函数
  isLoginRequired = name => {
    if (!name) {
      return true
    }
    // 处理 Symbol 类型
    const target = typeof name === 'symbol' ? name.description : name
    return !notLoginMark[target]
  }
 
  return isLoginRequired(routeName)
}
 
const router = createRouter({
  scrollBehavior: () => ({ y: 0 }),
  base: process.env.BASE_URL,
  history: createWebHashHistory(),
  routes,
})
 
router.beforeEach((to, from, next) => {
  // 登录验证
  if (isLoginRequired(to.name) && !store.state.loggedIn) {
    next({ path: '/login' })
    return
  }
 
  // TODO: tab 模式重复点击验证
 
  // 权限验证
  if (store?.state && store?.getters) {
    const { permissions, user } = store.getters
    if (to.path !== '/about' && !Util.hasPermission(permissions, to.meta, user)) {
      ElMessage.error('您无此页面的权限哟')
      next({ path: '/about' })
      return
    }
  }
 
  // 路由发生变化重新计时
  autoJump(router)
 
  // 路由发生变化修改页面title
  if (to.meta.title) {
    document.title = to.meta.title
  }
 
  next()
})
 
export default router