package myAliSms import ( "encoding/json" "errors" "github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi" ) type Client struct { accessKeyId string accessSecret string signName string templateCode string } func NewClient(accessKeyId string, accessSecret string) *Client { return &Client{accessKeyId: accessKeyId, accessSecret: accessSecret} } func (t *Client) SetSignName(signName string) *Client { t.signName = signName return t } func (t *Client) SetTemplateCode(code string) *Client { t.templateCode = code return t } func (t *Client) SendSms(m map[string]interface{}, phone string) error { if t.signName == "" { return errors.New("签名不能为空") } client, err := dysmsapi.NewClientWithAccessKey("cn-hangzhou", t.accessKeyId, t.accessSecret) if err != nil { return err } request := dysmsapi.CreateSendSmsRequest() request.Scheme = "https" request.PhoneNumbers = phone request.SignName = t.signName request.TemplateCode = t.templateCode request.TemplateParam = t.mapToJson(m) response, err := client.SendSms(request) if err != nil { return err } else { if response.Code == "OK" { return nil } else { return errors.New(response.Message) } } } func (t *Client) mapToJson(TemplateParamMap map[string]interface{}) string { mjson, _ := json.Marshal(TemplateParamMap) return string(mjson) }