sms.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package services
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_mobile_admin/services/alarm_msg"
  6. "hongze/hongze_mobile_admin/utils"
  7. "io/ioutil"
  8. "net/http"
  9. "net/url"
  10. )
  11. const SmsExpireMinute = 15
  12. func SendSmsCode(mobile, vCode string) bool {
  13. //tplId := "65692"
  14. tplId := "254663"
  15. result, err := sendSms(mobile, tplId, vCode)
  16. if err != nil {
  17. fmt.Println("发送短信失败")
  18. return false
  19. }
  20. fmt.Println("result", string(result))
  21. var netReturn map[string]interface{}
  22. err = json.Unmarshal(result, &netReturn)
  23. if err != nil {
  24. //go SendEmail("短信验证码发送失败", "err:"+err.Error()+" result"+string(result), utils.EmailSendToUsers)
  25. go alarm_msg.SendAlarmMsg("短信验证码发送失败, Err:"+err.Error()+";Result:"+string(result), 2)
  26. return false
  27. }
  28. if netReturn["error_code"].(float64) == 0 {
  29. fmt.Printf("接口返回result字段是:\r\n%v", netReturn["result"])
  30. return true
  31. } else {
  32. // 忽略错误的手机号码这种错误
  33. if netReturn["error_code"].(float64) != 205401 {
  34. go alarm_msg.SendAlarmMsg("短信验证码发送失败, Result:"+string(result), 2)
  35. }
  36. return false
  37. }
  38. }
  39. func sendSms(mobile, tplId, code string) (rs []byte, err error) {
  40. var Url *url.URL
  41. apiURL := "http://v.juhe.cn/sms/send"
  42. //初始化参数
  43. param := url.Values{}
  44. //配置请求参数,方法内部已处理urlencode问题,中文参数可以直接传参
  45. param.Set("mobile", mobile) //接受短信的用户手机号码
  46. param.Set("tpl_id", tplId) //您申请的短信模板ID,根据实际情况修改
  47. tplVal := fmt.Sprintf(`#code#=%s&#m#=%d`, code, SmsExpireMinute)
  48. param.Set("tpl_value", tplVal) //您设置的模板变量,根据实际情况
  49. param.Set("key", utils.JhGnAppKey) //应用APPKEY(应用详细页查询)
  50. Url, err = url.Parse(apiURL)
  51. if err != nil {
  52. fmt.Printf("解析url错误:\r\n%v", err)
  53. return nil, err
  54. }
  55. //如果参数中有中文参数,这个方法会进行URLEncode
  56. Url.RawQuery = param.Encode()
  57. resp, err := http.Get(Url.String())
  58. if err != nil {
  59. fmt.Println("err:", err)
  60. return nil, err
  61. }
  62. defer resp.Body.Close()
  63. return ioutil.ReadAll(resp.Body)
  64. }
  65. // SendSmsCodeGj 发送国际短信
  66. func SendSmsCodeGj(mobile, vCode, areaNum string) bool {
  67. result, err := sendSmsGj(mobile, vCode, areaNum)
  68. if err != nil {
  69. fmt.Println("发送短信失败")
  70. return false
  71. }
  72. fmt.Println("result", string(result))
  73. var netReturn map[string]interface{}
  74. err = json.Unmarshal(result, &netReturn)
  75. if err != nil {
  76. //go SendEmail("短信验证码发送失败", "err:"+err.Error()+" result"+string(result), utils.EmailSendToUsers)
  77. go alarm_msg.SendAlarmMsg("短信验证码发送失败, Err:"+err.Error()+";Result:"+string(result), 2)
  78. return false
  79. }
  80. if netReturn["error_code"].(float64) == 0 {
  81. fmt.Printf("接口返回result字段是:\r\n%v", netReturn["result"])
  82. return true
  83. } else {
  84. // 忽略错误的手机号码这种错误
  85. if netReturn["error_code"].(float64) != 205401 {
  86. go alarm_msg.SendAlarmMsg("短信验证码发送失败, Result:"+string(result), 2)
  87. }
  88. return false
  89. }
  90. }
  91. // sendSmsGj 发送国际短信
  92. func sendSmsGj(mobile, code, areaNum string) (rs []byte, err error) {
  93. var Url *url.URL
  94. apiURL := "http://v.juhe.cn/smsInternational/send.php"
  95. //初始化参数
  96. param := url.Values{}
  97. //配置请求参数,方法内部已处理urlencode问题,中文参数可以直接传参
  98. param.Set("mobile", mobile) //接受短信的用户手机号码
  99. param.Set("tplId", "10054") //您申请的短信模板ID,根据实际情况修改
  100. param.Set("tplValue", "#code#="+code) //您设置的模板变量,根据实际情况
  101. param.Set("key", utils.JhGjAppKey) //应用APPKEY(应用详细页查询)
  102. param.Set("areaNum", areaNum) //应用APPKEY(应用详细页查询)
  103. Url, err = url.Parse(apiURL)
  104. if err != nil {
  105. fmt.Printf("解析url错误:\r\n%v", err)
  106. return nil, err
  107. }
  108. //如果参数中有中文参数,这个方法会进行URLEncode
  109. Url.RawQuery = param.Encode()
  110. resp, err := http.Get(Url.String())
  111. if err != nil {
  112. fmt.Println("err:", err)
  113. return nil, err
  114. }
  115. defer resp.Body.Close()
  116. return ioutil.ReadAll(resp.Body)
  117. }