index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. import page from '@/config/page'
  4. import store from '@/vendors/vuex'
  5. import { renewalHead } from '@/model/indexRx'
  6. Vue.use(Router)
  7. // 组件实验室
  8. // const _Laboratory = () => import('@/components/base/_laboratory')
  9. /* Layout */
  10. import Layout from '../layout/index'
  11. /**
  12. * constantRoutes
  13. * 没有权限要求的基本页
  14. * 所有角色都可以访问
  15. * */
  16. export const constantRoutes = [
  17. {
  18. path: '/about',
  19. component: () => import('@/views/info/about'),
  20. hidden: true
  21. },
  22. {
  23. path: '/privacyAgreement',
  24. component: () => import('@/views/info/privacyAgreement'),
  25. hidden: true
  26. },
  27. {
  28. path: '/userAgreement',
  29. component: () => import('@/views/info/userAgreement'),
  30. hidden: true
  31. },
  32. {
  33. path: '/login',
  34. component: () =>
  35. import(/* webpackChunkName: "login" */ '@/views/login/index'),
  36. hidden: true
  37. },
  38. {
  39. path: '/ship_login',
  40. component: () =>
  41. import(/* webpackChunkName: "logindship" */ '@/views/login/index_ship'),
  42. hidden: true
  43. },
  44. {
  45. path: '/404',
  46. component: () => page('404'),
  47. hidden: true
  48. },
  49. {
  50. path: '/401',
  51. component: () =>
  52. import(/* webpackChunkName: "401" */ '@/views/errorPage/401'),
  53. hidden: true
  54. },
  55. {
  56. path: '/help',
  57. component: () => import('@/views/helpCenter/index'),
  58. hidden: true
  59. },
  60. {
  61. path: '/',
  62. component: Layout,
  63. redirect: { name: 'home' },
  64. meta: { title: 'home', icon: '-index-copy' },
  65. children: [
  66. {
  67. path: 'home',
  68. component: () => page('home'),
  69. name: 'home',
  70. meta: { title: 'home', icon: '-index-copy', affix: true, auth: 'homePage' },
  71. hidden: true
  72. }
  73. ]
  74. }
  75. ]
  76. // process.env.NODE_ENV === 'development' &&
  77. // constantRoutes.push({ path: '/lab', name: '_Laboratory', component: _Laboratory })
  78. let cofigRouter = []
  79. const modulesFiles = require.context('../views', true, /.js$/)
  80. modulesFiles.keys().forEach((model_item, key) => {
  81. if (model_item === 'index.js' || modulesFiles(model_item).default === undefined || modulesFiles(model_item).default.path === undefined) return
  82. cofigRouter = cofigRouter.concat(modulesFiles(model_item).default)
  83. })
  84. // 需要根据用户角色动态加载的路由
  85. export const asyncRoutes = cofigRouter
  86. const createRouter = () =>
  87. new Router({
  88. // mode: 'history',
  89. scrollBehavior: () => ({
  90. y: 0
  91. }),
  92. linkActiveClass: 'active', // router-link .active样式
  93. routes: constantRoutes
  94. })
  95. const router = createRouter()
  96. export function resetRouter() {
  97. const newRouter = createRouter()
  98. router.matcher = newRouter.matcher // reset router
  99. }
  100. router.beforeEach(async (to, from, next) => {
  101. // const { roles, isTrainDialog, guideInfo } = store.getters
  102. const { roles } = store.getters
  103. if (roles && roles.length && to.path !== '/help') {
  104. // if (Object.keys(guideInfo).length === 0) {
  105. // store.dispatch('user/getTrainInfo')
  106. // }
  107. // if (isTrainDialog.length === 0) {
  108. // store.dispatch('user/getNewLogin')
  109. // }
  110. if (
  111. to.matched[1] &&
  112. to.matched[1].parent &&
  113. to.matched[1].parent.meta &&
  114. to.matched[1].parent.meta.title
  115. ) {
  116. sessionStorage.setItem(
  117. 'ws-pf_moduleName',
  118. to.matched[1].parent.meta.title
  119. )
  120. } else {
  121. sessionStorage.setItem('ws-pf_moduleName', to.meta.title)
  122. }
  123. if (to.meta.module || to.meta.auth) {
  124. sessionStorage.setItem('ws-pf_authority', to.meta.auth || to.meta.module)
  125. }
  126. }
  127. sessionStorage.setItem('ws-pf_menuName', to.meta.title)
  128. const { baseInfo } = store.getters
  129. if (Object.keys(baseInfo).length === 0) {
  130. store.dispatch('user/getBaseInfo')
  131. }
  132. let data = {
  133. title: '',
  134. routingUri: ''
  135. }
  136. if (to.name && to.meta.shortcutEntrance) {
  137. if (to.meta.shortcutEntrance.indexOf(',') == -1) {
  138. if (to.name == 'knowledgeList' || to.name == 'dataManageMentList') {
  139. data.title = to.meta.shortcutEntrance
  140. } else {
  141. data.title = to.meta.shortcutEntrance + ',' + to.meta.title
  142. }
  143. if (to.path.split('/').length > 3 && to.path.split('/')[1] != 'crew') {
  144. data.routingUri = to.path
  145. .split('/')
  146. .slice(0, -1)
  147. .join('/')
  148. } else {
  149. data.routingUri = to.path
  150. }
  151. } else {
  152. data.title = to.meta.shortcutEntrance
  153. if (
  154. to.name != 'certificateList' &&
  155. to.name != 'impaMa' &&
  156. to.name != 'applyMa' &&
  157. to.name != 'shipMa' &&
  158. to.name != 'theLibraryMa' &&
  159. to.name != 'inventoryManagementMa'
  160. ) {
  161. data.routingUri =
  162. to.path.split('/').length > 2
  163. ? to.path
  164. .split('/')
  165. .slice(0, -1)
  166. .join('/')
  167. : to.path
  168. } else {
  169. data.routingUri = to.path
  170. }
  171. }
  172. renewalHead(data).toPromise().then(() => {
  173. next()
  174. })
  175. } else {
  176. next()
  177. }
  178. })
  179. export default router