Forráskód Böngészése

feat(api): 增加节点区域和最大防护配置

- 在 GlobalLimit 和 Host 结构中添加 NodeArea 和 ConfigMaxProtection 字段
- 实现 GetNodeArea 方法以获取节点区域对应的套餐 ID- 更新服务逻辑,根据配置的最大防护值选择合适的套餐
fusu 1 hónapja
szülő
commit
7f521513a8

+ 2 - 0
api/v1/globalLimit.go

@@ -21,6 +21,8 @@ type GlobalLimitRequireResponse struct {
 	MaxBytesMonth   string
 	IpCount   int
 	Operator int
+	NodeArea      string
+	ConfigMaxProtection string
 }
 
 type GeneralLimitRequireRequest struct {

+ 2 - 0
api/v1/host.go

@@ -30,4 +30,6 @@ type GlobalLimitConfigResponse struct {
 	DomainCount   int
 	Operator      int
 	IpCount       int
+	NodeArea      string
+	ConfigMaxProtection string
 }

+ 12 - 0
internal/repository/globallimit.go

@@ -23,6 +23,8 @@ type GlobalLimitRepository interface {
 	GetUserInfo(ctx context.Context, uid int64) (v1.UserInfo, error)
 	GetHostName(ctx context.Context,hostId int64) (string, error)
 	GetNodeId(ctx context.Context, cndWebId int) (int64, error)
+	// 获取套餐Id
+	GetNodeArea(ctx context.Context, nodeAreaName string) (int64, error)
 }
 
 func NewGlobalLimitRepository(
@@ -156,3 +158,13 @@ func (r *globalLimitRepository) GetNodeId(ctx context.Context, cndWebId int) (in
 	}
 	return nodeId, nil
 }
+
+// 获取cdn套餐ID
+func (r *globalLimitRepository) GetNodeArea(ctx context.Context, nodeAreaName string) (int64, error) {
+	var nodeId int64
+	if err := r.DBWithName(ctx,"cdn").WithContext(ctx).Table("cloud_plans").Where("name = ?", nodeAreaName).Select("id").Scan(&nodeId).Error; err != nil {
+		return 0, err
+	}
+	return nodeId, nil
+
+}

+ 22 - 1
internal/service/globallimit.go

@@ -150,6 +150,8 @@ func (s *globalLimitService) GlobalLimitRequire(ctx context.Context, req v1.Glob
 	res.MaxBytesMonth = configCount.MaxBytesMonth
 	res.Operator = configCount.Operator
 	res.IpCount = configCount.IpCount
+	res.NodeArea = configCount.NodeArea
+	res.ConfigMaxProtection = configCount.ConfigMaxProtection
 	domain, err := s.hostRep.GetDomainById(ctx, req.HostId)
 	if err != nil {
 		return v1.GlobalLimitRequireResponse{}, err
@@ -258,9 +260,28 @@ func (s *globalLimitService) AddGlobalLimit(ctx context.Context, req v1.GlobalLi
 		return err
 	}
 
+	// 获取套餐ID
+	maxProtection := strings.Split(require.ConfigMaxProtection, "G")
+	maxProtectionInt, err := strconv.Atoi(maxProtection[0])
+	if err != nil {
+		return err
+	}
+	var planId int64
+	if maxProtectionInt < 1000 {
+		NodeAreaName := fmt.Sprintf("%s-%dT",require.NodeArea, 1)
+		planId, err = s.globalLimitRepository.GetNodeArea(ctx, NodeAreaName)
+		if err != nil {
+			return err
+		}
+	}
+	if planId == 0 {
+		planId = 6
+	}
+
+
 	ruleId, err := s.cdnService.BindPlan(ctx, v1.Plan{
 		UserId:    userId,
-		PlanId: 	4,
+		PlanId:    planId,
 		DayTo:     outputTimeStr,
 		Name:      require.GlobalLimitName,
 		IsFree:    true,

+ 11 - 0
internal/service/host.go

@@ -42,6 +42,7 @@ const (
 	ConfigDomainCount          = "防御域名(需要备案)"
 	ConfigOperator             = "高防线路"
 	ConfigIpCount              = "高防节点IP"
+	NodeArea                   = "节点区域"
 )
 
 // unitSuffixMap 存储需要去除的单位后缀
@@ -210,6 +211,8 @@ func (s *hostService) GetGlobalLimitConfig(ctx context.Context, hostId int) (v1.
 		Bps:           "0",
 		IpCount:       0,
 		Operator:      0,
+		NodeArea:      "",
+		ConfigMaxProtection: "",
 	}
 	if val, ok := configsMap[ConfigBps]; ok {
 		data.Bps = val
@@ -243,6 +246,14 @@ func (s *hostService) GetGlobalLimitConfig(ctx context.Context, hostId int) (v1.
 			return data, err
 		}
 	}
+
+	if val, ok := configsMap[NodeArea]; ok {
+		data.NodeArea = val
+	}
+
+	if val, ok := configsMap[ConfigMaxProtection]; ok {
+		data.ConfigMaxProtection = val
+	}
 	return data, nil
 }