sms.go 3.6 KB

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