@@ -5,20 +5,16 @@ import { StatusEnum } from '@src/enums';
55import { AccountTypeEnum } from '@src/enums/account.type.enum' ;
66import { mapToObj } from '@src/utils' ;
77import { FindOperator , In , Repository } from 'typeorm' ;
8- import { AccountRoleEntity } from '../accountRole/entities/account.role.entity' ;
98import { ResourcesEntity } from '../resources/entities/resources.entity' ;
10- import { RoleResourcesEntity } from '../roleResources/entities/role.resources.entity ' ;
9+ import { MenusRepository } from './menus.repository ' ;
1110import { ApiVo , MenusVo } from './vo/menus.vo' ;
1211
1312@Injectable ( )
1413export class MenusService {
1514 constructor (
16- @InjectRepository ( AccountRoleEntity )
17- private readonly accountRoleRepository : Repository < AccountRoleEntity > ,
18- @InjectRepository ( RoleResourcesEntity )
19- private readonly roleResourcesRepository : Repository < RoleResourcesEntity > ,
2015 @InjectRepository ( ResourcesEntity )
21- private readonly resourcesRepository : Repository < ResourcesEntity >
16+ private readonly resourcesRepository : Repository < ResourcesEntity > ,
17+ private readonly menusRepository : MenusRepository
2218 ) { }
2319
2420 /**
@@ -36,7 +32,7 @@ export class MenusService {
3632 query . set ( 'resourcesType' , In ( [ 0 , 1 ] ) ) ;
3733 // 如果是不是超级管理员就返回角色下的资源
3834 if ( accountType !== AccountTypeEnum . SUPER_ACCOUNT ) {
39- const resourcesIdList = await this . getResourcesIdList ( userInfo ) ;
35+ const resourcesIdList = await this . menusRepository . getResourcesIdList ( userInfo ) ;
4036 console . log ( resourcesIdList , '资源' ) ;
4137 query . set ( 'id' , In ( resourcesIdList ) ) ;
4238 }
@@ -68,7 +64,7 @@ export class MenusService {
6864 return [ ] ;
6965 }
7066 // 获取授权的资源id
71- const resourcesId = await this . getResourcesIdList ( userInfo ) ;
67+ const resourcesId = await this . menusRepository . getResourcesIdList ( userInfo ) ;
7268 console . log ( resourcesId , '全部资源' ) ;
7369 return await this . resourcesRepository . find ( {
7470 where : {
@@ -91,48 +87,9 @@ export class MenusService {
9187 * @return {* }
9288 */
9389 async getResourcesList ( userInfo : ICurrentUserType ) : Promise < ResourcesEntity [ ] > {
94- const resourcesId = await this . getResourcesIdList ( userInfo ) ;
90+ const resourcesId = await this . menusRepository . getResourcesIdList ( userInfo ) ;
9591 return await this . resourcesRepository . find ( {
9692 where : { id : In ( resourcesId ) } ,
9793 } ) ;
9894 }
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- }
13895}
0 commit comments