赫艾前端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
7.6 KiB

4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
  1. /**
  2. * 全站路由配置
  3. *
  4. * 建议:
  5. * 1. 代码中路由统一使用name属性跳转(不使用path属性)
  6. */
  7. import Vue from 'vue'
  8. import Router from 'vue-router'
  9. import http from '@/utils/httpRequest'
  10. import { isURL } from '@/utils/validate'
  11. import { clearLoginInfo } from '@/utils'
  12. import i18n from '@/i18n/i18n'
  13. Vue.use(Router)
  14. // Vue.use(VueRouter);
  15. // 开发环境不使用懒加载, 因为懒加载页面太多的话会造成webpack热更新太慢, 所以只有生产环境使用懒加载
  16. const _import = require('./import-' + process.env.NODE_ENV)
  17. // 全局路由(无需嵌套上左右整体布局)
  18. const globalRoutes = [
  19. { path: '/404', component: _import('common/404'), name: '404', meta: { title: '404未找到' } },
  20. { path: '/login', component: _import('common/login'), name: 'login', meta: { title: '登录' } },
  21. { path: '/thisWeekShipmentBoard', component: _import('modules/board/thisWeekShipmentBoard'), name: 'thisWeekShipmentBoard', meta: { title: '本周发货看板' } },
  22. { path: '/nextWeekShipmentBoard', component: _import('modules/board/nextWeekShipmentBoard'), name: 'nextWeekShipmentBoard', meta: { title: '下周发货看板' } },
  23. { path: '/transFerBoard', component: _import('modules/board/transFerBoard'), name: 'transFerBoard', meta: { title: '待入库看板' } },
  24. { path: '/productionBlankingBoard', component: _import('modules/board/productionBlankingBoard'), name: 'productionBlankingBoard', meta: { title: '生产打料看板' } },
  25. { path: '/soLiuhuaBoard', component: _import('modules/board/soLiuhuaBoard'), name: 'soLiuhuaBoard', meta: { title: '硫化看板' } },
  26. { path: '/SORoutingBoard', component: _import('modules/board/SORoutingBoard'), name: 'SORoutingBoard', meta: { title: '待生产工序看板' } },
  27. { path: '/padPart', component: _import('modules/pad/padPart1'), name: 'padPart1', meta: { title: '产品搜索' } },
  28. { path: '/padPartAttribute', component: _import('modules/pad/padPartAttribute'), name: 'padPartAttribute', meta: { title: '产品属性' } },
  29. { path: '/padPartPhoto', component: _import('modules/pad/padPartPhoto'), name: 'padPartPhoto', meta: { title: '产品图片' } },
  30. { path: '/taskReport', component: _import('modules/taskmanage/taskReport'), name: 'taskReport', meta: { title: '任务看板' } },
  31. { path: '/autoPrintStock', component: _import('modules/autoPrint/autoPrintStock'), name: 'autoPrintStock', meta: { title: '自动打印库存' } },
  32. ]
  33. // 主入口路由(需嵌套上左右整体布局)
  34. const mainRoutes = {
  35. path: '/',
  36. component: _import('main'),
  37. name: 'main',
  38. redirect: { name: 'home' },
  39. meta: { title: '主入口整体布局' },
  40. children: [
  41. // 通过meta对象设置路由展示方式
  42. // 1. isTab: 是否通过tab展示内容, true: 是, false: 否
  43. // 2. iframeUrl: 是否通过iframe嵌套展示内容, '以http[s]://开头': 是, '': 否
  44. // 提示: 如需要通过iframe嵌套展示内容, 但不通过tab打开, 请自行创建组件使用iframe处理!
  45. { path: '/home', component: _import('common/home'), name: 'home', meta: { title: '首页' } },
  46. { path: '/theme', component: _import('common/theme'), name: 'theme', meta: { title: '主题' } },
  47. // { path: '/demo-echarts', component: _import('demo/echarts'), name: 'demo-echarts', meta: { title: 'demo-echarts', isTab: true } },
  48. // { path: '/demo-ueditor', component: _import('demo/ueditor'), name: 'demo-ueditor', meta: { title: 'demo-ueditor', isTab: true } }
  49. ],
  50. beforeEnter (to, from, next) {
  51. let token = Vue.cookie.get('token')
  52. if (!token || !/\S/.test(token)) {
  53. clearLoginInfo()
  54. next({ name: 'login' })
  55. }
  56. next()
  57. }
  58. }
  59. const router = new Router({
  60. mode: 'hash',
  61. scrollBehavior: () => ({ y: 0 }),
  62. isAddDynamicMenuRoutes: false, // 是否已经添加动态(菜单)路由
  63. routes: globalRoutes.concat(mainRoutes)
  64. })
  65. router.beforeEach((to, from, next) => {
  66. // 添加动态(菜单)路由
  67. // 1. 已经添加 or 全局路由, 直接访问
  68. // 2. 获取菜单列表, 添加并保存本地存储
  69. if (router.options.isAddDynamicMenuRoutes || fnCurrentRouteType(to, globalRoutes) === 'global') {
  70. next()
  71. } else {
  72. http({
  73. url: http.adornUrl('/sys/menu/nav'),
  74. method: 'get',
  75. params: {
  76. 'l': i18n.locale, //i18n.locale
  77. menuType: "pc"
  78. }
  79. }).then(({data}) => {
  80. if (data && data.code === 0) {
  81. fnAddDynamicMenuRoutes(data.menuList)
  82. router.options.isAddDynamicMenuRoutes = true
  83. sessionStorage.setItem('menuList', JSON.stringify(data.menuList || '[]'))
  84. sessionStorage.setItem('permissions', JSON.stringify(data.permissions || '[]'))
  85. next({ ...to, replace: true })
  86. } else {
  87. sessionStorage.setItem('menuList', '[]')
  88. sessionStorage.setItem('permissions', '[]')
  89. next()
  90. }
  91. }).catch((e) => {
  92. console.log(`%c${e} 请求菜单列表和权限失败,跳转至登录页!!`, 'color:blue')
  93. router.push({ name: 'login' })
  94. })
  95. }
  96. })
  97. /**
  98. * 判断当前路由类型, global: 全局路由, main: 主入口路由
  99. * @param {*} route 当前路由
  100. */
  101. function fnCurrentRouteType (route, globalRoutes = []) {
  102. var temp = []
  103. for (var i = 0; i < globalRoutes.length; i++) {
  104. if (route.path === globalRoutes[i].path) {
  105. return 'global'
  106. } else if (globalRoutes[i].children && globalRoutes[i].children.length >= 1) {
  107. temp = temp.concat(globalRoutes[i].children)
  108. }
  109. }
  110. return temp.length >= 1 ? fnCurrentRouteType(route, temp) : 'main'
  111. }
  112. /**
  113. * 添加动态(菜单)路由
  114. * @param {*} menuList 菜单列表
  115. * @param {*} routes 递归创建的动态(菜单)路由
  116. */
  117. function fnAddDynamicMenuRoutes (menuList = [], routes = []) {
  118. var temp = []
  119. for (var i = 0; i < menuList.length; i++) {
  120. if (menuList[i].list && menuList[i].list.length >= 1) {
  121. temp = temp.concat(menuList[i].list)
  122. } else if (menuList[i].url && /\S/.test(menuList[i].url)) {
  123. menuList[i].url = menuList[i].url.replace(/^\//, '')
  124. var route = {
  125. path: menuList[i].url.replace('/', '-'),
  126. component: null,
  127. name: menuList[i].url.replace('/', '-'),
  128. meta: {
  129. menuId: menuList[i].menuId,
  130. title: menuList[i].name,
  131. isDynamic: true,
  132. isTab: true,
  133. iframeUrl: ''
  134. }
  135. }
  136. // url以http[s]://开头, 通过iframe展示
  137. if (isURL(menuList[i].url)) {
  138. route['path'] = `i-${menuList[i].menuId}`
  139. route['name'] = `i-${menuList[i].menuId}`
  140. route['meta']['iframeUrl'] = menuList[i].url
  141. } else {
  142. try {
  143. route['component'] = _import(`modules/${menuList[i].url}`) || null
  144. } catch (e) {}
  145. }
  146. routes.push(route)
  147. }
  148. }
  149. if (temp.length >= 1) {
  150. fnAddDynamicMenuRoutes(temp, routes)
  151. } else {
  152. mainRoutes.name = 'main-dynamic'
  153. mainRoutes.children = routes
  154. router.addRoutes([
  155. mainRoutes,
  156. { path: '*', redirect: { name: '404' } }
  157. ])
  158. sessionStorage.setItem('dynamicMenuRoutes', JSON.stringify(mainRoutes.children || '[]'))
  159. console.log('\n')
  160. console.log('%c!<-------------------- 动态(菜单)路由 s -------------------->', 'color:blue')
  161. console.log(mainRoutes.children)
  162. console.log('%c!<-------------------- 动态(菜单)路由 e -------------------->', 'color:blue')
  163. }
  164. }
  165. // const routes = [
  166. // {
  167. // path: '/orderBoard',
  168. // name: 'orderBoard',
  169. // component: () => import('../views/orderBoard.vue'),
  170. // },
  171. // {
  172. // path: '/scheduledBoard',
  173. // name: 'scheduledBoard',
  174. // component: () => import('../views/scheduledBoard.vue'),
  175. // },
  176. // ];
  177. // const router = new VueRouter({
  178. // routes,
  179. // });
  180. export default router