1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- package sms
- import (
- "encoding/json"
- "github.com/go-resty/resty/v2"
- )
- const (
- SUCCESS = "success"
- )
- type SMS struct {
- Appid string
- Signature string
- }
- func NewSMS(appid, signature string) *SMS {
- return &SMS{
- Appid: appid,
- Signature: signature,
- }
- }
- type SendRes struct {
- Status string `json:"status"`
- Send_id string `json:"send_id"`
- Fee int `json:"fee"`
- Msg string `json:"msg"`
- Code int `json:"code"`
- }
- type XSendRes struct {
- Status string `json:"status"`
- Send_id string `json:"send_id"`
- Fee float64 `json:"fee"`
- Msg string `json:"msg"`
- Code string `json:"code"`
- }
- // 短信发送
- func (t *SMS) Send(to, content string) (SendRes, error) {
- client := resty.New()
- resp, err := client.R().
- SetHeader("Content-Type", "application/x-www-form-urlencoded").
- SetFormData(map[string]string{
- "appid": t.Appid,
- "signature": t.Signature,
- "to": to,
- "content": content,
- }).
- SetResult(&SendRes{}).
- Post("https://api-v4.mysubmail.com/sms/send.json")
- if err != nil {
- return SendRes{}, err
- }
- temp := SendRes{}
- if err = json.Unmarshal(resp.Body(), &temp); err != nil {
- return SendRes{}, err
- }
- return temp, nil
- }
- // 短信模板发送
- func (t *SMS) SmsXSend(to, addr string) (XSendRes, error) {
- type Vars struct {
- Addr string `json:"addr"`
- }
- vars := Vars{Addr: addr}
- b, _ := json.Marshal(vars)
- client := resty.New()
- resp, err := client.R().
- SetHeader("Content-Type", "application/x-www-form-urlencoded").
- SetFormData(map[string]string{
- "appid": t.Appid,
- "signature": t.Signature,
- "to": to,
- "project": "ZZ7fYr",
- "vars": string(b),
- }).
- SetResult(&XSendRes{}).
- Post("https://api-v4.mysubmail.com/sms/xsend")
- if err != nil {
- return XSendRes{}, err
- }
- temp := XSendRes{}
- if err = json.Unmarshal(resp.Body(), &temp); err != nil {
- return XSendRes{}, err
- }
- return temp, nil
- }
|