diff --git a/controllers/tp_notification.go b/controllers/tp_notification.go index 6a2a547cb..b6674b701 100644 --- a/controllers/tp_notification.go +++ b/controllers/tp_notification.go @@ -340,7 +340,7 @@ func (c *TpNotification) ConfigSave() { // "status":2 // } - if input.NoticeType == models.NotificationConfigType_Message { + if input.NoticeType == models.NotificationConfigType_Message || input.NoticeType == models.NotificationConfigType_VerificationCode { var configInfo models.CloudServicesConfig_Ali @@ -350,7 +350,7 @@ func (c *TpNotification) ConfigSave() { return } - res := s.SaveNotificationConfigAli(configInfo, input.Status) + res := s.SaveNotificationConfigAli(input.NoticeType, configInfo, input.Status) if err != nil { response.SuccessWithMessage(400, res.Error(), (*context2.Context)(c.Ctx)) } @@ -452,11 +452,6 @@ func (c *TpNotification) SendMessage() { arr := strings.Split(input.Content, ",") - if len(arr) != 3 { - response.SuccessWithMessage(400, "content解析错误", (*context2.Context)(c.Ctx)) - return - } - // 获取用户租户id tenantId, ok := c.Ctx.Input.GetData("tenant_id").(string) if !ok { @@ -464,7 +459,14 @@ func (c *TpNotification) SendMessage() { return } - err = sendmessage.SendSMS_SMS_461790263(input.PhoneNumber, arr[0], arr[1], arr[2], tenantId) + if len(arr) == 3 { + err = sendmessage.SendSMS_SMS_461790263(input.PhoneNumber, arr[0], arr[1], arr[2], tenantId) + } else if len(arr) == 1 { + err = sendmessage.SendSMSVerificationCode(input.PhoneNumber, input.Content, tenantId) + } else { + response.SuccessWithMessage(400, "暂不支持的消息类型", (*context2.Context)(c.Ctx)) + return + } if err != nil { response.SuccessWithMessage(400, err.Error(), (*context2.Context)(c.Ctx)) diff --git a/initialize/send_message/message.go b/initialize/send_message/message.go index 9ab8599bc..619d87b17 100644 --- a/initialize/send_message/message.go +++ b/initialize/send_message/message.go @@ -30,29 +30,22 @@ func CreateClient(accessKeyId *string, accessKeySecret *string, endpoint string) // code 必须是 string // 如果是int,且发送的验证码为 0000,收到的是 0 -// func SendSMSVerificationCode(phoneNumber int, code, tenantId string) (err error) { +func SendSMSVerificationCode(phoneNumber int, code, tenantId string) (err error) { -// codeMap := make(map[string]string) -// codeMap["code"] = code - -// codeStr, _ := json.Marshal(codeMap) -// phoneNumberStr := strconv.Itoa(phoneNumber) + codeMap := make(map[string]string) + codeMap["code"] = code -// sendSmsRequest := &dysmsapi20170525.SendSmsRequest{ -// PhoneNumbers: tea.String(phoneNumberStr), -// SignName: tea.String("ThingsPanel"), -// TemplateCode: tea.String("SMS_98355081"), -// TemplateParam: tea.String(string(codeStr)), -// } + codeStr, _ := json.Marshal(codeMap) + phoneNumberStr := strconv.Itoa(phoneNumber) -// err = SendSMS(*sendSmsRequest, &util.RuntimeOptions{}, tenantId) + err = SendSMS(models.NotificationConfigType_VerificationCode, phoneNumberStr, codeStr, &util.RuntimeOptions{}, tenantId) -// if err != nil { -// log.Println(err) -// } + if err != nil { + log.Println(err) + } -// return err -// } + return err +} func SendSMS_SMS_461790263(phoneNumber int, level, name, time, tenantId string) (err error) { @@ -63,7 +56,7 @@ func SendSMS_SMS_461790263(phoneNumber int, level, name, time, tenantId string) codeStr, _ := json.Marshal(codeMap) phoneNumberStr := strconv.Itoa(phoneNumber) - err = SendSMS(phoneNumberStr, codeStr, &util.RuntimeOptions{}, tenantId) + err = SendSMS(models.NotificationConfigType_Message, phoneNumberStr, codeStr, &util.RuntimeOptions{}, tenantId) if err != nil { log.Println(err) } @@ -71,10 +64,10 @@ func SendSMS_SMS_461790263(phoneNumber int, level, name, time, tenantId string) return err } -func SendSMS(phoneNumberStr string, codeStr []byte, runtime *util.RuntimeOptions, tenantId string) (err error) { +func SendSMS(notificateType int, phoneNumberStr string, codeStr []byte, runtime *util.RuntimeOptions, tenantId string) (err error) { // 查找当前开启的SMS服务配置 - c, err := models.NotificationConfigByNoticeTypeAndStatus(models.NotificationConfigType_Message, models.NotificationSwitch_Open) + c, err := models.NotificationConfigByNoticeTypeAndStatus(notificateType, models.NotificationSwitch_Open) if err != nil { return err } @@ -85,11 +78,22 @@ func SendSMS(phoneNumberStr string, codeStr []byte, runtime *util.RuntimeOptions json.Unmarshal([]byte(c.Config), &aliConfig) } - request := &dysmsapi20170525.SendSmsRequest{ - PhoneNumbers: tea.String(phoneNumberStr), - SignName: tea.String(aliConfig.SignName), - TemplateCode: tea.String(aliConfig.TemplateCode), - TemplateParam: tea.String(string(codeStr)), + var request *dysmsapi20170525.SendSmsRequest + + if notificateType == models.NotificationConfigType_Message { + request = &dysmsapi20170525.SendSmsRequest{ + PhoneNumbers: tea.String(phoneNumberStr), + SignName: tea.String(aliConfig.SignName), + TemplateCode: tea.String(aliConfig.TemplateCode), + TemplateParam: tea.String(string(codeStr)), + } + } else if notificateType == models.NotificationConfigType_VerificationCode { + request = &dysmsapi20170525.SendSmsRequest{ + PhoneNumbers: tea.String(phoneNumberStr), + SignName: tea.String("ThingsPanel"), + TemplateCode: tea.String("SMS_98355081"), + TemplateParam: tea.String(string(codeStr)), + } } client, err := CreateClient(tea.String(aliConfig.AccessKeyId), @@ -100,9 +104,9 @@ func SendSMS(phoneNumberStr string, codeStr []byte, runtime *util.RuntimeOptions sendRes, err := client.SendSmsWithOptions(request, &util.RuntimeOptions{}) if err != nil { - models.SaveNotificationHistory(utils.GetUuid(), *request.TemplateParam, *request.PhoneNumbers, models.NotificationSendFail, models.NotificationConfigType_Message, tenantId) + models.SaveNotificationHistory(utils.GetUuid(), *request.TemplateParam, *request.PhoneNumbers, models.NotificationSendFail, notificateType, tenantId) } else { - models.SaveNotificationHistory(utils.GetUuid(), *request.TemplateParam, *request.PhoneNumbers, models.NotificationSendSuccess, models.NotificationConfigType_Message, tenantId) + models.SaveNotificationHistory(utils.GetUuid(), *request.TemplateParam, *request.PhoneNumbers, models.NotificationSendSuccess, notificateType, tenantId) } // 记录数据库 log.Println(sendRes.Body) diff --git a/models/third_party_cloud_services_config.go b/models/third_party_cloud_services_config.go index a89b6a46d..77f8feed7 100644 --- a/models/third_party_cloud_services_config.go +++ b/models/third_party_cloud_services_config.go @@ -5,8 +5,9 @@ import ( ) const ( - NotificationConfigType_Message = 1 - NotificationConfigType_Email = 2 + NotificationConfigType_Message = 1 // 短信告警信息 + NotificationConfigType_Email = 2 + NotificationConfigType_VerificationCode = 3 // 短信验证码 ) const ( diff --git a/services/tp_notification.go b/services/tp_notification.go index bcd5cced3..d39ca3602 100644 --- a/services/tp_notification.go +++ b/services/tp_notification.go @@ -212,9 +212,9 @@ func BatchGetNotificationGroups(id []string) ([]models.TpNotificationGroups, err return groupInfo, err } -func (*TpNotificationService) SaveNotificationConfigAli(config models.CloudServicesConfig_Ali, status int) (err error) { +func (*TpNotificationService) SaveNotificationConfigAli(noticeType int, config models.CloudServicesConfig_Ali, status int) (err error) { - exists, err := GetThirdPartyCloudServicesConfigByNoticeType(models.NotificationConfigType_Message) + exists, err := GetThirdPartyCloudServicesConfigByNoticeType(noticeType) if err != nil { return err } @@ -225,7 +225,7 @@ func (*TpNotificationService) SaveNotificationConfigAli(config models.CloudServi } t := models.ThirdPartyCloudServicesConfig{ Id: utils.GetUuid(), - NoticeType: models.NotificationConfigType_Message, + NoticeType: noticeType, Config: string(configStr), Status: status, } @@ -243,7 +243,7 @@ func (*TpNotificationService) SaveNotificationConfigAli(config models.CloudServi if tmp.CloudType == models.NotificationCloudType_Ali { t := models.ThirdPartyCloudServicesConfig{ Id: v.Id, - NoticeType: models.NotificationConfigType_Message, + NoticeType: noticeType, Config: string(configStr), Status: status, }