@@ -6,7 +6,6 @@ import { liveQueryModule } from 'open-leancloud-storage/live-query';
66import axios from 'axios' ;
77import { useQuery } from 'react-query' ;
88import { atom , selector , useRecoilValue , useSetRecoilState } from 'recoil' ;
9- import { mergeWith } from 'lodash-es' ;
109
1110LC . use ( authModule ) ;
1211LC . use ( cloudModule ) ;
@@ -85,69 +84,21 @@ export const GroupPermissionDescriptions: Record<keyof CustomerServicePermission
8584 statistics : '工单统计' ,
8685} ;
8786
88- interface GroupData {
89- permissions ?: CustomerServicePermissions ;
90- }
91-
92- const currentUserGroupsState = selector < GroupData [ ] > ( {
93- key : 'currentUserGroups' ,
94- get : async ( { get } ) => {
95- const currentUser = get ( currentUserState ) ;
96- if ( ! currentUser ) {
97- return [ ] ;
98- }
99- try {
100- const groupRoles = await auth
101- . queryRole ( )
102- . where ( 'name' , 'not-in' , [ 'customerService' , 'staff' , 'admin' , 'collaborator' ] )
103- . where ( 'users' , '==' , db . class ( '_User' ) . object ( currentUser . id ) )
104- . find ( ) ;
105-
106- const groups = await db
107- . query ( 'Group' )
108- . where (
109- 'role' ,
110- 'in' ,
111- groupRoles . map ( ( role ) => db . class ( '_Role' ) . object ( role . id ) )
112- )
113- . find ( ) ;
114-
115- return groups . map ( ( group ) => group . toJSON ( ) ) ;
116- } catch {
117- return [ ] ;
118- }
119- } ,
120- } ) ;
121-
12287const currentUserPermissions = selector ( {
12388 key : 'currentUserPermissions' ,
124- get : async ( { get } ) => {
125- const groups = get ( currentUserGroupsState ) ;
126-
127- return groups
128- . map ( ( g ) => ( { ...DefaultGroupPermission , ...g . permissions } ) )
129- . reduce < CustomerServicePermissions > (
130- ( acc , cur ) => mergeWith ( acc , cur , ( v1 : boolean , v2 : boolean ) => v1 || v2 ) ,
131- { } as CustomerServicePermissions
132- ) ;
89+ get : async ( ) => {
90+ const res = await http . get < CustomerServicePermissions > ( '/api/2/users/me/permissions' ) ;
91+ return res . data ;
13392 } ,
13493} ) ;
13594
13695export const useCurrentUserPermissions = ( ) => useRecoilValue ( currentUserPermissions ) ;
13796
13897const currentUserRolesState = selector ( {
13998 key : 'currentUserRoles' ,
140- get : async ( { get } ) => {
141- const currentUser = get ( currentUserState ) ;
142- if ( ! currentUser ) {
143- return [ ] ;
144- }
145- return auth
146- . queryRole ( )
147- . where ( 'name' , 'in' , [ 'customerService' , 'staff' , 'admin' ] )
148- . where ( 'users' , '==' , db . class ( '_User' ) . object ( currentUser . id ) )
149- . find ( )
150- . then ( ( roles ) => roles . map ( ( role ) => role . name ) ) ;
99+ get : async ( ) => {
100+ const res = await http . get < string [ ] > ( '/api/2/users/me/system-roles' ) ;
101+ return res . data ;
151102 } ,
152103} ) ;
153104
0 commit comments