116 lines
3.8 KiB
Go
116 lines
3.8 KiB
Go
package jdapi
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"git.rosy.net.cn/baseapi"
|
|
"git.rosy.net.cn/baseapi/platformapi"
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
)
|
|
|
|
const (
|
|
accessStorePageCookieName = "shop.o2o.jd.com1"
|
|
)
|
|
|
|
func (a *API) SetStoreCookie(storeCookie string) {
|
|
a.locker.Lock()
|
|
defer a.locker.Unlock()
|
|
a.storeCookie = storeCookie
|
|
}
|
|
|
|
func (a *API) GetStoreCookie() string {
|
|
a.locker.RLock()
|
|
defer a.locker.RUnlock()
|
|
return a.storeCookie
|
|
}
|
|
|
|
func (a *API) AccessStorePage(subURL string) (retVal map[string]interface{}, err error) {
|
|
storeCookie := a.GetStoreCookie()
|
|
if storeCookie == "" {
|
|
return nil, fmt.Errorf("需要设置Store Cookie才能使用此方法")
|
|
}
|
|
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
|
func() *http.Request {
|
|
fullURL := utils.GenerateGetURL(storeURL, subURL, nil)
|
|
// baseapi.SugarLogger.Debug(fullURL)
|
|
request, _ := http.NewRequest(http.MethodGet, fullURL, nil)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
request.AddCookie(&http.Cookie{
|
|
Name: accessStorePageCookieName,
|
|
Value: storeCookie,
|
|
})
|
|
return request
|
|
},
|
|
a.config,
|
|
func(response *http.Response) (errLevel string, err error) {
|
|
jsonResult1, err := utils.HTTPResponse2Json(response)
|
|
// baseapi.SugarLogger.Debug(utils.Format4Output(jsonResult1, false))
|
|
if err != nil {
|
|
return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong
|
|
}
|
|
retVal = jsonResult1
|
|
code := jsonResult1["code"].(string)
|
|
if code == ResponseCodeSuccess {
|
|
retVal = jsonResult1
|
|
return platformapi.ErrLevelSuccess, nil
|
|
}
|
|
newErr := utils.NewErrorCode(jsonResult1["msg"].(string), code)
|
|
if _, ok := exceedLimitCodes[code]; ok {
|
|
return platformapi.ErrLevelExceedLimit, newErr
|
|
} else if _, ok := canRetryCodes[code]; ok {
|
|
return platformapi.ErrLevelRecoverableErr, newErr
|
|
} else {
|
|
baseapi.SugarLogger.Debugf("jd AccessStorePage failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
|
|
return platformapi.ErrLevelCodeIsNotOK, newErr
|
|
}
|
|
})
|
|
return retVal, err
|
|
}
|
|
|
|
func (a *API) GetRealMobile4Order(orderId string) (mobile string, err error) {
|
|
retVal, err := a.GetStoreOrderInfo(orderId)
|
|
if err == nil {
|
|
return retVal["mobile"].(string), nil
|
|
}
|
|
return "", err
|
|
}
|
|
|
|
func (a *API) GetStoreOrderInfo(orderId string) (storeOrderInfo map[string]interface{}, err error) {
|
|
retVal, err := a.AccessStorePage(fmt.Sprintf("order/newManager/search?pageNo=1&pageSize=1&orderBy=&desc=true¶m=%s&stationNo=", orderId))
|
|
// baseapi.SugarLogger.Debug(utils.Format4Output(retVal, false))
|
|
if err == nil {
|
|
resultList := retVal["result"].(map[string]interface{})["newOrderinfoMains"].(map[string]interface{})["resultList"].([]interface{})
|
|
if len(resultList) > 0 {
|
|
return resultList[0].(map[string]interface{}), nil
|
|
}
|
|
return nil, fmt.Errorf("不能找到订单:%s的相关信息", orderId)
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
func (a *API) GetStoreOrderInfoList(fromTime, toTime string) (storeOrderList []map[string]interface{}, err error) {
|
|
pageSize := 100
|
|
pageNo := 1
|
|
urlTemplate := "order/newManager/tabQuery/all?o2oOrderType=10000&pageNo=%d&pageSize=%d&orderBy=&desc=true&startTimeQuery=%s&endTimeQuery=%s&stationNo="
|
|
for {
|
|
retVal, err := a.AccessStorePage(fmt.Sprintf(urlTemplate, pageNo, pageSize, url.QueryEscape(fromTime), url.QueryEscape(toTime)))
|
|
// baseapi.SugarLogger.Debug(utils.Format4Output(retVal, false))
|
|
if err == nil {
|
|
newOrderinfoMains := retVal["result"].(map[string]interface{})["newOrderinfoMains"].(map[string]interface{})
|
|
resultList := newOrderinfoMains["resultList"].([]interface{})
|
|
storeOrderList = append(storeOrderList, utils.Slice2MapSlice(resultList)...)
|
|
if len(storeOrderList) >= int(utils.MustInterface2Int64(newOrderinfoMains["totalCount"])) {
|
|
return storeOrderList, nil
|
|
}
|
|
pageNo++
|
|
} else {
|
|
return nil, err
|
|
}
|
|
}
|
|
return nil, err
|
|
}
|