Files
baseapi/platformapi/jdapi/store_page.go
2019-01-08 16:00:18 +08:00

93 lines
2.8 KiB
Go

package jdapi
import (
"fmt"
"net/http"
"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&param=%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
}