Browse Source

feat(GameShield): 根据 ID 获取游戏盾名称

- 在 GetGameShieldRuleIdRequest 结构体中添加 ID 字段
- 修改 GetGameShieldKey 方法,使用 ID 作为参数
- 新增 GetGameShieldIdByAppName 方法,用于根据 ID 获取游戏盾名称
-
fusu 3 months ago
parent
commit
b0b841b6e0

+ 1 - 0
api/v1/GameShield.go

@@ -30,6 +30,7 @@ type GameShieldSubmitResponse struct {
 type GetGameShieldRuleIdRequest struct {
 type GetGameShieldRuleIdRequest struct {
 	AppName string `json:"app_name" form:"app_name"`
 	AppName string `json:"app_name" form:"app_name"`
 	Name    string `json:"name" form:"name"`
 	Name    string `json:"name" form:"name"`
+	Id      int    `json:"id" form:"id"`
 }
 }
 
 
 type GetGameShieldRequiredResponse struct {
 type GetGameShieldRequiredResponse struct {

+ 2 - 1
internal/handler/gameshield.go

@@ -78,7 +78,8 @@ func (h *GameShieldHandler) GetGameShieldKey(ctx *gin.Context) {
 		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
 		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, err.Error())
 		return
 		return
 	}
 	}
-	res, err := h.crawlerService.GetKey(ctx, req.AppName)
+
+	res, err := h.gameShieldService.GetGameShieldKey(ctx, req.Id)
 	if err != nil {
 	if err != nil {
 		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
 		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
 		return
 		return

+ 1 - 0
internal/model/gameshield.go

@@ -14,6 +14,7 @@ type GameShield struct {
 	HostId         string
 	HostId         string
 	AppIp          string
 	AppIp          string
 	Checked        int
 	Checked        int
+	DunName        string
 }
 }
 
 
 func (m *GameShield) TableName() string {
 func (m *GameShield) TableName() string {

+ 22 - 0
internal/repository/gameshield.go

@@ -14,6 +14,8 @@ type GameShieldRepository interface {
 	GetGameShieldIsBuy(ctx context.Context, uid int64) (int64, error)
 	GetGameShieldIsBuy(ctx context.Context, uid int64) (int64, error)
 	GetGameShieldNextduedate(ctx context.Context, uid int64, productID string) (string, error)
 	GetGameShieldNextduedate(ctx context.Context, uid int64, productID string) (string, error)
 	GetGameShieldExistingIps(ctx context.Context, ip string) ([]string, error)
 	GetGameShieldExistingIps(ctx context.Context, ip string) ([]string, error)
+	GetGameShieldNameByAppName(ctx context.Context, appName string) (string, error)
+	GetGameShieldIdByAppName(ctx context.Context, id int64) (string, error)
 }
 }
 
 
 func NewGameShieldRepository(
 func NewGameShieldRepository(
@@ -102,3 +104,23 @@ func (r *gameShieldRepository) GetGameShieldExistingIps(ctx context.Context, ip
 	}
 	}
 	return res, nil
 	return res, nil
 }
 }
+
+func (r *gameShieldRepository) GetGameShieldNameByAppName(ctx context.Context, appName string) (string, error) {
+	var res string
+	if err := r.DB(ctx).Model(&model.GameShield{}).
+		Where("app_name = ?", appName).
+		Pluck("dun_name", &res).Error; err != nil {
+		return "", err
+	}
+	return res, nil
+}
+
+func (r *gameShieldRepository) GetGameShieldIdByAppName(ctx context.Context, id int64) (string, error) {
+	var res string
+	if err := r.DB(ctx).Model(&model.GameShield{}).
+		Where("id = ?", id).
+		Pluck("dun_name", &res).Error; err != nil {
+		return "", err
+	}
+	return res, nil
+}

+ 28 - 7
internal/service/gameshield.go

@@ -15,6 +15,7 @@ type GameShieldService interface {
 	SubmitGameShield(ctx context.Context, req *v1.GameShieldSubmitRequest) (string, error)
 	SubmitGameShield(ctx context.Context, req *v1.GameShieldSubmitRequest) (string, error)
 	EditGameShield(ctx context.Context, req *v1.GameShieldSubmitRequest) (string, error)
 	EditGameShield(ctx context.Context, req *v1.GameShieldSubmitRequest) (string, error)
 	DeleteGameShield(ctx context.Context, req int) (string, error)
 	DeleteGameShield(ctx context.Context, req int) (string, error)
+	GetGameShieldKey(ctx context.Context, id int) (string, error)
 }
 }
 
 
 func NewGameShieldService(
 func NewGameShieldService(
@@ -87,8 +88,9 @@ func (service *gameShieldService) SubmitGameShield(ctx context.Context, req *v1.
 	if err != nil {
 	if err != nil {
 		return "", err
 		return "", err
 	}
 	}
+	dunName := strconv.Itoa(req.Uid) + "_" + strconv.FormatInt(time.Now().Unix(), 10) + "_" + req.AppName
 	formData := map[string]interface{}{
 	formData := map[string]interface{}{
-		"app_name":         req.AppName,
+		"app_name":         dunName,
 		"gateway_group_id": 4,
 		"gateway_group_id": 4,
 		"backend":          require.Backend,
 		"backend":          require.Backend,
 		"expired_at":       require.ExpiredAt,
 		"expired_at":       require.ExpiredAt,
@@ -106,13 +108,13 @@ func (service *gameShieldService) SubmitGameShield(ctx context.Context, req *v1.
 	if res != "" {
 	if res != "" {
 		return "", fmt.Errorf(res)
 		return "", fmt.Errorf(res)
 	}
 	}
-	KeyAndField, err := service.required.GetKeyAndField(ctx, req.AppName, "rule_id")
+	KeyAndField, err := service.required.GetKeyAndField(ctx, dunName, "rule_id")
 	if err != nil {
 	if err != nil {
 		return "", err
 		return "", err
 	}
 	}
 	if err := service.gameShieldRepository.AddGameShield(ctx, &model.GameShield{
 	if err := service.gameShieldRepository.AddGameShield(ctx, &model.GameShield{
 		AppName:        req.AppName,
 		AppName:        req.AppName,
-		GatewayGroupId: 2,
+		GatewayGroupId: 4,
 		Backend:        require.Backend,
 		Backend:        require.Backend,
 		RuleId:         KeyAndField.FieldId,
 		RuleId:         KeyAndField.FieldId,
 		Key:            KeyAndField.Key,
 		Key:            KeyAndField.Key,
@@ -121,6 +123,7 @@ func (service *gameShieldService) SubmitGameShield(ctx context.Context, req *v1.
 		HostId:         req.HostId,
 		HostId:         req.HostId,
 		AppIp:          req.AppIp,
 		AppIp:          req.AppIp,
 		Checked:        req.Checked,
 		Checked:        req.Checked,
+		DunName:        dunName,
 	}); err != nil {
 	}); err != nil {
 		return "", err
 		return "", err
 	}
 	}
@@ -132,14 +135,16 @@ func (service *gameShieldService) EditGameShield(ctx context.Context, req *v1.Ga
 	if err != nil {
 	if err != nil {
 		return "", err
 		return "", err
 	}
 	}
-	tokenUrl := service.Url + "admin/info/rule/edit?&__goadmin_edit_pk=" + strconv.Itoa(req.RuleId)
+	tokenUrl := service.Url + "admin/info/rule/edit?&__goadmin_edit_pk=" + strconv.Itoa(req.RuleId) + "_" + req.AppName
 	tokens, err := service.crawlerService.GetFormTokens(ctx, tokenUrl, require.Cookie)
 	tokens, err := service.crawlerService.GetFormTokens(ctx, tokenUrl, require.Cookie)
 	if err != nil {
 	if err != nil {
 		return "", err
 		return "", err
 	}
 	}
 	sendUrl := service.Url + "admin/edit/rule"
 	sendUrl := service.Url + "admin/edit/rule"
+
+	dunName := strconv.Itoa(req.Uid) + "_" + strconv.FormatInt(time.Now().Unix(), 10)
 	formData := map[string]interface{}{
 	formData := map[string]interface{}{
-		"app_name":             req.AppName,
+		"app_name":             dunName,
 		"gateway_group_id":     4,
 		"gateway_group_id":     4,
 		"backend":              require.Backend,
 		"backend":              require.Backend,
 		"rule_id":              req.RuleId,
 		"rule_id":              req.RuleId,
@@ -160,13 +165,13 @@ func (service *gameShieldService) EditGameShield(ctx context.Context, req *v1.Ga
 	if res != "" {
 	if res != "" {
 		return "", fmt.Errorf(res)
 		return "", fmt.Errorf(res)
 	}
 	}
-	KeyAndField, err := service.required.GetKeyAndField(ctx, req.AppName, "rule_id")
+	KeyAndField, err := service.required.GetKeyAndField(ctx, dunName, "rule_id")
 	if err != nil {
 	if err != nil {
 		return "", err
 		return "", err
 	}
 	}
 	if err := service.gameShieldRepository.UpdateGameShield(ctx, &model.GameShield{
 	if err := service.gameShieldRepository.UpdateGameShield(ctx, &model.GameShield{
 		AppName:        req.AppName,
 		AppName:        req.AppName,
-		GatewayGroupId: 2,
+		GatewayGroupId: 4,
 		Backend:        require.Backend,
 		Backend:        require.Backend,
 		RuleId:         KeyAndField.FieldId,
 		RuleId:         KeyAndField.FieldId,
 		Key:            KeyAndField.Key,
 		Key:            KeyAndField.Key,
@@ -174,6 +179,7 @@ func (service *gameShieldService) EditGameShield(ctx context.Context, req *v1.Ga
 		HostId:         req.HostId,
 		HostId:         req.HostId,
 		AppIp:          req.AppIp,
 		AppIp:          req.AppIp,
 		Checked:        req.Checked,
 		Checked:        req.Checked,
+		DunName:        dunName,
 	}); err != nil {
 	}); err != nil {
 		return "", err
 		return "", err
 	}
 	}
@@ -195,3 +201,18 @@ func (service *gameShieldService) DeleteGameShield(ctx context.Context, id int)
 
 
 	return res, nil
 	return res, nil
 }
 }
+
+func (service *gameShieldService) GetGameShieldKey(ctx context.Context, id int) (string, error) {
+	dunName, err := service.gameShieldRepository.GetGameShieldIdByAppName(ctx, int64(id))
+	if err != nil {
+		return "", err
+	}
+	if dunName == "" {
+		return "", fmt.Errorf("没有该条数据")
+	}
+	res, err := service.crawlerService.GetKey(ctx, dunName)
+	if err != nil {
+		return "", err
+	}
+	return res, nil
+}