This commit is contained in:
2026-06-29 11:25:32 +08:00
parent 2eec7e5bbe
commit 40b50a5a5c
4 changed files with 42 additions and 19 deletions

View File

@@ -171,6 +171,16 @@ func (s *ApiApplicationServiceImpl) CallApi(ctx context.Context, cmd *commands.A
go s.asyncProcessDeduction(context.Background(), apiCall, validationResult)
return transactionId, "", ErrQueryEmpty
}
if errors.Is(err, ErrSuccessNoBill) {
encryptedResponse, encErr := crypto.AesEncrypt([]byte(response), validationResult.GetSecretKey())
if encErr != nil {
s.logger.Error("加密响应失败", zap.Error(encErr))
go s.asyncRecordFailure(context.Background(), apiCall, encErr)
return "", "", ErrSystem
}
go s.asyncSaveApiCall(context.Background(), apiCall, validationResult, response, decimal.Zero)
return transactionId, string(encryptedResponse), nil
}
if err != nil {
// 异步记录失败状态
go s.asyncRecordFailure(context.Background(), apiCall, err)
@@ -405,6 +415,11 @@ func (s *ApiApplicationServiceImpl) callExternalApi(ctx context.Context, cmd *co
callContext)
if err != nil {
var successNoBill *processors.SuccessNoBillError
if errors.As(err, &successNoBill) {
return string(successNoBill.Response), ErrSuccessNoBill
}
// 数据宝 10001/10006查空但计费由 CallApi 统一处理
if errors.Is(err, shujubao.ErrQueryEmpty) {
return "", ErrQueryEmptyBillable
@@ -440,10 +455,13 @@ func (s *ApiApplicationServiceImpl) callExternalApi(ctx context.Context, cmd *co
return string(response), nil
}
// asyncSaveApiCall 异步保存API调用记录
func (s *ApiApplicationServiceImpl) asyncSaveApiCall(ctx context.Context, apiCall *entities.ApiCall, validation *dto.ApiCallValidationResult, response string) {
// 标记为成功
apiCall.MarkSuccess(validation.GetAmount())
// asyncSaveApiCall 异步保存API调用记录;可选 cost 覆盖扣费金额(如成功但不扣费场景传 decimal.Zero
func (s *ApiApplicationServiceImpl) asyncSaveApiCall(ctx context.Context, apiCall *entities.ApiCall, validation *dto.ApiCallValidationResult, response string, cost ...decimal.Decimal) {
chargeAmount := validation.GetAmount()
if len(cost) > 0 {
chargeAmount = cost[0]
}
apiCall.MarkSuccess(chargeAmount)
// 检查TransactionID是否已存在避免重复创建
existingCall, err := s.apiCallRepository.FindByTransactionId(ctx, apiCall.TransactionId)