Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request ThingsPanel#215 from November-12/main
Browse files Browse the repository at this point in the history
add
  • Loading branch information
November-12 authored Sep 25, 2023
2 parents 526e758 + ee83a98 commit 3cc2921
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 42 deletions.
18 changes: 10 additions & 8 deletions controllers/tp_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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))
}
Expand Down Expand Up @@ -452,19 +452,21 @@ 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 {
response.SuccessWithMessage(400, "租户ID获取失败", (*context2.Context)(c.Ctx))
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))
Expand Down
60 changes: 32 additions & 28 deletions initialize/send_message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -63,18 +56,18 @@ 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)
}

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
}
Expand All @@ -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),
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions models/third_party_cloud_services_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
)

const (
NotificationConfigType_Message = 1
NotificationConfigType_Email = 2
NotificationConfigType_Message = 1 // 短信告警信息
NotificationConfigType_Email = 2
NotificationConfigType_VerificationCode = 3 // 短信验证码
)

const (
Expand Down
8 changes: 4 additions & 4 deletions services/tp_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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,
}
Expand All @@ -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,
}
Expand Down

0 comments on commit 3cc2921

Please sign in to comment.