测试jdapi

This commit is contained in:
启航 2024-03-04 13:35:35 +08:00
parent b1b46a5c45
commit 763da2e7dd
4 changed files with 152 additions and 0 deletions

Binary file not shown.

View File

@ -65,6 +65,18 @@
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>jd</groupId>
<artifactId>open-api-sdk</artifactId>
<version>2.0-2024-03-04</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/open-api-sdk-2.0-2024-03-04.jar</systemPath>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version> <!-- jd-sdk依赖 -->
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>

View File

@ -0,0 +1,66 @@
package com.qihang.jd.controller;
import com.jd.open.api.sdk.DefaultJdClient;
import com.jd.open.api.sdk.JdClient;
import com.jd.open.api.sdk.JdException;
import com.jd.open.api.sdk.request.order.OrderGetRequest;
import com.jd.open.api.sdk.request.order.PopOrderSearchRequest;
import com.jd.open.api.sdk.response.order.OrderGetResponse;
import com.jd.open.api.sdk.response.order.PopOrderSearchResponse;
import com.qihang.common.enums.HttpStatus;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/goods")
@RestController
@AllArgsConstructor
public class GoodsApiController {
private final String SERVER_URL = "https://api.jd.com/routerjson";
@RequestMapping(value = "/pull_goods", method = RequestMethod.POST)
public Integer pullGoodsList() throws Exception {
String accessToken = "8abd974c62c34778935b34b5952e6f68izdk";
String appKey="FB4CC3688E6F9065D4FF510A53BB60FF";
String appSecret="40e8c8b2427f4e6db8f4a39af27d719e";
JdClient client=new DefaultJdClient(SERVER_URL,accessToken,appKey,appSecret);
PopOrderSearchRequest request=new PopOrderSearchRequest();
request.setStartDate("2024-2-14 10:00:00");
request.setEndDate("2024-3-4 12:00:00");
request.setOrderState("TRADE_CANCELED");
request.setOptionalFields("itemInfoList,orderId,isShipmenttype,scDT,idSopShipmenttype,orderStartTime");
request.setPage("1");
request.setPageSize("20");
request.setSortType(1);
request.setDateType(0);
PopOrderSearchResponse response=client.execute(request);
// PopOrderEnSearchRequest request=new PopOrderEnSearchRequest();
// request.setStartDate("2020-04-04 00:20:35");
// request.setEndDate("2020-04-04 00:20:35");
// request.setOrderState("21");
// request.setOptionalFields("orderId,venderId");
// request.setSourceId("JOS");
// request.setPage("1");
// request.setPageSize("10");
// request.setSortType(1);
// request.setDateType(0);
// PopOrderEnSearchResponse response=client.execute(request);
// SpuGetModelOrItemNumListRequest request=new SpuGetModelOrItemNumListRequest();
// request.setUniqueCodeType(1);
// request.setCategoryId(1233);
// request.setPageNo(1);
// request.setPageSize(10);
// request.setBrandId(1122);
// request.setAppId("1784");
// request.setAppName("app名称");
// request.setBusinessIdentity("JOS");
// request.setIp("192.168.1.1");
// request.setUserAgent("zhangsan");
// SpuGetModelOrItemNumListResponse response=client.execute(request);
return 1;
}
}

View File

@ -0,0 +1,74 @@
package com.qihang.jd.controller;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.io.IOException;
@AllArgsConstructor
@Controller
public class OAuthController {
// private final SysPlatformService platformService;
// private final SysShopService shopService;
private static Logger log = LoggerFactory.getLogger(OAuthController.class);
@GetMapping("/jd-api/oauth")
public String aliOAuth(@RequestParam Integer shopId) throws IOException, InterruptedException {
//查询店铺信息
// var shop = shopService.selectShopById(shopId);
// SysPlatform platform = platformService.selectById(shop.getType());
// String url = "http://container.open.taobao.com/container?appkey=" + platform.getAppKey() + "&state=" + shopId;
String url = "https://open-oauth.jd.com/oauth2/to_login?app_key=FB4CC3688E6F9065D4FF510A53BB60FF&response_type=code&redirect_uri=http%3A%2F%2Fwww.qumei.com&state=2&scope=snsapi_base";
return "redirect:" + url;
}
@RequestMapping("/jd-api/code_callback")
public String callback(Model model, HttpServletRequest request) {
log.info("京东授权返回code");
String code = request.getParameter("code");//6RZ71G
//https://open-oauth.jd.com/oauth2/access_token?app_key=FB4CC3688E6F9065D4FF510A53BB60FF&app_secret=40e8c8b2427f4e6db8f4a39af27d719e&grant_type=authorization_code&code=6RZ71G
/***
*
* {"access_token":"c9f997d0e44d4f118033c6e8e329f9e0zdbl","expires_in":31535999,"refresh_token":"c293b96a03a94013a471e50ebc3d2f9czi5m","scope":"snsapi_base","open_id":"PjmgY-ntsszAW81q9NRErnrUTVHhD-HC64HuspupG8Q","uid":"8474668550","time":1709518959147,"token_type":"bearer","code":0,"xid":"o*AASTnxHZ0sdNj9M3XhiMqYf0NTViY_IfFlQem3U6w1lv-neCalw"}
*
*
* {
* "access_token": "c9f997d0e44d4f118033c6e8e329f9e0zdbl",
* "expires_in": 31535999,
* "refresh_token": "c293b96a03a94013a471e50ebc3d2f9czi5m",
* "scope": "snsapi_base",
* "open_id": "PjmgY-ntsszAW81q9NRErnrUTVHhD-HC64HuspupG8Q",
* "uid": "8474668550",
* "time": 1709518959147,
* "token_type": "bearer",
* "code": 0,
* "xid": "o*AASTnxHZ0sdNj9M3XhiMqYf0NTViY_IfFlQem3U6w1lv-neCalw"
* }
*
*
*/
String sessionKey = request.getParameter("top_session");
String state = request.getParameter("state");
try {
Integer shopId = Integer.parseInt(state);
// shopService.updateSessionKey(shopId, sessionKey);
return "redirect:/order/list?shopId=" + shopId;
} catch (Exception e) {
return "redirect:/?msg=callback_taobao_error";
}
}
}