测试pdd-api

This commit is contained in:
启航 2024-03-14 17:06:27 +08:45
parent 05eddd8ba1
commit b213517fdc
6 changed files with 47 additions and 8 deletions

View File

@ -11,7 +11,7 @@
Target Server Version : 80032
File Encoding : 65001
Date: 14/03/2024 16:10:31
Date: 14/03/2024 17:05:59
*/
SET NAMES utf8mb4;
@ -1446,7 +1446,7 @@ CREATE TABLE `sys_shop` (
INSERT INTO `sys_shop` VALUES (1, '天猫旗舰店', 1, '', 98, 1, 0, '', 59972781, '31014100', '7b0769269b0c0ca88949791c14eb3a5c', '6102522199aaa4a42a2e6be95d0a5e18657c1576ec563a0351855490', NULL, NULL, NULL, NULL, 'http://gw.api.taobao.com/router/rest');
INSERT INTO `sys_shop` VALUES (2, '京东旗舰店', 2, NULL, 9, 0, 0, NULL, 10706, NULL, NULL, '8abd974c62c34778935b34b5952e6f68izdk', 31535999, NULL, '919b21a3b9d247d08d7701cb8bcc2432ote5', NULL, NULL);
INSERT INTO `sys_shop` VALUES (3, '抖音旗舰店', 3, 'http://openapi.jinritemai.com', 87, 1, 1653672695, NULL, 90158786, '7249607407477720636', '36a12497-fb9d-4b54-8cd1-fd1617346687', '', NULL, NULL, NULL, NULL, '2');
INSERT INTO `sys_shop` VALUES (5, '拼多多旗舰店', 4, NULL, 99, 1, 1680698886, 'pdd10006159121', 430222062, NULL, NULL, '87f8044d2a5f45a489aa3a952785b0d35e61788a', NULL, NULL, NULL, NULL, NULL);
INSERT INTO `sys_shop` VALUES (5, '拼多多旗舰店', 4, NULL, 99, 1, 1680698886, 'pdd43022206254', 430222062, NULL, NULL, 'fbe9ebb468c5444cbcc6bdd6703a33b1d079884d', 84690, 1710404363, 'af81d95e5b9d41daa0e3e81f53e8ef530885256f', NULL, NULL);
-- ----------------------------
-- Table structure for sys_shop_pull_lasttime
@ -1546,7 +1546,7 @@ CREATE TABLE `sys_user` (
-- ----------------------------
-- Records of sys_user
-- ----------------------------
INSERT INTO `sys_user` VALUES (1, 103, 'admin', '启航oms', '00', '280645618@qq.com', '15888888888', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '0', '0', '127.0.0.1', '2024-03-13 17:18:32', 'admin', '2023-08-07 19:31:37', '', '2024-03-13 18:03:31', '管理员');
INSERT INTO `sys_user` VALUES (1, 103, 'admin', '启航oms', '00', '280645618@qq.com', '15888888888', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '0', '0', '127.0.0.1', '2024-03-14 15:29:43', 'admin', '2023-08-07 19:31:37', '', '2024-03-14 16:14:43', '管理员');
-- ----------------------------
-- Table structure for tao_goods

View File

@ -1,10 +1,14 @@
package com.qihang.pdd.controller;
import com.pdd.pop.sdk.common.util.JsonUtil;
import com.pdd.pop.sdk.http.PopAccessTokenClient;
import com.pdd.pop.sdk.http.PopClient;
import com.pdd.pop.sdk.http.PopHttpClient;
import com.pdd.pop.sdk.http.api.pop.request.PddOrderListGetRequest;
import com.pdd.pop.sdk.http.api.pop.request.PddPopAuthTokenRefreshRequest;
import com.pdd.pop.sdk.http.api.pop.response.PddOrderListGetResponse;
import com.pdd.pop.sdk.http.api.pop.response.PddPopAuthTokenRefreshResponse;
import com.pdd.pop.sdk.http.token.AccessTokenResponse;
import com.qihang.common.common.ApiResult;
import com.qihang.common.enums.EnumShopType;
import com.qihang.common.enums.HttpStatus;
@ -21,6 +25,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.net.URLEncoder;
@AllArgsConstructor
@RequestMapping("/order")
@RestController
@ -78,12 +84,14 @@ public class AjaxOrderPddController {
// String clientSecret = DataConfigObject.getInstance().getPddClientSecret();
// var shop = shopService.getShop(shopId);
// var settingEntity = thirdSettingService.getEntity(shop.getType());
ShopApiParams params = new ShopApiParams();
params.setAppKey(platform.getAppKey());
params.setAppSecret(platform.getAppSecret());
params.setAccessToken(shop.getAccessToken());
params.setTokenRequestUrl("http://localhost:3000/pdd_api2/oauth");
params.setApiRequestUrl(shop.getApiRequestUrl());
String url = "https://mms.pinduoduo.com/open.html?response_type=code&client_id=" + params.getAppKey() + "&redirect_uri=" + URLEncoder.encode(platform.getRedirectUri());
String accessToken = params.getAccessToken();
if(!StringUtils.hasText(accessToken)) {
@ -92,8 +100,34 @@ public class AjaxOrderPddController {
// if(!StringUtils.hasText(accessToken)) return new ApiResult<>(EnumResultVo.TokenFail.getIndex(), "参数错误accessToken为空",params);
// 获取店铺信息判断店铺是否一致
var shopResult = PddApiUtils.getShopInfo(params.getAppKey(), params.getAppSecret(), accessToken);
if (shopResult.getCode() != HttpStatus.SUCCESS)
return ApiResult.build(shopResult.getCode(), shopResult.getMsg(),params);
if (shopResult.getCode() != HttpStatus.SUCCESS) {
// if(shopResult.getCode() == HttpStatus.UNAUTHORIZED){
// // 生成AccessToken
// PopAccessTokenClient accessTokenClient = new PopAccessTokenClient(params.getAppKey(), params.getAppSecret());
// String code = "5d78cf7f5eb14e56b4fd08bdc9060eb334723348";
// try {
// AccessTokenResponse response = accessTokenClient.generate(code);
// if(response.getErrorResponse()!=null){
// log.info("/***************获取拼多多授权token错误"+response.getErrorResponse().getErrorMsg()+"**************/");
// }else{
// //保存accessToken
// shopService.updateSessionKey(reqData.getShopId(),response.getOwnerId(),response.getAccessToken(),response.getRefreshToken(),response.getExpiresIn().longValue());
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
return ApiResult.build(shopResult.getCode(), shopResult.getMsg(), params);
}else{
// 看看是否需要刷新一下token
PopClient client = new PopHttpClient(params.getAppKey(), params.getAppSecret());
PddPopAuthTokenRefreshRequest request = new PddPopAuthTokenRefreshRequest();
request.setRefreshToken(shop.getRefreshToken());
PddPopAuthTokenRefreshResponse response = client.syncInvoke(request);
System.out.println(response);
//保存accessToken
shopService.updateSessionKey(reqData.getShopId(),response.getPopAuthTokenRefreshResponse().getOwnerId(),response.getPopAuthTokenRefreshResponse().getAccessToken(),response.getPopAuthTokenRefreshResponse().getRefreshToken(),response.getPopAuthTokenRefreshResponse().getExpiresIn().longValue());
}
if (shopResult.getData().getMallId().longValue() != shop.getSellerId().longValue()) {
return ApiResult.build(HttpStatus.UNAUTHORIZED, "该店铺不是授权店铺",params);

View File

@ -24,6 +24,8 @@ public class PddApiUtils {
PddMallInfoGetRequest request = new PddMallInfoGetRequest();
PddMallInfoGetResponse response = client.syncInvoke(request, accessToken);
if (response.getErrorResponse() == null) {
// 刷新一下token
return ApiResult.build(HttpStatus.SUCCESS, "SUCCESS", response.getMallInfoGetResponse());
} else if (response.getErrorResponse().getErrorCode().intValue() == 10019) {
return ApiResult.build(HttpStatus.UNAUTHORIZED, "Token过期");

View File

@ -66,7 +66,7 @@ public class PddOAuthController {
System.out.println(shopId +"--token:" + response.getAccessToken()+",thirdId:"+response.getOwnerId()+",shopId:"+shopId);
// shopService.updateSessionKey(shopId,Long.parseLong(response.getOwnerId()),response.getAccessToken());
shopService.updateSessionKey(shopId,response.getAccessToken(),response.getRefreshToken());
shopService.updateSessionKey(shopId,response.getOwnerId(),response.getAccessToken(),response.getRefreshToken(),response.getExpiresIn().longValue());
// thirdSettingService.updateEntity(shopId, response.getAccessToken(), response.getRefreshToken(), response.getExpiresIn(),response.getOwnerId());
return "redirect:/pdd/getTokenSuccess?mallId="+response.getOwnerId();

View File

@ -11,5 +11,5 @@ import com.baomidou.mybatisplus.extension.service.IService;
public interface SysShopService extends IService<SysShop> {
SysShop selectShopById(Integer shopId);
void updateSessionKey(Integer shopId,String sessionKey,String refreshToken);
void updateSessionKey(Integer shopId,String ownerId,String sessionKey,String refreshToken,Long expiresIn);
}

View File

@ -24,13 +24,16 @@ public class SysShopServiceImpl extends ServiceImpl<SysShopMapper, SysShop>
}
@Override
public void updateSessionKey(Integer shopId, String sessionKey,String refreshToken) {
public void updateSessionKey(Integer shopId, String ownerId, String sessionKey, String refreshToken, Long expiresIn) {
SysShop shop = new SysShop();
shop.setId(shopId);
shop.setAccessToken(sessionKey);
shop.setRefreshToken(refreshToken);
shop.setExpiresIn(expiresIn);
shop.setAccessTokenBegin(System.currentTimeMillis() / 1000);
mapper.updateById(shop);
}
}