/**头部路由一级菜单点击逻辑 */ function headrRouter(initialState: any, history: any) { let ok = true let u = navigator?.userAgent let isPhone = !!u.match(/AppleWebKit.*Mobile.*/) || u?.indexOf('iPad') > -1 //登录时指向/辅助处理看当前用户的一级菜单有哪些,找到第一个符合的跳转 initialState?.menu?.data.forEach((item: { roles: string[], path: string, routes: { path: string, routes: any[] }[] }) => { if (ok) { ok = false 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 if (history?.location?.pathname === '/') { history.push(path) } } }) if (history?.location?.pathname === '/iaaData') { //当切换一级菜单的辅助跳转 history?.goBack() initialState?.menu?.data.forEach((item: { roles: string[], path: string, routes: { path: string, routes: any[] }[] }) => { if (item?.routes?.some((i: { path: string }) => i.path?.includes('/iaaData'))) { 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 isPhone ? history.push(path) : window.open(location.origin + '/#' + path) } }) } if (history?.location?.pathname === '/iaaSystem') { //当切换一级菜单的辅助跳转 history?.goBack() initialState?.menu?.data.forEach((item: { roles: string[], path: string, routes: { path: string, routes: any[] }[] }) => { if (item?.routes?.some((i: { path: string }) => i.path?.includes('/iaaSystem'))) { 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 isPhone ? history.push(path) : window.open(location.origin + '/#' + path) } }) } } //返回按钮路由 function btnFun(items: any) { return items?.childrenBtn?.map((btn: any,) => { return { name: btn.meta.title, key: btn?.menuId } }) } //返回子路由 function routeFun(items: any) { return items.children.map((route: any) => {//循环重组 if (route?.children?.length > 0) { let arr = routeFun(route) return { path: route.path, name: route.meta.title, component: route.component, routes: arr, icon: route.meta.icon, key: route?.menuId } } if (route?.childrenBtn?.length > 0) { let arr = btnFun(route) return { path: route.path, name: route.meta.title, component: route.component, routes: arr, icon: route.meta.icon, key: route?.menuId } } return { path: route.path, name: route.meta.title, component: route.component, icon: route.meta.icon, key: route?.menuId } }) } /**获取线上菜单并重组路由格式和权限*/ function getMyMenu(code: any, data: any,) { let newData: any = [] // let roles = userInfo?.roles.map((role: any) => role.roleKey)//权限从个人信息中取,以防路由列表中有错误 if (code === 200) { let new_data: any[] = [] // 循环判断不为空的主菜单加入 Object.keys(data).reverse().map((key) => { if (Array.isArray(data[key]) && data[key]?.length > 0) { new_data.push(data[key][0]) } }) // 处理顺序 new_data = new_data.sort((a, b) => { let v1 = a.orderNum let v2 = b.orderNum return v1 - v2 }) new_data.forEach((items: any) => { if (items?.children?.length === 0) {//假如路由的子路由只有一个代表当前组件没有子路由,将children中的component组件路径给外层component 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 } if (items?.children?.length >= 0) {//假如路由的子路由个数大于1代表有子路由 let arr: any[] = routeFun(items) newData.push({ path: items.path, name: items.meta.title, icon: items.meta.icon, routes: arr, key: items.menuId })//最后添加进routes , roles: roles } }) } return newData } const iaaSystem = { path: '/iaaSystem', routes: [ { path: '/iaaSystem/manage', name: 'IAA后台管理', access: 'manage', routes: [ { path: '/iaaSystem/manage/useApp', name: '应用管理', access: 'useApp', component: './iaaSystem/manage/useApp', }, { path: '/iaaSystem/manage/power', name: '角色分配', access: 'power', component: './iaaSystem/manage/power', }, { path: '/iaaSystem/manage/application', name: '应用管理', access: 'application', component: './iaaSystem/manage/application', }, { path: '/iaaSystem/manage/channel', name: '渠道管理', access: 'channel', component: './iaaSystem/manage/channel', }, { path: '/iaaSystem/manage/backPolicy', name: '回传策略管理', access: 'backPolicy', component: './iaaSystem/manage/backPolicy', } ] } ] } const iaaData = { path: '/iaaData', routes: [ { path: '/iaaData/tencentIaa', name: '腾讯', access: 'tencentIaa', routes: [ { path: '/iaaData/tencentIaa/adList', name: '广告列表', access: 'adList', component: './iaaData/tencentIaa/adList', }, { path: '/iaaData/tencentIaa/appEveryDayData', name: '应用每日数据', access: 'appEveryDayData', component: './iaaData/tencentIaa/appEveryDayData', }, { path: '/iaaData/tencentIaa/pitcherEveryDay', name: '投手每日数据', access: 'pitcherEveryDay', component: './iaaData/tencentIaa/pitcherEveryDay', }, ] } ] } export { headrRouter, getMyMenu, iaaSystem, iaaData }