123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- package thirdparty
- import (
- "bytes"
- "encoding/json"
- "fmt"
- "gd_service/common.in/config"
- id2 "gd_service/common.in/id"
- "gd_service/common.in/utils"
- "go.uber.org/zap"
- //"gd_service/common.in/config"
- "net/url"
- //"github.com/tidwall/gjson"
- "io/ioutil"
- "net/http"
- "time"
- )
- var publicDybdKey = []byte(`
- -----BEGIN PUBLIC KEY-----
- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDxCRsGHGsaSeavh45EzMtQlx0BvY9TkHuQ1ENF20bLEipKkIFjiZmdiYXXl2GsRj5+yovs5ZkKZZX8zUxN/dPLDqKhTsVn4d8EcZ38QlkTbVNXdbqlmS2OaD0EmJoYbH5ZVY/8AnH0AdonK2x/Xvg4zZeZbGMeN8zmNQf2XwLAVQIDAQAB
- -----END PUBLIC KEY-----
- `)
- func DybdGetToken(appKey string,httpTimeout int)(result []byte, err error){
- defer func() {
- l.Info("thirdparty",
- zap.String("api", config.Conf.ThirdPart.DybdTokenUrl),
- zap.String("request", appKey),
- zap.String("response", utils.MarshalJsonString(result)))
- }()
- if httpTimeout == 0 {
- httpTimeout = 30
- }
- client := &http.Client{}
- data := make(map[string]string)
- data["appkey"] = appKey
- data["input"] = "18782990639"
- data["password"] = "hly@880213"
- client.Timeout = time.Duration(httpTimeout) * time.Second
- bData ,_:= json.Marshal(data)
- req, err := http.NewRequest("POST", config.Conf.ThirdPart.DybdTokenUrl, bytes.NewBufferString(string(bData)))
- if err != nil {
- return nil, err
- }
- req.Header.Set("Content-Type", "application/json")
- 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)
- return
- }
- func DyBdClientPostOld(api string, data map[string]string, httpTimeout int) (result []byte, err error) {
- defer func() {
- l.Info("thirdparty",
- zap.String("api", api),
- zap.String("request", utils.MarshalJsonString(data)),
- zap.String("response", utils.MarshalJsonString(result)))
- }()
- if httpTimeout == 0 {
- httpTimeout = 30
- }
- client := &http.Client{}
- data["pageNo"] = "1"
- data["pageNo"] = "10"
- client.Timeout = time.Duration(httpTimeout) * time.Second
- //requestData, _ := json.Marshal(data)
- //fmt.Println("request data :", string(requestData))
- v := url.Values{}
- for k, v1 := range data {
- v.Add(k, v1)
- }
- req, err := http.NewRequest("GET", api+"?"+v.Encode(), nil)
- if err != nil {
- return nil, err
- }
- now := time.Now()
- id, _ := id2.GetDistributedUniqueID()
- nonce := fmt.Sprintf("%d", id)
- aesKey := fmt.Sprintf("%d", now.UnixNano()/1e3)
- stamp := fmt.Sprintf("%d", now.UnixNano()/1e6)
- header := map[string]string{}
- header["appId"] = "test010"
- header["appSecret"] = "VePz0pi@"
- header["timestamp"] = stamp
- header["nonce"] = nonce
- aesEncryptData, err := config.Base64RsaEncrypt([]byte(aesKey), publicDybdKey)
- if err != nil {
- return nil, err
- }
- header["aesSecretEncrypt"] = string(aesEncryptData)
- signStr := fmt.Sprintf("%s:%s:%s:%s:%s", header["appId"], header["appSecret"], header["aesSecretEncrypt"], header["timestamp"], header["nonce"])
- sign, err := config.Base64AESECBEncrypt(signStr, aesKey)
- if err != nil {
- return nil, err
- }
- header["sign"] = string(sign)
- for k, v := range header {
- req.Header.Set(k, v)
- }
- req.Header.Set("Content-Type", "application/json")
- 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)
- return
- }
- func DyBdClientPost(api string, data map[string]string, httpTimeout int,token string) (result []byte, err error) {
- defer func() {
- l.Info("thirdparty",
- zap.String("api", api),
- zap.String("request", utils.MarshalJsonString(data)),
- zap.String("response", utils.MarshalJsonString(result)))
- }()
- if httpTimeout == 0 {
- httpTimeout = 30
- }
- client := &http.Client{}
- data["pageNo"] = "1"
- data["pageNo"] = "10"
- data["qqfyyId"] = "test"
- data["qqfyymc"] = "test"
- data["qqrId"] = "test"
- data["qqr"] = "test"
- client.Timeout = time.Duration(httpTimeout) * time.Second
- //requestData, _ := json.Marshal(data)
- //fmt.Println("request data :", string(requestData))
- v := url.Values{}
- for k, v1 := range data {
- v.Add(k, v1)
- }
- req, err := http.NewRequest("GET", api+"?"+v.Encode(), nil)
- if err != nil {
- return nil, err
- }
- req.Header.Set("Content-Type", "application/json")
- req.Header.Set("app-token", token)
- 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)
- return
- }
|