dy.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2019 autocareai.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package thirdparty
  4. import (
  5. "bytes"
  6. "crypto/cipher"
  7. "crypto/des"
  8. "gd_vehicle/common.in/config"
  9. "encoding/base64"
  10. "fmt"
  11. "gd_vehicle/common.in/utils"
  12. "go.uber.org/zap"
  13. "io/ioutil"
  14. "net/http"
  15. "net/url"
  16. "time"
  17. )
  18. func padding(src []byte,blocksize int) []byte {
  19. n:=len(src)
  20. padnum:=blocksize-n%blocksize
  21. pad:=bytes.Repeat([]byte{byte(padnum)},padnum)
  22. dst:=append(src,pad...)
  23. return dst
  24. }
  25. func encryptDES(src []byte,key []byte) []byte {
  26. block,_:=des.NewCipher(key)
  27. src=padding(src,block.BlockSize())
  28. blockmode:=cipher.NewCBCEncrypter(block,key)
  29. blockmode.CryptBlocks(src,src)
  30. return src
  31. }
  32. func DyIdentityVerifyHttpPost(api string, paramData map[string]string, httpTimeout int) (result []byte, err error) {
  33. defer func() {
  34. l.Info("thirdparty",
  35. zap.String("api", api),
  36. zap.String("request", utils.MarshalJsonString(paramData)),
  37. zap.String("response", utils.MarshalJsonString(result)))
  38. }()
  39. client := &http.Client{}
  40. //setHttpProxy(client)
  41. if httpTimeout == 0 {
  42. httpTimeout = 30
  43. }
  44. name := paramData["name"]
  45. idcert := paramData["id_card"]
  46. client.Timeout = time.Duration(httpTimeout) * time.Second
  47. data := fmt.Sprintf("name=%s&identityCard=%s",name,idcert)
  48. r := encryptDES([]byte(data),[]byte(config.Conf.ThirdPart.DyKey))
  49. b :=base64.StdEncoding.EncodeToString(r)
  50. param:= string(b)
  51. a := url.Values{}
  52. a.Add("params", param)
  53. a.Add("apiKey", "RS02100")
  54. a.Add("username", config.Conf.ThirdPart.DyUsername)
  55. a.Add("format", "json")
  56. requestParam := a.Encode()
  57. defer func() {
  58. l.Info("thirdparty",
  59. zap.String("api", api),
  60. zap.String("request", requestParam),
  61. zap.String("response", string(result)))
  62. }()
  63. req, err := http.NewRequest("POST", api+"?"+requestParam, nil)
  64. if err != nil {
  65. return nil, err
  66. }
  67. resp, err := client.Do(req)
  68. if err != nil {
  69. return nil, err
  70. }
  71. if resp.StatusCode != http.StatusOK {
  72. return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
  73. }
  74. defer resp.Body.Close()
  75. result, err = ioutil.ReadAll(resp.Body)
  76. if err != nil {
  77. return nil, err
  78. }
  79. return result, nil
  80. }