- 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}}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user