另客网go项目公用的代码库

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package notice
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. )
  10. // SendChineseSMS 发送国内手机验证码
  11. func SendChineseSMS(user, pass, mobile, content string) error {
  12. params := make(map[string]interface{})
  13. params["account"] = user
  14. params["password"] = pass
  15. params["phone"] = mobile
  16. params["msg"] = content
  17. params["report"] = true
  18. bytesData, err := json.Marshal(params)
  19. if err != nil {
  20. return err
  21. }
  22. reader := bytes.NewReader(bytesData)
  23. url := "http://smssh1.253.com/msg/send/json"
  24. request, err := http.NewRequest("POST", url, reader)
  25. if err != nil {
  26. return err
  27. }
  28. request.Header.Set("Content-Type", "application/json;charset=UTF-8")
  29. client := http.Client{}
  30. resp, err := client.Do(request)
  31. if err != nil {
  32. return err
  33. }
  34. defer resp.Body.Close()
  35. respBytes, err := ioutil.ReadAll(resp.Body)
  36. if err != nil {
  37. return err
  38. }
  39. result := struct {
  40. Code string `json:"code"`
  41. MsgID string `json:"msgId"`
  42. Time string `json:"time"`
  43. ErrorMsg string `json:"errorMsg"`
  44. }{}
  45. err = json.Unmarshal(respBytes, &result)
  46. if err != nil {
  47. return err
  48. }
  49. if result.Code != "0" {
  50. switch result.Code {
  51. case "103":
  52. return fmt.Errorf("one verification code per minute per phone")
  53. case "107":
  54. return fmt.Errorf("phone number format is not correct")
  55. case "135":
  56. return fmt.Errorf("up to 5 verification codes a day")
  57. default:
  58. return fmt.Errorf(result.ErrorMsg)
  59. }
  60. }
  61. return nil
  62. }
  63. // SendForeignSMS 发送国外手机验证码
  64. func SendForeignSMS(user, pass, mobile, content string) error {
  65. params := make(map[string]interface{})
  66. params["account"] = user
  67. params["password"] = pass
  68. params["mobile"] = mobile
  69. params["msg"] = content
  70. params["report"] = true
  71. bytesData, err := json.Marshal(params)
  72. if err != nil {
  73. return err
  74. }
  75. reader := bytes.NewReader(bytesData)
  76. url := "http://intapi.253.com/send/json"
  77. request, err := http.NewRequest("POST", url, reader)
  78. if err != nil {
  79. return err
  80. }
  81. request.Header.Set("Content-Type", "application/json;charset=UTF-8")
  82. client := http.Client{}
  83. resp, err := client.Do(request)
  84. if err != nil {
  85. return err
  86. }
  87. defer resp.Body.Close()
  88. respBytes, err := ioutil.ReadAll(resp.Body)
  89. if err != nil {
  90. return err
  91. }
  92. result := struct {
  93. Code string `json:"code"`
  94. Error string `json:"error"`
  95. MsgID string `json:"msgid"`
  96. }{}
  97. err = json.Unmarshal(respBytes, &result)
  98. if err != nil {
  99. return err
  100. }
  101. if result.Code != "0" {
  102. return errors.New(result.Error)
  103. }
  104. return nil
  105. }