You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.4 KiB

6 months ago
import { defineStore } from 'pinia';
import { RouteMenu } from '/nerv-base/router/helper/menu-helper';
import { ModuleMenu } from '/nerv-base/router/types';
import { appConfigStore } from '/nerv-base/store/modules/app-config';
import { transformRouteToMenu } from '/nerv-base/router/helper/route-helper';
import { NSAxios } from '/nerv-lib/util/http/axios';
interface RouteState {
route: Recordable[];
routeModule: RouteMenu[];
routeModuleObject: Recordable;
}
export const useRouteStore = defineStore({
id: 'route',
state: (): RouteState => {
return { route: [], routeModule: [], routeModuleObject: {} };
},
getters: {},
actions: {
async getRouteModule() {
const initList = [];
this.route.forEach((item) => {
if (item.name !== 'root') {
initList.push(transformRouteToMenu(item));
}
});
this.dealNoChildrenMenu(initList);
this.routeModule = initList;
return this.routeModule;
},
async setRoute(route: Recordable[]) {
this.route = route;
await this.getRouteModule();
},
/**
* noChildrenMenu
*
*/
dealNoChildrenMenu(item: any) {
item.forEach((sitem: any) => {
if (
Object.prototype.toString.call(sitem.menus) === '[object Array]' &&
sitem.menus.length !== 0
) {
if (sitem.menus.findIndex((x) => x.type === 'menus') === -1) {
sitem.type = 'noChildrenMenu';
} else {
this.dealNoChildrenMenu(sitem.menus);
}
} else {
if (sitem.route?.meta?.hideChildren) {
sitem.type = 'noChildrenMenu';
}
}
});
},
/**
*
*/
syncRoute() {
const appConfig = appConfigStore();
const routeModuleObject: Recordable = {};
function loop(routeModule: ModuleMenu[]) {
for (let i = 0, j = routeModule.length; i < j; i++) {
routeModuleObject[routeModule[i].code] = routeModule[i];
routeModule[i].menus && loop(routeModule[i].menus);
}
}
loop(this.routeModule);
this.routeModuleObject = routeModuleObject;
function loopRoute(routeList) {
for (let i = 0, j = routeList.length; i < j; i++) {
const route = routeList[i];
!route.meta && (route.meta = {});
if (routeModuleObject[route.name] === undefined) {
console.error(`route ${route.name} is not in menu`);
return;
}
if (!route.meta.index || route.meta.index !== routeModuleObject[route.name].sort) {
route.meta.index = routeModuleObject[route.name].sort;
}
if (!route.meta.title || route.meta.title !== routeModuleObject[route.name].label) {
route.meta.title = routeModuleObject[route.name].label;
}
route.children && loopRoute(route.children);
}
}
loopRoute(this.route);
this.route.sort((a, b) => {
return a.meta?.index - b.meta?.index;
});
// const initPcResource = { application: {}, menus: [] };
let initPcResource = [];
6 months ago
this.routeModule.sort((a, b) => {
return a.route?.meta?.index - b.route?.meta?.index;
});
const info = JSON.parse(JSON.stringify(this.routeModule));
initRouteMouleList(info);
function initRouteMouleList(info) {
info.forEach((item, index) => {
item.sort = index;
if (item.menus) {
initRouteMouleList(item.menus);
}
if (item.extend) {
let isNull = true;
Object.keys(item.extend).forEach((key) => {
if (item.extend[key]) {
isNull = false;
}
});
if (isNull) {
delete item.extend;
}
}
delete item.route;
});
}
// initPcResource.application = appConfig.resourceInfo?.application as object;
initPcResource = appConfig.resourceInfo?.dealReosurceList
6 months ago
? appConfig.resourceInfo?.dealReosurceList(info)
: info;
new NSAxios({
headers: {
'Content-Type': 'application/json;charset=UTF-8',
'QA-token': appConfig.resourceInfo?.token as string,
},
withCredentials: true,
timeout: 3 * 1000,
})
.post(appConfig.resourceInfo?.api as string, initPcResource)
.then((res) => {
console.log(res);
});
},
},
});