sms.go 4.9 KB

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