|
@@ -1,14 +1,19 @@
|
|
|
package com.fjhx.victoriatourist.service.jd.impl;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.date.DateUnit;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.extra.spring.SpringUtil;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
|
|
+import com.fjhx.victoriatourist.entity.jd.dto.JdInfoDto;
|
|
|
+import com.fjhx.victoriatourist.entity.jd.dto.JdInfoSelectDto;
|
|
|
import com.fjhx.victoriatourist.entity.jd.po.JdBack;
|
|
|
import com.fjhx.victoriatourist.entity.jd.po.JdBackDetails;
|
|
|
import com.fjhx.victoriatourist.entity.jd.po.JdOrder;
|
|
|
import com.fjhx.victoriatourist.entity.jd.po.JdOrderDetails;
|
|
|
+import com.fjhx.victoriatourist.entity.jd.vo.JdInfoVo;
|
|
|
import com.fjhx.victoriatourist.service.jd.*;
|
|
|
import com.jd.open.api.sdk.DefaultJdClient;
|
|
|
import com.jd.open.api.sdk.JdClient;
|
|
@@ -38,8 +43,10 @@ import org.apache.http.client.methods.HttpGet;
|
|
|
import org.apache.http.client.methods.HttpRequestBase;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import java.io.IOException;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.text.SimpleDateFormat;
|
|
@@ -53,7 +60,10 @@ public class JdApiServiceImpl implements JdApiService {
|
|
|
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
private static final String appKey = "ECCD5CE82A501C7E0D8BB41C030C447D";
|
|
|
private static final String appSecret = "5b55c3f3eb874b0d8746ff28e7f91f64";
|
|
|
+ private static final String redirect_uri = "/open/jd/callback";
|
|
|
private final String tenantId = "prod".equals(SpringUtil.getActiveProfile()) ? "wdly" : "wdlytest";
|
|
|
+ private static final String ACCESS_TOKEN = "access_token";
|
|
|
+ private static final String REFRESH_TOKEN = "refresh_token";
|
|
|
|
|
|
@Autowired
|
|
|
private JdOrderService jdOrderService;
|
|
@@ -63,78 +73,77 @@ public class JdApiServiceImpl implements JdApiService {
|
|
|
private JdBackService jdBackService;
|
|
|
@Autowired
|
|
|
private JdBackDetailsService jdBackDetailsService;
|
|
|
+ @Resource
|
|
|
+ private JdInfoService jdInfoService;
|
|
|
|
|
|
/**
|
|
|
* 获取AccessToken
|
|
|
*/
|
|
|
@Override
|
|
|
public void getAccessTokenByCode(String code) {
|
|
|
- if (ObjectUtil.isEmpty(code)) {
|
|
|
- log.error("获取AccessToken失败:code不能为空");
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
//获取AccessToken并存入数据库
|
|
|
String path = "https://open-oauth.jd.com/oauth2/access_token?app_key=%s&app_secret=%s&grant_type=authorization_code&code=%s";
|
|
|
path = String.format(path, appKey, appSecret, code);
|
|
|
try {
|
|
|
JSONObject responseJson = getJSON(new HttpGet(path));
|
|
|
- if (ObjectUtil.isNotEmpty(responseJson.getString("access_token")) && ObjectUtil.isNotEmpty(responseJson.getString("refresh_token"))) {
|
|
|
- log.info("成功获取AccessToken并保存" + responseJson);
|
|
|
- ConfigUtil.set("jd_token_json", responseJson.toString());
|
|
|
- } else {
|
|
|
- log.error("获取AccessToken失败:" + responseJson);
|
|
|
- }
|
|
|
+ dealJdResponse(responseJson);
|
|
|
} catch (Exception e) {
|
|
|
log.error("获取AccessToken出错:" + e.getMessage(), e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String build2LoginUrl() {
|
|
|
+ String url = "https://open-oauth.jd.com/oauth2/to_login?app_key=%s&response_type=code&redirect_uri=%s&state=%s&scope=snsapi_base";
|
|
|
+ //TODO redirect_uri需要加上项目的域名
|
|
|
+ url = String.format(url, appKey, redirect_uri, DateUtil.format(new Date(), "yyyyMMddHHmmss"));
|
|
|
+ return url;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 刷新Token
|
|
|
*/
|
|
|
- private void refreshToken() {
|
|
|
- String jdTokenJsonStr = ConfigUtil.get("jd_token_json");
|
|
|
- if (ObjectUtil.isEmpty(jdTokenJsonStr)) {
|
|
|
- log.error("jd_token_json为空无法刷新Token请检查");
|
|
|
+ private void refreshToken(JdInfoVo jdInfoVo) {
|
|
|
+ if (ObjectUtil.isEmpty(jdInfoVo.getAccessToken())) {
|
|
|
+ log.error("accessToken为空无法刷新Token请检查");
|
|
|
return;
|
|
|
}
|
|
|
- JSONObject jdTokenJson = JSONObject.parseObject(jdTokenJsonStr);
|
|
|
- if (ObjectUtil.isEmpty(jdTokenJson.get("refresh_token"))) {
|
|
|
- log.error("refresh_token为空无法刷新Token请检查");
|
|
|
+ if (ObjectUtil.isEmpty(jdInfoVo.getRefreshToken())) {
|
|
|
+ log.error("refreshToken为空无法刷新Token请检查");
|
|
|
return;
|
|
|
}
|
|
|
String path = "https://open-oauth.jd.com/oauth2/refresh_token?app_key=%s&app_secret=%s&grant_type=refresh_token&refresh_token=%s";
|
|
|
- path = String.format(path, appKey, appSecret, jdTokenJson.get("refresh_token"));
|
|
|
+ path = String.format(path, appKey, appSecret, jdInfoVo.getRefreshToken());
|
|
|
try {
|
|
|
JSONObject responseJson = getJSON(new HttpGet(path));
|
|
|
- if (ObjectUtil.isNotEmpty(responseJson.getString("access_token")) && ObjectUtil.isNotEmpty(responseJson.getString("refresh_token"))) {
|
|
|
- log.info("成功刷新AccessToken并保存" + responseJson);
|
|
|
- ConfigUtil.set("jd_token_json", responseJson.toString());
|
|
|
- } else {
|
|
|
- log.error("刷新AccessToken失败:" + responseJson);
|
|
|
- }
|
|
|
+ dealJdResponse(responseJson);
|
|
|
} catch (Exception e) {
|
|
|
log.error("刷新AccessToken出错:" + e.getMessage(), e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @DSTransactional
|
|
|
- //项目启动时运行一次
|
|
|
-// @PostConstruct
|
|
|
- // 每1分钟执行一次
|
|
|
+// @DSTransactional
|
|
|
+ //每小时执行一次
|
|
|
+ @Scheduled(cron = "0 0 0/1 * * ? ")
|
|
|
+ //每分钟执行一次
|
|
|
// @Scheduled(cron = "0 0/1 * * * ? ")
|
|
|
- void orderSync() throws Exception {
|
|
|
-
|
|
|
- JSONObject jdTokenJson = JSONObject.parseObject(ConfigUtil.get("jd_token_json"));
|
|
|
- if (ObjectUtil.isEmpty(jdTokenJson)) {
|
|
|
- log.error("京东token详细为空请检查");
|
|
|
- return;
|
|
|
+ void orderSync(){
|
|
|
+ JdInfoVo jdInfoVo = jdInfoService.getOne();
|
|
|
+ //如果过期时间减去当前时间小于90分钟,则要刷新token
|
|
|
+ if(ObjectUtil.isNotNull(jdInfoVo.getExpireTime())){
|
|
|
+ long between = DateUtil.between(DateUtil.date(), jdInfoVo.getExpireTime(), DateUnit.MINUTE, false);
|
|
|
+ if(between <= 0){
|
|
|
+ if(jdInfoVo.getOnline()>0){
|
|
|
+ jdInfoVo.setOnline(0);
|
|
|
+ jdInfoService.updateById(jdInfoVo);
|
|
|
+ }
|
|
|
+ }else if(90 >= between){
|
|
|
+ refreshToken(jdInfoVo);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- JdClient client = new DefaultJdClient("http://api.jd.com/routerjson", jdTokenJson.getString("access_token"), appKey, appSecret);
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -303,6 +312,44 @@ public class JdApiServiceImpl implements JdApiService {
|
|
|
return calendar.getTime();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将jd返回的json对象转换成jdInfoVo
|
|
|
+ * @author hj
|
|
|
+ * @date 2023/12/10 22:03
|
|
|
+ * @param responseJson
|
|
|
+ * @param jdInfoVo
|
|
|
+ */
|
|
|
+ private void convertJdInfoVO(JSONObject responseJson, JdInfoVo jdInfoVo) {
|
|
|
+ jdInfoVo.setAccessToken(responseJson.getString(ACCESS_TOKEN));
|
|
|
+ jdInfoVo.setRefreshToken(responseJson.getString(REFRESH_TOKEN));
|
|
|
+ int expires_in = responseJson.getIntValue("expires_in", 0);
|
|
|
+ int offsetHour = 24;
|
|
|
+ if(expires_in>0){
|
|
|
+ offsetHour = expires_in/60/60;
|
|
|
+ }
|
|
|
+ jdInfoVo.setExpireTime(DateUtil.offsetHour(new Date(), offsetHour));
|
|
|
+ jdInfoVo.setLastRefreshTime(DateUtil.date());
|
|
|
+ jdInfoVo.setOnline(1);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理jd返回对象
|
|
|
+ * @author hj
|
|
|
+ * @date 2023/12/10 22:07
|
|
|
+ * @param responseJson
|
|
|
+ */
|
|
|
+ private void dealJdResponse(JSONObject responseJson) {
|
|
|
+ if (ObjectUtil.isNotEmpty(responseJson.getString(ACCESS_TOKEN)) && ObjectUtil.isNotEmpty(responseJson.getString(REFRESH_TOKEN))) {
|
|
|
+ log.info("成功获取AccessToken并保存" + responseJson);
|
|
|
+ JdInfoVo jdInfoVo = jdInfoService.getOne();
|
|
|
+ convertJdInfoVO(responseJson, jdInfoVo);
|
|
|
+ JdInfoDto jdInfoDto = BeanUtil.copyProperties(jdInfoVo, JdInfoDto.class);
|
|
|
+ jdInfoService.add(jdInfoDto);
|
|
|
+ } else {
|
|
|
+ log.error("获取AccessToken失败:" + responseJson);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
JdClient client = new DefaultJdClient("http://api.jd.com/routerjson", "a1f79f4670fd43a28fa2de02abd86f03ytfm", appKey, appSecret);
|
|
|
|