sms.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/services/alarm_msg"
  6. "eta_gn/eta_api/utils"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. "net/url"
  11. "strings"
  12. )
  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. smsCond := ` AND conf_key = ? `
  29. smsPars := make([]interface{}, 0)
  30. smsPars = append(smsPars, "SmsJhgnAppKey")
  31. conf := new(models.BusinessConf)
  32. conf, e := conf.GetItemByCondition(smsCond, smsPars)
  33. if e != nil {
  34. if utils.IsErrNoRow(e) {
  35. err = fmt.Errorf("请先配置聚合短信Appkey")
  36. return
  37. }
  38. err = fmt.Errorf("获取聚合短信配置信息失败, Err: %s", e.Error())
  39. return
  40. }
  41. if conf.ConfVal == "" {
  42. err = fmt.Errorf("请先配置聚合短信Appkey")
  43. return
  44. }
  45. result, e := sendSms(conf.ConfVal, mobile, tplId, vCode)
  46. if e != nil {
  47. err = fmt.Errorf("send sms err: %s", e.Error())
  48. return
  49. }
  50. res = string(result)
  51. var netReturn map[string]interface{}
  52. if e = json.Unmarshal(result, &netReturn); e != nil {
  53. err = fmt.Errorf("json unmarshal err: %s", e.Error())
  54. return
  55. }
  56. errCode, ok := netReturn["error_code"].(float64)
  57. if !ok {
  58. err = fmt.Errorf("result code err")
  59. return
  60. }
  61. if errCode != 0 && errCode != 205401 {
  62. err = fmt.Errorf("err code %f", errCode)
  63. return
  64. }
  65. if errCode == 0 {
  66. flag = true
  67. }
  68. return
  69. }
  70. func sendSms(jhGnAppKey, mobile, tplId, code string) (rs []byte, err error) {
  71. var Url *url.URL
  72. apiURL := "http://v.juhe.cn/sms/send"
  73. param := url.Values{}
  74. param.Set("mobile", mobile) //接受短信的用户手机号码
  75. param.Set("tpl_id", tplId) //您申请的短信模板ID,根据实际情况修改
  76. tplVal := fmt.Sprintf(`#code#=%s&#m#=%d`, code, utils.VerifyCodeExpireMinute)
  77. param.Set("tpl_value", tplVal) //您设置的模板变量,根据实际情况
  78. param.Set("key", jhGnAppKey) //应用APPKEY(应用详细页查询)
  79. Url, err = url.Parse(apiURL)
  80. if err != nil {
  81. fmt.Printf("解析url错误:\r\n%v", err)
  82. return nil, err
  83. }
  84. Url.RawQuery = param.Encode()
  85. resp, err := http.Get(Url.String())
  86. if err != nil {
  87. fmt.Println("err:", err)
  88. return nil, err
  89. }
  90. defer resp.Body.Close()
  91. return ioutil.ReadAll(resp.Body)
  92. }
  93. func sendSmsGj(jhGjAppKey, mobile, code, areaNum, tplId, tplValue string) (rs []byte, err error) {
  94. var Url *url.URL
  95. apiURL := "http://v.juhe.cn/smsInternational/send.php"
  96. param := url.Values{}
  97. param.Set("mobile", mobile) //接受短信的用户手机号码
  98. param.Set("tplId", tplId) //您申请的短信模板ID,根据实际情况修改
  99. if strings.Contains(tplValue, "#code#=%s") {
  100. tplValue = strings.Replace(tplValue, "#code#=%s", "#code#="+code, 1)
  101. }
  102. if strings.Contains(tplValue, "#m#=%d") {
  103. tplValue = strings.Replace(tplValue, "#m#=%d", fmt.Sprintf("#m#=%d", utils.VerifyCodeExpireMinute), 1)
  104. }
  105. param.Set("tplValue", tplValue) //您设置的模板变量,根据实际情况
  106. param.Set("key", jhGjAppKey) //应用APPKEY(应用详细页查询)
  107. param.Set("areaNum", areaNum) //应用APPKEY(应用详细页查询)
  108. Url, err = url.Parse(apiURL)
  109. if err != nil {
  110. fmt.Printf("解析url错误:\r\n%v", err)
  111. return nil, err
  112. }
  113. Url.RawQuery = param.Encode()
  114. resp, err := http.Get(Url.String())
  115. if err != nil {
  116. fmt.Println("err:", err)
  117. return nil, err
  118. }
  119. utils.FileLog.Info("sendSmsGj:param:" + Url.String())
  120. defer resp.Body.Close()
  121. body, err := ioutil.ReadAll(resp.Body)
  122. utils.FileLog.Info("sendSmsGj:result:" + string(body))
  123. return body, err
  124. }
  125. type SmsClient interface {
  126. SendUserLoginCode(UserLoginSmsCodeReq) (UserLoginSmsCodeResult, error)
  127. }
  128. func NewSmsClient() (cli SmsClient, err error) {
  129. confMap, e := models.GetBusinessConf()
  130. if e != nil {
  131. err = fmt.Errorf("GetBusinessConf err: %s", e.Error())
  132. return
  133. }
  134. if confMap[models.BusinessConfSmsClient] == models.BusinessConfClientFlagNanHua {
  135. return new(NanHuaSms), nil
  136. }
  137. return new(HzSms), nil
  138. }
  139. type HzSms struct{}
  140. type UserLoginSmsCodeReq struct {
  141. TelAreaCode string `description:"区号"`
  142. Mobile string `description:"手机号"`
  143. VerifyCode string `description:"短信验证码"`
  144. }
  145. type UserLoginSmsCodeResult struct {
  146. Success bool `description:"发送是否成功"`
  147. Message string `description:"提示信息"`
  148. RequestId string `description:"发送ID,用于回查发送状态"`
  149. }
  150. func (cli *HzSms) SendUserLoginCode(req UserLoginSmsCodeReq) (result UserLoginSmsCodeResult, err error) {
  151. if req.Mobile == "" || req.VerifyCode == "" {
  152. err = fmt.Errorf("参数有误, Mobile: %s, VerifyCode: %s", req.Mobile, req.VerifyCode)
  153. return
  154. }
  155. confMap, e := models.GetBusinessConf()
  156. if e != nil {
  157. err = fmt.Errorf("GetBusinessConf err: %s", e.Error())
  158. return
  159. }
  160. var netReturn map[string]interface{}
  161. if req.TelAreaCode == utils.TelAreaCodeHome {
  162. tplId := confMap[models.BusinessConfLoginSmsTpId]
  163. if tplId == "" {
  164. err = fmt.Errorf("请先配置短信模板")
  165. return
  166. }
  167. appKey := confMap[models.BusinessConfSmsJhgnAppKey]
  168. if appKey == "" {
  169. err = fmt.Errorf("请先配置聚合短信AppKey")
  170. return
  171. }
  172. smsRes, e := sendSms(appKey, req.Mobile, tplId, req.VerifyCode)
  173. if e != nil {
  174. err = fmt.Errorf("send sms err: %s", e.Error())
  175. return
  176. }
  177. if e = json.Unmarshal(smsRes, &netReturn); e != nil {
  178. err = fmt.Errorf("json unmarshal err: %s", e.Error())
  179. return
  180. }
  181. }
  182. if req.TelAreaCode != utils.TelAreaCodeHome {
  183. tplId := confMap[models.BusinessConfLoginSmsGjTpId]
  184. if tplId == "" {
  185. err = fmt.Errorf("请先配置短信模板")
  186. return
  187. }
  188. appKey := confMap[models.BusinessConfSmsJhgjAppKey]
  189. if appKey == "" {
  190. err = fmt.Errorf("请先配置聚合短信AppKey")
  191. return
  192. }
  193. tplValue := confMap[models.BusinessConfSmsJhgjVariable]
  194. if tplValue == "" {
  195. tplValue = "#code#=%s"
  196. }
  197. smsRes, e := sendSmsGj(appKey, req.Mobile, req.VerifyCode, req.TelAreaCode, tplId, tplValue)
  198. if e != nil {
  199. err = fmt.Errorf("send gj sms err: %s", e.Error())
  200. return
  201. }
  202. if e = json.Unmarshal(smsRes, &netReturn); e != nil {
  203. err = fmt.Errorf("json unmarshal err: %s", e.Error())
  204. return
  205. }
  206. }
  207. errCode, ok := netReturn["error_code"].(float64)
  208. if !ok {
  209. err = fmt.Errorf("result code err")
  210. return
  211. }
  212. if errCode != 0 && errCode != 205401 {
  213. err = fmt.Errorf("err code %f", errCode)
  214. return
  215. }
  216. if errCode == 0 {
  217. result.Success = true
  218. }
  219. return
  220. }