// @ts-ignore import httpVueLoader from '@/common/tool/httpVueLoader'; import { config } from '@/config'; import axios from 'axios'; /** * 通过菜单生成路由 * @param menu * @return {any} */ function generateRoutesFromMenu(menu: any, params: any, init?: boolean) { // const currentMenu = [{ // "path": params.applicationCode + '/async/custom', // "code": 'custom', // "name": params.applicationCode + '_' + params.siteCode + '_' + 'custom', // "title": '异步自定义组件', // 'isShow': true, // "meta": { // "id": '111', // "title": '异步自定义组件', // "contentUrl": 'http://10.60.136.145:8090/devtest/Custom.vue', // "local": false, // "code": 'custom', // "siteId": params.siteCode, // "applicationId": params.applicationId // }, // "children": [] // }]; menu = (menu || []); if (init) { const routeList: any = []; menu.forEach((menuItem: any) => { routeList.push(formatRouteItem(menuItem, params)); }); menu = (routeList && routeList.length > 0) ? [{ path: '/', component: () => import('@/layout/index.vue'), redirect: '/home', children: routeList }] : []; } return menu.map((item: any) => { if (!init) item.component = { template: '' }; if (item.children) item.children = generateRoutesFromMenu(item.children, params); return item; }); } function getComponentContent(route: any) { return new Promise(function (resolve, reject) { if (route.meta.local) resolve(resolve1 => require([`@/views/${route.meta.contentUrl}.vue`], resolve1)); resolve(httpVueLoader(route.meta.contentUrl)); }); } function getHttpVue(url: string, currentName?: string) { return (url.indexOf('http') > -1) ? httpVueLoader(url, '', currentName)() : new Promise((resolve) => resolve(resolve1 => require([`@/views/${url}`], resolve1))) } /** * 获取菜单 */ function formatRoutes(menu: any) { if (!menu || (menu.length === 0)) return []; return menu.map((element: any) => { const isDefaultMenu = ['visual'].includes(element.serviceCode); return { "path": element.serviceUri, "name": element.serviceName, "title": element.serviceName, "code": element.serviceCode, "component": isDefaultMenu ? "Main" : "Container", "isShow": element.isShow, "meta": { "id": element.id, "code": element.serviceCode }, "redirect": element.serviceRedirectUri }; }); } /** * 处理菜单权限格式 * @param menu 当前菜单对象 * @param comp 上级菜单对象 * @return {any[]} */ function formatAuth(menu: any, comp?: any) { let childNode = [], serviceFunctions = []; if (menu.childNode && menu.childNode.length) { childNode = menu.childNode.map((element: any) => { const component = comp && (element.components.length === 1) ? { "component": element.components[0].cvalue } : { "components": { "default": "Middleware", "aside": "SidebarVisual" } }; return formatRouteItem(element, component, menu); }); } if (menu.serviceFunctions && menu.serviceFunctions.length) { serviceFunctions = menu.serviceFunctions.map((element: any) => { const component = comp && (element.components.length === 1) ? { "component": element.components[0].cvalue } : {}; return formatRouteItem(element, component, menu); }); } return childNode.concat(serviceFunctions); } /** * 处理菜单路由Item * @param element 当前菜单节点 * @param params 组件页面 * @param menu 菜单 * @return {{any}} */ function formatRouteItem(element: any, params: any, menu?: any) { const pagePath: any = {}; const { applicationCode, siteCode } = params; try { const pagePath1 = JSON.parse(element.pagePath); pagePath.path = '/' + applicationCode + '/' + siteCode + pagePath1.path; pagePath.isLocal = !!pagePath1.local; pagePath.contentUrl = pagePath1.page; } catch { pagePath.path = '/404'; pagePath.isLocal = false; pagePath.contentUrl = ''; } return { "path": pagePath.path, "code": element.code, "name": applicationCode + '_' + siteCode + '_' + element.name, "title": element.name, 'isShow': element.active, "meta": { "id": element.id, "title": element.name, "contentUrl": pagePath.contentUrl, "local": pagePath.isLocal, "code": element.code, "siteId": element.siteId, "applicationId": element.applicationId }, "icon": element.icon, "children": element.children ? element.children.map((item: any) => formatRouteItem(item, params)) : [] }; } function clearSession() { const setting: any = sessionStorage.getItem('setting'); sessionStorage.clear(); sessionStorage.setItem('setting', setting); } /** * 获取系统信息 * @param to * @param next * @param callback */ function getSystemData(to: any, next: any, callback?: any) { const setting: any = JSON.parse(sessionStorage.getItem('setting')); let pathname = window.document.location.pathname || '/dt/'; if (pathname === '/') { if (setting) { sessionStorage.clear(); return next({ path: '/login' }); } return callback(); } pathname = pathname.split('/')[1]; if (!setting || (setting.baseUrl !== pathname)) { axios.get(config.staticApi + '/' + pathname + '/config.json?time=' + new Date().getTime()) .then(function (res) { sessionStorage.clear(); sessionStorage.setItem('setting', JSON.stringify({ baseUrl: pathname, ...res.data })); next({ path: '/login' }); }).catch((error: any) => { if (pathname !== 'dt') window.location.href = window.document.location.origin; else { sessionStorage.clear(); sessionStorage.setItem('setting', JSON.stringify({ baseUrl: pathname, icon_login: require('../../../public/images/listening.svg'), icon_page: require('../../../public/images/listening.svg'), title_login: '谛听工业互联网平台', title_page: '谛听工业互联网平台', home_page: '' })); next({ path: '/login' }); } }) } else { callback(); } } export { generateRoutesFromMenu, formatRoutes, getComponentContent, getSystemData, getHttpVue, clearSession };