- export low level jdapi.
- access jd order info.
This commit is contained in:
@@ -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}}
|
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 := make(map[string]string)
|
||||||
params["v"] = "1.0"
|
params["v"] = "1.0"
|
||||||
params["format"] = "json"
|
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) {
|
func (j JDAPI) AccessJDQueryNoPage(apiStr string, jdParams map[string]string, keyToRemove, keyToKeep []string) (interface{}, error) {
|
||||||
jsonResult, err := j.accessJDQuery(apiStr, jdParams)
|
jsonResult, err := j.AccessJDQuery(apiStr, jdParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return jsonResult, err
|
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) {
|
func NormalJDQueryHavePageResultParser(data map[string]interface{}, totalCount int) ([]interface{}, int) {
|
||||||
normalJDQueryHavePageResultParser := func(data map[string]interface{}, totalCount int) ([]interface{}, int) {
|
var result map[string]interface{}
|
||||||
var result map[string]interface{}
|
var retVal []interface{}
|
||||||
var retVal []interface{}
|
|
||||||
|
|
||||||
tempResult := data["result"]
|
tempResult := data["result"]
|
||||||
if resultStr, ok := tempResult.(string); ok {
|
if resultStr, ok := tempResult.(string); ok {
|
||||||
if err := utils.UnmarshalUseNumber([]byte(resultStr), &tempResult); err != nil {
|
if err := utils.UnmarshalUseNumber([]byte(resultStr), &tempResult); err != nil {
|
||||||
panic("Wrong format")
|
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 {
|
if pageResultParser == nil {
|
||||||
pageResultParser = normalJDQueryHavePageResultParser
|
pageResultParser = NormalJDQueryHavePageResultParser
|
||||||
}
|
}
|
||||||
|
|
||||||
localJdParams := make(map[string]string)
|
localJdParams := make(map[string]string)
|
||||||
@@ -323,7 +323,7 @@ func (j JDAPI) accessJDQueryHavePage(apiStr string, jdParams map[string]string,
|
|||||||
for {
|
for {
|
||||||
localJdParams["pageNo"] = strconv.FormatInt(int64(curPage), 10)
|
localJdParams["pageNo"] = strconv.FormatInt(int64(curPage), 10)
|
||||||
localJdParams["pageSize"] = strconv.FormatInt(int64(pageSize), 10)
|
localJdParams["pageSize"] = strconv.FormatInt(int64(pageSize), 10)
|
||||||
jsonResult, err := j.accessJDQuery(apiStr, localJdParams)
|
jsonResult, err := j.AccessJDQuery(apiStr, localJdParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,9 +23,9 @@ func TestTest(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAccessJDQuery(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 {
|
if err != nil {
|
||||||
t.Fatalf("Error when accessing accessJDQuery: %v", err)
|
t.Fatalf("Error when accessing AccessJDQuery: %v", err)
|
||||||
} else {
|
} else {
|
||||||
code := result["code"].(string)
|
code := result["code"].(string)
|
||||||
if code != "0" {
|
if code != "0" {
|
||||||
@@ -35,9 +35,9 @@ func TestAccessJDQuery(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAccessJDQueryNoPage(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 {
|
if err != nil {
|
||||||
t.Fatalf("accessJDQueryNoPage return error:%v", err)
|
t.Fatalf("AccessJDQueryNoPage return error:%v", err)
|
||||||
}
|
}
|
||||||
cityInfo := result.([]interface{})
|
cityInfo := result.([]interface{})
|
||||||
if len(cityInfo) == 0 {
|
if len(cityInfo) == 0 {
|
||||||
@@ -57,9 +57,9 @@ func TestAccessJDQueryHavePage(t *testing.T) {
|
|||||||
"pageNo": "1",
|
"pageNo": "1",
|
||||||
"pageSize": "20",
|
"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 {
|
if err != nil {
|
||||||
t.Fatalf("accessJDQueryHavePage return error:%v", err)
|
t.Fatalf("AccessJDQueryHavePage return error:%v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(skuInfo) == 0 {
|
if len(skuInfo) == 0 {
|
||||||
|
|||||||
@@ -1,18 +1,63 @@
|
|||||||
package jdapi
|
package jdapi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
"git.rosy.net.cn/baseapi/utils"
|
"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) {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j JDAPI) QuerySignleOrder(orderId string) ([]interface{}, error) {
|
func (j JDAPI) QuerySingleOrder(orderId string) ([]interface{}, error) {
|
||||||
jdParams := make(map[string]string)
|
jdParams := make(map[string]string)
|
||||||
jdParams["orderId"] = orderId
|
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) {
|
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),
|
"isAgreed": utils.Bool2String(isAgreed),
|
||||||
"operator": getJDOperator(),
|
"operator": getJDOperator(),
|
||||||
}
|
}
|
||||||
return j.accessJDQueryNoPage("order/es/query", jdParams, nil, nil)
|
return j.AccessJDQueryNoPage("order/es/query", jdParams, nil, nil)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,16 +4,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
type II interface {
|
|
||||||
foo()
|
|
||||||
}
|
|
||||||
|
|
||||||
type TT struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t TT) foo() {
|
|
||||||
|
|
||||||
}
|
|
||||||
func TestDictKeysMan(t *testing.T) {
|
func TestDictKeysMan(t *testing.T) {
|
||||||
testData := map[string]interface{}{"k1": "v1", "k2": "v2"}
|
testData := map[string]interface{}{"k1": "v1", "k2": "v2"}
|
||||||
result1 := DictKeysMan(testData, []string{"k1"}, nil).(map[string]interface{})
|
result1 := DictKeysMan(testData, []string{"k1"}, nil).(map[string]interface{})
|
||||||
|
|||||||
Reference in New Issue
Block a user