sms.go 4.0 KB

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