Sfoglia il codice sorgente

update ApiKeyController

reghao 4 giorni fa
parent
commit
f1b8befbef

+ 6 - 2
auth/auth-service/src/main/java/cn/reghao/tnb/auth/app/controller/ApiKeyController.java

@@ -3,6 +3,7 @@ package cn.reghao.tnb.auth.app.controller;
 import cn.reghao.tnb.auth.app.model.dto.ApiKeyDto;
 import cn.reghao.tnb.auth.app.model.po.ApiKey;
 import cn.reghao.tnb.auth.app.model.vo.ApiKeyCreateVO;
+import cn.reghao.tnb.auth.app.service.AccountTokenService;
 import cn.reghao.tnb.auth.app.service.ApiKeyService;
 import cn.reghao.tnb.common.web.WebResult;
 import org.springframework.http.ResponseEntity;
@@ -18,15 +19,18 @@ import java.util.List;
 @RestController
 @RequestMapping("/api/account/apikey")
 public class ApiKeyController {
+    private final AccountTokenService accountTokenService;
     private final ApiKeyService apiKeyService;
 
-    public ApiKeyController(ApiKeyService apiKeyService) {
+    public ApiKeyController(AccountTokenService accountTokenService, ApiKeyService apiKeyService) {
+        this.accountTokenService = accountTokenService;
         this.apiKeyService = apiKeyService;
     }
 
     @PostMapping
     public String createKey(@RequestBody @Validated ApiKeyDto apiKeyDto) {
-        ApiKeyCreateVO vo = apiKeyService.createApiKey(apiKeyDto);
+        long userId = accountTokenService.getAuthToken().getUserId();
+        ApiKeyCreateVO vo = apiKeyService.createApiKey(apiKeyDto, userId);
         return WebResult.success(vo);
     }
 

+ 2 - 7
auth/auth-service/src/main/java/cn/reghao/tnb/auth/app/service/ApiKeyService.java

@@ -21,15 +21,12 @@ import java.util.UUID;
  */
 @Service
 public class ApiKeyService {
-    private final AccountTokenService accountTokenService;
     private final StringRedisTemplate redisTemplate;
     private final ApiKeyMapper apiKeyMapper;
     private final ObjectMapper objectMapper = new ObjectMapper();
     private static final String REDIS_PREFIX = "open_api:ak:";
 
-    public ApiKeyService(AccountTokenService accountTokenService, StringRedisTemplate redisTemplate,
-                         ApiKeyMapper apiKeyMapper) {
-        this.accountTokenService = accountTokenService;
+    public ApiKeyService(StringRedisTemplate redisTemplate, ApiKeyMapper apiKeyMapper) {
         this.redisTemplate = redisTemplate;
         this.apiKeyMapper = apiKeyMapper;
     }
@@ -38,9 +35,7 @@ public class ApiKeyService {
      * 为用户创建新的 AK/SK
      */
     @Transactional(rollbackFor = Exception.class)
-    public ApiKeyCreateVO createApiKey(ApiKeyDto apiKeyDto) {
-        long userId = accountTokenService.getAuthToken().getUserId();
-
+    public ApiKeyCreateVO createApiKey(ApiKeyDto apiKeyDto, long userId) {
         String appName = apiKeyDto.getAppName();
         List<String> scopes =  apiKeyDto.getScopes();