Просмотр исходного кода

fix(internal/service): 修复编辑全局限制时的空指针异常并优化数据更新逻辑

- 在更新全局限制时增加对 oldData 的空值检查,避免空指针异常
- 在创建全局限制时添加 State 字段初始化,确保状态正确
fusu 1 месяц назад
Родитель
Сommit
f44ffbb7a6
1 измененных файлов с 2 добавлено и 1 удалено
  1. 2 1
      internal/service/globallimit.go

+ 2 - 1
internal/service/globallimit.go

@@ -345,7 +345,7 @@ func (s *globalLimitService) AddGlobalLimit(ctx context.Context, req v1.GlobalLi
 	if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
 		return err
 	}
-	if oldData.Id != 0 {
+	if oldData!= nil && oldData.Id != 0 {
 		err = s.globalLimitRepository.UpdateGlobalLimitByHostId(ctx, &model.GlobalLimit{
 			HostId:         req.HostId,
 			Uid:            req.Uid,
@@ -376,6 +376,7 @@ func (s *globalLimitService) AddGlobalLimit(ctx context.Context, req v1.GlobalLi
 		GatewayGroupId: gatewayGroupId,
 		CdnUid:         int(userId),
 		Comment:        req.Comment,
+		State:          1,
 		ExpiredAt:      expiredAt,
 	})
 	if err != nil {