京东eclp物流新参数

This commit is contained in:
苏尹岚
2020-03-06 13:48:51 +08:00
parent 81db7b0bb3
commit 0f9d642c94
2 changed files with 68 additions and 17 deletions

View File

@@ -13,7 +13,8 @@ import (
) )
const ( const (
prodURL = "https://api.jd.com/routerjson" prodURL = "https://api.jd.com/routerjson"
prodURL2 = "https://oauth.jd.com/oauth"
sigKey = "sign" sigKey = "sign"
IsvSource = "ISV0020000000068" IsvSource = "ISV0020000000068"
@@ -22,8 +23,10 @@ const (
WarehouseNo = "110014006" WarehouseNo = "110014006"
CustomerCode = "028K595510" CustomerCode = "028K595510"
OrderMark = "00000" OrderMark = "00000"
// CustomerCode2 = "028K588716" // CustomerCode2 = "028K588716"
state = "1212"
orderType10 = "10" //订单号类型(10代表ECLP订单号, 20代表商家订单号) orderType10 = "10" //订单号类型(10代表ECLP订单号, 20代表商家订单号)
orderType20 = "20" orderType20 = "20"
) )
@@ -32,6 +35,7 @@ type API struct {
accessToken string accessToken string
appKey string appKey string
appSecret string appSecret string
redirectUri string
client *http.Client client *http.Client
config *platformapi.APIConfig config *platformapi.APIConfig
} }
@@ -127,7 +131,7 @@ type SearchShopStockResult struct {
PageNumber string `json:"pageNumber"` PageNumber string `json:"pageNumber"`
} }
func New(accessToken, appKey, appSecret string, config ...*platformapi.APIConfig) *API { func New(accessToken, appKey, appSecret, redirectUri string, config ...*platformapi.APIConfig) *API {
curConfig := platformapi.DefAPIConfig curConfig := platformapi.DefAPIConfig
if len(config) > 0 { if len(config) > 0 {
curConfig = *config[0] curConfig = *config[0]
@@ -136,6 +140,7 @@ func New(accessToken, appKey, appSecret string, config ...*platformapi.APIConfig
accessToken: accessToken, accessToken: accessToken,
appKey: appKey, appKey: appKey,
appSecret: appSecret, appSecret: appSecret,
redirectUri: redirectUri,
client: &http.Client{Timeout: curConfig.ClientTimeout}, client: &http.Client{Timeout: curConfig.ClientTimeout},
config: &curConfig, config: &curConfig,
} }
@@ -162,15 +167,21 @@ func (a *API) signParam(params map[string]interface{}) (sig string) {
return sig return sig
} }
func (a *API) AccessAPI(action string, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) { func (a *API) AccessAPI(action string, url string, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) {
params := make(map[string]interface{}) params := make(map[string]interface{})
params["access_token"] = a.accessToken var fullURL string
params["app_key"] = a.appKey if url == prodURL2 {
params["timestamp"] = utils.Time2Str(time.Now()) params = utils.MergeMaps(params, bizParams)
params = utils.MergeMaps(params, bizParams) fullURL = utils.GenerateGetURL(url, action, nil)
signStr := a.signParam(params) } else {
params["sign"] = signStr params["access_token"] = a.accessToken
fullURL := utils.GenerateGetURL(prodURL, "method="+action, nil) params["app_key"] = a.appKey
params["timestamp"] = utils.Time2Str(time.Now())
params = utils.MergeMaps(params, bizParams)
signStr := a.signParam(params)
params["sign"] = signStr
fullURL = utils.GenerateGetURL(url, "method="+action, nil)
}
err = platformapi.AccessPlatformAPIWithRetry(a.client, err = platformapi.AccessPlatformAPIWithRetry(a.client,
func() *http.Request { func() *http.Request {
request, _ := http.NewRequest(http.MethodPost, fullURL, strings.NewReader(utils.Map2URLValues(params).Encode())) request, _ := http.NewRequest(http.MethodPost, fullURL, strings.NewReader(utils.Map2URLValues(params).Encode()))
@@ -200,7 +211,7 @@ func (a *API) AccessAPI(action string, bizParams map[string]interface{}) (retVal
//获取销售平台信息 //获取销售平台信息
//https://open.jd.com/home/home#/doc/api?apiCateId=138&apiId=947&apiName=jingdong.eclp.master.querySpSource //https://open.jd.com/home/home#/doc/api?apiCateId=138&apiId=947&apiName=jingdong.eclp.master.querySpSource
func (a *API) QuerySpSource() (querySpSourceResult *QuerySpSourceResult, err error) { func (a *API) QuerySpSource() (querySpSourceResult *QuerySpSourceResult, err error) {
result, err := a.AccessAPI("jingdong.eclp.master.querySpSource", nil) result, err := a.AccessAPI("jingdong.eclp.master.querySpSource", prodURL, nil)
if err == nil { if err == nil {
utils.Map2StructByJson(result, &querySpSourceResult, false) utils.Map2StructByJson(result, &querySpSourceResult, false)
} }
@@ -210,7 +221,7 @@ func (a *API) QuerySpSource() (querySpSourceResult *QuerySpSourceResult, err err
//销售出库单下发 //销售出库单下发
//https://open.jd.com/home/home#/doc/api?apiCateId=138&apiId=928&apiName=jingdong.eclp.order.addOrder //https://open.jd.com/home/home#/doc/api?apiCateId=138&apiId=928&apiName=jingdong.eclp.order.addOrder
func (a *API) AddOrder(addOrderParam *AddOrderParam) (addOrderResult *AddOrderResult, err error) { func (a *API) AddOrder(addOrderParam *AddOrderParam) (addOrderResult *AddOrderResult, err error) {
result, err := a.AccessAPI("jingdong.eclp.order.addOrder", utils.Struct2FlatMap(addOrderParam)) result, err := a.AccessAPI("jingdong.eclp.order.addOrder", prodURL, utils.Struct2FlatMap(addOrderParam))
if err == nil { if err == nil {
utils.Map2StructByJson(result, &addOrderResult, false) utils.Map2StructByJson(result, &addOrderResult, false)
} }
@@ -220,7 +231,7 @@ func (a *API) AddOrder(addOrderParam *AddOrderParam) (addOrderResult *AddOrderRe
//销售出库单取消 //销售出库单取消
//https://open.jd.com/home/home#/doc/api?apiCateId=138&apiId=957&apiName=jingdong.eclp.order.cancelOrder //https://open.jd.com/home/home#/doc/api?apiCateId=138&apiId=957&apiName=jingdong.eclp.order.cancelOrder
func (a *API) CancelOrder(eclpSoNo string) (cancelOrderResult *CancelOrderResult, err error) { func (a *API) CancelOrder(eclpSoNo string) (cancelOrderResult *CancelOrderResult, err error) {
result, err := a.AccessAPI("jingdong.eclp.order.cancelOrder", map[string]interface{}{ result, err := a.AccessAPI("jingdong.eclp.order.cancelOrder", prodURL, map[string]interface{}{
"eclpSoNo": eclpSoNo, "eclpSoNo": eclpSoNo,
}) })
if err == nil { if err == nil {
@@ -232,7 +243,7 @@ func (a *API) CancelOrder(eclpSoNo string) (cancelOrderResult *CancelOrderResult
//销售出库单状态查询 //销售出库单状态查询
//https://jos.jd.com/api/detail.htm?apiName=jingdong.eclp.order.queryOrderStatus&id=929 //https://jos.jd.com/api/detail.htm?apiName=jingdong.eclp.order.queryOrderStatus&id=929
func (a *API) QueryOrderStatus(eclpSoNo string) (queryOrderStatus *QueryOrderStatusResult, err error) { func (a *API) QueryOrderStatus(eclpSoNo string) (queryOrderStatus *QueryOrderStatusResult, err error) {
result, err := a.AccessAPI("jingdong.eclp.order.queryOrderStatus", map[string]interface{}{ result, err := a.AccessAPI("jingdong.eclp.order.queryOrderStatus", prodURL, map[string]interface{}{
"eclpSoNo": eclpSoNo, "eclpSoNo": eclpSoNo,
}) })
if err == nil { if err == nil {
@@ -244,7 +255,7 @@ func (a *API) QueryOrderStatus(eclpSoNo string) (queryOrderStatus *QueryOrderSta
//查询物流跟踪消息 //查询物流跟踪消息
//https://open.jd.com/home/home#/doc/api?apiCateId=138&apiId=2378&apiName=jingdong.eclp.order.getTrackMessagePlusByOrder //https://open.jd.com/home/home#/doc/api?apiCateId=138&apiId=2378&apiName=jingdong.eclp.order.getTrackMessagePlusByOrder
func (a *API) GetTrackMessagePlusByOrder(bizCode string) (getTrackMessagePlusByOrderResult *GetTrackMessagePlusByOrderResult, err error) { func (a *API) GetTrackMessagePlusByOrder(bizCode string) (getTrackMessagePlusByOrderResult *GetTrackMessagePlusByOrderResult, err error) {
result, err := a.AccessAPI("jingdong.eclp.order.getTrackMessagePlusByOrder", map[string]interface{}{ result, err := a.AccessAPI("jingdong.eclp.order.getTrackMessagePlusByOrder", prodURL, map[string]interface{}{
"bizCode": bizCode, "bizCode": bizCode,
"customerCode": CustomerCode, "customerCode": CustomerCode,
"type": orderType20, "type": orderType20,
@@ -258,9 +269,41 @@ func (a *API) GetTrackMessagePlusByOrder(bizCode string) (getTrackMessagePlusByO
//查询仓库商品库存 //查询仓库商品库存
//https://open.jd.com/home/home#/doc/api?apiCateId=138&apiId=3396&apiName=jingdong.eclp.stock.searchShopStock //https://open.jd.com/home/home#/doc/api?apiCateId=138&apiId=3396&apiName=jingdong.eclp.stock.searchShopStock
func (a *API) SearchShopStock(searchShopStockParam *SearchShopStockParam) (searchShopStockResult *SearchShopStockResult, err error) { func (a *API) SearchShopStock(searchShopStockParam *SearchShopStockParam) (searchShopStockResult *SearchShopStockResult, err error) {
result, err := a.AccessAPI("jingdong.eclp.order.searchShopStock", utils.Struct2FlatMap(searchShopStockParam)) result, err := a.AccessAPI("jingdong.eclp.order.searchShopStock", prodURL, utils.Struct2FlatMap(searchShopStockParam))
if err == nil { if err == nil {
utils.Map2StructByJson(result, &searchShopStockResult, false) utils.Map2StructByJson(result, &searchShopStockResult, false)
} }
return searchShopStockResult, err return searchShopStockResult, err
} }
//获取tokenCode
//https://oauth.jd.com/oauth/authorize?response_type=code&client_id=YOUR_APP_KEY&redirect_uri=YOUR_REGISTERED_REDIRECT_URI&state=YOUR_CUSTOM_CODE
func (a *API) GetTokenInfo() (code string, err error) {
result, err := a.AccessAPI("authorize", prodURL2, map[string]interface{}{
"response_type": "code",
"client_id": a.appKey,
"redirect_uri": a.redirectUri,
"state": state,
})
if err == nil {
code = result["code"].(string)
}
return code, err
}
//获取token
//https://oauth.jd.com/oauth/token?grant_type=authorization_code&client_id=YOUR_APP_KEY&redirect_uri=YOUR_REGISTERED_REDIRECT_URI&code=GET_CODE&state=YOUR_CUSTOM_CODE&client_secret= YOUR_APP_SECRET
func (a *API) GetToken() (token string, err error) {
code, err := a.GetTokenInfo()
result, err := a.AccessAPI("token", prodURL2, map[string]interface{}{
"grant_type": "authorization_code",
"client_id": a.appKey,
"redirect_uri": a.redirectUri,
"code": code,
"state": state,
})
if err == nil {
token = result["token"].(string)
}
return token, err
}

View File

@@ -16,7 +16,7 @@ func init() {
logger, _ := zap.NewDevelopment() logger, _ := zap.NewDevelopment()
sugarLogger = logger.Sugar() sugarLogger = logger.Sugar()
baseapi.Init(sugarLogger) baseapi.Init(sugarLogger)
api = New("", "", "") api = New("", "", "", "")
} }
func TestQuerySpSource(t *testing.T) { func TestQuerySpSource(t *testing.T) {
@@ -38,3 +38,11 @@ func TestGetTrackMessagePlusByOrder(t *testing.T) {
func TestSearchShopStock(t *testing.T) { func TestSearchShopStock(t *testing.T) {
api.SearchShopStock(&SearchShopStockParam{}) api.SearchShopStock(&SearchShopStockParam{})
} }
func TestGetTokenInfo(t *testing.T) {
api.GetTokenInfo()
}
func TestGetToken(t *testing.T) {
api.GetToken()
}