sms.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/services/alarm_msg"
  6. "eta/eta_api/utils"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. "net/url"
  11. )
  12. // SendSmsCode 发送国内短信
  13. func SendSmsCode(mobile, vCode, tplId string) (flag bool) {
  14. if mobile == "" || vCode == "" || tplId == "" {
  15. return
  16. }
  17. var (
  18. err error
  19. res string
  20. )
  21. defer func() {
  22. if err != nil {
  23. tips := fmt.Sprintf("短信验证码发送失败, Err: %s; Result: %s", err.Error(), res)
  24. utils.FileLog.Info("%s", tips)
  25. go alarm_msg.SendAlarmMsg(tips, 2)
  26. }
  27. }()
  28. // 获取配置好的短信模版
  29. smsCond := ` AND conf_key = ? `
  30. smsPars := make([]interface{}, 0)
  31. smsPars = append(smsPars, "SmsJhgnAppKey")
  32. conf := new(models.BusinessConf)
  33. conf, e := conf.GetItemByCondition(smsCond, smsPars)
  34. if e != nil {
  35. if e.Error() == utils.ErrNoRow() {
  36. err = fmt.Errorf("请先配置聚合短信Appkey")
  37. return
  38. }
  39. err = fmt.Errorf("获取聚合短信配置信息失败, Err: %s", e.Error())
  40. return
  41. }
  42. if conf.ConfVal == "" {
  43. err = fmt.Errorf("请先配置聚合短信Appkey")
  44. return
  45. }
  46. result, e := sendSms(conf.ConfVal, mobile, tplId, vCode)
  47. if e != nil {
  48. err = fmt.Errorf("send sms err: %s", e.Error())
  49. return
  50. }
  51. res = string(result)
  52. var netReturn map[string]interface{}
  53. if e = json.Unmarshal(result, &netReturn); e != nil {
  54. err = fmt.Errorf("json unmarshal err: %s", e.Error())
  55. return
  56. }
  57. errCode, ok := netReturn["error_code"].(float64)
  58. if !ok {
  59. err = fmt.Errorf("result code err")
  60. return
  61. }
  62. // 忽略错误的手机号码这种错误
  63. if errCode != 0 && errCode != 205401 {
  64. err = fmt.Errorf("err code %f", errCode)
  65. return
  66. }
  67. // 发送成功
  68. if errCode == 0 {
  69. flag = true
  70. }
  71. return
  72. }
  73. // sendSms 发送国内短信
  74. func sendSms(jhGnAppKey, mobile, tplId, code string) (rs []byte, err error) {
  75. var Url *url.URL
  76. apiURL := "http://v.juhe.cn/sms/send"
  77. //初始化参数
  78. param := url.Values{}
  79. //配置请求参数,方法内部已处理urlencode问题,中文参数可以直接传参
  80. param.Set("mobile", mobile) //接受短信的用户手机号码
  81. param.Set("tpl_id", tplId) //您申请的短信模板ID,根据实际情况修改
  82. tplVal := fmt.Sprintf(`#code#=%s&#m#=%d`, code, utils.VerifyCodeExpireMinute)
  83. param.Set("tpl_value", tplVal) //您设置的模板变量,根据实际情况
  84. param.Set("key", 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, tplId string) bool {
  102. var err error
  103. defer func() {
  104. if err != nil {
  105. tips := fmt.Sprintf("短信验证码发送失败, Err: %s", err.Error())
  106. utils.FileLog.Info("%s", tips)
  107. go alarm_msg.SendAlarmMsg(tips, 2)
  108. }
  109. }()
  110. // 获取配置好的短信模版
  111. smsCond := ` AND conf_key = ? `
  112. smsPars := make([]interface{}, 0)
  113. smsPars = append(smsPars, "SmsJhgjAppKey")
  114. conf := new(models.BusinessConf)
  115. conf, e := conf.GetItemByCondition(smsCond, smsPars)
  116. if e != nil {
  117. if e.Error() == utils.ErrNoRow() {
  118. err = fmt.Errorf("请先配置聚合短信Appkey")
  119. return false
  120. }
  121. err = fmt.Errorf("获取聚合短信配置信息失败, Err: %s", e.Error())
  122. return false
  123. }
  124. if conf.ConfVal == "" {
  125. err = fmt.Errorf("请先配置聚合短信Appkey")
  126. return false
  127. }
  128. result, err := sendSmsGj(conf.ConfVal, mobile, vCode, areaNum, tplId)
  129. if err != nil {
  130. fmt.Println("发送短信失败")
  131. return false
  132. }
  133. fmt.Println("result", string(result))
  134. var netReturn map[string]interface{}
  135. err = json.Unmarshal(result, &netReturn)
  136. if err != nil {
  137. err = fmt.Errorf("短信验证码发送失败, Err:" + err.Error() + ";Result:" + string(result))
  138. return false
  139. }
  140. if netReturn["error_code"].(float64) == 0 {
  141. fmt.Printf("接口返回result字段是:\r\n%v", netReturn["result"])
  142. return true
  143. } else {
  144. // 忽略错误的手机号码这种错误
  145. if netReturn["error_code"].(float64) != 205401 {
  146. err = fmt.Errorf("短信验证码发送失败, Result:" + string(result))
  147. }
  148. return false
  149. }
  150. }
  151. // sendSmsGj 发送国际短信
  152. func sendSmsGj(jhGjAppKey, mobile, code, areaNum, tplId string) (rs []byte, err error) {
  153. var Url *url.URL
  154. apiURL := "http://v.juhe.cn/smsInternational/send.php"
  155. //初始化参数
  156. param := url.Values{}
  157. //配置请求参数,方法内部已处理urlencode问题,中文参数可以直接传参
  158. param.Set("mobile", mobile) //接受短信的用户手机号码
  159. //param.Set("tplId", "10054") //您申请的短信模板ID,根据实际情况修改
  160. param.Set("tplId", tplId) //您申请的短信模板ID,根据实际情况修改
  161. param.Set("tplValue", "#code#="+code) //您设置的模板变量,根据实际情况
  162. param.Set("key", jhGjAppKey) //应用APPKEY(应用详细页查询)
  163. param.Set("areaNum", areaNum) //应用APPKEY(应用详细页查询)
  164. Url, err = url.Parse(apiURL)
  165. if err != nil {
  166. fmt.Printf("解析url错误:\r\n%v", err)
  167. return nil, err
  168. }
  169. //如果参数中有中文参数,这个方法会进行URLEncode
  170. Url.RawQuery = param.Encode()
  171. resp, err := http.Get(Url.String())
  172. if err != nil {
  173. fmt.Println("err:", err)
  174. return nil, err
  175. }
  176. utils.FileLog.Info("sendSmsGj:param:" + Url.String())
  177. defer resp.Body.Close()
  178. body, err := ioutil.ReadAll(resp.Body)
  179. utils.FileLog.Info("sendSmsGj:result:" + string(body))
  180. return body, err
  181. }