fix
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"time"
|
||||
"tyapi-server/internal/application/api"
|
||||
"tyapi-server/internal/application/api/commands"
|
||||
"tyapi-server/internal/application/api/dto"
|
||||
"tyapi-server/internal/shared/crypto"
|
||||
"tyapi-server/internal/shared/interfaces"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -194,24 +192,8 @@ func (h *ApiHandler) EncryptParams(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 获取用户的SecretKey
|
||||
apiKeys, err := h.appService.GetUserApiKeys(c.Request.Context(), userID)
|
||||
if err != nil {
|
||||
h.logger.Error("获取用户API密钥失败", zap.Error(err))
|
||||
h.responseBuilder.BadRequest(c, "获取API密钥失败")
|
||||
return
|
||||
}
|
||||
|
||||
// 将JSON对象转换为字节数组
|
||||
jsonData, err := json.Marshal(cmd.Data)
|
||||
if err != nil {
|
||||
h.logger.Error("序列化参数失败", zap.Error(err))
|
||||
h.responseBuilder.BadRequest(c, "参数序列化失败")
|
||||
return
|
||||
}
|
||||
|
||||
// 加密参数
|
||||
encryptedData, err := crypto.AesEncrypt(jsonData, apiKeys.SecretKey)
|
||||
// 调用应用服务层进行加密
|
||||
encryptedData, err := h.appService.EncryptParams(c.Request.Context(), userID, &cmd)
|
||||
if err != nil {
|
||||
h.logger.Error("加密参数失败", zap.Error(err))
|
||||
h.responseBuilder.BadRequest(c, "加密参数失败")
|
||||
@@ -224,6 +206,43 @@ func (h *ApiHandler) EncryptParams(c *gin.Context) {
|
||||
h.responseBuilder.Success(c, response, "加密成功")
|
||||
}
|
||||
|
||||
// DecryptParams 解密参数
|
||||
// @Summary 解密参数
|
||||
// @Description 使用密钥解密加密的数据
|
||||
// @Tags API调试
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security Bearer
|
||||
// @Param request body commands.DecryptCommand true "解密请求"
|
||||
// @Success 200 {object} map[string]interface{} "解密成功"
|
||||
// @Failure 400 {object} map[string]interface{} "请求参数错误"
|
||||
// @Failure 401 {object} map[string]interface{} "未授权"
|
||||
// @Failure 500 {object} map[string]interface{} "解密失败"
|
||||
// @Router /api/v1/decrypt [post]
|
||||
func (h *ApiHandler) DecryptParams(c *gin.Context) {
|
||||
userID := h.getCurrentUserID(c)
|
||||
if userID == "" {
|
||||
h.responseBuilder.Unauthorized(c, "用户未登录")
|
||||
return
|
||||
}
|
||||
|
||||
var cmd commands.DecryptCommand
|
||||
if err := h.validator.BindAndValidate(c, &cmd); err != nil {
|
||||
h.responseBuilder.BadRequest(c, "请求参数错误")
|
||||
return
|
||||
}
|
||||
|
||||
// 调用应用服务层进行解密
|
||||
decryptedData, err := h.appService.DecryptParams(c.Request.Context(), userID, &cmd)
|
||||
if err != nil {
|
||||
h.logger.Error("解密参数失败", zap.Error(err))
|
||||
h.responseBuilder.BadRequest(c, "解密参数失败")
|
||||
return
|
||||
}
|
||||
|
||||
h.responseBuilder.Success(c, decryptedData, "解密成功")
|
||||
}
|
||||
|
||||
// getCurrentUserID 获取当前用户ID
|
||||
func (h *ApiHandler) getCurrentUserID(c *gin.Context) string {
|
||||
if userID, exists := c.Get("user_id"); exists {
|
||||
|
||||
Reference in New Issue
Block a user