shellrequest.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package shell
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "go-common/app/admin/main/growup/conf"
  8. "go-common/library/log"
  9. "go-common/library/net/http/blademaster"
  10. "net/http"
  11. )
  12. //SignInterface set signature
  13. type SignInterface interface {
  14. SetSign(sign string)
  15. SetCustomerID(customerID string)
  16. SetSignType(signType string)
  17. }
  18. //OrderInfo order info
  19. type OrderInfo struct {
  20. Brokerage string `json:"brokerage"`
  21. Mid int64 `json:"mid"`
  22. ThirdCoin string `json:"thirdCoin"`
  23. ThirdCtime string `json:"thirdCtime"`
  24. ThirdOrderNo string `json:"thirdOrderNo"`
  25. }
  26. //OrderRequest shell order request
  27. type OrderRequest struct {
  28. CustomerID string `json:"customerId"`
  29. ProductName string `json:"productName"`
  30. Data []OrderInfo `json:"data"`
  31. NotifyURL string `json:"notifyUrl"`
  32. Rate string `json:"rate"`
  33. SignType string `json:"signType"`
  34. Timestamp string `json:"timestamp"`
  35. Sign string `json:"sign,omitempty"`
  36. }
  37. //SetSign set sign
  38. func (o *OrderRequest) SetSign(sign string) {
  39. o.Sign = sign
  40. }
  41. //SetCustomerID set customId
  42. func (o *OrderRequest) SetCustomerID(customerID string) {
  43. o.CustomerID = customerID
  44. }
  45. //SetSignType set signtype
  46. func (o *OrderRequest) SetSignType(signType string) {
  47. o.SignType = signType
  48. }
  49. //OrderResponse shell order response
  50. type OrderResponse struct {
  51. Errno int `json:"errno"`
  52. Msg string `json:"msg"`
  53. }
  54. const (
  55. //CallbackStatusCreate 创建中状态
  56. CallbackStatusCreate = "CREATE"
  57. //CallbackStatusSuccess 成功
  58. CallbackStatusSuccess = "SUCCESS"
  59. //CallbackStatusFail 失败
  60. CallbackStatusFail = "FAIL"
  61. )
  62. //OrderCallbackJSON MsgContent in OrderCallbackParam
  63. type OrderCallbackJSON struct {
  64. CustomerID string `json:"customerId"`
  65. Status string `json:"status"`
  66. ThirdOrderNo string `json:"thirdOrderNo"`
  67. Mid string `json:"mid"`
  68. Ext json.RawMessage `json:"ext"`
  69. Timestamp string `json:"timestamp"`
  70. SignType string `json:"signType"`
  71. Sign string `json:"sign"`
  72. }
  73. //IsSuccess success
  74. func (o *OrderCallbackJSON) IsSuccess() bool {
  75. return o.Status == CallbackStatusSuccess
  76. }
  77. //IsFail fail
  78. func (o *OrderCallbackJSON) IsFail() bool {
  79. return o.Status == CallbackStatusFail
  80. }
  81. //IsCreate creating
  82. func (o *OrderCallbackJSON) IsCreate() bool {
  83. return o.Status == CallbackStatusCreate
  84. }
  85. //OrderCallbackParam call back url param
  86. type OrderCallbackParam struct {
  87. MsgID string `form:"msgId"`
  88. MsgContent string `form:"msgContent"`
  89. }
  90. //OrderCheckRequest request
  91. type OrderCheckRequest struct {
  92. Sign string `json:"sign,omitempty"`
  93. SignType string `json:"signType"`
  94. CustomerID string `json:"customerId"`
  95. Timestamp int64 `json:"timestamp"`
  96. ThirdOrderNos string `json:"thirdOrderNos"`
  97. }
  98. //SetSign set sign
  99. func (o *OrderCheckRequest) SetSign(sign string) {
  100. o.Sign = sign
  101. }
  102. //SetCustomerID set customer id
  103. func (o *OrderCheckRequest) SetCustomerID(customerID string) {
  104. o.CustomerID = customerID
  105. }
  106. //SetSignType set sign type, always "MD5"
  107. func (o *OrderCheckRequest) SetSignType(signType string) {
  108. o.SignType = signType
  109. }
  110. //OrderStatusData call back data
  111. type OrderStatusData struct {
  112. ThirdOrderNo string `json:"thirdOrderNo"`
  113. Status string `json:"status"`
  114. Mid string `json:"mid"`
  115. }
  116. //IsSuccess is successful
  117. func (o *OrderStatusData) IsSuccess() bool {
  118. return o.Status == CallbackStatusSuccess
  119. }
  120. //OrderCheckResponse response
  121. type OrderCheckResponse struct {
  122. Errno int `json:"errno"`
  123. Msg string `json:"msg"`
  124. Orders []OrderStatusData `json:"data"`
  125. }
  126. //Client shell client
  127. type Client struct {
  128. conf conf.ShellConfig
  129. CustomID string
  130. Token string
  131. HTTPClient *blademaster.Client
  132. isDebug bool
  133. }
  134. //New client
  135. func New(conf *conf.ShellConfig, httpClient *blademaster.Client) *Client {
  136. return &Client{
  137. CustomID: conf.CustomID,
  138. Token: conf.Token,
  139. HTTPClient: httpClient,
  140. conf: *conf,
  141. }
  142. }
  143. //SetDebug set debug
  144. func (s *Client) SetDebug(isDebug bool) {
  145. s.isDebug = isDebug
  146. }
  147. //SendOrderRequest send order rquest
  148. func (s *Client) SendOrderRequest(ctx context.Context, req *OrderRequest) (res *OrderResponse, err error) {
  149. var host = s.conf.PayHost
  150. if host == "" {
  151. host = "pay.bilibili.co"
  152. }
  153. var url = "http://" + host + "/bk-int/brokerage/rechargeBrokerage"
  154. res = &OrderResponse{}
  155. err = s.SendShellRequest(ctx, url, req, res)
  156. if err != nil {
  157. log.Error("send order request fail, err=%s", err)
  158. }
  159. return
  160. }
  161. //SendCheckOrderRequest send check order request
  162. func (s *Client) SendCheckOrderRequest(ctx context.Context, req *OrderCheckRequest) (res *OrderCheckResponse, err error) {
  163. var host = s.conf.PayHost
  164. if host == "" {
  165. host = "pay.bilibili.co"
  166. }
  167. var url = "http://" + host + "/bk-int/brokerage/queryRechargeBrokerage"
  168. res = &OrderCheckResponse{}
  169. err = s.SendShellRequest(ctx, url, req, res)
  170. if err != nil {
  171. log.Error("send check order request fail, err=%s", err)
  172. }
  173. return
  174. }
  175. //SendShellRequest send request
  176. func (s *Client) SendShellRequest(ctx context.Context, url string, req interface{}, res interface{}) (err error) {
  177. r, ok := req.(SignInterface)
  178. if !ok {
  179. err = fmt.Errorf("cast fail, req is not SignInterface")
  180. return
  181. }
  182. r.SetSignType("MD5")
  183. r.SetCustomerID(s.CustomID)
  184. sign, err := Sign(r, s.Token)
  185. if err != nil {
  186. return
  187. }
  188. r.SetSign(sign)
  189. jsonStr, err := json.Marshal(r)
  190. if s.isDebug {
  191. log.Info("send request, url=%s, req=%s", url, jsonStr)
  192. }
  193. if err != nil {
  194. return
  195. }
  196. if err != nil {
  197. return
  198. }
  199. var buffer = bytes.NewBuffer(jsonStr)
  200. httpreq, _ := http.NewRequest("POST", url, buffer)
  201. httpreq.Header.Set("Content-Type", "application/json")
  202. bs, err := s.HTTPClient.Raw(ctx, httpreq)
  203. if s.isDebug {
  204. log.Info("req=%s, response=%s", jsonStr, bs)
  205. }
  206. if err != nil {
  207. log.Error("get response err, err=%s, response=%s", err, string(bs))
  208. return
  209. }
  210. if err = json.Unmarshal(bs, res); err != nil {
  211. log.Error("json decode err, response=%s", string(bs))
  212. }
  213. return
  214. }