From 51da9471030aa37a7c20a6d94a1e03964ec6f25b Mon Sep 17 00:00:00 2001 From: gazebo Date: Wed, 6 Jun 2018 14:49:07 +0800 Subject: [PATCH] - export low level jdapi. - access jd order info. --- platform/jdapi/jdapi.go | 100 +++++++++++++++++------------------ platform/jdapi/jdapi_test.go | 12 ++--- platform/jdapi/order.go | 53 +++++++++++++++++-- utils/utils_test.go | 10 ---- 4 files changed, 105 insertions(+), 70 deletions(-) diff --git a/platform/jdapi/jdapi.go b/platform/jdapi/jdapi.go index 9236e80e..081f5788 100644 --- a/platform/jdapi/jdapi.go +++ b/platform/jdapi/jdapi.go @@ -125,7 +125,7 @@ func NewJDAPI(token, appKey, appSecret string, logger *zap.Logger) *JDAPI { return &JDAPI{token, appKey, appSecret, logger, logger.Sugar(), http.Client{Timeout: time.Second * 10}} } -func (j JDAPI) accessJDQuery(apiStr string, jdParams map[string]string) (result map[string]interface{}, err error) { +func (j JDAPI) AccessJDQuery(apiStr string, jdParams map[string]string) (map[string]interface{}, error) { params := make(map[string]string) params["v"] = "1.0" params["format"] = "json" @@ -211,8 +211,8 @@ func (j JDAPI) accessJDQuery(apiStr string, jdParams map[string]string) (result } } -func (j JDAPI) accessJDQueryNoPage(apiStr string, jdParams map[string]string, keyToRemove, keyToKeep []string) (interface{}, error) { - jsonResult, err := j.accessJDQuery(apiStr, jdParams) +func (j JDAPI) AccessJDQueryNoPage(apiStr string, jdParams map[string]string, keyToRemove, keyToKeep []string) (interface{}, error) { + jsonResult, err := j.AccessJDQuery(apiStr, jdParams) if err != nil { return jsonResult, err } @@ -244,57 +244,57 @@ func (j JDAPI) accessJDQueryNoPage(apiStr string, jdParams map[string]string, ke } } -func (j JDAPI) accessJDQueryHavePage(apiStr string, jdParams map[string]string, keyToRemove, keyToKeep []string, pageResultParser PageResultParser) ([]interface{}, error) { - normalJDQueryHavePageResultParser := func(data map[string]interface{}, totalCount int) ([]interface{}, int) { - var result map[string]interface{} - var retVal []interface{} +func NormalJDQueryHavePageResultParser(data map[string]interface{}, totalCount int) ([]interface{}, int) { + var result map[string]interface{} + var retVal []interface{} - tempResult := data["result"] - if resultStr, ok := tempResult.(string); ok { - if err := utils.UnmarshalUseNumber([]byte(resultStr), &tempResult); err != nil { - panic("Wrong format") - } + tempResult := data["result"] + if resultStr, ok := tempResult.(string); ok { + if err := utils.UnmarshalUseNumber([]byte(resultStr), &tempResult); err != nil { + panic("Wrong format") } - - result = tempResult.(map[string]interface{}) - - if totalCount == 0 { - for _, totalCountKey := range jdResultPageTotalCountKeys { - if totalCount2, ok := result[totalCountKey]; ok { - totalCountInt64, _ := totalCount2.(json.Number).Int64() - totalCount = int(totalCountInt64) - if totalCount == 0 { - return make([]interface{}, 0), 0 - } - break - } - } - if totalCount == 0 { - panic("can not find totalCount key") - } - } - - for _, inner2ResultKey := range jdResultPageInner2DataKeys { - if inner2Result, ok := result[inner2ResultKey]; ok { - - if inner2ResultStr, ok := inner2Result.(string); ok { - err := utils.UnmarshalUseNumber([]byte(inner2ResultStr), &retVal) - if err != nil { - panic("can not unmarshal inner2result") - } - } else { - retVal = inner2Result.([]interface{}) - } - - return retVal, totalCount - } - } - - panic("wrong format") } + result = tempResult.(map[string]interface{}) + + if totalCount == 0 { + for _, totalCountKey := range jdResultPageTotalCountKeys { + if totalCount2, ok := result[totalCountKey]; ok { + totalCountInt64, _ := totalCount2.(json.Number).Int64() + totalCount = int(totalCountInt64) + if totalCount == 0 { + return make([]interface{}, 0), 0 + } + break + } + } + if totalCount == 0 { + panic("can not find totalCount key") + } + } + + for _, inner2ResultKey := range jdResultPageInner2DataKeys { + if inner2Result, ok := result[inner2ResultKey]; ok { + + if inner2ResultStr, ok := inner2Result.(string); ok { + err := utils.UnmarshalUseNumber([]byte(inner2ResultStr), &retVal) + if err != nil { + panic("can not unmarshal inner2result") + } + } else { + retVal = inner2Result.([]interface{}) + } + + return retVal, totalCount + } + } + + panic("wrong format") +} + +func (j JDAPI) AccessJDQueryHavePage(apiStr string, jdParams map[string]string, keyToRemove, keyToKeep []string, pageResultParser PageResultParser) ([]interface{}, error) { if pageResultParser == nil { - pageResultParser = normalJDQueryHavePageResultParser + pageResultParser = NormalJDQueryHavePageResultParser } localJdParams := make(map[string]string) @@ -323,7 +323,7 @@ func (j JDAPI) accessJDQueryHavePage(apiStr string, jdParams map[string]string, for { localJdParams["pageNo"] = strconv.FormatInt(int64(curPage), 10) localJdParams["pageSize"] = strconv.FormatInt(int64(pageSize), 10) - jsonResult, err := j.accessJDQuery(apiStr, localJdParams) + jsonResult, err := j.AccessJDQuery(apiStr, localJdParams) if err != nil { return nil, err } diff --git a/platform/jdapi/jdapi_test.go b/platform/jdapi/jdapi_test.go index f52300ed..5f049691 100644 --- a/platform/jdapi/jdapi_test.go +++ b/platform/jdapi/jdapi_test.go @@ -23,9 +23,9 @@ func TestTest(t *testing.T) { } func TestAccessJDQuery(t *testing.T) { - result, err := jdapi.accessJDQuery("address/allcities", nil) + result, err := jdapi.AccessJDQuery("address/allcities", nil) if err != nil { - t.Fatalf("Error when accessing accessJDQuery: %v", err) + t.Fatalf("Error when accessing AccessJDQuery: %v", err) } else { code := result["code"].(string) if code != "0" { @@ -35,9 +35,9 @@ func TestAccessJDQuery(t *testing.T) { } func TestAccessJDQueryNoPage(t *testing.T) { - result, err := jdapi.accessJDQueryNoPage("address/allcities", nil, []string{"yn"}, nil) + result, err := jdapi.AccessJDQueryNoPage("address/allcities", nil, []string{"yn"}, nil) if err != nil { - t.Fatalf("accessJDQueryNoPage return error:%v", err) + t.Fatalf("AccessJDQueryNoPage return error:%v", err) } cityInfo := result.([]interface{}) if len(cityInfo) == 0 { @@ -57,9 +57,9 @@ func TestAccessJDQueryHavePage(t *testing.T) { "pageNo": "1", "pageSize": "20", } - skuInfo, err := jdapi.accessJDQueryHavePage("pms/querySkuInfos", jdParams, nil, []string{"skuName", "skuId"}, nil) + skuInfo, err := jdapi.AccessJDQueryHavePage("pms/querySkuInfos", jdParams, nil, []string{"skuName", "skuId"}, nil) if err != nil { - t.Fatalf("accessJDQueryHavePage return error:%v", err) + t.Fatalf("AccessJDQueryHavePage return error:%v", err) } if len(skuInfo) == 0 { diff --git a/platform/jdapi/order.go b/platform/jdapi/order.go index 5e14b90d..0d65aece 100644 --- a/platform/jdapi/order.go +++ b/platform/jdapi/order.go @@ -1,18 +1,63 @@ package jdapi import ( + "encoding/json" + "git.rosy.net.cn/baseapi/utils" ) +type LegacyJDOrderInfo struct { + Code string `json:"code"` + Msg string `json:"msg"` + Result map[string]interface{} `json:"result"` + Detail string `json:"detail"` + Success bool `json:"success"` + OrderStatus int `json:"-"` + OrderStatusTime string `json:"-"` +} + func (j JDAPI) OrderQuery(jdParams map[string]string) (retVal []interface{}, err error) { - retVal, err = j.accessJDQueryHavePage("order/es/query", jdParams, nil, nil, nil) + retVal, err = j.AccessJDQueryHavePage("order/es/query", jdParams, nil, nil, nil) return } -func (j JDAPI) QuerySignleOrder(orderId string) ([]interface{}, error) { +func (j JDAPI) QuerySingleOrder(orderId string) ([]interface{}, error) { jdParams := make(map[string]string) jdParams["orderId"] = orderId - return j.accessJDQueryHavePage("order/es/query", jdParams, nil, nil, nil) + return j.AccessJDQueryHavePage("order/es/query", jdParams, nil, nil, nil) +} + +func (j JDAPI) LegacyQuerySingleOrder(orderId string) (*LegacyJDOrderInfo, error) { + jdParams := make(map[string]string) + jdParams["orderId"] = orderId + + result, err := j.AccessJDQuery("order/es/query", jdParams) + if err != nil { + return nil, err + } + + dataStr, _ := result["data"].(string) + var data map[string]interface{} + utils.UnmarshalUseNumber([]byte(dataStr), &data) + + resultList, _ := NormalJDQueryHavePageResultParser(data, 0) + orderInfo, _ := resultList[0].(map[string]interface{}) + orderStatus64, _ := orderInfo["orderStatus"].(json.Number).Int64() + + var dataResult map[string]interface{} + utils.UnmarshalUseNumber([]byte(data["result"].(string)), &dataResult) + dataResult["resultList"] = resultList + + retVal := &LegacyJDOrderInfo{} + retVal.OrderStatus = int(orderStatus64) + retVal.OrderStatusTime = orderInfo["orderStatusTime"].(string) + retVal.Code = result["code"].(string) + retVal.Msg = result["msg"].(string) + + retVal.Result = dataResult + retVal.Success = true + + return retVal, nil } func (j JDAPI) OrderAcceptOperate(orderId string, isAgreed bool) (interface{}, error) { @@ -21,5 +66,5 @@ func (j JDAPI) OrderAcceptOperate(orderId string, isAgreed bool) (interface{}, e "isAgreed": utils.Bool2String(isAgreed), "operator": getJDOperator(), } - return j.accessJDQueryNoPage("order/es/query", jdParams, nil, nil) + return j.AccessJDQueryNoPage("order/es/query", jdParams, nil, nil) } diff --git a/utils/utils_test.go b/utils/utils_test.go index d609e74a..a7b06e89 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -4,16 +4,6 @@ import ( "testing" ) -type II interface { - foo() -} - -type TT struct { -} - -func (t TT) foo() { - -} func TestDictKeysMan(t *testing.T) { testData := map[string]interface{}{"k1": "v1", "k2": "v2"} result1 := DictKeysMan(testData, []string{"k1"}, nil).(map[string]interface{})