Skip to content

Commit 6215214

Browse files
committed
feat: 按钮权限
1 parent 2922d4a commit 6215214

File tree

3 files changed

+61
-59
lines changed

3 files changed

+61
-59
lines changed

src/api/menus/menus.controller.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { Controller, Get, Param, ParseIntPipe, Query, UseGuards } from '@nestjs/common';
1+
import { Controller, Get, Query, UseGuards } from '@nestjs/common';
22
import { CurrentUser, ICurrentUserType } from '@src/decorators';
33
import { AuthGuard } from '@src/guard/auth.guard';
4-
import { ResourcesEntity } from '../resources/entities/resources.entity';
54
import { MenusService } from './menus.service';
6-
import { MenusVo } from './vo/menus.vo';
5+
import { ApiVo, MenusVo } from './vo/menus.vo';
76

87
@UseGuards(AuthGuard)
98
@Controller('menus')
@@ -15,16 +14,11 @@ export class MenusController {
1514
return await this.menusService.getAllMenusApi(userInfo);
1615
}
1716

18-
@Get('menusId')
19-
async getMenusIdByNameApi(@Query('urlName') urlName: string): Promise<number | undefined> {
20-
return await this.menusService.getMenusIdByNameApi(urlName);
21-
}
22-
23-
@Get('/btnList/:id')
24-
async getBtnByMenusIdApi(
17+
@Get('btnList')
18+
async getBtnByMenusUrlApi(
2519
@CurrentUser('userInfo') userInfo: ICurrentUserType,
26-
@Param('id', new ParseIntPipe()) id: number
27-
): Promise<ResourcesEntity[]> {
28-
return await this.menusService.getBtnByMenusIdApi(id, userInfo);
20+
@Query('urlName') urlName: string
21+
): Promise<ApiVo[]> {
22+
return await this.menusService.getBtnByMenusUrlApi(urlName, userInfo);
2923
}
3024
}

src/api/menus/menus.service.ts

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { FindOperator, In, Repository } from 'typeorm';
88
import { AccountRoleEntity } from '../accountRole/entities/account.role.entity';
99
import { ResourcesEntity } from '../resources/entities/resources.entity';
1010
import { RoleResourcesEntity } from '../roleResources/entities/role.resources.entity';
11-
import { MenusVo } from './vo/menus.vo';
11+
import { ApiVo, MenusVo } from './vo/menus.vo';
1212

