sms.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package util
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io/ioutil"
  8. "log"
  9. "net/http"
  10. "net/url"
  11. "strings"
  12. "time"
  13. )
  14. type SMSManager struct {
  15. username string
  16. password string
  17. apikey string
  18. }
  19. func NewSMSManager(username string, password string, apikey string) *SMSManager {
  20. return &SMSManager{
  21. username: username,
  22. password: password,
  23. apikey: apikey,
  24. }
  25. }
  26. var defaultSMSManager *SMSManager
  27. var internationalSMSManager *SMSManager
  28. var yingxiaoSMSManager *SMSManager
  29. func init() {
  30. defaultSMSManager = &SMSManager{
  31. username: "yundongmai",
  32. password: "asdf1234",
  33. apikey: "da06d4451dbcd1d08f4825c56d5d52e8",
  34. }
  35. internationalSMSManager = &SMSManager{
  36. username: "ydmgj",
  37. password: "asdf1234",
  38. apikey: "9dede1f8011b4d2dddcbaf69c893b4bb",
  39. }
  40. yingxiaoSMSManager = &SMSManager{
  41. username: "ydmyx",
  42. password: "asdf1234",
  43. apikey: "83526ef70935b1a50b8f377d6120b4d4",
  44. }
  45. }
  46. const SMS_API_URL = "http://m.5c.com.cn/api/send/index.php"
  47. func SendSMS(mobile string, content string) error {
  48. return defaultSMSManager.Send(mobile, content)
  49. }
  50. func SendWorldSMS(mobile string, content string) error {
  51. return internationalSMSManager.Send(mobile, content)
  52. }
  53. func SendYXSms(mobile string, content string) error {
  54. return yingxiaoSMSManager.Send(mobile, content)
  55. }
  56. func SendSMSByGet(mobile string, content string) error {
  57. return defaultSMSManager.SendByGet(mobile, content)
  58. }
  59. func (sms *SMSManager) Send(mobile string, content string) error {
  60. var params = map[string]interface{}{
  61. "type": "send",
  62. "username": sms.username,
  63. "password": sms.password,
  64. "apikey": sms.apikey,
  65. "mobile": mobile,
  66. "content": content,
  67. "encode": "utf-8",
  68. }
  69. jsonBytes, err := json.Marshal(params)
  70. if err != nil {
  71. return err
  72. }
  73. jsonBytes = bytes.Replace(jsonBytes, []byte("\\u0026"), []byte("&"), -1)
  74. jsonStr := string(jsonBytes)
  75. var sparams = url.Values{}
  76. sparams.Add("format", "json")
  77. sparams.Add("data", jsonStr)
  78. jsonStr = sparams.Encode()
  79. res, err := http.Post(SMS_API_URL, "application/x-www-form-urlencoded", strings.NewReader(jsonStr))
  80. if err != nil {
  81. return err
  82. }
  83. defer res.Body.Close()
  84. jsonBytes, err = ioutil.ReadAll(res.Body)
  85. if err != nil {
  86. return err
  87. }
  88. result := make(map[string]string)
  89. err = json.Unmarshal(jsonBytes, &result)
  90. if err != nil {
  91. log.Println(err, string(jsonBytes))
  92. return err
  93. }
  94. if result["status"] != "success" {
  95. return errors.New(result["msg"])
  96. }
  97. return nil
  98. }
  99. func (sms *SMSManager) SendByGet(mobile string, content string) error {
  100. var sparams = url.Values{
  101. "username": []string{sms.username},
  102. "password": []string{sms.password},
  103. "apikey": []string{sms.apikey},
  104. "mobile": []string{mobile},
  105. "content": []string{content},
  106. "startTime": []string{fmt.Sprintf("%d", time.Now().Unix())},
  107. "encode": []string{"utf-8"},
  108. }
  109. paramStr := sparams.Encode()
  110. res, err := http.Get(SMS_API_URL + "?" + paramStr)
  111. if err != nil {
  112. return err
  113. }
  114. defer res.Body.Close()
  115. jsonBytes, err := ioutil.ReadAll(res.Body)
  116. if err != nil {
  117. return err
  118. }
  119. result := string(jsonBytes)
  120. if !strings.HasPrefix(result, "success:") {
  121. return errors.New(result)
  122. }
  123. return nil
  124. }