1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- // Copyright 2019 autocareai.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package thirdparty
- import (
- "bytes"
- "crypto/cipher"
- "crypto/des"
- "gd_vehicle/common.in/config"
- "encoding/base64"
- "fmt"
- "gd_vehicle/common.in/utils"
- "go.uber.org/zap"
- "io/ioutil"
- "net/http"
- "net/url"
- "time"
- )
- func padding(src []byte,blocksize int) []byte {
- n:=len(src)
- padnum:=blocksize-n%blocksize
- pad:=bytes.Repeat([]byte{byte(padnum)},padnum)
- dst:=append(src,pad...)
- return dst
- }
- func encryptDES(src []byte,key []byte) []byte {
- block,_:=des.NewCipher(key)
- src=padding(src,block.BlockSize())
- blockmode:=cipher.NewCBCEncrypter(block,key)
- blockmode.CryptBlocks(src,src)
- return src
- }
- func DyIdentityVerifyHttpPost(api string, paramData map[string]string, httpTimeout int) (result []byte, err error) {
- defer func() {
- l.Info("thirdparty",
- zap.String("api", api),
- zap.String("request", utils.MarshalJsonString(paramData)),
- zap.String("response", utils.MarshalJsonString(result)))
- }()
- client := &http.Client{}
- //setHttpProxy(client)
- if httpTimeout == 0 {
- httpTimeout = 30
- }
- name := paramData["name"]
- idcert := paramData["id_card"]
- client.Timeout = time.Duration(httpTimeout) * time.Second
- data := fmt.Sprintf("name=%s&identityCard=%s",name,idcert)
- r := encryptDES([]byte(data),[]byte(config.Conf.ThirdPart.DyKey))
- b :=base64.StdEncoding.EncodeToString(r)
- param:= string(b)
- a := url.Values{}
- a.Add("params", param)
- a.Add("apiKey", "RS02100")
- a.Add("username", config.Conf.ThirdPart.DyUsername)
- a.Add("format", "json")
- requestParam := a.Encode()
- defer func() {
- l.Info("thirdparty",
- zap.String("api", api),
- zap.String("request", requestParam),
- zap.String("response", string(result)))
- }()
- req, err := http.NewRequest("POST", api+"?"+requestParam, nil)
- if err != nil {
- return nil, err
- }
- resp, err := client.Do(req)
- if err != nil {
- return nil, err
- }
- if resp.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
- }
- defer resp.Body.Close()
- result, err = ioutil.ReadAll(resp.Body)
- if err != nil {
- return nil, err
- }
- return result, nil
- }
|