client.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package chuanglan
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "net/http"
  8. "net/url"
  9. "strconv"
  10. "go-common/app/job/main/sms/conf"
  11. "go-common/app/job/main/sms/model"
  12. smsmdl "go-common/app/service/main/sms/model"
  13. "go-common/library/log"
  14. xhttp "go-common/library/net/http/blademaster"
  15. )
  16. // Client .
  17. type Client struct {
  18. conf conf.Provider
  19. client *xhttp.Client
  20. }
  21. type response struct {
  22. Code string `json:"code"`
  23. MsgID string `json:"msgId"`
  24. ErrMsg string `json:"errorMsg"`
  25. Time string `json:"time"`
  26. }
  27. // GetPid get pid
  28. func (v *Client) GetPid() int32 {
  29. return smsmdl.ProviderChuangLan
  30. }
  31. // NewClient new ChuangLan
  32. func NewClient(c *conf.Config) *Client {
  33. return &Client{
  34. conf: *c.Provider,
  35. client: xhttp.NewClient(c.HTTPClient),
  36. }
  37. }
  38. // SendSms send sms
  39. func (v *Client) SendSms(ctx context.Context, r *smsmdl.ModelSend) (msgid string, err error) {
  40. msg := model.SmsPrefix + r.Content
  41. params := make(map[string]interface{})
  42. params["account"] = v.conf.ChuangLanSmsUser
  43. params["password"] = v.conf.ChuangLanSmsPwd
  44. params["phone"] = r.Mobile
  45. params["msg"] = url.QueryEscape(msg)
  46. params["report"] = "true"
  47. uri := v.conf.ChuangLanSmsURL
  48. msgid, err = v.post(ctx, uri, params)
  49. return
  50. }
  51. // SendActSms send act sms
  52. func (v *Client) SendActSms(ctx context.Context, r *smsmdl.ModelSend) (msgid string, err error) {
  53. msg := model.SmsPrefix + r.Content + model.SmsSuffixChuangLan
  54. params := make(map[string]interface{})
  55. params["account"] = v.conf.ChuangLanActUser
  56. params["password"] = v.conf.ChuangLanActPwd
  57. params["phone"] = r.Mobile
  58. params["msg"] = url.QueryEscape(msg)
  59. params["report"] = "true"
  60. uri := v.conf.ChuangLanActURL
  61. msgid, err = v.post(ctx, uri, params)
  62. return
  63. }
  64. // SendBatchActSms send batch act sms
  65. func (v *Client) SendBatchActSms(ctx context.Context, r *smsmdl.ModelSend) (msgid string, err error) {
  66. msgid, err = v.SendActSms(ctx, r)
  67. return
  68. }
  69. // SendInternationalSms send international sms
  70. func (v *Client) SendInternationalSms(ctx context.Context, r *smsmdl.ModelSend) (msgid string, err error) {
  71. msg := model.SmsPrefix + r.Content
  72. params := make(map[string]interface{})
  73. params["account"] = v.conf.ChuangLanInternationUser
  74. params["password"] = v.conf.ChuangLanInternationPwd
  75. params["mobile"] = r.Country + r.Mobile
  76. params["msg"] = msg
  77. uri := v.conf.ChuangLanInternationURL
  78. bytesData, err := json.Marshal(params)
  79. if err != nil {
  80. log.Error("ChuangLan send international Marshal error(%v)", err)
  81. return
  82. }
  83. reader := bytes.NewReader(bytesData)
  84. request, err := http.NewRequest(http.MethodPost, uri, reader)
  85. if err != nil {
  86. log.Error("ChuangLan send international NewRequest err(%v)", err)
  87. return
  88. }
  89. request.Header.Set("Content-Type", "application/json;charset=UTF-8")
  90. type internation struct {
  91. Code string `json:"code"`
  92. Msgid string `json:"msgid"`
  93. Error string `json:"error"`
  94. }
  95. res := &internation{}
  96. if err = v.client.Do(ctx, request, res); err != nil {
  97. log.Error("ChuangLan send international client.Do err(%v)", err)
  98. return
  99. }
  100. if res.Code != "0" {
  101. err = fmt.Errorf("ChuangLan send international sms code(%v) err(%v)", res.Code, res.Error)
  102. return
  103. }
  104. msgid = res.Msgid
  105. return
  106. }
  107. func (v *Client) post(ctx context.Context, uri string, params map[string]interface{}) (msgid string, err error) {
  108. bytesData, err := json.Marshal(params)
  109. if err != nil {
  110. log.Error("ChuangLan Marshal error(%v)", err)
  111. return
  112. }
  113. reader := bytes.NewReader(bytesData)
  114. request, err := http.NewRequest(http.MethodPost, uri, reader)
  115. if err != nil {
  116. log.Error("ChuangLan NewRequest err(%v)", err)
  117. return
  118. }
  119. request.Header.Set("Content-Type", "application/json;charset=UTF-8")
  120. res := &response{}
  121. if err = v.client.Do(ctx, request, res); err != nil {
  122. log.Error("ChuangLan client.Do err(%v)", err)
  123. return
  124. }
  125. if res.Code != "0" {
  126. err = fmt.Errorf("ChuangLan send sms code(%v) err(%v)", res.Code, res.ErrMsg)
  127. return
  128. }
  129. msgid = res.MsgID
  130. log.Info("url(%s) body(%v) resp(%+v)", uri, params, res)
  131. return
  132. }
  133. // Callback .
  134. type Callback struct {
  135. MsgID string `json:"msgId"`
  136. Mobile string `json:"mobile"`
  137. Status string `json:"status"`
  138. Desc string `json:"statusDesc"`
  139. NotifyTime string `json:"notifyTime"`
  140. ReportTime string `json:"reportTime"`
  141. Length string `json:"length"`
  142. }
  143. type callbackResponse struct {
  144. Code int `json:"ret"`
  145. Result []*Callback `json:"result"`
  146. }
  147. // Callback sms callbacks.
  148. func (v *Client) Callback(ctx context.Context, account, pwd, url string, count int) (callbacks []*Callback, err error) {
  149. params := make(map[string]interface{})
  150. params["account"] = account
  151. params["password"] = pwd
  152. params["count"] = strconv.Itoa(count)
  153. bs, err := json.Marshal(params)
  154. if err != nil {
  155. log.Error("ChuangLan sms callback Marshal error(%v)", err)
  156. return
  157. }
  158. request, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(bs))
  159. if err != nil {
  160. log.Error("ChuangLan sms callback NewRequest err(%v)", err)
  161. return
  162. }
  163. request.Header.Set("Content-Type", "application/json;charset=UTF-8")
  164. res := &callbackResponse{}
  165. if err = v.client.Do(ctx, request, res); err != nil {
  166. log.Error("ChuangLan sms callback client.Do err(%v)", err)
  167. return
  168. }
  169. if res.Code != 0 {
  170. err = fmt.Errorf("ChuangLan sms callback code(%d)", res.Code)
  171. return
  172. }
  173. callbacks = res.Result
  174. return
  175. }
  176. // CallbackInternational sms callbacks.
  177. func (v *Client) CallbackInternational(ctx context.Context, count int) (callbacks []*Callback, err error) {
  178. params := make(map[string]interface{})
  179. params["account"] = v.conf.ChuangLanInternationUser
  180. params["password"] = v.conf.ChuangLanInternationPwd
  181. params["count"] = strconv.Itoa(count)
  182. bs, err := json.Marshal(params)
  183. if err != nil {
  184. log.Error("ChuangLan international sms callback Marshal error(%v)", err)
  185. return
  186. }
  187. request, err := http.NewRequest(http.MethodPost, v.conf.ChuangLanInternationalCallbackURL, bytes.NewReader(bs))
  188. if err != nil {
  189. log.Error("ChuangLan international sms callback NewRequest err(%v)", err)
  190. return
  191. }
  192. request.Header.Set("Content-Type", "application/json;charset=UTF-8")
  193. type intCallbackResponse struct {
  194. Code int `json:"code"`
  195. Error string `json:"error"`
  196. Result []*Callback `json:"result"`
  197. }
  198. res := &intCallbackResponse{}
  199. if err = v.client.Do(ctx, request, res); err != nil {
  200. log.Error("ChuangLan international sms callback client.Do err(%v)", err)
  201. return
  202. }
  203. if res.Code != 0 {
  204. err = fmt.Errorf("ChuangLan international sms callback code(%d)", res.Code)
  205. return
  206. }
  207. callbacks = res.Result
  208. return
  209. }