123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- // Copyright 2019 autocareai.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package thirdparty
- import (
- "bytes"
- "fmt"
- "gd_vehicle/common.in/id"
- "io/ioutil"
- "net/http"
- "net/url"
- "strings"
- "time"
- "encoding/json"
- "gd_vehicle/common.in/config"
- "gd_vehicle/common.in/utils"
- "go.uber.org/zap"
- )
- func concatGetURL(url string, data map[string]string) string {
- if data == nil {
- return url
- }
- for k, v := range data {
- url = url + "&" + k + "=" + v
- }
- url = strings.Replace(url, "&", "?", 1)
- return url
- }
- func ZrLogin(hostPath string,httpTimeout int) (body []byte, err error) {
- api := hostPath + "/api/login"
- formData := url.Values{}
- //data := make(map[string]string)
- formData.Set("username",config.Conf.ThirdPart.ZrUsername)
- formData.Set("password",config.Conf.ThirdPart.ZrPassword)
- //fmt.Println("login 1111111111111:",config.Conf.ThirdPart.ZrUsername,config.Conf.ThirdPart.ZrPassword)
- reqBody := bytes.NewBufferString(formData.Encode())
- defer func() {
- l.Info("thirdparty",
- zap.String("api", api),
- zap.String("request", formData.Encode()),
- zap.String("response", string(body)))
- }()
- if httpTimeout == 0 {
- httpTimeout = 60
- }
- client := &http.Client{
- Timeout: time.Duration(httpTimeout) * time.Second,
- }
- req, err := http.NewRequest("POST", api, reqBody)
- if err != nil {
- return body, err
- }
- req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
- resp, err := client.Do(req)
- if err != nil {
- return body, err
- }
- defer resp.Body.Close()
- body, err = ioutil.ReadAll(resp.Body)
- if err != nil {
- return body, err
- }
- return body, nil
- }
- func ZrHttpClient(api,token string,data map[string]interface{}, httpTimeout int) (body []byte, err error) {
- defer func() {
- l.Info("thirdparty",
- zap.String("api", api),
- zap.String("request", utils.MarshalJsonString(data)),
- zap.String("response", string(body)))
- }()
- noceId,_ := id.GetDistributedUniqueID()
- uniqId := fmt.Sprintf("%d%d0000",noceId,time.Now().Unix())
- data["userCode"] = uniqId
- data["empowerNo"] = uniqId
- fmt.Println("uniqId11111111111:",uniqId)
- delete(data,"plateType")
- if httpTimeout == 0 {
- httpTimeout = 60
- }
- client := &http.Client{
- Timeout: time.Duration(httpTimeout) * time.Second,
- }
- jsonData, err := json.Marshal(data)
- if err != nil {
- return nil, err
- }
- req, err := http.NewRequest("POST", api, bytes.NewBuffer(jsonData))
- if err != nil {
- return body, err
- }
- //fmt.Println("token:",token)
- req.Header.Add("Content-Type", "application/json")
- req.Header.Add("token",token)
- resp, err := client.Do(req)
- if err != nil {
- return body, err
- }
- defer resp.Body.Close()
- body, err = ioutil.ReadAll(resp.Body)
- if err != nil {
- return body, err
- }
- return body, nil
- }
|