分离pkg

This commit is contained in:
2024-12-18 03:39:12 +08:00
parent 6f28ddd69b
commit bfe2dd913c
43 changed files with 0 additions and 2957 deletions

View File

@ -1,61 +0,0 @@
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)
}