sms.go 4.3 KB

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