sms.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package utils
  2. import (
  3. "encoding/json"
  4. "github.com/go-resty/resty/v2"
  5. )
  6. const (
  7. SUCCESS = "success"
  8. )
  9. type SMS struct {
  10. Appid string
  11. Signature string
  12. }
  13. func NewSMS(appid, signature string) *SMS {
  14. return &SMS{
  15. Appid: appid,
  16. Signature: signature,
  17. }
  18. }
  19. type SendRes struct {
  20. Status string `json:"status"`
  21. Send_id string `json:"send_id"`
  22. Fee int `json:"fee"`
  23. Msg string `json:"msg"`
  24. Code int `json:"code"`
  25. }
  26. // Send 短信发送
  27. func (t *SMS) Send(to, content string) (SendRes, error) {
  28. client := resty.New()
  29. resp, err := client.R().
  30. SetHeader("Content-Type", "application/x-www-form-urlencoded").
  31. SetFormData(map[string]string{
  32. "appid": t.Appid,
  33. "signature": t.Signature,
  34. "to": to,
  35. "content": content,
  36. }).
  37. SetResult(&SendRes{}).
  38. Post("https://api-v4.mysubmail.com/sms/send.json")
  39. if err != nil {
  40. return SendRes{}, err
  41. }
  42. temp := SendRes{}
  43. if err = json.Unmarshal(resp.Body(), &temp); err != nil {
  44. return SendRes{}, err
  45. }
  46. return temp, nil
  47. }