Skip to content

Commit 36ec4d6

Browse files
committed
feat: 修改角色和资源
1 parent d4437ae commit 36ec4d6

7 files changed

+47
-16
lines changed

nestjs-mysql-api.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,11 @@ CREATE TABLE `role_resources` (
135135
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY key COMMENT '主键id',
136136
`resources_id` int(11) NOT NULL COMMENT '关联到resources表主键id',
137137
`role_id` int(11) NOT NULL COMMENT '关联到role表主键id',
138+
`type` tinyint(4) not null DEFAULT 0 comment '类型,0表示菜单1表示按钮',
138139
`created_at` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) COMMENT '创建时间',
139140
`updated_at` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) COMMENT '更新时间',
140141
`deleted_at` timestamp(6) NULL DEFAULT NULL COMMENT '软删除时间',
141-
UNIQUE INDEX `UK_resources_role`(`resources_id`, `role_id`)
142+
UNIQUE INDEX `UK_resources_role_type`(`resources_id`, `role_id`,`type`)
142143
) ENGINE = InnoDB DEFAULT CHARSET=utf8mb4 COMMENT = '角色和资源中间表';
143144

144145

src/api/resources/resources.controller.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { AuthGuard } from '@src/guard/auth.guard';
1414
import { ResourcesDto } from './dto/resources.dto';
1515
import { QueryResourcesDto } from './dto/resources.query.dto';
1616
import { ResourcesService } from './resources.service';
17-
import { ResourcesListVo, ResourcesVo, SimplenessResourceVo } from './vo/resources.vo';
17+
import { ResourcesListVo, SimplenessResourceVo } from './vo/resources.vo';
1818

1919
@UseGuards(AuthGuard)
2020
@Controller('resources')
@@ -53,9 +53,9 @@ export class ResourcesController {
5353
return await this.resourcesService.getResourceCatalogApi(catalogType);
5454
}
5555

