This commit is contained in:
2025-07-28 23:44:01 +08:00
parent f8ac02e7c9
commit 10d086414b
6 changed files with 68 additions and 24 deletions

View File

@@ -5,6 +5,7 @@ import (
"errors"
"tyapi-server/internal/application/api/commands"
"tyapi-server/internal/application/api/dto"
"tyapi-server/internal/application/api/utils"
"tyapi-server/internal/config"
entities "tyapi-server/internal/domains/api/entities"
"tyapi-server/internal/domains/api/repositories"
@@ -80,6 +81,14 @@ func (s *ApiApplicationServiceImpl) CallApi(ctx context.Context, cmd *commands.A
businessError = ErrInvalidAccessId
return ErrInvalidAccessId
}
// 3. 查产品
product, err := s.productManagementService.GetProductByCode(txCtx, cmd.ApiName)
if err != nil {
s.logger.Error("查产品失败", zap.Error(err))
businessError = ErrProductNotFound
return ErrProductNotFound
}
apiCall.ProductId = &product.ID
// 加入UserId
apiCall.UserId = &apiUser.UserId
if apiUser.IsFrozen() {
@@ -109,14 +118,6 @@ func (s *ApiApplicationServiceImpl) CallApi(ctx context.Context, cmd *commands.A
businessError = ErrArrears
return ErrArrears
}
// 3. 查产品
product, err := s.productManagementService.GetProductByCode(txCtx, cmd.ApiName)
if err != nil {
s.logger.Error("查产品失败", zap.Error(err))
businessError = ErrProductNotFound
return ErrProductNotFound
}
// 4. 查订阅
subscription, err := s.productSubscriptionService.GetUserSubscribedProduct(txCtx, apiUser.UserId, product.ID)
if err != nil {
@@ -129,7 +130,6 @@ func (s *ApiApplicationServiceImpl) CallApi(ctx context.Context, cmd *commands.A
businessError = ErrNotSubscribed
return ErrNotSubscribed
}
apiCall.ProductId = &product.ID
if !product.IsValid() {
s.logger.Error("产品已停用", zap.String("productId", product.ID))
businessError = ErrProductDisabled
@@ -172,7 +172,7 @@ func (s *ApiApplicationServiceImpl) CallApi(ctx context.Context, cmd *commands.A
businessError = ErrSystem
return ErrSystem
}
apiCall.ResponseData = &encryptedResponse
// apiCall.ResponseData = &encryptedResponse
// 8. 更新订阅使用次数
subscription.IncrementAPIUsage(1)
@@ -193,7 +193,7 @@ func (s *ApiApplicationServiceImpl) CallApi(ctx context.Context, cmd *commands.A
apiCall.Cost = &subscription.Price
// 10. 标记成功
apiCall.MarkSuccess(encryptedResponse, subscription.Price)
apiCall.MarkSuccess(subscription.Price)
return nil
})
@@ -392,6 +392,8 @@ func (s *ApiApplicationServiceImpl) GetUserApiCalls(ctx context.Context, userID
}
if call.ErrorMsg != nil {
item.ErrorMsg = call.ErrorMsg
// 添加翻译后的错误信息
item.TranslatedErrorMsg = utils.TranslateErrorMsg(call.ErrorType, call.ErrorMsg)
}
items = append(items, item)

View File

@@ -53,6 +53,7 @@ type ApiCallRecordResponse struct {
Cost *string `json:"cost,omitempty"`
ErrorType *string `json:"error_type,omitempty"`
ErrorMsg *string `json:"error_msg,omitempty"`
TranslatedErrorMsg *string `json:"translated_error_msg,omitempty"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
@@ -88,3 +89,5 @@ func NewErrorResponse(code int, message, transactionId string) *ApiCallResponse
Data: "",
}
}

View File

@@ -0,0 +1,40 @@
package utils
// TranslateErrorMsg 翻译错误信息
func TranslateErrorMsg(errorType, errorMsg *string) *string {
if errorType == nil || errorMsg == nil {
return nil
}
// 错误类型到中文描述的映射
errorTypeTranslations := map[string]string{
"invalid_access": "无效的访问凭证",
"frozen_account": "账户已被冻结",
"invalid_ip": "IP地址未授权",
"arrears": "账户余额不足",
"not_subscribed": "未订阅该产品",
"product_not_found": "产品不存在",
"product_disabled": "产品已停用",
"system_error": "系统内部错误",
"datasource_error": "数据源异常",
"invalid_param": "参数校验失败",
"decrypt_fail": "参数解密失败",
"query_empty": "查询结果为空",
}
// 获取错误类型的中文描述
translatedType, exists := errorTypeTranslations[*errorType]
if !exists {
// 如果没有找到对应的翻译,返回原始错误信息
return errorMsg
}
// 构建翻译后的错误信息
translatedMsg := translatedType
if *errorMsg != "" && *errorMsg != *errorType {
// 如果原始错误信息不是错误类型本身,则组合显示
translatedMsg = translatedType + "" + *errorMsg
}
return &translatedMsg
}

View File

@@ -94,17 +94,16 @@ func NewApiCall(accessId, requestParams, clientIp string) (*ApiCall, error) {
}
// MarkSuccess 标记为成功
func (a *ApiCall) MarkSuccess(responseData string, cost decimal.Decimal) error {
func (a *ApiCall) MarkSuccess(cost decimal.Decimal) error {
// 校验除ErrorMsg和ErrorType外所有字段不能为空
if a.ID == "" || a.AccessId == "" || a.TransactionId == "" || a.RequestParams == "" || a.Status == "" || a.StartAt.IsZero() {
return errors.New("ApiCall字段不能为空除ErrorMsg和ErrorType")
}
// 可选字段也要有值
if a.UserId == nil || a.ProductId == nil || responseData == "" {
return errors.New("ApiCall标记成功时UserId、ProductId、ResponseData不能为空")
if a.UserId == nil || a.ProductId == nil {
return errors.New("ApiCall标记成功时UserId、ProductId不能为空")
}
a.Status = ApiCallStatusSuccess
a.ResponseData = &responseData
endAt := time.Now()
a.EndAt = &endAt
a.Cost = &cost