routerConfig.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**头部路由一级菜单点击逻辑 */
  2. function headrRouter(initialState: any, history: any) {
  3. let ok = true
  4. let u = navigator?.userAgent
  5. let isPhone = !!u.match(/AppleWebKit.*Mobile.*/) || u?.indexOf('iPad') > -1
  6. //登录时指向/辅助处理看当前用户的一级菜单有哪些,找到第一个符合的跳转
  7. initialState?.menu?.data.forEach((item: { roles: string[], path: string, routes: { path: string, routes: any[] }[] }) => {
  8. if (ok) {
  9. ok = false
  10. let path = item?.routes?.length > 0 ? item?.routes[0]?.routes?.length > 0 ? item?.routes[0]?.routes[0]?.path ? item?.routes[0]?.routes[0]?.path : item?.routes[0].path : item?.routes[0].path : item?.routes[0].path
  11. if (history?.location?.pathname === '/') {
  12. history.push(path)
  13. }
  14. }
  15. })
  16. if (history?.location?.pathname === '/launchSystemNew') { //当切换一级菜单的辅助跳转
  17. history?.goBack()
  18. initialState?.menu?.data.forEach((item: { roles: string[], path: string, routes: { path: string, routes: any[] }[] }) => {
  19. if (item?.routes?.some((i: { path: string }) => i.path?.includes('/launchSystemNew'))) {
  20. let path = item?.routes?.length > 0 ? (item?.routes[0]?.routes?.length > 0 && item?.routes[0]?.routes[0]?.path) ? item?.routes[0]?.routes[0]?.path : item?.routes[0].path : item?.routes[0].path
  21. isPhone ? history.push(path) : window.open(location.origin + '/#' + path)
  22. }
  23. })
  24. }
  25. if (history?.location?.pathname === '/adMonitor') { //当切换一级菜单的辅助跳转
  26. history?.goBack()
  27. initialState?.menu?.data.forEach((item: { roles: string[], path: string, routes: { path: string, routes: any[] }[] }) => {
  28. if (item?.routes?.some((i: { path: string }) => i.path?.includes('/adMonitor'))) {
  29. let path = item?.routes?.length > 0 ? (item?.routes[0]?.routes?.length > 0 && item?.routes[0]?.routes[0]?.path) ? item?.routes[0]?.routes[0]?.path : item?.routes[0].path : item?.routes[0].path
  30. isPhone ? history.push(path) : window.open(location.origin + '/#' + path)
  31. }
  32. })
  33. }
  34. }
  35. //返回按钮路由
  36. function btnFun(items: any) {
  37. return items?.childrenBtn?.map((btn: any,) => {
  38. return { name: btn.meta.title, key: btn?.menuId }
  39. })
  40. }
  41. //返回子路由
  42. function routeFun(items: any) {
  43. return items.children.map((route: any) => {//循环重组
  44. // console.log(route)
  45. if (route?.children?.length > 0) {
  46. let arr = routeFun(route)
  47. return { path: route.path, name: route.meta.title, component: route.component, routes: arr, icon: route.meta.icon, key: route?.menuId }
  48. }
  49. if (route?.childrenBtn?.length > 0) {
  50. let arr = btnFun(route)
  51. return { path: route.path, name: route.meta.title, component: route.component, routes: arr, icon: route.meta.icon, key: route?.menuId }
  52. }
  53. return { path: route.path, name: route.meta.title, component: route.component, icon: route.icon, key: route?.menuId }
  54. })
  55. }
  56. /**获取线上菜单并重组路由格式和权限*/
  57. function getMyMenu(code: any, data: any,) {
  58. let newData: any = []
  59. // let roles = userInfo?.roles.map((role: any) => role.roleKey)//权限从个人信息中取,以防路由列表中有错误
  60. if (code === 200) {
  61. let new_data: any[] = []
  62. // 循环判断不为空的主菜单加入
  63. Object.keys(data)?.reverse()?.map((key) => {
  64. if (Array.isArray(data[key]) && data[key]?.length > 0) {
  65. new_data.push(data[key][0])
  66. }
  67. })
  68. // 处理顺序
  69. new_data = new_data?.sort((a, b) => {
  70. let v1 = a.orderNum
  71. let v2 = b.orderNum
  72. return v1 - v2
  73. })
  74. new_data?.forEach((items: any) => {
  75. if (items?.children?.length === 0) {//假如路由的子路由只有一个代表当前组件没有子路由,将children中的component组件路径给外层component
  76. newData.push({ path: items.children[0].path, name: items.meta.title, icon: items.meta.icon, component: items.children[0].component, key: items.menuId }) // roles: roles
  77. }
  78. if (items?.children?.length >= 0) {//假如路由的子路由个数大于1代表有子路由
  79. let arr: any[] = routeFun(items)
  80. newData.push({ path: items.path, name: items.meta.title, icon: items.meta.icon, routes: arr, key: items.menuId })//最后添加进routes , roles: roles
  81. }
  82. })
  83. }
  84. return newData
  85. }
  86. /** 投放系统 */
  87. const launchSystem = {
  88. path: '/launchSystemNew',
  89. routes: [
  90. {
  91. path: '/launchSystemNew/adMonitorList',
  92. name: '广告监控',
  93. component: './adMonitor/adMonitorList',
  94. access: 'adMonitorList',
  95. },
  96. {
  97. path: '/launchSystemNew/account',
  98. name: '广告账户管理',
  99. access: 'account',
  100. component: './launchSystemNew/account',
  101. },
  102. {
  103. path: '/launchSystemNew/adq',
  104. name: '腾讯广告',
  105. component: './launchSystemNew/adq',
  106. access: 'adq',
  107. },
  108. {
  109. path: '/launchSystemNew/launchManage',
  110. name: '广告投放',
  111. access: 'launchManage',
  112. routes: [
  113. {
  114. path: '/launchSystemNew/launchManage/localAd',
  115. name: '广告模板',
  116. access: 'localAd',
  117. component: './launchSystemNew/launchManage/localAd',
  118. },
  119. {
  120. path: '/launchSystemNew/launchManage/createAd',
  121. name: '创建广告',
  122. access: 'createAd',
  123. component: './launchSystemNew/launchManage/createAd',
  124. },
  125. {
  126. path: '/launchSystemNew/launchManage/taskList',
  127. name: '任务列表',
  128. access: 'taskList',
  129. component: './launchSystemNew/launchManage/taskList',
  130. }
  131. ]
  132. },
  133. {
  134. path: '/launchSystemNew/material',
  135. name: '素材管理',
  136. access: 'material',
  137. icon: 'DatabaseOutlined',
  138. routes: [
  139. {
  140. name: '本地素材',
  141. path: '/launchSystemNew/material/cloud',
  142. access: 'cloud',
  143. component: './launchSystemNew/material/cloud',
  144. },
  145. ],
  146. },
  147. ]
  148. }
  149. /** 广告监控 */
  150. const adMonitor = {
  151. path: '/adMonitor',
  152. routes: [
  153. {
  154. path: '/adMonitor/adMonitorList',
  155. name: '广告监控',
  156. component: './adMonitor/adMonitorList',
  157. access: 'adMonitorList',
  158. },
  159. ]
  160. }
  161. export {
  162. headrRouter,
  163. getMyMenu,
  164. launchSystem,
  165. adMonitor
  166. }