fix: 菜单tree修复

This commit is contained in:
2024-11-27 15:49:02 +08:00
parent 5b382a124a
commit a607cfed56
4 changed files with 18 additions and 27 deletions

View File

@@ -38,7 +38,7 @@ export default function FormComponent(props: Props) {
const {data} = await getMenu(); const {data} = await getMenu();
const mergeMenu = [ const mergeMenu = [
{ {
id: 0, id: null,
name: '无父级' name: '无父级'
}, },
...data ...data
@@ -73,7 +73,7 @@ export default function FormComponent(props: Props) {
<Form.Item hidden field="id"> <Form.Item hidden field="id">
<Input/> <Input/>
</Form.Item> </Form.Item>
<Form.Item label="父级菜单" field="parentId" rules={[{required: true}]}> <Form.Item label="父级菜单" field="parentId">
<Select <Select
placeholder="请选择父级菜单" placeholder="请选择父级菜单"
allowClear allowClear

View File

@@ -12,14 +12,11 @@ export type IRoute = AuthParams & {
icon?: string icon?: string
}; };
const generatorMenu = (data, parentId?:string) => { const generatorMenu = (data, parentId = '-') => {
const menu = []; const menu = [];
for (const item of data) { for (const item of data) {
item.key = item.url; item.key = item.url;
if(!parentId){ if ((item.parentId || '-') === parentId) {
menu.push(item);
}else{
if (item.parentId === parentId) {
const children = generatorMenu(data, item.id); const children = generatorMenu(data, item.id);
if (children.length) { if (children.length) {
item.children = children; item.children = children;
@@ -27,7 +24,6 @@ const generatorMenu = (data, parentId?:string) => {
menu.push(item); menu.push(item);
} }
} }
}
return menu; return menu;
}; };

View File

@@ -1 +1 @@
DATABASE_URL="postgresql://postgres:ultimate@81.70.149.52:8000/promise?schema=public" DATABASE_URL="postgresql://postgres:ultimate@81.70.149.52:8000/nestjs-admin?schema=public"

View File

@@ -35,14 +35,10 @@ export class MenuService {
}); });
} }
generatorMenu(data, parentId?: string) { generatorMenu(data, parentId = '-') {
const menu = []; const menu = [];
for (const item of data) { for (const item of data) {
// 不存在父id 代表是顶级菜单 if ((item.parentId || '-') === parentId) {
if (!parentId) {
menu.push(item);
} else {
if (item.parentId === parentId) {
const children = this.generatorMenu(data, item.id); const children = this.generatorMenu(data, item.id);
if (children.length) { if (children.length) {
item.children = children; item.children = children;
@@ -50,13 +46,12 @@ export class MenuService {
menu.push(item); menu.push(item);
} }
} }
}
return menu; return menu;
} }
async getTreeMenu() { async getTreeMenu() {
const query: Prisma.SysMenuFindManyArgs = { const query: Prisma.SysMenuFindManyArgs = {
orderBy: [{ sort: 'desc' }, { id: 'asc' }], orderBy: [{ sort: 'desc' }, { createdAt: 'desc' }],
}; };
const list = await this.prisma.sysMenu.findMany(query); const list = await this.prisma.sysMenu.findMany(query);
return this.generatorMenu(list); return this.generatorMenu(list);