From eec044c49cea550d123c63123ad454af08699c7e Mon Sep 17 00:00:00 2001 From: richboo111 Date: Fri, 13 Jan 2023 16:26:26 +0800 Subject: [PATCH] 1 --- platformapi/uuptapi/uupt_test.go | 90 ++++++------- platformapi/uuptapi/uuptapi.go | 218 ++++++++++++++++--------------- platformapi/uuptapi/waybill.go | 57 ++++---- 3 files changed, 186 insertions(+), 179 deletions(-) diff --git a/platformapi/uuptapi/uupt_test.go b/platformapi/uuptapi/uupt_test.go index fd7e8e38..81e1159a 100644 --- a/platformapi/uuptapi/uupt_test.go +++ b/platformapi/uuptapi/uupt_test.go @@ -1,14 +1,29 @@ package uuptapi -// -//import ( -// "fmt" -// "testing" -//) -// -//var ( -// api = New("55c4542ae60e4d348edcfc93b06dd302", "76b362c06b1b4baa9e47bab6387a5356", "8d8464e7c9354c1e88a3f5afa2a7922e") -//) +import ( + "fmt" + "git.rosy.net.cn/baseapi" + "git.rosy.net.cn/baseapi/utils" + "go.uber.org/zap" + "testing" + "time" +) + +var ( + api *API = New("55c4542ae60e4d348edcfc93b06dd302", "76b362c06b1b4baa9e47bab6387a5356", "8d8464e7c9354c1e88a3f5afa2a7922e") + sugarLogger *zap.SugaredLogger +) + +func init() { + logger, _ := zap.NewDevelopment() + sugarLogger = logger.Sugar() + baseapi.Init(sugarLogger) + + //api = New("6705486294797503379", "c1e6c280-e618-4103-9d0a-673bc54fb22e", "5375691", "cabrXQf9eFMVWVYg4hNlwu") + //token, _ := api.GetAccessToken() + //api.accessToken = token.BusinessDataObj.AccessToken +} + // //func TestSignParam(t *testing.T) { // testStr := map[string]interface{}{ @@ -25,39 +40,24 @@ package uuptapi // fmt.Print(randStr()) //} // -////计算运单价格 -//func TestGetWaybillPrice(t *testing.T) { -// needPayMoney, priceToken, err := api.GetOrderPrice(&GetOrderPriceReq{ -// //BaseReqInfo: &BaseReqInfo{ -// //Sign: "A9D4D4380B6012F2E9A00E37243306D4", -// // NonceStr: "01c3ce7ce7604ab49d12ec69dfbdcfe8", -// //Timestamp: "1672998838", -// // OpenID: "8d8464e7c9354c1e88a3f5afa2a7922e", -// // AppID: "55c4542ae60e4d348edcfc93b06dd302", -// //}, -// OriginID: "T00001", -// FromAddress: "郑州国际会展中心", -// ToAddress: "商务外环路17号", -// CityName: "郑州市", -// SendType: "0", -// ToLat: "34.784275", -// ToLng: "113.736445", -// FromLat: "34.778494", -// FromLng: "113.736858", -// }) -// -// fmt.Println(needPayMoney, priceToken, err) -//} -// -//type testStruct struct { -// name string -//} -// -//func Test(t *testing.T) { -// var temp testStruct -// //var test bool -// //test = true -// //temp.name = "hdueu" -// fmt.Println(temp == testStruct{}) -// //fmt.Print(utils.IsNil(temp)) -//} +//计算运单价格 +func TestGetWaybillPrice(t *testing.T) { + + needPayMoney, priceToken, err := api.GetOrderPrice(&GetOrderPriceReq{ + OriginID: "T00001", + FromAddress: "郑州国际会展中心", + ToAddress: "商务外环路17号", + CityName: "郑州市", + SendType: "0", + ToLat: "34.784275", + ToLng: "113.736445", + FromLat: "34.778494", + FromLng: "113.736858", + }) + + fmt.Println(needPayMoney, priceToken, err) +} + +func Test(t *testing.T) { + fmt.Println(utils.Int64ToStr(time.Now().Unix() * 1000)) +} diff --git a/platformapi/uuptapi/uuptapi.go b/platformapi/uuptapi/uuptapi.go index 27e6e5cb..f5047d6f 100644 --- a/platformapi/uuptapi/uuptapi.go +++ b/platformapi/uuptapi/uuptapi.go @@ -1,107 +1,115 @@ package uuptapi -// -//import ( -// "crypto/md5" -// "encoding/json" -// "fmt" -// "git.rosy.net.cn/baseapi/platformapi" -// "git.rosy.net.cn/baseapi/utils" -// "math/rand" -// "net/http" -// "sort" -// "strings" -// "time" -//) -// -//const ( -// BaseURL = "https://openapi.uupt.com/v2_0" -// ReturnSuccess = "ok" -// ReturnFail = "fail" -// signKey = "sign" -// secretKey = "secret" -// UUOpenID = "8d8464e7c9354c1e88a3f5afa2a7922e" -// UUAppID = "55c4542ae60e4d348edcfc93b06dd302" -// letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" -//) -// -//func (a *API) MakeUURequestHead() map[string]interface{} { -// requestParam := make(map[string]interface{}, 5) -// requestParam["sign"] = a.sign -// requestParam["timestamp"] = a.timestamp -// requestParam["nonce_str"] = a.noncestr -// requestParam["openid"] = a.openid -// requestParam["appid"] = a.appid -// return requestParam -//} -//func New(appID, appKey, openID string) *API { -// if appID == "" { -// return nil -// } -// return &API{ -// appid: appID, -// appKey: appKey, -// openid: openID, -// } -//} -// -//func (a *API) signParam(params map[string]interface{}) (sign string) { -// keyValues := make([]string, 0) -// for k, v := range params { -// if k != signKey { -// if temp := fmt.Sprint(v); temp != "" { -// keyValues = append(keyValues, k+"="+temp) -// } -// } -// } -// sort.Sort(sort.StringSlice(keyValues)) //字典升序 -// sign = strings.Join(keyValues, "&") -// sign += "&key=" + a.appKey //末尾拼接密钥 -// sign = strings.ToUpper(sign) //大写 -// return fmt.Sprintf("%X", md5.Sum([]byte(sign))) -//} -// -//func (a *API) AccessAPI(baseUrl, actionApi, method string, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) { -// bizParams["timestamp"] = utils.Int64ToStr(time.Now().Unix() * 1000) -// bizParams["sign"] = a.signParam(bizParams) -// //序列化 -// data, err := json.Marshal(bizParams) -// if err != nil { -// return nil, err -// } -// //完整请求url -// fullPath := utils.GenerateGetURL(baseUrl, actionApi, nil) -// //发送请求 -// sendUrl := func() *http.Request { -// var request *http.Request -// if method == RequestPost { -// request, _ = http.NewRequest(http.MethodPost, fullPath, strings.NewReader(string(data))) -// } else { -// request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(baseUrl, actionApi, bizParams), nil) -// } -// return request -// } -// //数据解析 -// dataMarshal := func(response *http.Response, bodyStr string, jsonResp map[string]interface{}) (errMsg string, err error) { -// if jsonResp == nil { -// return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil") -// } -// if jsonResp["return_code"] != ReturnSuccess { -// errMsg = platformapi.ErrLevelGeneralFail -// err = utils.NewErrorCode(jsonResp["return_msg"].(string), utils.Int64ToStr(utils.MustInterface2Int64(jsonResp["return_code"]))) -// } -// retVal = jsonResp -// return errMsg, err -// } -// err = platformapi.AccessPlatformAPIWithRetry(a.client, sendUrl, a.config, dataMarshal) -// return retVal, err -//} -// -////以下为辅助函数 -//func randStr() string { -// b := make([]byte, 16) -// for i := range b { -// b[i] = letters[rand.Intn(len(letters))] -// } -// return string(b) -//} +import ( + "crypto/md5" + "encoding/json" + "fmt" + "git.rosy.net.cn/baseapi/platformapi" + "git.rosy.net.cn/baseapi/utils" + "math/rand" + "net/http" + "sort" + "strings" + "time" +) + +const ( + BaseURL = "https://openapi.uupt.com/v2_0" + ReturnSuccess = "ok" + ReturnFail = "fail" + signKey = "sign" + secretKey = "secret" + UUOpenID = "8d8464e7c9354c1e88a3f5afa2a7922e" + UUAppID = "55c4542ae60e4d348edcfc93b06dd302" + letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" +) + +func (a *API) MakeUURequestHead() map[string]interface{} { + requestParam := make(map[string]interface{}, 5) + requestParam["sign"] = a.sign + requestParam["timestamp"] = a.timestamp + requestParam["nonce_str"] = a.noncestr + requestParam["openid"] = a.openid + requestParam["appid"] = a.appid + return requestParam +} +func New(appID, appKey, openID string, config ...*platformapi.APIConfig) *API { + curConfig := platformapi.DefAPIConfig + if len(config) > 0 { + curConfig = *config[0] + } + if appID == "" { + return nil + } + return &API{ + appid: appID, + appKey: appKey, + openid: openID, + client: &http.Client{Timeout: curConfig.ClientTimeout}, + config: &curConfig, + } +} + +func (a *API) signParam(params map[string]interface{}) (sign string) { + keyValues := make([]string, 0) + for k, v := range params { + if k != signKey { + if temp := fmt.Sprint(v); temp != "" { + keyValues = append(keyValues, k+"="+temp) + } + } + } + sort.Sort(sort.StringSlice(keyValues)) //字典升序 + sign = strings.Join(keyValues, "&") + sign += "&key=" + a.appKey //末尾拼接密钥 + sign = strings.ToUpper(sign) //大写 + return fmt.Sprintf("%X", md5.Sum([]byte(sign))) +} + +func (a *API) AccessAPI(baseUrl, actionApi, method string, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) { + bizParams["timestamp"] = utils.Int64ToStr(time.Now().Unix() * 1000) + bizParams["nonce_str"] = randStr() + bizParams["sign"] = a.signParam(bizParams) + //序列化 + data, err := json.Marshal(bizParams) + if err != nil { + return nil, err + } + //完整请求url + fullPath := utils.GenerateGetURL(baseUrl, actionApi, nil) + //发送请求 + sendUrl := func() *http.Request { + var request *http.Request + if method == RequestPost { + request, _ = http.NewRequest(http.MethodPost, fullPath, strings.NewReader(string(data))) + } else { + request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(baseUrl, actionApi, bizParams), nil) + } + //request.Header.Set("Content-Type", "form-data") + request.Header.Set("Content-Type", "application/json") + return request + } + //数据解析 + dataMarshal := func(response *http.Response, bodyStr string, jsonResp map[string]interface{}) (errMsg string, err error) { + if jsonResp == nil { + return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil") + } + if jsonResp["return_code"] != ReturnSuccess { + errMsg = platformapi.ErrLevelGeneralFail + err = utils.NewErrorCode(jsonResp["return_msg"].(string), utils.Int64ToStr(utils.MustInterface2Int64(jsonResp["return_code"]))) + } + retVal = jsonResp + return errMsg, err + } + err = platformapi.AccessPlatformAPIWithRetry(a.client, sendUrl, a.config, dataMarshal) + return retVal, err +} + +//以下为辅助函数 +func randStr() string { + b := make([]byte, 16) + for i := range b { + b[i] = letters[rand.Intn(len(letters))] + } + return string(b) +} diff --git a/platformapi/uuptapi/waybill.go b/platformapi/uuptapi/waybill.go index 00e80b1f..5901a924 100644 --- a/platformapi/uuptapi/waybill.go +++ b/platformapi/uuptapi/waybill.go @@ -1,31 +1,30 @@ package uuptapi -// -//import ( -// "encoding/json" -// "git.rosy.net.cn/baseapi/utils" -//) -// -////计算订单价格 -//func (a *API) GetOrderPrice(req *GetOrderPriceReq) (needPayMoney, priceToken string, err error) { -// preOrder := a.MakeUURequestHead() -// bytes, err := json.Marshal(req) -// if err != nil { -// return "", "", err -// } -// preOrder["param"] = string(bytes) -// resp, err := a.AccessAPI(BaseURL, "getorderprice.ashx", RequestPost, preOrder) -// if err != nil { -// return "", "", err -// } -// retVal := GetOrderPriceResp{} -// if err := json.Unmarshal([]byte(resp["param"].(string)), &retVal); err != nil { -// return "", "", err -// } -// con := retVal == GetOrderPriceResp{} -// if !utils.IsNil(retVal) && !con { -// needPayMoney = retVal.NeedPayMoney -// priceToken = retVal.PriceToken -// } -// return needPayMoney, priceToken, nil -//} +import ( + "encoding/json" + "git.rosy.net.cn/baseapi/utils" +) + +//计算订单价格 +func (a *API) GetOrderPrice(req *GetOrderPriceReq) (needPayMoney, priceToken string, err error) { + preOrder := a.MakeUURequestHead() + bytes, err := json.Marshal(req) + if err != nil { + return "", "", err + } + preOrder["param"] = string(bytes) + resp, err := a.AccessAPI(BaseURL, "getorderprice.ashx", RequestPost, preOrder) + if err != nil { + return "", "", err + } + retVal := GetOrderPriceResp{} + if err := json.Unmarshal([]byte(resp["param"].(string)), &retVal); err != nil { + return "", "", err + } + con := retVal == GetOrderPriceResp{} + if !utils.IsNil(retVal) && !con { + needPayMoney = retVal.NeedPayMoney + priceToken = retVal.PriceToken + } + return needPayMoney, priceToken, nil +}