gd.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package thirdparty
  4. import (
  5. "bytes"
  6. "gd_management/errors"
  7. "encoding/hex"
  8. "encoding/json"
  9. "fmt"
  10. "io/ioutil"
  11. "net/http"
  12. "strconv"
  13. "strings"
  14. "time"
  15. "gd_management/common.in/config"
  16. "gd_management/common.in/utils"
  17. "github.com/tidwall/gjson"
  18. "go.uber.org/zap"
  19. )
  20. type GdResp struct {
  21. Code int64 `json:"code"`
  22. Msg string `json:"msg"`
  23. Data string `json:"data"`
  24. }
  25. func concatGdGetURL(url string, data map[string]string) string {
  26. if data == nil {
  27. return url
  28. }
  29. for k, v := range data {
  30. url = url + "&" + k + "=" + v
  31. }
  32. url = strings.Replace(url, "&", "?", 1)
  33. return url
  34. }
  35. func GdClientPost(api string, data map[string]interface{}) (result []byte, err error) {
  36. jsonData, err := json.Marshal(data)
  37. if err != nil {
  38. return nil, err
  39. }
  40. var realData = map[string]string{
  41. "data": string(jsonData),
  42. }
  43. datas, err := json.Marshal(realData)
  44. if err != nil {
  45. return nil, err
  46. }
  47. client := &http.Client{}
  48. client.Timeout = 60 * time.Second
  49. req, err := http.NewRequest("POST", api, bytes.NewBuffer(datas))
  50. if err != nil {
  51. r, _ := json.Marshal(data)
  52. l.Error("thirdparty",
  53. zap.String("call", api),
  54. zap.String("params", string(r)),
  55. zap.String("error", err.Error()))
  56. return nil, err
  57. }
  58. timestamp := strconv.FormatInt(time.Now().Unix(), 10)
  59. req.Header.Set("app_key", config.Conf.GdApiAppKey)
  60. req.Header.Set("ts", timestamp)
  61. signtext := fmt.Sprintf("%s%s%s%s", timestamp, string(jsonData), config.Conf.GdApiAppKey, config.Conf.GdApiAppSecret)
  62. req.Header.Set("sign", utils.MD5(signtext))
  63. req.Header.Set("Content-Type", "application/json")
  64. resp, err := client.Do(req)
  65. if err != nil {
  66. r, _ := json.Marshal(data)
  67. l.Error("thirdparty",
  68. zap.String("call", api),
  69. zap.String("params", string(r)),
  70. zap.String("error", err.Error()))
  71. return nil, err
  72. }
  73. defer resp.Body.Close()
  74. if resp.StatusCode != http.StatusOK {
  75. return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
  76. }
  77. result, err = ioutil.ReadAll(resp.Body)
  78. if err != nil {
  79. r, _ := json.Marshal(data)
  80. l.Error("thirdparty",
  81. zap.String("call", api),
  82. zap.String("params", string(r)),
  83. zap.String("error", err.Error()))
  84. return nil, err
  85. }
  86. //fmt.Println("result:",string(result))
  87. var gdResp GdResp
  88. err = json.Unmarshal(result, &gdResp)
  89. if err != nil {
  90. r, _ := json.Marshal(data)
  91. l.Error("thirdparty",
  92. zap.String("call", api),
  93. zap.String("params", string(r)),
  94. zap.String("error", err.Error()))
  95. //fmt.Println("unmrshal error")
  96. return nil, err
  97. }
  98. if gdResp.Code != 0 {
  99. r, _ := json.Marshal(data)
  100. l.Error("thirdparty",
  101. zap.String("call", api),
  102. zap.String("params", string(r)),
  103. zap.String("error", gdResp.Msg))
  104. //fmt.Println("code is:",gdResp.Code)
  105. return nil, errors.VendorError
  106. }
  107. return []byte(gdResp.Data), nil
  108. }
  109. func GdClientGet(api string, data map[string]interface{}) (result []byte, err error) {
  110. jsonData, err := json.Marshal(data)
  111. if err != nil {
  112. return nil, err
  113. }
  114. client := &http.Client{}
  115. client.Timeout = 10 * time.Second
  116. rData := map[string]string{"data": string(jsonData)}
  117. req, err := http.NewRequest("GET", concatGdGetURL(api, rData), nil)
  118. if err != nil {
  119. r, _ := json.Marshal(data)
  120. l.Error("thirdparty",
  121. zap.String("call", api),
  122. zap.String("params", string(r)),
  123. zap.String("error", err.Error()))
  124. return nil, err
  125. }
  126. timestamp := strconv.FormatInt(time.Now().Unix(), 10)
  127. req.Header.Set("app_key", config.Conf.GdApiAppKey)
  128. req.Header.Set("ts", timestamp)
  129. signtext := fmt.Sprintf("%s%s%s%s", timestamp, string(jsonData), config.Conf.GdApiAppKey, config.Conf.GdApiAppSecret)
  130. req.Header.Set("sign", utils.MD5(signtext))
  131. req.Header.Set("Content-Type", "application/json")
  132. resp, err := client.Do(req)
  133. if err != nil {
  134. r, _ := json.Marshal(data)
  135. l.Error("thirdparty",
  136. zap.String("call", api),
  137. zap.String("params", string(r)),
  138. zap.String("error", err.Error()))
  139. return nil, err
  140. }
  141. defer resp.Body.Close()
  142. if resp.StatusCode != http.StatusOK {
  143. return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
  144. }
  145. result, err = ioutil.ReadAll(resp.Body)
  146. if err != nil {
  147. r, _ := json.Marshal(data)
  148. l.Error("thirdparty",
  149. zap.String("call", api),
  150. zap.String("params", string(r)),
  151. zap.String("error", err.Error()))
  152. return nil, err
  153. }
  154. var gdResp GdResp
  155. err = json.Unmarshal(result, &gdResp)
  156. if err != nil {
  157. r, _ := json.Marshal(data)
  158. l.Error("thirdparty",
  159. zap.String("call", api),
  160. zap.String("params", string(r)),
  161. zap.String("error", err.Error()))
  162. return nil, err
  163. }
  164. if gdResp.Code != 0 {
  165. r, _ := json.Marshal(data)
  166. l.Error("thirdparty",
  167. zap.String("call", api),
  168. zap.String("params", string(r)),
  169. zap.String("error", gdResp.Msg))
  170. return nil, errors.VendorError
  171. }
  172. return []byte(gdResp.Data), nil
  173. }
  174. func GdClientCryptoGet(api string, data map[string]interface{}) (result []byte, err error) {
  175. jsonData, err := json.Marshal(data)
  176. if err != nil {
  177. return nil, err
  178. }
  179. client := &http.Client{}
  180. client.Timeout = 10 * time.Second
  181. bytes, _ := config.AesEncrypt(string(jsonData), config.Conf.GdApiAppSecret)
  182. rData := map[string]string{"data": hex.EncodeToString(bytes)}
  183. req, err := http.NewRequest("GET", concatGdGetURL(api, rData), nil)
  184. if err != nil {
  185. return nil, err
  186. }
  187. timestamp := strconv.FormatInt(time.Now().Unix(), 10)
  188. req.Header.Set("app_key", config.Conf.GdApiAppKey)
  189. req.Header.Set("ts", timestamp)
  190. signtext := fmt.Sprintf("%s%s%s%s", timestamp, hex.EncodeToString(bytes), config.Conf.GdApiAppKey, config.Conf.GdApiAppSecret)
  191. req.Header.Set("sign", utils.MD5(signtext))
  192. req.Header.Set("Content-Type", "application/json")
  193. resp, err := client.Do(req)
  194. if err != nil {
  195. return nil, err
  196. }
  197. defer resp.Body.Close()
  198. if resp.StatusCode != http.StatusOK {
  199. return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
  200. }
  201. rByte, err := ioutil.ReadAll(resp.Body)
  202. if err != nil {
  203. r, _ := json.Marshal(data)
  204. l.Error("thirdparty",
  205. zap.String("call", api),
  206. zap.String("params", string(r)),
  207. zap.String("error", err.Error()))
  208. return nil, err
  209. }
  210. //fmt.Println(string(rByte), " error: ", err)
  211. res := gjson.GetBytes(rByte, "data").String()
  212. result, err = config.AesDecrypt([]byte(res), []byte(config.Conf.GdApiAppSecret))
  213. if err != nil {
  214. r, _ := json.Marshal(data)
  215. l.Error("thirdparty",
  216. zap.String("call", api),
  217. zap.String("params", string(r)),
  218. zap.String("error", err.Error()))
  219. return nil, err
  220. }
  221. var gdResp GdResp
  222. err = json.Unmarshal(result, &gdResp)
  223. if err != nil {
  224. r, _ := json.Marshal(data)
  225. l.Error("thirdparty",
  226. zap.String("call", api),
  227. zap.String("params", string(r)),
  228. zap.String("error", err.Error()))
  229. return nil, err
  230. }
  231. if gdResp.Code != 0 {
  232. r, _ := json.Marshal(data)
  233. l.Error("thirdparty",
  234. zap.String("call", api),
  235. zap.String("params", string(r)),
  236. zap.String("error", gdResp.Msg))
  237. return nil, errors.VendorError
  238. }
  239. return []byte(gdResp.Data), nil
  240. }