fix
This commit is contained in:
@@ -458,57 +458,141 @@ func (s *ApiApplicationServiceImpl) GetAdminApiCalls(ctx context.Context, filter
|
||||
// 转换为响应DTO
|
||||
var items []dto.ApiCallRecordResponse
|
||||
for _, call := range calls {
|
||||
// 基础字段安全检查
|
||||
if call.ID == "" {
|
||||
s.logger.Warn("跳过无效的API调用记录:ID为空")
|
||||
continue
|
||||
}
|
||||
|
||||
item := dto.ApiCallRecordResponse{
|
||||
ID: call.ID,
|
||||
AccessId: call.AccessId,
|
||||
UserId: *call.UserId,
|
||||
TransactionId: call.TransactionId,
|
||||
ClientIp: call.ClientIp,
|
||||
Status: call.Status,
|
||||
StartAt: call.StartAt.Format("2006-01-02 15:04:05"),
|
||||
CreatedAt: call.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
UpdatedAt: call.UpdatedAt.Format("2006-01-02 15:04:05"),
|
||||
}
|
||||
|
||||
// 安全设置用户ID
|
||||
if call.UserId != nil && *call.UserId != "" {
|
||||
item.UserId = *call.UserId
|
||||
} else {
|
||||
item.UserId = "未知用户"
|
||||
}
|
||||
|
||||
// 安全设置时间字段
|
||||
if !call.StartAt.IsZero() {
|
||||
item.StartAt = call.StartAt.Format("2006-01-02 15:04:05")
|
||||
} else {
|
||||
item.StartAt = "未知时间"
|
||||
}
|
||||
|
||||
if !call.CreatedAt.IsZero() {
|
||||
item.CreatedAt = call.CreatedAt.Format("2006-01-02 15:04:05")
|
||||
} else {
|
||||
item.CreatedAt = "未知时间"
|
||||
}
|
||||
|
||||
if !call.UpdatedAt.IsZero() {
|
||||
item.UpdatedAt = call.UpdatedAt.Format("2006-01-02 15:04:05")
|
||||
} else {
|
||||
item.UpdatedAt = "未知时间"
|
||||
}
|
||||
|
||||
// 处理可选字段
|
||||
if call.ProductId != nil {
|
||||
if call.ProductId != nil && *call.ProductId != "" {
|
||||
item.ProductId = call.ProductId
|
||||
}
|
||||
|
||||
// 从映射中获取产品名称
|
||||
if productName, exists := productNameMap[call.ID]; exists {
|
||||
if productName, exists := productNameMap[call.ID]; exists && productName != "" {
|
||||
item.ProductName = &productName
|
||||
}
|
||||
if call.EndAt != nil {
|
||||
|
||||
// 安全设置结束时间
|
||||
if call.EndAt != nil && !call.EndAt.IsZero() {
|
||||
endAt := call.EndAt.Format("2006-01-02 15:04:05")
|
||||
item.EndAt = &endAt
|
||||
}
|
||||
|
||||
// 安全设置费用
|
||||
if call.Cost != nil {
|
||||
cost := call.Cost.String()
|
||||
item.Cost = &cost
|
||||
if cost != "" {
|
||||
item.Cost = &cost
|
||||
}
|
||||
}
|
||||
if call.ErrorType != nil {
|
||||
|
||||
// 安全设置错误类型
|
||||
if call.ErrorType != nil && *call.ErrorType != "" {
|
||||
item.ErrorType = call.ErrorType
|
||||
}
|
||||
if call.ErrorMsg != nil {
|
||||
|
||||
// 安全设置错误信息
|
||||
if call.ErrorMsg != nil && *call.ErrorMsg != "" {
|
||||
item.ErrorMsg = call.ErrorMsg
|
||||
// 添加翻译后的错误信息
|
||||
item.TranslatedErrorMsg = utils.TranslateErrorMsg(call.ErrorType, call.ErrorMsg)
|
||||
if call.ErrorType != nil && *call.ErrorType != "" {
|
||||
item.TranslatedErrorMsg = utils.TranslateErrorMsg(call.ErrorType, call.ErrorMsg)
|
||||
}
|
||||
}
|
||||
|
||||
// 获取用户信息和企业名称
|
||||
if call.UserId != nil {
|
||||
// 获取用户信息和企业名称(增强空指针防护)
|
||||
if call.UserId != nil && *call.UserId != "" {
|
||||
user, err := s.userRepo.GetByIDWithEnterpriseInfo(ctx, *call.UserId)
|
||||
if err == nil {
|
||||
if err == nil && user.ID != "" {
|
||||
companyName := "未知企业"
|
||||
if user.EnterpriseInfo != nil {
|
||||
|
||||
// 安全获取企业名称
|
||||
if user.EnterpriseInfo != nil && user.EnterpriseInfo.CompanyName != "" {
|
||||
companyName = user.EnterpriseInfo.CompanyName
|
||||
}
|
||||
|
||||
item.CompanyName = &companyName
|
||||
|
||||
// 安全构建用户响应
|
||||
item.User = &dto.UserSimpleResponse{
|
||||
ID: user.ID,
|
||||
CompanyName: companyName,
|
||||
Phone: user.Phone,
|
||||
}
|
||||
|
||||
// 验证用户数据的完整性
|
||||
if user.Phone == "" {
|
||||
s.logger.Warn("用户手机号为空",
|
||||
zap.String("user_id", user.ID),
|
||||
zap.String("call_id", call.ID))
|
||||
item.User.Phone = "未知手机号"
|
||||
}
|
||||
} else {
|
||||
// 用户查询失败或用户数据不完整时的处理
|
||||
if err != nil {
|
||||
s.logger.Warn("获取用户信息失败",
|
||||
zap.String("user_id", *call.UserId),
|
||||
zap.String("call_id", call.ID),
|
||||
zap.Error(err))
|
||||
} else if user.ID == "" {
|
||||
s.logger.Warn("用户ID为空",
|
||||
zap.String("call_user_id", *call.UserId),
|
||||
zap.String("call_id", call.ID))
|
||||
}
|
||||
|
||||
// 设置默认值
|
||||
defaultCompanyName := "未知企业"
|
||||
item.CompanyName = &defaultCompanyName
|
||||
item.User = &dto.UserSimpleResponse{
|
||||
ID: "未知用户",
|
||||
CompanyName: defaultCompanyName,
|
||||
Phone: "未知手机号",
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 用户ID为空时的处理
|
||||
defaultCompanyName := "未知企业"
|
||||
item.CompanyName = &defaultCompanyName
|
||||
item.User = &dto.UserSimpleResponse{
|
||||
ID: "未知用户",
|
||||
CompanyName: defaultCompanyName,
|
||||
Phone: "未知手机号",
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user