@@ -8,7 +8,7 @@ import { FindOperator, In, Repository } from 'typeorm';
88import { AccountRoleEntity } from '../accountRole/entities/account.role.entity' ;
99import { ResourcesEntity } from '../resources/entities/resources.entity' ;
1010import { 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 ( )
1414export 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}
0 commit comments