sms.go 4.3 KB

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