index.js 5.8 KB

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