@@ -5,20 +5,16 @@ import { StatusEnum } from '@src/enums';
5
5
import { AccountTypeEnum } from '@src/enums/account.type.enum' ;
6
6
import { mapToObj } from '@src/utils' ;
7
7
import { FindOperator , In , Repository } from 'typeorm' ;
8
- import { AccountRoleEntity } from '../accountRole/entities/account.role.entity' ;
9
8
import { ResourcesEntity } from '../resources/entities/resources.entity' ;
10
- import { RoleResourcesEntity } from '../roleResources/entities/role.resources.entity ' ;
9
+ import { MenusRepository } from './menus.repository ' ;
11
10
import { ApiVo , MenusVo } from './vo/menus.vo' ;
12
11
13
12
@Injectable ( )
14
13
export class MenusService {
15
14
constructor (
16
- @InjectRepository ( AccountRoleEntity )
17
- private readonly accountRoleRepository : Repository < AccountRoleEntity > ,
18
- @InjectRepository ( RoleResourcesEntity )
19
- private readonly roleResourcesRepository : Repository < RoleResourcesEntity > ,
20
15
@InjectRepository ( ResourcesEntity )
21
- private readonly resourcesRepository : Repository < ResourcesEntity >
16
+ private readonly resourcesRepository : Repository < ResourcesEntity > ,
17
+ private readonly menusRepository : MenusRepository
22
18
) { }
23
19
24
20
/**
@@ -36,7 +32,7 @@ export class MenusService {
36
32
query . set ( 'resourcesType' , In ( [ 0 , 1 ] ) ) ;
37
33
// 如果是不是超级管理员就返回角色下的资源
38
34
if ( accountType !== AccountTypeEnum . SUPER_ACCOUNT ) {
39
- const resourcesIdList = await this . getResourcesIdList ( userInfo ) ;
35
+ const resourcesIdList = await this . menusRepository . getResourcesIdList ( userInfo ) ;
40
36
console . log ( resourcesIdList , '资源' ) ;
41
37
query . set ( 'id' , In ( resourcesIdList ) ) ;
42
38
}
@@ -68,7 +64,7 @@ export class MenusService {
68
64
return [ ] ;
69
65
}
70
66
// 获取授权的资源id
71
- const resourcesId = await this . getResourcesIdList ( userInfo ) ;
67
+ const resourcesId = await this . menusRepository . getResourcesIdList ( userInfo ) ;
72
68
console . log ( resourcesId , '全部资源' ) ;
73
69
return await this . resourcesRepository . find ( {
74
70
where : {
@@ -91,48 +87,9 @@ export class MenusService {
91
87
* @return {* }
92
88
*/
93
89
async getResourcesList ( userInfo : ICurrentUserType ) : Promise < ResourcesEntity [ ] > {
94
- const resourcesId = await this . getResourcesIdList ( userInfo ) ;
90
+ const resourcesId = await this . menusRepository . getResourcesIdList ( userInfo ) ;
95
91
return await this . resourcesRepository . find ( {
96
92
where : { id : In ( resourcesId ) } ,
97
93
} ) ;
98
94
}
99
- /**
100
- * @Author :
101
- * @Date : 2023-05-20 17:08:22
102
- * @LastEditors :
103
- * @Description : 内部使用,根据当前用户获取授权的资源id
104
- * @param {ICurrentUserType } userInfo
105
- * @return {* }
106
- */
107
- private async getResourcesIdList ( userInfo : ICurrentUserType ) : Promise < number [ ] > {
108
- const { accountType } = userInfo ;
109
- if ( accountType == AccountTypeEnum . SUPER_ACCOUNT ) {
110
- const resourcesEntity : Pick < ResourcesEntity , 'id' > [ ] = await this . resourcesRepository . find ( {
111
- select : [ 'id' ] ,
112
- } ) ;
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 ) ;
136
- }
137
- }
138
95
}
0 commit comments