From 030674707257d998d04016f89087d85d0fb7c73b Mon Sep 17 00:00:00 2001 From: "November.12" <845255519@qq.com> Date: Wed, 27 Sep 2023 10:41:32 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=89=8B=E5=8A=A8=E6=BF=80=E6=B4=BB?= =?UTF-8?q?=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/tp_scenario_strategy.go | 29 +++++++++++++++++++++++ routers/router.go | 12 ++++++---- validate/tp_scenario_strategy_validate.go | 2 +- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/controllers/tp_scenario_strategy.go b/controllers/tp_scenario_strategy.go index 5fe350ab8..27a26b5ed 100644 --- a/controllers/tp_scenario_strategy.go +++ b/controllers/tp_scenario_strategy.go @@ -162,3 +162,32 @@ func (TpScenarioStrategyController *TpScenarioStrategyController) Delete() { utils.SuccessWithMessage(400, "无法删除;可能的原因:1.被自动化关联的场景无法删除,需要先取消关联;"+req_err.Error(), (*context2.Context)(TpScenarioStrategyController.Ctx)) } } + +// 激活 +func (TpScenarioStrategyController *TpScenarioStrategyController) Activate() { + TpScenarioStrategyIdValidate := valid.TpScenarioStrategyIdValidate{} + err := json.Unmarshal(TpScenarioStrategyController.Ctx.Input.RequestBody, &TpScenarioStrategyIdValidate) + if err != nil { + fmt.Println("参数解析失败", err.Error()) + } + v := validation.Validation{} + status, _ := v.Valid(TpScenarioStrategyIdValidate) + if !status { + for _, err := range v.Errors { + // 获取字段别称 + alias := gvalid.GetAlias(TpScenarioStrategyIdValidate, err.Field) + message := strings.Replace(err.Message, err.Field, alias, 1) + utils.SuccessWithMessage(1000, message, (*context2.Context)(TpScenarioStrategyController.Ctx)) + break + } + return + } + + var s services.TpScenarioActionService + + if s.ExecuteScenarioAction(TpScenarioStrategyIdValidate.Id) != nil { + utils.SuccessWithMessage(400, err.Error(), (*context2.Context)(TpScenarioStrategyController.Ctx)) + return + } + utils.SuccessWithMessage(200, "success", (*context2.Context)(TpScenarioStrategyController.Ctx)) +} diff --git a/routers/router.go b/routers/router.go index 8dbe1e349..6c1e4fff6 100644 --- a/routers/router.go +++ b/routers/router.go @@ -29,11 +29,11 @@ func init() { api := web.NewNamespace("/api", // 登录 - web.NSRouter("/auth/login", &controllers.AuthController{}, "*:Login"), //ty - web.NSRouter("/auth/logout", &controllers.AuthController{}, "*:Logout"), //ty - web.NSRouter("/auth/refresh", &controllers.AuthController{}, "*:Refresh"), //ty - web.NSRouter("/auth/me", &controllers.AuthController{}, "*:Me"), //ty - web.NSRouter("/auth/change_password", &controllers.AuthController{}, "*:ChangePassword"), //ty + web.NSRouter("/auth/login", &controllers.AuthController{}, "*:Login"), //ty + web.NSRouter("/auth/logout", &controllers.AuthController{}, "*:Logout"), //ty + web.NSRouter("/auth/refresh", &controllers.AuthController{}, "*:Refresh"), //ty + web.NSRouter("/auth/me", &controllers.AuthController{}, "*:Me"), //ty + web.NSRouter("/auth/change_password", &controllers.AuthController{}, "*:ChangePassword"), //ty // 获取验证码 web.NSRouter("/auth/captcha", &controllers.TpNotification{}, "*:GetCaptcha"), @@ -427,6 +427,8 @@ func init() { web.NSRouter("/scenario/strategy/detail", &controllers.TpScenarioStrategyController{}, "*:Detail"), web.NSRouter("/scenario/strategy/edit", &controllers.TpScenarioStrategyController{}, "*:Edit"), web.NSRouter("/scenario/strategy/delete", &controllers.TpScenarioStrategyController{}, "*:Delete"), + // 手动激活一次 + web.NSRouter("/scenario/strategy/activate", &controllers.TpScenarioStrategyController{}, "*:Activate"), web.NSRouter("/v1/automation/add", &controllers.TpAutomationController{}, "*:Add"), web.NSRouter("/v1/automation/list", &controllers.TpAutomationController{}, "*:List"), diff --git a/validate/tp_scenario_strategy_validate.go b/validate/tp_scenario_strategy_validate.go index e5638cb7c..a5adf4408 100644 --- a/validate/tp_scenario_strategy_validate.go +++ b/validate/tp_scenario_strategy_validate.go @@ -39,5 +39,5 @@ type RspTpScenarioStrategyPaginationValidate struct { } type TpScenarioStrategyIdValidate struct { - Id string `json:"id" gorm:"primaryKey" valid:"Required;MaxSize(36)"` + Id string `json:"id" alias:"id" gorm:"primaryKey" valid:"Required;MaxSize(36)"` } From 0b95d149c60c8208026305af3b33c5d160f1ddd5 Mon Sep 17 00:00:00 2001 From: "November.12" <845255519@qq.com> Date: Wed, 27 Sep 2023 10:55:34 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E6=96=87=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/tp_scenario_strategy.go | 2 +- models/tp_scenario_log.go | 5 +++++ services/conditions_service.go | 2 +- services/tp_scenario_action_service.go | 10 ++++++++-- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/controllers/tp_scenario_strategy.go b/controllers/tp_scenario_strategy.go index 27a26b5ed..ec265c56c 100644 --- a/controllers/tp_scenario_strategy.go +++ b/controllers/tp_scenario_strategy.go @@ -185,7 +185,7 @@ func (TpScenarioStrategyController *TpScenarioStrategyController) Activate() { var s services.TpScenarioActionService - if s.ExecuteScenarioAction(TpScenarioStrategyIdValidate.Id) != nil { + if s.ExecuteScenarioAction(TpScenarioStrategyIdValidate.Id, models.ManualActivation) != nil { utils.SuccessWithMessage(400, err.Error(), (*context2.Context)(TpScenarioStrategyController.Ctx)) return } diff --git a/models/tp_scenario_log.go b/models/tp_scenario_log.go index 5c9889b39..de09926c6 100644 --- a/models/tp_scenario_log.go +++ b/models/tp_scenario_log.go @@ -12,3 +12,8 @@ type TpScenarioLog struct { func (t *TpScenarioLog) TableName() string { return "tp_scenario_log" } + +const ( + AutomaticallyActivated = 1 + ManualActivation = 2 +) diff --git a/services/conditions_service.go b/services/conditions_service.go index 0d0cce87a..54d7ed33d 100644 --- a/services/conditions_service.go +++ b/services/conditions_service.go @@ -474,7 +474,7 @@ func (*ConditionsService) ExecuteAutomationAction(automationId string, automatio automationLogDetail.TargetId = automationAction.ScenarioStrategyId //触发场景 var scenarioActionService TpScenarioActionService - err := scenarioActionService.ExecuteScenarioAction(automationAction.ScenarioStrategyId) + err := scenarioActionService.ExecuteScenarioAction(automationAction.ScenarioStrategyId, models.AutomaticallyActivated) if err != nil { logMessage = "触发场景失败:" + err.Error() automationLogDetail.ProcessDescription = "触发场景失败:" + err.Error() diff --git a/services/tp_scenario_action_service.go b/services/tp_scenario_action_service.go index 2c85f180f..7ad8fed56 100644 --- a/services/tp_scenario_action_service.go +++ b/services/tp_scenario_action_service.go @@ -89,7 +89,7 @@ func (*TpScenarioActionService) DeleteTpScenarioAction(tp_automation_action mode } // 触发场景动作 -func (*TpScenarioActionService) ExecuteScenarioAction(scenarioStrategyId string) error { +func (*TpScenarioActionService) ExecuteScenarioAction(scenarioStrategyId string, source int) error { var scenarioActions []models.TpScenarioAction result := psql.Mydb.Model(&models.TpScenarioAction{}).Where("scenario_strategy_id = ?", scenarioStrategyId).Find(&scenarioActions) if result.Error != nil { @@ -106,7 +106,13 @@ func (*TpScenarioActionService) ExecuteScenarioAction(scenarioStrategyId string) logs.Error(result.Error) return result.Error } - scenarioLog.ProcessDescription = "执行成功" + + if source == models.AutomaticallyActivated { + scenarioLog.ProcessDescription = "执行成功" + } else { + scenarioLog.ProcessDescription = "执行激活" + } + scenarioLog.ProcessResult = "1" for _, scenarioAction := range scenarioActions { var scenarioLogDetail models.TpScenarioLogDetail