index.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import {createRouter, createWebHistory} from 'vue-router'
  2. import LayoutCom from '@/layout/index.vue'
  3. import staticRouter from './modules/static'
  4. import store from '@/store/index'
  5. import TempCom from '@/views/global/temp.vue'
  6. import RouterViewCom from "@/layout/router-view.vue";
  7. // import {stagingRouterMap, stagingRouter} from './modules/staging'
  8. // import {systemRouterMap, systemRouter} from './modules/system'
  9. import {ElMessage} from "element-plus";
  10. import {toLogin} from "@/utils/permissions";
  11. export const RoutersMap: any = new Map([
  12. // ...stagingRouterMap,
  13. // ...systemRouterMap,
  14. ])
  15. const routes = [
  16. ...staticRouter,
  17. {
  18. path: '/',
  19. name: store.state.menu.menuRootName, // 菜单根路由name标识
  20. component: LayoutCom,
  21. children: [
  22. ]
  23. },
  24. { path: '/:pathMatch(.*)*', name: 'NotFound', component: TempCom }
  25. ]
  26. const router = createRouter({
  27. history: createWebHistory(),
  28. routes,
  29. });
  30. router.beforeEach((to, from , next) => {
  31. if (to.path === "/login") {
  32. if (sessionStorage.getItem("sg_token")) {
  33. location.replace('/')
  34. } else {
  35. next()
  36. }
  37. } else {
  38. if (sessionStorage.getItem("sg_token")) {
  39. getInit(to, next)
  40. } else if (to.query.axToken) {
  41. sessionStorage.setItem("sg_token", <string>to.query.axToken);
  42. setTimeout(() => {//虽然不知道为什么要加宏任务,但是我不敢改
  43. delete to.query.axToken
  44. let url = `${window.location.origin + window.location.pathname}?`
  45. for (let queryKey in to.query) {
  46. url += `${queryKey}=${to.query[queryKey]}&`
  47. }
  48. window.location.replace(url.slice(0, url.length - 1));
  49. getInit(to, next)
  50. }, 0);
  51. } else {
  52. toLogin()
  53. }
  54. }
  55. })
  56. const getInit = (to: any, next: any) => {
  57. document.title = to?.meta?.title || (import.meta as any).env.VITE_PROJECT_TITLE
  58. if (to.path === '/') {
  59. const firstRoute = store.state.menu.allRouters?.[0]
  60. if (firstRoute) {
  61. next({
  62. path: firstRoute.redirect || firstRoute.path
  63. })
  64. }
  65. } else {
  66. const m = to.matched[1]?.children?.map((v: any) => {
  67. v.expend = v.children?.some((s: any) => s.path === to.path)
  68. return v
  69. })
  70. store.dispatch('menu/LOAD_SUB_MENU_ROUTER', m || [])
  71. if (store.state.menu.subMenuRouter.length > 0) {
  72. store.dispatch('menu/LOAD_NAVIGATION', {
  73. path: to.path,
  74. name: to.name,
  75. title: to.meta.title
  76. })
  77. }
  78. next()
  79. }
  80. }
  81. export const initMainRouter = async () => {
  82. if (sessionStorage.getItem("sg_token") && location.pathname !== '/login') {
  83. setRouters()
  84. // await Promise.all([
  85. // store.dispatch('app/LOAD_USER_INFO'),
  86. // ]).then(async ([u]) => {
  87. // if (u.roles.some(v => ['ZBY', 'ZBGL'].includes(v.permissionValue))) {
  88. // setRouters()
  89. // store.dispatch('app/LOAD_PUBLIC_CONFIG', {key: 'sign.begin.time'})
  90. // store.dispatch('app/LOAD_PUBLIC_CONFIG', {key: 'sign.remind.time'})
  91. // store.dispatch('app/LOAD_PUBLIC_CONFIG', {key: 'sign.end.time'})
  92. // store.dispatch('app/LOAD_PUBLIC_CONFIG', {key: 'log.submit.time'})
  93. // store.dispatch('app/LOAD_PUBLIC_CONFIG', {key: 'log.remind.time'})
  94. // store.dispatch('app/LOAD_PUBLIC_CONFIG', {key: 'week.submit.date'})
  95. // store.dispatch('app/LOAD_PUBLIC_CONFIG', {key: 'week.submit.time'})
  96. // store.dispatch('app/LOAD_PUBLIC_CONFIG', {key: 'week.remind.time'})
  97. // store.dispatch('app/LOAD_SIGN_INFO')
  98. // store.dispatch('app/LOAD_DAILY_INFO')
  99. // store.dispatch('app/LOAD_WEEKLY_INFO')
  100. // } else {
  101. // ElMessage({
  102. // duration: 0,
  103. // type: 'warning',
  104. // message: '该账号无系统角色!点击关闭按钮跳转到登录页。',
  105. // showClose: true,
  106. // onClose: () => toLogin()
  107. // })
  108. // }
  109. // })
  110. }
  111. }
  112. const setRouters = () => {
  113. const deep = (arr: Array<any>, parentArr: Array<any>) => {
  114. return arr.map((v, i) => {
  115. const _pA = [...parentArr, v]
  116. const obj: any = {
  117. meta: {
  118. res: v,
  119. title: v.meta.title,
  120. icon: v.meta.icon,
  121. }
  122. }
  123. obj.name = v.menuCode
  124. obj.path = '/' + _pA.map(v => v.path.replaceAll('/', '')).join('/')
  125. if (v.children?.length > 0) {
  126. obj.children = deep(v.children, _pA)
  127. obj.component = RouterViewCom
  128. // obj.redirect = v.redirect
  129. } else {
  130. obj.component = RoutersMap.get(obj.name)?.component || TempCom
  131. }
  132. return obj
  133. })
  134. }
  135. // @ts-ignore
  136. const deepRedirect = (item: any) => {
  137. if (item.children?.length > 0) {
  138. return deepRedirect(item.children[0])
  139. } else {
  140. return item.path
  141. }
  142. }
  143. const arr = []
  144. // const arr = [stagingRouter, systemRouter]
  145. const menuRouters = deep(arr, [])
  146. // 初始化一级菜单默认重定向地址
  147. menuRouters.forEach(v => {
  148. if (v.children?.length > 0 && !v.redirect) {
  149. v.redirect = deepRedirect(v.children[0])
  150. }
  151. })
  152. // 动态添加路由
  153. menuRouters.forEach(v => {
  154. router.addRoute(store.state.menu.menuRootName, v)
  155. })
  156. store.dispatch('menu/LOAD_ROUTERS', menuRouters)
  157. }
  158. export default router;