diff --git a/platformapi/jdeclpapi/jdeclpapi.go b/platformapi/jdeclpapi/jdeclpapi.go index 79f2e713..ac058ca2 100644 --- a/platformapi/jdeclpapi/jdeclpapi.go +++ b/platformapi/jdeclpapi/jdeclpapi.go @@ -13,7 +13,8 @@ import ( ) const ( - prodURL = "https://api.jd.com/routerjson" + prodURL = "https://api.jd.com/routerjson" + prodURL2 = "https://oauth.jd.com/oauth" sigKey = "sign" IsvSource = "ISV0020000000068" @@ -22,8 +23,10 @@ const ( WarehouseNo = "110014006" CustomerCode = "028K595510" OrderMark = "00000" + // CustomerCode2 = "028K588716" + state = "1212" orderType10 = "10" //订单号类型(10:代表ECLP订单号, 20:代表商家订单号) orderType20 = "20" ) @@ -32,6 +35,7 @@ type API struct { accessToken string appKey string appSecret string + redirectUri string client *http.Client config *platformapi.APIConfig } @@ -127,7 +131,7 @@ type SearchShopStockResult struct { 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 if len(config) > 0 { curConfig = *config[0] @@ -136,6 +140,7 @@ func New(accessToken, appKey, appSecret string, config ...*platformapi.APIConfig accessToken: accessToken, appKey: appKey, appSecret: appSecret, + redirectUri: redirectUri, client: &http.Client{Timeout: curConfig.ClientTimeout}, config: &curConfig, } @@ -162,15 +167,21 @@ func (a *API) signParam(params map[string]interface{}) (sig string) { 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["access_token"] = a.accessToken - 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(prodURL, "method="+action, nil) + var fullURL string + if url == prodURL2 { + params = utils.MergeMaps(params, bizParams) + fullURL = utils.GenerateGetURL(url, action, nil) + } else { + params["access_token"] = a.accessToken + 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, func() *http.Request { 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 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 { 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 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 { 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 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, }) 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 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, }) 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 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, "customerCode": CustomerCode, "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 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 { utils.Map2StructByJson(result, &searchShopStockResult, false) } 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 +} diff --git a/platformapi/jdeclpapi/jdeclpapi_test.go b/platformapi/jdeclpapi/jdeclpapi_test.go index b5133b29..70af6c5c 100644 --- a/platformapi/jdeclpapi/jdeclpapi_test.go +++ b/platformapi/jdeclpapi/jdeclpapi_test.go @@ -16,7 +16,7 @@ func init() { logger, _ := zap.NewDevelopment() sugarLogger = logger.Sugar() baseapi.Init(sugarLogger) - api = New("", "", "") + api = New("", "", "", "") } func TestQuerySpSource(t *testing.T) { @@ -38,3 +38,11 @@ func TestGetTrackMessagePlusByOrder(t *testing.T) { func TestSearchShopStock(t *testing.T) { api.SearchShopStock(&SearchShopStockParam{}) } + +func TestGetTokenInfo(t *testing.T) { + api.GetTokenInfo() +} + +func TestGetToken(t *testing.T) { + api.GetToken() +}