1313
@Injectable()
1414
export class MenusService {
@@ -48,44 +48,37 @@ export class MenusService {
4848
.getMany();
4949
}
5050

51-
/**
52-
* @Author:
53-
* @Date: 2023-05-20 17:19:02
54-
* @LastEditors:
55-
* @Description: 根据菜单url获取菜单id
56-
* @param {string} urlName
57-
* @return {*}
58-
*/
59-
async getMenusIdByNameApi(urlName: string): Promise<number | undefined> {
60-
const resourcesEntity = await this.resourcesRepository.findOne({
61-
where: { url: urlName },
62-
select: ['id'],
63-
});
64-
return resourcesEntity?.id ?? 0;
65-
}
66-
6751
/**
6852
* @Author:
6953
* @Date: 2023-05-20 17:02:48
7054
* @LastEditors:
7155
* @Description: 根据菜单id获取授权的按钮
72-
* @param {number} id
56+
* @param {string} urlName
57+
* @param {ICurrentUserType} userInfo
7358
* @return {*}
7459
*/
75-
async getBtnByMenusIdApi(id: number, userInfo: ICurrentUserType): Promise<ResourcesEntity[]> {
76-
// if (Object.is(userInfo.isSuper, SuperAdminEnum.IS_SUPER)) {
77-
// return await this.resourcesRepository.find({
78-
// where: { parentId: id, resourcesType: 1 },
79-
// order: { sort: 'DESC', id: 'DESC' },
80-
// });
81-
// } else {
82-
83-
// }
60+
async getBtnByMenusUrlApi(urlName: string, userInfo: ICurrentUserType): Promise<ApiVo[]> {
61+
const resourcesEntity: Pick<ResourcesEntity, 'id'> | null =
62+
await this.resourcesRepository.findOne({
63+
where: { url: urlName },
64+
select: ['id'],
65+
});
66+
console.log(resourcesEntity, '????');
67+
if (!resourcesEntity?.id) {
68+
return [];
69+
}
8470
// 获取授权的资源id
8571
const resourcesId = await this.getResourcesIdList(userInfo);
72+
console.log(resourcesId, '全部资源');
8673
return await this.resourcesRepository.find({
87-
where: { id: In(resourcesId), resourcesType: 1, parentId: id },
88-
order: { sort: 'DESC', id: 'DESC' },
74+
where: {
75+
id: In(resourcesId),
76+
resourcesType: 2,
77+
parentId: resourcesEntity?.id,
78+
status: StatusEnum.NORMAL,
79+
},
80+
order: { sort: 'ASC', id: 'DESC' },
81+
select: ['id', 'title'],
8982
});
9083
}
9184

@@ -112,24 +105,34 @@ export class MenusService {
112105
* @return {*}
113106
*/
114107
private async getResourcesIdList(userInfo: ICurrentUserType): Promise<number[]> {
115-
// 1.查询当前用户授权的角色
116-
const accountRoleEntity: Pick<AccountRoleEntity, 'roleId'>[] =
117-
await this.accountRoleRepository.find({
118-
where: { accountId: userInfo.id },
119-
select: ['roleId'],
120-
});
121-
if (!accountRoleEntity.length) {
122-
return [];
123-
}
124-
// 2.根据角色查询授权的资源
125-
const roleResourcesEntity: Pick<RoleResourcesEntity, 'resourcesId'>[] =
126-
await this.roleResourcesRepository.find({
127-
where: { roleId: In(accountRoleEntity.map((item) => item.roleId)) },
128-
select: ['resourcesId'],
108+
const { accountType } = userInfo;
109+
if (accountType == AccountTypeEnum.SUPER_ACCOUNT) {
110+
const resourcesEntity: Pick<ResourcesEntity, 'id'>[] = await this.resourcesRepository.find({
111+
select: ['id'],
129112
});
130-
if (!roleResourcesEntity.length) {
131-
return [];
113+
return resourcesEntity.map((item: Pick<ResourcesEntity, 'id'>) => item.id);
114+
} else {
115+
const query = new Map<string, FindOperator<string>>();
116+
// 1.查询当前用户授权的角色
117+
const accountRoleEntity: Pick<AccountRoleEntity, 'roleId'>[] =
118+
await this.accountRoleRepository.find({
119+
where: { accountId: userInfo.id },
120+
select: ['roleId'],
121+
});
122+
if (!accountRoleEntity.length) {
123+
return [];
124+
}
125+
query.set('roleId', In(accountRoleEntity.map((item) => item.roleId)));
126+
// 2.根据角色查询授权的资源
127+
const roleResourcesEntity: Pick<RoleResourcesEntity, 'resourcesId'>[] =
128+
await this.roleResourcesRepository.find({
129+
where: mapToObj(query),
130+
select: ['resourcesId'],
131+
});
132+
if (!roleResourcesEntity.length) {
133+
return [];
134+
}
135+
return roleResourcesEntity.map((item) => item.resourcesId);
132136
}
133-
return roleResourcesEntity.map((item) => item.resourcesId);
134137
}
135138
}

src/api/menus/vo/menus.vo.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ export class MenusVo {
1111
parentId!: number; // 上一级id
1212
status!: number; // 状态:0是正常,1是禁止
1313
}
14+
15+
export class ApiVo {
16+
id!: number;
17+
title!: string; // 接口标题,或菜单标题
18+
}

0 commit comments

Comments
 (0)