2023-12-21 22:17:40 +08:00
|
|
|
package sms
|
|
|
|
|
|
|
|
import (
|
2024-09-25 13:13:35 +08:00
|
|
|
"fmt"
|
2023-12-21 22:17:40 +08:00
|
|
|
"service/bizcommon/util"
|
|
|
|
"service/library/configcenter"
|
|
|
|
"service/library/logger"
|
|
|
|
|
|
|
|
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
|
|
|
|
dysmsapi "github.com/alibabacloud-go/dysmsapi-20170525/v3/client"
|
|
|
|
"github.com/alibabacloud-go/tea/tea"
|
|
|
|
)
|
|
|
|
|
|
|
|
var defaultDysmsapiClient *dysmsapi.Client
|
|
|
|
|
|
|
|
func Init(cfg *configcenter.DysmsapiConfig) (err error) {
|
|
|
|
|
|
|
|
config := &openapi.Config{
|
|
|
|
AccessKeyId: tea.String(cfg.AccessKeyId),
|
|
|
|
AccessKeySecret: tea.String(cfg.AccessKeySecret),
|
|
|
|
}
|
|
|
|
// 访问的域名
|
|
|
|
config.Endpoint = tea.String("dysmsapi.aliyuncs.com")
|
|
|
|
|
|
|
|
defaultDysmsapiClient, err = dysmsapi.NewClient(config)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("NewDysmsapiClient fail, cfg: %v, err: %v", util.ToJson(config), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func SendSms(request *dysmsapi.SendSmsRequest) (err error) {
|
|
|
|
_result, err := defaultDysmsapiClient.SendSms(request)
|
|
|
|
logger.Info("SendSms result:%v", _result)
|
2024-09-25 13:13:35 +08:00
|
|
|
if _result == nil || _result.Body == nil {
|
|
|
|
return fmt.Errorf("短信发送失败")
|
|
|
|
}
|
|
|
|
if util.DerefInt32(_result.StatusCode) != 200 {
|
|
|
|
return fmt.Errorf("短信发送失败")
|
|
|
|
}
|
|
|
|
if util.DerefString(_result.Body.Code) != "OK" {
|
|
|
|
return fmt.Errorf(util.DerefString(_result.Body.Message))
|
|
|
|
}
|
2023-12-21 22:17:40 +08:00
|
|
|
if err != nil {
|
|
|
|
logger.Error("SendSms failed : %v", err)
|
2024-09-25 13:13:35 +08:00
|
|
|
return
|
2023-12-21 22:17:40 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|