1
1
import { HttpException , HttpStatus , Injectable } from '@nestjs/common' ;
2
2
import { ConfigService } from '@nestjs/config' ;
3
3
import { InjectRepository } from '@nestjs/typeorm' ;
4
+ import { ResourcesTypeEnum } from '@src/enums/resources.type.enum' ;
4
5
import { LoggerService } from '@src/plugin/logger/logger.service' ;
5
6
import { RedisService } from '@src/plugin/redis/redis.service' ;
6
7
import { ToolsService } from '@src/plugin/tools/tools.service' ;
7
- import { Repository , SelectQueryBuilder } from 'typeorm' ;
8
+ import { In , Repository , SelectQueryBuilder } from 'typeorm' ;
8
9
import { AccountEntity } from '../account/entities/account.entity' ;
10
+ import { AccountRoleEntity } from '../accountRole/entities/account.role.entity' ;
11
+ import { ResourcesEntity } from '../resources/entities/resources.entity' ;
12
+ import { RoleResourcesEntity } from '../roleResources/entities/role.resources.entity' ;
9
13
import { LoginDto } from './dto/login.dto' ;
10
14
import { LoginAccountVo , LoginTokenDataVo , LoginVo } from './vo/login.vo' ;
11
15
@@ -14,6 +18,12 @@ export class LoginService {
14
18
constructor (
15
19
@InjectRepository ( AccountEntity )
16
20
private readonly accountRepository : Repository < AccountEntity > ,
21
+ @InjectRepository ( AccountRoleEntity )
22
+ private readonly accountRoleRepository : Repository < AccountRoleEntity > ,
23
+ @InjectRepository ( RoleResourcesEntity )
24
+ private readonly roleResourcesRepository : Repository < RoleResourcesEntity > ,
25
+ @InjectRepository ( ResourcesEntity )
26
+ private readonly resourcesRepository : Repository < ResourcesEntity > ,
17
27
private readonly toolsService : ToolsService ,
18
28
private readonly loggerService : LoggerService ,
19
29
private readonly redisService : RedisService ,
@@ -106,9 +116,39 @@ export class LoginService {
106
116
const token = this . toolsService . uuidToken ;
107
117
const refreshToken = this . toolsService . uuidToken ;
108
118
const sign = this . toolsService . uuidToken ;
119
+ // 根据当前用户获取授权的按钮权限
120
+ const accountRoleEntityList : Pick < AccountRoleEntity , 'roleId' > [ ] =
121
+ await this . accountRoleRepository . find ( {
122
+ where : {
123
+ accountId : accountEntity . id ,
124
+ } ,
125
+ select : [ 'roleId' ] ,
126
+ } ) ;
127
+ const roleIdList = accountRoleEntityList . map ( ( item ) => item . roleId ) ;
128
+ const roleResourcesList : Pick < RoleResourcesEntity , 'resourcesId' > [ ] =
129
+ await this . roleResourcesRepository . find ( {
130
+ where : {
131
+ type : 1 ,
132
+ roleId : In ( roleIdList ) ,
133
+ } ,
134
+ select : [ 'resourcesId' ] ,
135
+ } ) ;
136
+ const resourcesIdList = roleResourcesList . map ( ( item ) => item . resourcesId ) ;
137
+ // 查询全部的资源
138
+ const resourcesEntity = await this . resourcesRepository . find ( {
139
+ where : {
140
+ id : In ( resourcesIdList ) ,
141
+ } ,
142
+ select : [ 'url' , 'method' , 'title' , 'resourcesType' ] ,
143
+ } ) ;
144
+ console . log ( resourcesEntity , '1111' ) ;
145
+ const resources = resourcesEntity . filter ( ( item ) => item . resourcesType == ResourcesTypeEnum . API ) ;
146
+ console . log ( resources , '全部的资源' ) ;
147
+ // 根据角色idList去查询授权的按钮
109
148
// 根据资源id获取资源信息
110
149
const redisData : LoginTokenDataVo = {
111
150
userInfo : accountEntity , // 用户信息
151
+ authApi : resources , // 授权的api接口
112
152
sign,
113
153
} ;
114
154
// 正常token
0 commit comments