utimes_db.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package thirdparty
  2. import (
  3. "bytes"
  4. "gd_management/common.in/config"
  5. "encoding/json"
  6. "go.uber.org/zap"
  7. "io/ioutil"
  8. "net/http"
  9. "net/url"
  10. "strconv"
  11. "time"
  12. "gd_management/common.in/utils"
  13. )
  14. func UtimesHttpPost(api string, data map[string]string) (result []byte, err error) {
  15. defer func() {
  16. l.Info("thirdparty",
  17. zap.String("api", api),
  18. zap.String("request", utils.MarshalJsonString(data)),
  19. zap.String("response", utils.MarshalJsonString(result)))
  20. }()
  21. if data == nil {
  22. data = make(map[string]string)
  23. }
  24. data["dwkey"] = config.Conf.ThirdPart.DbDwkey
  25. data["to_ken"] = config.Conf.ThirdPart.DbToken
  26. data["sjc"] = strconv.FormatInt(time.Now().Unix(), 10)
  27. v, err := json.Marshal(data)
  28. if err != nil {
  29. return nil, err
  30. }
  31. encrypt, err := utils.DesEncrypt(utimesEncryptkey(), utimesEncryptIv(), v)
  32. if err != nil {
  33. return nil, err
  34. }
  35. values := url.Values{}
  36. values.Set("data", string(utils.Base64Encode(encrypt)))
  37. client := &http.Client{}
  38. client.Timeout = 60 * time.Second
  39. req, err := http.NewRequest("POST", api, bytes.NewBufferString(values.Encode()))
  40. if err != nil {
  41. return nil, err
  42. }
  43. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  44. resp, err := client.Do(req)
  45. if err != nil {
  46. return nil, err
  47. }
  48. defer resp.Body.Close()
  49. body, err := ioutil.ReadAll(resp.Body)
  50. if err != nil {
  51. return nil, err
  52. }
  53. unBase64, err := utils.Base64Decode(body)
  54. if err != nil {
  55. return nil, err
  56. }
  57. result, err = utils.DesDecrypt(utimesEncryptkey(), utimesEncryptIv(), unBase64)
  58. return result, err
  59. }
  60. func utimesEncryptkey() []byte {
  61. return []byte(config.Conf.ThirdPart.DbEncKey)
  62. }
  63. func utimesEncryptIv() []byte {
  64. return []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
  65. }