赫艾前端
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.

204 lines
9.5 KiB

4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 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: '/SeqNoForLiuHuaJiChu', component: _import('modules/board/SeqNoForLiuHuaJiChu'), name: 'SeqNoForLiuHuaJiChu', meta: { title: '挤出看板' } },
  27. { path: '/SORoutingBoardForJianYan', component: _import('modules/board/SORoutingBoardForJianYan'), name: 'SORoutingBoardForJianYan', meta: { title: '检验工序看板' } },
  28. { path: '/padPart', component: _import('modules/pad/padPart1'), name: 'padPart1', meta: { title: '产品搜索' } },
  29. { path: '/padPartAttribute', component: _import('modules/pad/padPartAttribute'), name: 'padPartAttribute', meta: { title: '产品属性' } },
  30. { path: '/padPartPhoto', component: _import('modules/pad/padPartPhoto'), name: 'padPartPhoto', meta: { title: '产品图片' } },
  31. { path: '/padCODelNotify', component: _import('modules/pad/padCODelNotify'), name: 'padCODelNotify', meta: { title: '发货通知单' } },
  32. { path: '/taskReport', component: _import('modules/taskmanage/taskReport'), name: 'taskReport', meta: { title: '任务看板' } },
  33. { path: '/autoPrintStock', component: _import('modules/autoPrint/autoPrintStock'), name: 'autoPrintStock', meta: { title: '自动打印库存' } },
  34. { path: '/autoPrint', component: _import('modules/autoPrint/autoPrint'), name: 'autoPrint', meta: { title: '自动打印标签' } },
  35. { path: '/autoPrintStockHunlian', component: _import('modules/autoPrint/autoPrintStockHunlian'), name: 'autoPrintStockHunlian', meta: { title: '自动打印混炼胶库存' } },
  36. { path: '/SORoutingBoardForXiuBian', component: _import('modules/board/SORoutingBoardForXiuBian'), name: 'SORoutingBoardForXiuBian', meta: { title: '修边工序看板' } },
  37. { path: '/PODetailForW', component: _import('modules/board/PODetailForW'), name: 'PODetailForW', meta: { title: '外购件检验看板' } },
  38. { path: '/transFerBoardForHunlian', component: _import('modules/board/transFerBoardForHunlian'), name: 'transFerBoardForHunlian', meta: { title: '混炼待入库看板' } },
  39. { path: '/SORoutingBoardForBaoZhuang', component: _import('modules/board/SORoutingBoardForBaoZhuang'), name: 'SORoutingBoardForBaoZhuang', meta: { title: '包装工序看板' } },
  40. { path: '/test', component: _import('modules/board/test'), name: 'test', meta: { title: 'test' } },
  41. { path: '/SeqNoForLiuHua', component: _import('modules/board/SeqNoForLiuHua'), name: 'SeqNoForLiuHua', meta: { title: '生产派工单看板' } },
  42. { path: '/SORoutingBoardForShuiZhu', component: _import('modules/board/SORoutingBoardForShuiZhu'), name: 'SORoutingBoardForShuiZhu', meta: { title: '水煮烘烤工序看板' } },
  43. { path: '/inspectBoard', component: _import('modules/board/inspectBoard'), name: 'inspectBoard', meta: { title: '实验室检验看板' } },
  44. { path: '/sopBoard', component: _import('modules/board/sopBoard'), name: 'sopBoard', meta: { title: 'SOP电视机' } },
  45. ]
  46. // 主入口路由(需嵌套上左右整体布局)
  47. const mainRoutes = {
  48. path: '/',
  49. component: _import('main'),
  50. name: 'main',
  51. redirect: { name: 'home' },
  52. meta: { title: '主入口整体布局' },
  53. children: [
  54. // 通过meta对象设置路由展示方式
  55. // 1. isTab: 是否通过tab展示内容, true: 是, false: 否
  56. // 2. iframeUrl: 是否通过iframe嵌套展示内容, '以http[s]://开头': 是, '': 否
  57. // 提示: 如需要通过iframe嵌套展示内容, 但不通过tab打开, 请自行创建组件使用iframe处理!
  58. { path: '/home', component: _import('common/home'), name: 'home', meta: { title: '首页' } },
  59. { path: '/theme', component: _import('common/theme'), name: 'theme', meta: { title: '主题' } },
  60. // { path: '/demo-echarts', component: _import('demo/echarts'), name: 'demo-echarts', meta: { title: 'demo-echarts', isTab: true } },
  61. // { path: '/demo-ueditor', component: _import('demo/ueditor'), name: 'demo-ueditor', meta: { title: 'demo-ueditor', isTab: true } }
  62. ],
  63. beforeEnter (to, from, next) {
  64. let token = Vue.cookie.get('token')
  65. if (!token || !/\S/.test(token)) {
  66. clearLoginInfo()
  67. next({ name: 'login' })
  68. }
  69. next()
  70. }
  71. }
  72. const router = new Router({
  73. mode: 'hash',
  74. scrollBehavior: () => ({ y: 0 }),
  75. isAddDynamicMenuRoutes: false, // 是否已经添加动态(菜单)路由
  76. routes: globalRoutes.concat(mainRoutes)
  77. })
  78. router.beforeEach((to, from, next) => {
  79. // 添加动态(菜单)路由
  80. // 1. 已经添加 or 全局路由, 直接访问
  81. // 2. 获取菜单列表, 添加并保存本地存储
  82. if (router.options.isAddDynamicMenuRoutes || fnCurrentRouteType(to, globalRoutes) === 'global') {
  83. next()
  84. } else {
  85. http({
  86. url: http.adornUrl('/sys/menu/nav'),
  87. method: 'get',
  88. params: {
  89. 'l': i18n.locale, //i18n.locale
  90. menuType: "pc"
  91. }
  92. }).then(({data}) => {
  93. if (data && data.code === 0) {
  94. fnAddDynamicMenuRoutes(data.menuList)
  95. router.options.isAddDynamicMenuRoutes = true
  96. sessionStorage.setItem('menuList', JSON.stringify(data.menuList || '[]'))
  97. sessionStorage.setItem('permissions', JSON.stringify(data.permissions || '[]'))
  98. next({ ...to, replace: true })
  99. } else {
  100. sessionStorage.setItem('menuList', '[]')
  101. sessionStorage.setItem('permissions', '[]')
  102. next()
  103. }
  104. }).catch((e) => {
  105. console.log(`%c${e} 请求菜单列表和权限失败,跳转至登录页!!`, 'color:blue')
  106. router.push({ name: 'login' })
  107. })
  108. }
  109. })
  110. /**
  111. * 判断当前路由类型, global: 全局路由, main: 主入口路由
  112. * @param {*} route 当前路由
  113. */
  114. function fnCurrentRouteType (route, globalRoutes = []) {
  115. var temp = []
  116. for (var i = 0; i < globalRoutes.length; i++) {
  117. if (route.path === globalRoutes[i].path) {
  118. return 'global'
  119. } else if (globalRoutes[i].children && globalRoutes[i].children.length >= 1) {
  120. temp = temp.concat(globalRoutes[i].children)
  121. }
  122. }
  123. return temp.length >= 1 ? fnCurrentRouteType(route, temp) : 'main'
  124. }
  125. /**
  126. * 添加动态(菜单)路由
  127. * @param {*} menuList 菜单列表
  128. * @param {*} routes 递归创建的动态(菜单)路由
  129. */
  130. function fnAddDynamicMenuRoutes (menuList = [], routes = []) {
  131. var temp = []
  132. for (var i = 0; i < menuList.length; i++) {
  133. if (menuList[i].list && menuList[i].list.length >= 1) {
  134. temp = temp.concat(menuList[i].list)
  135. } else if (menuList[i].url && /\S/.test(menuList[i].url)) {
  136. menuList[i].url = menuList[i].url.replace(/^\//, '')
  137. var route = {
  138. path: menuList[i].url.replace('/', '-'),
  139. component: null,
  140. name: menuList[i].url.replace('/', '-'),
  141. meta: {
  142. menuId: menuList[i].menuId,
  143. title: menuList[i].name,
  144. isDynamic: true,
  145. isTab: true,
  146. iframeUrl: ''
  147. }
  148. }
  149. // url以http[s]://开头, 通过iframe展示
  150. if (isURL(menuList[i].url)) {
  151. route['path'] = `i-${menuList[i].menuId}`
  152. route['name'] = `i-${menuList[i].menuId}`
  153. route['meta']['iframeUrl'] = menuList[i].url
  154. } else {
  155. try {
  156. route['component'] = _import(`modules/${menuList[i].url}`) || null
  157. } catch (e) {}
  158. }
  159. routes.push(route)
  160. }
  161. }
  162. if (temp.length >= 1) {
  163. fnAddDynamicMenuRoutes(temp, routes)
  164. } else {
  165. mainRoutes.name = 'main-dynamic'
  166. mainRoutes.children = routes
  167. router.addRoutes([
  168. mainRoutes,
  169. { path: '*', redirect: { name: '404' } }
  170. ])
  171. sessionStorage.setItem('dynamicMenuRoutes', JSON.stringify(mainRoutes.children || '[]'))
  172. console.log('\n')
  173. console.log('%c!<-------------------- 动态(菜单)路由 s -------------------->', 'color:blue')
  174. console.log(mainRoutes.children)
  175. console.log('%c!<-------------------- 动态(菜单)路由 e -------------------->', 'color:blue')
  176. }
  177. }
  178. // const routes = [
  179. // {
  180. // path: '/orderBoard',
  181. // name: 'orderBoard',
  182. // component: () => import('../views/orderBoard.vue'),
  183. // },
  184. // {
  185. // path: '/scheduledBoard',
  186. // name: 'scheduledBoard',
  187. // component: () => import('../views/scheduledBoard.vue'),
  188. // },
  189. // ];
  190. // const router = new VueRouter({
  191. // routes,
  192. // });
  193. export default router