jinlin
2024-01-31 9025b9cf7ec8610003d445a31d93e35e7bd73c2e
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import http from '../utils/request'
import tip from '../utils/tips'
import store from '../store'
import {isURL} from '../utils/validate'
 
let $router
let $moduleRoutes // 模块路由(基于主入口布局页面)
let $fnGetComponent // 模块引用方法
export function setConfig(router, moduleRoutes, fnGetComponent) {
  $router = router
  $moduleRoutes = moduleRoutes
  $fnGetComponent = fnGetComponent
}
 
// 页面路由(独立页面)
export const pageRoutes = [
  {
    path: '/404',
    component: () => import('../views/pages/404'),
    name: '404',
    meta: {title: '404未找到'},
    beforeEnter(to, from, next) {
      // 拦截处理特殊业务场景
      // 如果, 重定向路由包含__双下划线, 为临时添加路由
      if (/__.*/.test(to.redirectedFrom)) {
        return next(to.redirectedFrom.replace(/__.*/, ''))
      }
      next()
    }
  }
]
 
export function beforeEach(to, from, next) {
  // 添加动态(菜单)路由
  // 已添加或者当前路由为页面路由, 可直接访问
  if (window.SITE_CONFIG['dynamicMenuRoutesHasAdded'] || fnCurrentRouteIsPageRoute(to, pageRoutes)) {
    return next()
  }
 
  // 获取字典
  store.dispatch('getDictList')
 
  // 获取菜单列表, 添加并全局变量保存
  http.get('/sys/menu/nav').then(res => {
    if (res.code !== 0) {
      return
    }
    let menuList = res.data
    console.log(res.data,'res.data res.data')
    // 给开发者添加代码生成功能
    if (window.SITE_CONFIG['nodeEnv'] === 'development') {
      menuList[0].children.push({id: 'development', name: '开发工具', icon: 'icon-solution', url: 'development'})
    }
 
    // 默认选择第一个系统
    if (menuList && menuList.length > 0) {
      window.SITE_CONFIG['menuList'] = menuList
      fnAddDynamicMenuRoutes(window.SITE_CONFIG['menuList'], [])
    } else {
      tip.error('您没有使用系统的权限!')
      return next({name: 'login'})
    }
 
    if (from.path === '/') {
      next({...to, replace: true})
    } else {
      next()
    }
  }).catch(() => {})
}
 
/**
 * 判断当前路由是否为页面路由
 * @param {*} route 当前路由
 * @param {*} pageRoutes 页面路由
 */
function fnCurrentRouteIsPageRoute(route, pageRoutes = []) {
  var temp = []
  for (var i = 0; i < pageRoutes.length; i++) {
    if (route.path === pageRoutes[i].path) {
      return true
    }
    if (pageRoutes[i].children && pageRoutes[i].children.length >= 1) {
      temp = temp.concat(pageRoutes[i].children)
    }
  }
  return temp.length >= 1 ? fnCurrentRouteIsPageRoute(route, temp) : false
}
 
/**
 * 添加动态(菜单)路由
 * @param {*} menuList 菜单列表
 * @param {*} routes 递归创建的动态(菜单)路由
 */
function fnAddDynamicMenuRoutes(menuList = [], routes = []) {
  var temp = []
  for (var i = 0; i < menuList.length; i++) {
    if (menuList[i].children && menuList[i].children.length >= 1) {
      temp = temp.concat(menuList[i].children)
      continue
    }
    // 组装路由
    var route = {
      path: '',
      component: null,
      name: '',
      meta: {
        ...window.SITE_CONFIG['contentTabDefault'],
        menuId: menuList[i].id,
        title: menuList[i].name,
        remark: menuList[i].remark
      }
    }
    // eslint-disable-next-line
    let URL = (menuList[i].url || '').replace(/{{([^}}]+)?}}/g, (s1, s2) => eval(s2)) // URL支持{{ window.xxx }}占位符变量
    if (isURL(URL)) {
      route['path'] = route['name'] = `i-${menuList[i].id}`
      route['meta']['iframeURL'] = URL
    } else {
      URL = URL.replace(/^\//, '').replace(/_/g, '-')
      route['path'] = route['name'] = URL.replace(/\//g, '-')
      route['component'] = () => {
        if (URL === 'development') {
          return import('../views/pages/development')
        } else if (PackagesViews.indexOf(URL + '.vue') >= 0) {
          return import(`../views/modules/${URL}`)
        } else {
          return $fnGetComponent(URL)
        }
      }
    }
    routes.push(route)
  }
  if (temp.length >= 1) {
    return fnAddDynamicMenuRoutes(temp, routes)
  }
  // 添加路由
  $router.addRoutes([
    {
      ...$moduleRoutes,
      name: 'main-dynamic-menu',
      children: routes
    },
    {path: '*', redirect: {name: '404'}}
  ])
  window.SITE_CONFIG['dynamicMenuRoutes'] = routes
  window.SITE_CONFIG['dynamicMenuRoutesHasAdded'] = true
}
 
