juhe_sms_sender.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package sms
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_ht_api/common/component/config"
  5. logger "eta/eta_mini_ht_api/common/component/log"
  6. "eta/eta_mini_ht_api/common/exception"
  7. "eta/eta_mini_ht_api/common/utils/sms"
  8. "fmt"
  9. "io"
  10. "net/http"
  11. "net/url"
  12. )
  13. const (
  14. apiURL = "http://v.juhe.cn/sms/send"
  15. SendSuccess = "发送成功"
  16. SendFail = "发送失败"
  17. )
  18. type JuheSmsSender struct {
  19. tplId string
  20. SmsExpireMinute int
  21. Key string
  22. }
  23. func (jh *JuheSmsSender) InitSender(config *config.SMSConfig) {
  24. jh.tplId = config.GetJhTlpId()
  25. jh.SmsExpireMinute = config.GetJhExpireMinute()
  26. jh.Key = config.GetKey()
  27. }
  28. func (jh *JuheSmsSender) GetExpireMinute() int {
  29. return jh.SmsExpireMinute
  30. }
  31. type JHResp struct {
  32. Reason string `json:"reason"`
  33. Result Result `json:"result"`
  34. ErrorCode int
  35. }
  36. type Result struct {
  37. Sid string `json:"sid"`
  38. Fee int `json:"fee"`
  39. Count int `json:"count"`
  40. }
  41. func (jh *JuheSmsSender) SendSms(mobile string, code string, _ int) (result string, err error) {
  42. var Url *url.URL
  43. //初始化参数
  44. param := url.Values{}
  45. //配置请求参数,方法内部已处理urlencode问题,中文参数可以直接传参
  46. param.Set("mobile", mobile) //接受短信的用户手机号码
  47. param.Set("tpl_id", jh.tplId) //您申请的短信模板ID,根据实际情况修改
  48. tplVal := fmt.Sprintf(`#code#=%s&#m#=%d`, code, jh.SmsExpireMinute)
  49. param.Set("tpl_value", tplVal) //您设置的模板变量,根据实际情况
  50. param.Set("key", jh.Key) //应用APPKEY(应用详细页查询)
  51. Url, err = url.Parse(apiURL)
  52. if err != nil {
  53. logger.Warn("[短信发送]解析url错误:%v", err)
  54. return SendFail, exception.New(exception.SendingSMSFailed)
  55. }
  56. //如果参数中有中文参数,这个方法会进行URLEncode
  57. Url.RawQuery = param.Encode()
  58. resp, err := http.Get(Url.String())
  59. if err != nil {
  60. logger.Warn("[短信发送]发送失败:%v", err)
  61. return SendFail, exception.New(exception.SendingSMSFailed)
  62. }
  63. body, _ := io.ReadAll(resp.Body)
  64. var jsonResp JHResp
  65. err = json.Unmarshal(body, &jsonResp)
  66. if err != nil {
  67. logger.Warn("[短信发送]解析jh应答失败:%v", err)
  68. return SendFail, exception.New(exception.SendingSMSFailed)
  69. }
  70. if jsonResp.ErrorCode != 0 {
  71. logger.Warn("[短信发送]发送失败%v", jsonResp.Reason)
  72. return SendFail, exception.New(exception.SendingSMSFailed)
  73. }
  74. defer func(Body io.ReadCloser) {
  75. closeErr := Body.Close()
  76. if closeErr != nil {
  77. logger.Error("关闭Response失败:%v", closeErr)
  78. }
  79. }(resp.Body)
  80. logger.Info("[短信发送]发送成功,sid:%v", jsonResp.Result.Sid)
  81. return SendSuccess, nil
  82. }
  83. func NewJHClient() sms.SMSClient {
  84. return &JuheSmsSender{}
  85. }
  86. func init() {
  87. sms.Register(sms.Juhe, NewJHClient)
  88. }