53 lines
1.3 KiB
Go
53 lines
1.3 KiB
Go
package sms
|
|
|
|
import (
|
|
"fmt"
|
|
"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)
|
|
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))
|
|
}
|
|
if err != nil {
|
|
logger.Error("SendSms failed : %v", err)
|
|
return
|
|
}
|
|
return
|
|
}
|