// 添加动态路由
export function addDynamicRoute(routeParams) {
  // 组装路由名称, 并判断是否已添加, 如是: 则直接跳转
  var routeName = routeParams.routeName
  var dynamicRoute = window.SITE_CONFIG['dynamicRoutes'].filter(item => item.name === routeName)[0]
  if (dynamicRoute) {
    return $router.push({
      name: routeName,
      params: routeParams.params
    })
  }
  // 否则: 添加并全局变量保存, 再跳转
  dynamicRoute = {
    path: routeName,
    component: () => {
      if (PackagesViews.indexOf(routeParams.path) >= 0) {
        return import(`../views/modules/${routeParams.path}`)
      } else {
        return $fnGetComponent(routeParams.path)
      }
    },
    name: routeName,
    meta: {
      ...window.SITE_CONFIG['contentTabDefault'],
      menuId: routeParams.menuId,
      title: `${routeParams.title}`,
      remark: `${routeParams.remark}`
    }
  }
  $router.addRoutes([{
    ...$moduleRoutes,
    name: `main-dynamic__${dynamicRoute.name}`,
    children: [dynamicRoute]
  }])
  window.SITE_CONFIG['dynamicRoutes'].push(dynamicRoute)
  $router.push({
    name: dynamicRoute.name,
    params: routeParams.params
  })
}
 
// 框架页面find $PWD/* | xargs ls -d | grep '.vue'
export const PackagesViews = [
  'bpm/definition/definition-add-or-update.vue',
  'bpm/definition/definition-list.vue',
  'bpm/definition/definition-versions.vue',
  'bpm/definition/start-workflow.vue',
  'bpm/instance/instance-image.vue',
  'bpm/instance/instance-list.vue',
  'bpm/task/task-complete.vue',
  'bpm/task/task-list.vue',
  'bpm/task/task-opinion.vue',
  'form/businessObject/bus-object-add-or-update.vue',
  'form/businessObject/bus-object-dialog.vue',
  'form/businessObject/bus-object.vue',
  'form/businessTable/bus-table-add-or-update.vue',
  'form/businessTable/bus-table-dialog.vue',
  'form/businessTable/bus-table.vue',
  'form/dynamicForm/dynamic-form.vue',
  'form/formDefinition/form-definition-add-or-update.vue',
  'form/formDefinition/form-definition.vue',
  'form/formTemplate/form-template-add-or-update.vue',
  'form/formTemplate/form-template.vue',
  'message/mail-log.vue',
  'message/mail-template-add-or-update.vue',
  'message/mail-template-config.vue',
  'message/mail-template-send.vue',
  'message/mail-template.vue',
  'message/sms-log.vue',
  'message/sms-send.vue',
  'message/sms-template-add-or-update.vue',
  'message/sms-template.vue',
  'notice/notice-add-or-update.vue',
  'notice/notice-user-view.vue',
  'notice/notice-user.vue',
  'notice/notice-view.vue',
  'notice/notice.vue',
  'oss/oss.vue',
  'sys/company-add-or-update.vue',
  'sys/company.vue',
  'sys/dept-add-or-update.vue',
  'sys/dept.vue',
  'sys/dict-data-add-or-update.vue',
  'sys/dict-data.vue',
  'sys/dict-type-add-or-update.vue',
  'sys/dict-type.vue',
  'sys/job-add-or-update.vue',
  'sys/job.vue',
  'sys/log-error.vue',
  'sys/log-login.vue',
  'sys/log-operation.vue',
  'sys/menu-add-or-update.vue',
  'sys/menu.vue',
  'sys/params-add-or-update.vue',
  'sys/params.vue',
  'sys/post-add-or-update.vue',
  'sys/post.vue',
  'sys/role-add-or-update.vue',
  'sys/role-data-scope.vue',
  'sys/role-menu.vue',
  'sys/role.vue',
  'sys/tenant-add-or-update.vue',
  'sys/tenant-menu.vue',
  'sys/tenant.vue',
  'sys/user-add-or-update.vue',
  'sys/user-role.vue',
  'sys/user.vue'
]