oa_based/src/modules/system/menu/menu.controller.ts

110 lines
3.2 KiB
TypeScript
Raw Normal View History

2024-02-28 08:32:35 +08:00
import {
BadRequestException,
Body,
Controller,
Delete,
Get,
Post,
Put,
2024-02-29 09:29:03 +08:00
Query
2024-02-28 17:02:46 +08:00
} from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { flattenDeep } from 'lodash';
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
import { ApiResult } from '~/common/decorators/api-result.decorator';
import { IdParam } from '~/common/decorators/id-param.decorator';
import { ApiSecurityAuth } from '~/common/decorators/swagger.decorator';
import {
Perm,
definePermission,
2024-02-29 09:29:03 +08:00
getDefinePermissions
2024-02-28 17:02:46 +08:00
} from '~/modules/auth/decorators/permission.decorator';
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
import { MenuDto, MenuQueryDto, MenuUpdateDto } from './menu.dto';
import { MenuItemInfo } from './menu.model';
import { MenuService } from './menu.service';
2024-04-10 17:39:17 +08:00
import { IsMobile } from '~/common/decorators/http.decorator';
import { ResourceDeviceEnum } from '~/constants/enum';
2024-02-28 08:32:35 +08:00
export const permissions = definePermission('system:menu', {
LIST: 'list',
CREATE: 'create',
READ: 'read',
UPDATE: 'update',
2024-02-29 09:29:03 +08:00
DELETE: 'delete'
2024-02-28 17:02:46 +08:00
} as const);
2024-02-28 08:32:35 +08:00
@ApiTags('System - 菜单权限模块')
@ApiSecurityAuth()
@Controller('menus')
export class MenuController {
constructor(private menuService: MenuService) {}
@Get()
@ApiOperation({ summary: '获取所有菜单列表' })
@ApiResult({ type: [MenuItemInfo] })
@Perm(permissions.LIST)
async list(@Query() dto: MenuQueryDto) {
2024-04-10 17:39:17 +08:00
return this.menuService.list({
...dto
});
2024-02-28 08:32:35 +08:00
}
@Get(':id')
@ApiOperation({ summary: '获取菜单或权限信息' })
@Perm(permissions.READ)
async info(@IdParam() id: number) {
2024-02-28 17:02:46 +08:00
return this.menuService.getMenuItemAndParentInfo(id);
2024-02-28 08:32:35 +08:00
}
@Post()
@ApiOperation({ summary: '新增菜单或权限' })
@Perm(permissions.CREATE)
async create(@Body() dto: MenuDto): Promise<void> {
// check
2024-02-28 17:02:46 +08:00
await this.menuService.check(dto);
if (!dto.parentId) dto.parentId = null;
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
await this.menuService.create(dto);
2024-02-28 08:32:35 +08:00
if (dto.type === 2) {
// 如果是权限发生更改,则刷新所有在线用户的权限
2024-02-28 17:02:46 +08:00
await this.menuService.refreshOnlineUserPerms();
2024-02-28 08:32:35 +08:00
}
}
@Put(':id')
@ApiOperation({ summary: '更新菜单或权限' })
@Perm(permissions.UPDATE)
2024-02-28 17:02:46 +08:00
async update(@IdParam() id: number, @Body() dto: MenuUpdateDto): Promise<void> {
2024-02-28 08:32:35 +08:00
// check
2024-02-28 17:02:46 +08:00
await this.menuService.check(dto);
if (dto.parentId === -1 || !dto.parentId) dto.parentId = null;
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
await this.menuService.update(id, dto);
2024-02-28 08:32:35 +08:00
if (dto.type === 2) {
// 如果是权限发生更改,则刷新所有在线用户的权限
2024-02-28 17:02:46 +08:00
await this.menuService.refreshOnlineUserPerms();
2024-02-28 08:32:35 +08:00
}
}
@Delete(':id')
@ApiOperation({ summary: '删除菜单或权限' })
@Perm(permissions.DELETE)
async delete(@IdParam() id: number): Promise<void> {
if (await this.menuService.checkRoleByMenuId(id))
2024-02-28 17:02:46 +08:00
throw new BadRequestException('该菜单存在关联角色,无法删除');
2024-02-28 08:32:35 +08:00
// 如果有子目录,一并删除
2024-02-28 17:02:46 +08:00
const childMenus = await this.menuService.findChildMenus(id);
await this.menuService.deleteMenuItem(flattenDeep([id, childMenus]));
2024-02-28 08:32:35 +08:00
// 刷新在线用户权限
2024-02-28 17:02:46 +08:00
await this.menuService.refreshOnlineUserPerms();
2024-02-28 08:32:35 +08:00
}
@Get('permissions')
@ApiOperation({ summary: '获取后端定义的所有权限集' })
async getPermissions(): Promise<string[]> {
2024-02-28 17:02:46 +08:00
return getDefinePermissions();
2024-02-28 08:32:35 +08:00
}
}