sms.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. // SendSmsCode 发送国内短信
  14. func SendSmsCode(mobile, vCode, tplId string) (flag bool) {
  15. if mobile == "" || vCode == "" || tplId == "" {
  16. return
  17. }
  18. var (
  19. err error
  20. res string
  21. )
  22. defer func() {
  23. if err != nil {
  24. tips := fmt.Sprintf("短信验证码发送失败, Err: %s; Result: %s", err.Error(), res)
  25. utils.FileLog.Info("%s", tips)
  26. go alarm_msg.SendAlarmMsg(tips, 2)
  27. }
  28. }()
  29. // 获取配置好的短信模版
  30. smsCond := ` AND conf_key = ? `
  31. smsPars := make([]interface{}, 0)
  32. smsPars = append(smsPars, "SmsJhgnAppKey")
  33. conf := new(models.BusinessConf)
  34. conf, e := conf.GetItemByCondition(smsCond, smsPars)
  35. if e != nil {
  36. if utils.IsErrNoRow(e) {
  37. err = fmt.Errorf("请先配置聚合短信Appkey")
  38. return
  39. }
  40. err = fmt.Errorf("获取聚合短信配置信息失败, Err: %s", e.Error())
  41. return
  42. }
  43. if conf.ConfVal == "" {
  44. err = fmt.Errorf("请先配置聚合短信Appkey")
  45. return
  46. }
  47. result, e := sendSms(conf.ConfVal, mobile, tplId, vCode)
  48. if e != nil {
  49. err = fmt.Errorf("send sms err: %s", e.Error())
  50. return
  51. }
  52. res = string(result)
  53. var netReturn map[string]interface{}
  54. if e = json.Unmarshal(result, &netReturn); e != nil {
  55. err = fmt.Errorf("json unmarshal err: %s", e.Error())
  56. return
  57. }
  58. errCode, ok := netReturn["error_code"].(float64)
  59. if !ok {
  60. err = fmt.Errorf("result code err")
  61. return
  62. }
  63. // 忽略错误的手机号码这种错误
  64. if errCode != 0 && errCode != 205401 {
  65. err = fmt.Errorf("err code %f", errCode)
  66. return
  67. }
  68. // 发送成功
  69. if errCode == 0 {
  70. flag = true
  71. }
  72. return
  73. }
  74. // sendSms 发送国内短信
  75. func sendSms(jhGnAppKey, 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. tplVal := fmt.Sprintf(`#code#=%s&#m#=%d`, code, utils.VerifyCodeExpireMinute)
  84. param.Set("tpl_value", tplVal) //您设置的模板变量,根据实际情况
  85. param.Set("key", jhGnAppKey) //应用APPKEY(应用详细页查询)
  86. Url, err = url.Parse(apiURL)
  87. if err != nil {
  88. fmt.Printf("解析url错误:\r\n%v", err)
  89. return nil, err
  90. }
  91. //如果参数中有中文参数,这个方法会进行URLEncode
  92. Url.RawQuery = param.Encode()
  93. resp, err := http.Get(Url.String())
  94. if err != nil {
  95. fmt.Println("err:", err)
  96. return nil, err
  97. }
  98. defer resp.Body.Close()
  99. return ioutil.ReadAll(resp.Body)
  100. }
  101. // SendSmsCodeGj 发送国际短信
  102. //func SendSmsCodeGj(mobile, vCode, areaNum, tplId string) bool {
  103. // var err error
  104. // defer func() {
  105. // if err != nil {
  106. // tips := fmt.Sprintf("短信验证码发送失败, Err: %s", err.Error())
  107. // utils.FileLog.Info("%s", tips)
  108. // go alarm_msg.SendAlarmMsg(tips, 2)
  109. // }
  110. // }()
  111. // // 获取配置好的短信模版
  112. // smsCond := ` AND conf_key = ? `
  113. // smsPars := make([]interface{}, 0)
  114. // smsPars = append(smsPars, "SmsJhgjAppKey")
  115. // conf := new(models.BusinessConf)
  116. // conf, e := conf.GetItemByCondition(smsCond, smsPars)
  117. // if e != nil {
  118. // if utils.IsErrNoRow(e) {
  119. // err = fmt.Errorf("请先配置聚合短信Appkey")
  120. // return false
  121. // }
  122. // err = fmt.Errorf("获取聚合短信配置信息失败, Err: %s", e.Error())
  123. // return false
  124. // }
  125. // if conf.ConfVal == "" {
  126. // err = fmt.Errorf("请先配置聚合短信Appkey")
  127. // return false
  128. // }
  129. // result, err := sendSmsGj(conf.ConfVal, mobile, vCode, areaNum, tplId)
  130. // if err != nil {
  131. // fmt.Println("发送短信失败")
  132. // return false
  133. // }
  134. // fmt.Println("result", string(result))
  135. // var netReturn map[string]interface{}
  136. // err = json.Unmarshal(result, &netReturn)
  137. // if err != nil {
  138. // err = fmt.Errorf("短信验证码发送失败, Err:" + err.Error() + ";Result:" + string(result))
  139. // return false
  140. // }
  141. // if netReturn["error_code"].(float64) == 0 {
  142. // fmt.Printf("接口返回result字段是:\r\n%v", netReturn["result"])
  143. // return true
  144. // } else {
  145. // // 忽略错误的手机号码这种错误
  146. // if netReturn["error_code"].(float64) != 205401 {
  147. // err = fmt.Errorf("短信验证码发送失败, Result:" + string(result))
  148. // }
  149. // return false
  150. // }
  151. //}
  152. // sendSmsGj 发送国际短信
  153. func sendSmsGj(jhGjAppKey, mobile, code, areaNum, tplId, tplValue string) (rs []byte, err error) {
  154. var Url *url.URL
  155. apiURL := "http://v.juhe.cn/smsInternational/send.php"
  156. //初始化参数
  157. param := url.Values{}
  158. //配置请求参数,方法内部已处理urlencode问题,中文参数可以直接传参
  159. param.Set("mobile", mobile) //接受短信的用户手机号码
  160. //param.Set("tplId", "10054") //您申请的短信模板ID,根据实际情况修改
  161. param.Set("tplId", tplId) //您申请的短信模板ID,根据实际情况修改
  162. if strings.Contains(tplValue, "#code#=%s") {
  163. tplValue = strings.Replace(tplValue, "#code#=%s", "#code#="+code, 1)
  164. }
  165. if strings.Contains(tplValue, "#m#=%d") {
  166. tplValue = strings.Replace(tplValue, "#m#=%d", fmt.Sprintf("#m#=%d", utils.VerifyCodeExpireMinute), 1)
  167. }
  168. //param.Set("tplValue", "#code#="+code) //您设置的模板变量,根据实际情况
  169. param.Set("tplValue", tplValue) //您设置的模板变量,根据实际情况
  170. param.Set("key", jhGjAppKey) //应用APPKEY(应用详细页查询)
  171. param.Set("areaNum", areaNum) //应用APPKEY(应用详细页查询)
  172. Url, err = url.Parse(apiURL)
  173. if err != nil {
  174. fmt.Printf("解析url错误:\r\n%v", err)
  175. return nil, err
  176. }
  177. //如果参数中有中文参数,这个方法会进行URLEncode
  178. Url.RawQuery = param.Encode()
  179. resp, err := http.Get(Url.String())
  180. if err != nil {
  181. fmt.Println("err:", err)
  182. return nil, err
  183. }
  184. utils.FileLog.Info("sendSmsGj:param:" + Url.String())
  185. defer resp.Body.Close()
  186. body, err := ioutil.ReadAll(resp.Body)
  187. utils.FileLog.Info("sendSmsGj:result:" + string(body))
  188. return body, err
  189. }
  190. type SmsClient interface {
  191. SendUserLoginCode(UserLoginSmsCodeReq) (UserLoginSmsCodeResult, error)
  192. }
  193. func NewSmsClient() (cli SmsClient, err error) {
  194. confMap, e := models.GetBusinessConf()
  195. if e != nil {
  196. err = fmt.Errorf("GetBusinessConf err: %s", e.Error())
  197. return
  198. }
  199. if confMap[models.BusinessConfSmsClient] == models.BusinessConfClientFlagNanHua {
  200. return new(NanHuaSms), nil
  201. }
  202. return new(HzSms), nil
  203. }
  204. type HzSms struct{}
  205. type UserLoginSmsCodeReq struct {
  206. TelAreaCode string `description:"区号"`
  207. Mobile string `description:"手机号"`
  208. VerifyCode string `description:"短信验证码"`
  209. }
  210. type UserLoginSmsCodeResult struct {
  211. Success bool `description:"发送是否成功"`
  212. Message string `description:"提示信息"`
  213. RequestId string `description:"发送ID,用于回查发送状态"`
  214. }
  215. // SendUserLoginCode 发送用户登录验证码
  216. func (cli *HzSms) SendUserLoginCode(req UserLoginSmsCodeReq) (result UserLoginSmsCodeResult, err error) {
  217. if req.Mobile == "" || req.VerifyCode == "" {
  218. err = fmt.Errorf("参数有误, Mobile: %s, VerifyCode: %s", req.Mobile, req.VerifyCode)
  219. return
  220. }
  221. confMap, e := models.GetBusinessConf()
  222. if e != nil {
  223. err = fmt.Errorf("GetBusinessConf err: %s", e.Error())
  224. return
  225. }
  226. // 国内短信
  227. var netReturn map[string]interface{}
  228. if req.TelAreaCode == utils.TelAreaCodeHome {
  229. tplId := confMap[models.BusinessConfLoginSmsTpId]
  230. if tplId == "" {
  231. err = fmt.Errorf("请先配置短信模板")
  232. return
  233. }
  234. appKey := confMap[models.BusinessConfSmsJhgnAppKey]
  235. if appKey == "" {
  236. err = fmt.Errorf("请先配置聚合短信AppKey")
  237. return
  238. }
  239. smsRes, e := sendSms(appKey, req.Mobile, tplId, req.VerifyCode)
  240. if e != nil {
  241. err = fmt.Errorf("send sms err: %s", e.Error())
  242. return
  243. }
  244. if e = json.Unmarshal(smsRes, &netReturn); e != nil {
  245. err = fmt.Errorf("json unmarshal err: %s", e.Error())
  246. return
  247. }
  248. }
  249. // 国际短信
  250. if req.TelAreaCode != utils.TelAreaCodeHome {
  251. tplId := confMap[models.BusinessConfLoginSmsGjTpId]
  252. if tplId == "" {
  253. err = fmt.Errorf("请先配置短信模板")
  254. return
  255. }
  256. appKey := confMap[models.BusinessConfSmsJhgjAppKey]
  257. if appKey == "" {
  258. err = fmt.Errorf("请先配置聚合短信AppKey")
  259. return
  260. }
  261. tplValue := confMap[models.BusinessConfSmsJhgjVariable]
  262. if tplValue == "" {
  263. // 默认初版变量
  264. tplValue = "#code#=%s"
  265. }
  266. smsRes, e := sendSmsGj(appKey, req.Mobile, req.VerifyCode, req.TelAreaCode, tplId, tplValue)
  267. if e != nil {
  268. err = fmt.Errorf("send gj sms err: %s", e.Error())
  269. return
  270. }
  271. if e = json.Unmarshal(smsRes, &netReturn); e != nil {
  272. err = fmt.Errorf("json unmarshal err: %s", e.Error())
  273. return
  274. }
  275. }
  276. errCode, ok := netReturn["error_code"].(float64)
  277. if !ok {
  278. err = fmt.Errorf("result code err")
  279. return
  280. }
  281. // 忽略错误的手机号码这种错误
  282. if errCode != 0 && errCode != 205401 {
  283. err = fmt.Errorf("err code %f", errCode)
  284. return
  285. }
  286. // 发送成功
  287. if errCode == 0 {
  288. result.Success = true
  289. }
  290. return
  291. }