dybd.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package thirdparty
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "gd_service/common.in/config"
  7. id2 "gd_service/common.in/id"
  8. "gd_service/common.in/utils"
  9. "go.uber.org/zap"
  10. //"gd_service/common.in/config"
  11. "net/url"
  12. //"github.com/tidwall/gjson"
  13. "io/ioutil"
  14. "net/http"
  15. "time"
  16. )
  17. var publicDybdKey = []byte(`
  18. -----BEGIN PUBLIC KEY-----
  19. MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDxCRsGHGsaSeavh45EzMtQlx0BvY9TkHuQ1ENF20bLEipKkIFjiZmdiYXXl2GsRj5+yovs5ZkKZZX8zUxN/dPLDqKhTsVn4d8EcZ38QlkTbVNXdbqlmS2OaD0EmJoYbH5ZVY/8AnH0AdonK2x/Xvg4zZeZbGMeN8zmNQf2XwLAVQIDAQAB
  20. -----END PUBLIC KEY-----
  21. `)
  22. func DybdGetToken(appKey string,httpTimeout int)(result []byte, err error){
  23. defer func() {
  24. l.Info("thirdparty",
  25. zap.String("api", config.Conf.ThirdPart.DybdTokenUrl),
  26. zap.String("request", appKey),
  27. zap.String("response", utils.MarshalJsonString(result)))
  28. }()
  29. if httpTimeout == 0 {
  30. httpTimeout = 30
  31. }
  32. client := &http.Client{}
  33. data := make(map[string]string)
  34. data["appkey"] = appKey
  35. data["input"] = "18782990639"
  36. data["password"] = "hly@880213"
  37. client.Timeout = time.Duration(httpTimeout) * time.Second
  38. bData ,_:= json.Marshal(data)
  39. req, err := http.NewRequest("POST", config.Conf.ThirdPart.DybdTokenUrl, bytes.NewBufferString(string(bData)))
  40. if err != nil {
  41. return nil, err
  42. }
  43. req.Header.Set("Content-Type", "application/json")
  44. resp, err := client.Do(req)
  45. if err != nil {
  46. return nil, err
  47. }
  48. if resp.StatusCode != http.StatusOK {
  49. return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
  50. }
  51. defer resp.Body.Close()
  52. result, err = ioutil.ReadAll(resp.Body)
  53. return
  54. }
  55. func DyBdClientPostOld(api string, data map[string]string, httpTimeout int) (result []byte, err error) {
  56. defer func() {
  57. l.Info("thirdparty",
  58. zap.String("api", api),
  59. zap.String("request", utils.MarshalJsonString(data)),
  60. zap.String("response", utils.MarshalJsonString(result)))
  61. }()
  62. if httpTimeout == 0 {
  63. httpTimeout = 30
  64. }
  65. client := &http.Client{}
  66. data["pageNo"] = "1"
  67. data["pageNo"] = "10"
  68. client.Timeout = time.Duration(httpTimeout) * time.Second
  69. //requestData, _ := json.Marshal(data)
  70. //fmt.Println("request data :", string(requestData))
  71. v := url.Values{}
  72. for k, v1 := range data {
  73. v.Add(k, v1)
  74. }
  75. req, err := http.NewRequest("GET", api+"?"+v.Encode(), nil)
  76. if err != nil {
  77. return nil, err
  78. }
  79. now := time.Now()
  80. id, _ := id2.GetDistributedUniqueID()
  81. nonce := fmt.Sprintf("%d", id)
  82. aesKey := fmt.Sprintf("%d", now.UnixNano()/1e3)
  83. stamp := fmt.Sprintf("%d", now.UnixNano()/1e6)
  84. header := map[string]string{}
  85. header["appId"] = "test010"
  86. header["appSecret"] = "VePz0pi@"
  87. header["timestamp"] = stamp
  88. header["nonce"] = nonce
  89. aesEncryptData, err := config.Base64RsaEncrypt([]byte(aesKey), publicDybdKey)
  90. if err != nil {
  91. return nil, err
  92. }
  93. header["aesSecretEncrypt"] = string(aesEncryptData)
  94. signStr := fmt.Sprintf("%s:%s:%s:%s:%s", header["appId"], header["appSecret"], header["aesSecretEncrypt"], header["timestamp"], header["nonce"])
  95. sign, err := config.Base64AESECBEncrypt(signStr, aesKey)
  96. if err != nil {
  97. return nil, err
  98. }
  99. header["sign"] = string(sign)
  100. for k, v := range header {
  101. req.Header.Set(k, v)
  102. }
  103. req.Header.Set("Content-Type", "application/json")
  104. resp, err := client.Do(req)
  105. if err != nil {
  106. return nil, err
  107. }
  108. if resp.StatusCode != http.StatusOK {
  109. return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
  110. }
  111. defer resp.Body.Close()
  112. result, err = ioutil.ReadAll(resp.Body)
  113. return
  114. }
  115. func DyBdClientPost(api string, data map[string]string, httpTimeout int,token string) (result []byte, err error) {
  116. defer func() {
  117. l.Info("thirdparty",
  118. zap.String("api", api),
  119. zap.String("request", utils.MarshalJsonString(data)),
  120. zap.String("response", utils.MarshalJsonString(result)))
  121. }()
  122. if httpTimeout == 0 {
  123. httpTimeout = 30
  124. }
  125. client := &http.Client{}
  126. data["pageNo"] = "1"
  127. data["pageNo"] = "10"
  128. data["qqfyyId"] = "test"
  129. data["qqfyymc"] = "test"
  130. data["qqrId"] = "test"
  131. data["qqr"] = "test"
  132. client.Timeout = time.Duration(httpTimeout) * time.Second
  133. //requestData, _ := json.Marshal(data)
  134. //fmt.Println("request data :", string(requestData))
  135. v := url.Values{}
  136. for k, v1 := range data {
  137. v.Add(k, v1)
  138. }
  139. req, err := http.NewRequest("GET", api+"?"+v.Encode(), nil)
  140. if err != nil {
  141. return nil, err
  142. }
  143. req.Header.Set("Content-Type", "application/json")
  144. req.Header.Set("app-token", token)
  145. resp, err := client.Do(req)
  146. if err != nil {
  147. return nil, err
  148. }
  149. if resp.StatusCode != http.StatusOK {
  150. return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
  151. }
  152. defer resp.Body.Close()
  153. result, err = ioutil.ReadAll(resp.Body)
  154. return
  155. }