123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- // Copyright 2019 autocareai.com. All rights reserved.
- // Use of this source code is governed by autocareai.com.
- package thirdparty
- import (
- "bytes"
- "fmt"
- "io/ioutil"
- "net/http"
- "strings"
- "time"
- "encoding/json"
- "gd_service/common.in/config"
- "gd_service/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"
- data := make(map[string]string)
- data["username"] = config.Conf.ThirdPart.ZrUsername
- data["password"] = config.Conf.ThirdPart.ZrPassword
- defer func() {
- l.Info("thirdparty",
- zap.String("api", api),
- zap.String("request", utils.MarshalJsonString(data)),
- zap.String("response", string(body)))
- }()
- if httpTimeout == 0 {
- httpTimeout = 60
- }
- client := &http.Client{
- Timeout: time.Duration(httpTimeout) * time.Second,
- }
- req, err := http.NewRequest("POST", concatGetURL(api, data), nil)
- if err != nil {
- return body, err
- }
- req.Header.Add("Content-Type", "application/json")
- 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]string, 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)))
- }()
- 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
- }
|