fix: aliyunsms send error
This commit is contained in:
@@ -7,10 +7,7 @@ import (
|
||||
"message-nest/service/send_ins_service"
|
||||
"message-nest/service/send_way_service"
|
||||
|
||||
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
|
||||
dysmsapi "github.com/alibabacloud-go/dysmsapi-20170525/v4/client"
|
||||
util "github.com/alibabacloud-go/tea-utils/v2/service"
|
||||
"github.com/alibabacloud-go/tea/tea"
|
||||
"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
|
||||
)
|
||||
|
||||
type AliyunSMSChannel struct{ *BaseChannel }
|
||||
@@ -43,7 +40,7 @@ func (c *AliyunSMSChannel) SendUnified(msgObj interface{}, ins models.SendTasksI
|
||||
}
|
||||
|
||||
// 创建阿里云短信客户端
|
||||
client, err := c.createClient(auth.AccessKeyId, auth.AccessKeySecret)
|
||||
client, err := c.createClient(auth.AccessKeyId, auth.AccessKeySecret, auth.RegionId)
|
||||
if err != nil {
|
||||
return "", fmt.Sprintf("创建阿里云短信客户端失败: %s", err.Error())
|
||||
}
|
||||
@@ -60,47 +57,34 @@ func (c *AliyunSMSChannel) SendUnified(msgObj interface{}, ins models.SendTasksI
|
||||
|
||||
templateParamJSON, _ := json.Marshal(templateParam)
|
||||
|
||||
// 创建请求对象
|
||||
request := dysmsapi.CreateSendSmsRequest()
|
||||
request.Scheme = "https" // 使用HTTPS协议
|
||||
|
||||
// 设置请求参数
|
||||
request.PhoneNumbers = config.PhoneNumber // 接收短信的手机号码
|
||||
request.SignName = auth.SignName // 短信签名名称
|
||||
request.TemplateCode = config.TemplateCode // 短信模板ID
|
||||
request.TemplateParam = string(templateParamJSON) // 短信模板变量对应的实际值,JSON格式
|
||||
|
||||
// 发送短信
|
||||
sendSmsRequest := &dysmsapi.SendSmsRequest{
|
||||
PhoneNumbers: tea.String(config.PhoneNumber),
|
||||
SignName: tea.String(auth.SignName),
|
||||
TemplateCode: tea.String(config.TemplateCode),
|
||||
TemplateParam: tea.String(string(templateParamJSON)),
|
||||
response, err := client.SendSms(request)
|
||||
if err != nil {
|
||||
return "", fmt.Sprintf("发送短信失败: %s", err.Error())
|
||||
}
|
||||
|
||||
runtime := &util.RuntimeOptions{}
|
||||
tryErr := func() error {
|
||||
defer func() {
|
||||
if r := tea.Recover(recover()); r != nil {
|
||||
// 处理panic
|
||||
}
|
||||
}()
|
||||
|
||||
response, _err := client.SendSmsWithOptions(sendSmsRequest, runtime)
|
||||
if _err != nil {
|
||||
return _err
|
||||
}
|
||||
|
||||
if response.Body.Code != nil && *response.Body.Code != "OK" {
|
||||
return fmt.Errorf("发送失败: %s - %s", tea.StringValue(response.Body.Code), tea.StringValue(response.Body.Message))
|
||||
}
|
||||
|
||||
return nil
|
||||
}()
|
||||
|
||||
if tryErr != nil {
|
||||
return "", tryErr.Error()
|
||||
// 处理响应
|
||||
if response.Code != "OK" {
|
||||
return "", fmt.Sprintf("发送失败: %s - %s", response.Code, response.Message)
|
||||
}
|
||||
|
||||
return "", ""
|
||||
return fmt.Sprintf("RequestId: %s, BizId: %s", response.RequestId, response.BizId), ""
|
||||
}
|
||||
|
||||
// createClient 创建阿里云短信客户端
|
||||
func (c *AliyunSMSChannel) createClient(accessKeyId, accessKeySecret string) (*dysmsapi.Client, error) {
|
||||
config := &openapi.Config{
|
||||
AccessKeyId: tea.String(accessKeyId),
|
||||
AccessKeySecret: tea.String(accessKeySecret),
|
||||
Endpoint: tea.String("dysmsapi.aliyuncs.com"),
|
||||
func (c *AliyunSMSChannel) createClient(accessKeyId, accessKeySecret, regionId string) (*dysmsapi.Client, error) {
|
||||
if regionId == "" {
|
||||
regionId = "cn-hangzhou"
|
||||
}
|
||||
return dysmsapi.NewClient(config)
|
||||
return dysmsapi.NewClientWithAccessKey(regionId, accessKeyId, accessKeySecret)
|
||||
}
|
||||
|
||||
@@ -221,6 +221,7 @@ type WayDetailAliyunSMS struct {
|
||||
AccessKeyId string `json:"access_key_id" validate:"required,max=100" label:"AccessKeyId"`
|
||||
AccessKeySecret string `json:"access_key_secret" validate:"required,max=100" label:"AccessKeySecret"`
|
||||
SignName string `json:"sign_name" validate:"required,max=50" label:"短信签名"`
|
||||
RegionId string `json:"region_id" validate:"required,max=50" label:"区域ID"`
|
||||
}
|
||||
|
||||
func (w *WayDetailAliyunSMS) Validate(authJson string) (string, interface{}) {
|
||||
|
||||
Reference in New Issue
Block a user