From c4223bbf963e00f12d38501967193333641835aa Mon Sep 17 00:00:00 2001 From: richboo111 Date: Mon, 9 Jan 2023 13:50:02 +0800 Subject: [PATCH] 1 --- .../tiktok_shop/tiktok_api/transport_test.go | 31 ++- platformapi/uuptapi/uupt_model.go | 6 +- platformapi/uuptapi/uupt_test.go | 84 +++++-- platformapi/uuptapi/uuptapi.go | 206 +++++++++--------- platformapi/uuptapi/waybill.go | 41 +++- 5 files changed, 227 insertions(+), 141 deletions(-) diff --git a/platformapi/tiktok_shop/tiktok_api/transport_test.go b/platformapi/tiktok_shop/tiktok_api/transport_test.go index 201c4089..7ae010f9 100644 --- a/platformapi/tiktok_shop/tiktok_api/transport_test.go +++ b/platformapi/tiktok_shop/tiktok_api/transport_test.go @@ -3,6 +3,7 @@ package tiktok_api import ( "fmt" "git.rosy.net.cn/baseapi/utils" + "reflect" "testing" ) @@ -16,10 +17,34 @@ func TestCancelWaybill(t *testing.T) { err := a.ShopOrderDispatcher(62490423, "5017358149445080799", 2) fmt.Println(err) } + +type tete struct { + ids []int64 + name string +} + func Test(t *testing.T) { - a := make([]int64, 0) - Strings2Objs(`[1262627,1723872383]`, &a) - fmt.Println(a) + //var a []int64 + var b interface{} + //stType := reflect.TypeOf(a) + //stValue := reflect.ValueOf(a) + x := reflect.TypeOf(b) + fmt.Println(reflect.TypeOf(a) == reflect.TypeOf([]int64{})) + switch x { + case reflect.TypeOf([]int64{}): + + fmt.Println(1) + case reflect.TypeOf([]string{}): + fmt.Println(2) + + } + //fmt.Println(stType.NumField()) + //fmt.Println(stType.Field(0)) + //temp := stType.Field(0) + //fmt.Println(temp.Type) + + //Strings2Objs(`[1262627,1723872383]`, &a) + //fmt.Printf("%v", reflect.TypeOf(a).NumField()) } func Strings2Objs(strAndObjAddPairs ...interface{}) (err error) { str := "" diff --git a/platformapi/uuptapi/uupt_model.go b/platformapi/uuptapi/uupt_model.go index 121ec59b..84e6500d 100644 --- a/platformapi/uuptapi/uupt_model.go +++ b/platformapi/uuptapi/uupt_model.go @@ -122,9 +122,8 @@ type GetOrderPriceReq struct { //计算订单价格返回体 type GetOrderPriceResp struct { - BaseRespInfo *BaseRespInfo + //必返回 OriginID string `json:"origin_id"` //第三方对接平台订单id - PriceToken string `json:"price_token"` //金额令牌,提交订单前必须先计算价格 TotalMoney string `json:"total_money"` //订单总金额(优惠前) NeedPayMoney string `json:"need_paymoney"` //实际需要支付金额 TotalPriceOff string `json:"total_priceoff"` //总优惠金额 @@ -133,6 +132,9 @@ type GetOrderPriceResp struct { AddFee string `json:"addfee"` //加价金额 GoodsInsuranceMoney string `json:"goods_insurancemoney"` //商品保价金额 ExpiresIn string `json:"expires_in"` //price_token的过期时间(单位:秒) + //非必返回 + PriceToken string `json:"price_token"` //金额令牌,提交订单前必须先计算价格 + BaseRespInfo *BaseRespInfo } //发布订单请求体 diff --git a/platformapi/uuptapi/uupt_test.go b/platformapi/uuptapi/uupt_test.go index 09a0f23a..fd7e8e38 100644 --- a/platformapi/uuptapi/uupt_test.go +++ b/platformapi/uuptapi/uupt_test.go @@ -1,25 +1,63 @@ package uuptapi -import ( - "fmt" - "testing" -) - -var ( - api = New("55c4542ae60e4d348edcfc93b06dd302", "76b362c06b1b4baa9e47bab6387a5356", "8d8464e7c9354c1e88a3f5afa2a7922e") -) - -func TestSignParam(t *testing.T) { - testStr := map[string]interface{}{ - "origin_id": "T00001", - "from_address": "郑州国际会展中心", - "from_usernote": "1层", - "to_address": "商务外环路17号", - "to_usernote": "1楼101", - } - ans := api.signParam(testStr) - fmt.Print(ans) -} -func TestRandStr(t *testing.T) { - fmt.Print(randStr()) -} +// +//import ( +// "fmt" +// "testing" +//) +// +//var ( +// api = New("55c4542ae60e4d348edcfc93b06dd302", "76b362c06b1b4baa9e47bab6387a5356", "8d8464e7c9354c1e88a3f5afa2a7922e") +//) +// +//func TestSignParam(t *testing.T) { +// testStr := map[string]interface{}{ +// "origin_id": "T00001", +// "from_address": "郑州国际会展中心", +// "from_usernote": "1层", +// "to_address": "商务外环路17号", +// "to_usernote": "1楼101", +// } +// ans := api.signParam(testStr) +// fmt.Print(ans) +//} +//func TestRandStr(t *testing.T) { +// 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)) +//} diff --git a/platformapi/uuptapi/uuptapi.go b/platformapi/uuptapi/uuptapi.go index 2d610c18..27e6e5cb 100644 --- a/platformapi/uuptapi/uuptapi.go +++ b/platformapi/uuptapi/uuptapi.go @@ -1,103 +1,107 @@ 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 ( - ReturnSuccess = "ok" - ReturnFail = "fail" - signKey = "sign" - secretKey = "secret" - 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) *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) +//} diff --git a/platformapi/uuptapi/waybill.go b/platformapi/uuptapi/waybill.go index 0bdb4981..00e80b1f 100644 --- a/platformapi/uuptapi/waybill.go +++ b/platformapi/uuptapi/waybill.go @@ -1,14 +1,31 @@ package uuptapi -import "encoding/json" - -//计算订单价格 -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) - return "", "", 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 +//}