56-
@Get('menusList')
57-
async getMenusListApi(): Promise<ResourcesVo[]> {
58-
return await this.resourcesService.getMenusListApi();
56+
@Get('list')
57+
async getResourcesListApi(@Query('type') type: number): Promise<SimplenessResourceVo[]> {
58+
return await this.resourcesService.getResourcesListApi(type);
5959
}
6060

6161
@Get('menus/:id')

src/api/resources/resources.service.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,20 @@ export class ResourcesService {
158158
* @Author: 水痕
159159
* @Date: 2023-10-08 08:07:50
160160
* @LastEditors: 水痕
161-
* @Description: 获取一级和二级下的菜单
161+
* @Description: type=0表示菜单,1表示按钮
162162
* @return {*}
163163
*/
164-
async getMenusListApi(): Promise<ResourcesVo[]> {
165-
return await this.resourcesRepository.find({ where: { resourcesType: In([0, 1]) } });
164+
async getResourcesListApi(type: number): Promise<SimplenessResourceVo[]> {
165+
let resourcesType: any = [];
166+
if (type == 0) {
167+
resourcesType = [0, 1];
168+
} else if (type == 1) {
169+
resourcesType = [0, 1, 2];
170+
}
171+
return await this.resourcesRepository.find({
172+
where: { resourcesType: In(resourcesType), status: StatusEnum.NORMAL },
173+
select: ['id', 'title', 'parentId'],
174+
});
166175
}
167176
/**
168177
* @Author: 水痕

src/api/roleResources/dto/role.resources.dto.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IsInt, IsNotEmpty, IsArray } from 'class-validator';
1+
import { IsInt, IsNotEmpty, IsArray, IsOptional, Min, Max } from 'class-validator';
22
import { Type } from 'class-transformer';
33

44
export class RoleResourcesDto {
@@ -10,4 +10,11 @@ export class RoleResourcesDto {
1010
// @ArrayMinSize(1, { message: '资源至少一个' })
1111
@IsArray({ message: '资源ID列表必须是一个数组' })
1212
readonly resourceList!: number[];
13+
14+
@Max(1, { message: '类型id最大值为1' })
15+
@Min(0, { message: '类型id最小值为0' })
16+
@IsInt({ message: '类型id必须是整数' })
17+
@Type(() => Number)
18+
@IsOptional({ message: '类型,0表示菜单1表示按钮' })
19+
type!: number;
1320
}

src/api/roleResources/entities/role.resources.entity.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,12 @@ export class RoleResourcesEntity extends SharedEntity {
1717
comment: '关联到role表主键id',
1818
})
1919
roleId!: number;
20+
21+
@Column({
22+
type: 'tinyint',
23+
name: 'type',
24+
default: '0',
25+
comment: '类型,0表示菜单1表示按钮',
26+
})
27+
type!: number;
2028
}

src/api/roleResources/role.resources.controller.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Controller, Get, Post, Body, Param, ParseIntPipe, UseGuards } from '@nestjs/common';
2+
import { Query } from '@nestjs/common/decorators';
23
import { AuthGuard } from '@src/guard/auth.guard';
34
import { ResourcesEntity } from '../resources/entities/resources.entity';
45
import { RoleResourcesDto } from './dto/role.resources.dto';
@@ -16,8 +17,9 @@ export class RoleResourcesController {
1617

1718
@Get(':roleId')
1819
async getResourceByRoleIdApi(
19-
@Param('roleId', new ParseIntPipe()) roleId: number
20+
@Param('roleId', new ParseIntPipe()) roleId: number,
21+
@Query('type') type: number
2022
): Promise<ResourcesEntity[]> {
21-
return await this.roleResourcesService.getResourceByRoleIdApi(roleId);
23+
return await this.roleResourcesService.getResourceByRoleIdApi(roleId, type);
2224
}
2325
}

src/api/roleResources/role.resources.service.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ export class RoleResourcesService {
2424
* @return {*}
2525
*/
2626
async dispatchResourcesApi(req: RoleResourcesDto): Promise<string> {
27-
const { roleId } = req;
27+
const { roleId, type } = req;
2828
// 处理如果没传递的时候直接清空
2929
if (!req.resourceList.length) {
30-
await this.roleResourcesRepository.delete({ roleId });
30+
await this.roleResourcesRepository.delete({ roleId, type });
3131
return '分配资源成功';
3232
}
3333
// 根据当前角色查询之前的资源
3434
const roleResourcesEntity: Pick<RoleResourcesEntity, 'resourcesId'>[] =
3535
await this.roleResourcesRepository.find({
36-
where: { roleId },
36+
where: { roleId, type },
3737
select: ['resourcesId'],
3838
});
3939
if (roleResourcesEntity.length) {
@@ -53,6 +53,7 @@ export class RoleResourcesService {
5353
where: {
5454
roleId: req.roleId,
5555
resourcesId: In(roleResourceDeleteList),
56+
type,
5657
},
5758
select: ['id'],
5859
});
@@ -68,6 +69,7 @@ export class RoleResourcesService {
6869
return {
6970
resourcesId: item,
7071
roleId: req.roleId,
72+
type,
7173
};
7274
});
7375
// 创建
@@ -92,6 +94,7 @@ export class RoleResourcesService {
9294
return {
9395
roleId: req.roleId,
9496
resourcesId: item,
97+
type,
9598
};
9699
});
97100
const data1 = this.roleResourcesRepository.create(data);
@@ -106,12 +109,13 @@ export class RoleResourcesService {
106109
* @LastEditors: 水痕
107110
* @Description: 根据角色获取授权的资源
108111
* @param {number} roleId
112+
* @param {number} type
109113
* @return {*}
110114
*/
111-
async getResourceByRoleIdApi(roleId: number): Promise<ResourcesEntity[]> {
115+
async getResourceByRoleIdApi(roleId: number, type: number): Promise<ResourcesEntity[]> {
112116
const roleResourcesEntity: Pick<RoleResourcesEntity, 'resourcesId'>[] =
113117
await this.roleResourcesRepository.find({
114-
where: { roleId },
118+
where: { roleId, type },
115119
select: ['resourcesId'],
116120
});
117121
const resourceIdList: number[] = roleResourcesEntity.map((item) => item.resourcesId);

0 commit comments

Comments
 (0)