sms.go 3.9 KB

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