Merge branch 'master' of https://codeup.aliyun.com/64d9c5feeceb191898f636d7/honor-dd-light-ds-java
commit
48974cd1c2
@ -0,0 +1,27 @@
|
||||
package com.ms.api.spi.timer;
|
||||
|
||||
import com.jinritemai.cloud.base.api.BaseRequest;
|
||||
import com.jinritemai.cloud.base.api.BaseResponse;
|
||||
import com.jinritemai.cloud.base.api.ExtensionService;
|
||||
import com.jinritemai.cloud.base.api.ExtensionServiceHandler;
|
||||
import com.ms.api.common.R;
|
||||
import com.ms.api.common.Ret;
|
||||
import com.ms.api.common.TimerBaseService;
|
||||
import com.ms.api.dto.ItemDTO;
|
||||
import com.ms.api.task.CheckAuditStatusTimeoutTaskService;
|
||||
import com.ms.api.task.RefreshTokenTimerTaskService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
@ExtensionService("RefreshTokenTimer")
|
||||
@Slf4j
|
||||
public class RefreshTokenTimerService extends TimerBaseService implements ExtensionServiceHandler<ItemDTO, Ret> {
|
||||
@Autowired
|
||||
RefreshTokenTimerTaskService refreshTokenTimerTaskService;
|
||||
|
||||
@Override
|
||||
public BaseResponse<Ret> handle(BaseRequest<ItemDTO> req) {
|
||||
refreshTokenTimerTaskService.runTask();
|
||||
return R.ok(Ret.success());
|
||||
}
|
||||
}
|
@ -0,0 +1,141 @@
|
||||
package com.ms.api.task;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.doudian.open.api.token_refresh.TokenRefreshRequest;
|
||||
import com.doudian.open.api.token_refresh.TokenRefreshResponse;
|
||||
import com.doudian.open.api.token_refresh.param.TokenRefreshParam;
|
||||
import com.ms.api.common.E;
|
||||
import com.ms.api.common.R;
|
||||
import com.ms.api.common.Ret;
|
||||
import com.ms.api.common.TaskBaseService;
|
||||
import com.ms.api.consts.StatusConst;
|
||||
import com.ms.api.service.MaterialAuditStatusBufferService;
|
||||
import com.ms.api.service.ShopService;
|
||||
import com.ms.api.tool.CommonTool;
|
||||
import com.ms.api.tool.SecurityTool;
|
||||
import com.ms.api.tool.TimeTool;
|
||||
import com.ms.dal.entity.MaterialAuditStatusBuffer;
|
||||
import com.ms.dal.entity.MaterialBizAuditStatusLog;
|
||||
import com.ms.dal.entity.Shop;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.http.client.utils.DateUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.sql.Time;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
/**
|
||||
* 刷新抖店token
|
||||
*/
|
||||
|
||||
@Configuration
|
||||
@Component
|
||||
@Slf4j
|
||||
public class RefreshTokenTimerTaskService extends TaskBaseService {
|
||||
@Autowired
|
||||
ShopService shopService;
|
||||
|
||||
/**
|
||||
* 同时开启任务处理数量
|
||||
*/
|
||||
public int getCorePoolSiz() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
/**
|
||||
* 任务管理器名称
|
||||
*/
|
||||
public String getTaskExecutorName() {
|
||||
return "RefreshTokenTimerTaskPool";
|
||||
}
|
||||
|
||||
@Bean(name = "RefreshTokenTimerTaskPool")
|
||||
@Override
|
||||
public Executor getAsyncExecutor() {
|
||||
return super.getAsyncExecutor();
|
||||
}
|
||||
|
||||
@Resource(name = "RefreshTokenTimerTaskPool")
|
||||
protected Executor taskPool;
|
||||
|
||||
@Override
|
||||
protected Executor getTaskPool() {
|
||||
return taskPool;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runTask() {
|
||||
super.runTask();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取任务
|
||||
*/
|
||||
@Override
|
||||
public Object getTask() {
|
||||
String gmtExpireStart = TimeTool.getDateTimeBySeconds(3600);
|
||||
String gmtExpireEnd = TimeTool.getDateTimeBySeconds(-3600);
|
||||
List<Shop> shops = shopService.getExpiresTokenShops(gmtExpireStart, gmtExpireEnd);
|
||||
return shops;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理任务
|
||||
*
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Object processTask(Object params) {
|
||||
List<Shop> shops = (List<Shop>) params;
|
||||
for (int i=0; i < shops.size(); i++) {
|
||||
Shop shop = shops.get(i);
|
||||
Long shopId = shop.getShopId();
|
||||
// 不理解
|
||||
// $failCnt = $this->redis->hGet($redisKey, $shopId);
|
||||
// if ($failCnt > 5) {
|
||||
// continue;
|
||||
// }
|
||||
String oldAccessToken = SecurityTool.decodeByAES(shop.getAccessToken());
|
||||
String oldRefreshToken = SecurityTool.decodeByAES(shop.getRefreshToken());
|
||||
TokenRefreshRequest request = new TokenRefreshRequest();
|
||||
TokenRefreshParam param = request.getParam();
|
||||
param.setRefreshToken(oldRefreshToken);
|
||||
param.setGrantType("refresh_token");
|
||||
TokenRefreshResponse response = request.execute();
|
||||
String newAccessToken = response.getData().getAccessToken();
|
||||
String newRefreshToken = response.getData().getRefreshToken();
|
||||
Long newExpires = response.getData().getExpiresIn();
|
||||
if (ObjectUtil.isEmpty(newAccessToken)) {
|
||||
E.throwMSException("获取accessToken失败");
|
||||
}
|
||||
if (ObjectUtil.isEmpty(newAccessToken)) {
|
||||
Map result = new HashMap();
|
||||
result.put("msg", "accessToken无需更新");
|
||||
return R.ok(Ret.success());
|
||||
}
|
||||
shop.setExpiresIn(newExpires.toString());
|
||||
shop.setRefreshToken(SecurityTool.encodeByAES(newRefreshToken));
|
||||
shop.setAccessToken(SecurityTool.encodeByAES(newAccessToken));
|
||||
shop.setGmtExpire(new Date(TimeTool.getDateTimeBySeconds(new Long(newExpires).intValue())));
|
||||
shop.setGmtModified(new Date(TimeTool.getDateTimeBySeconds(0)));
|
||||
shopService.updateByPrimaryKey(shop);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理任务
|
||||
*
|
||||
* @param params
|
||||
*/
|
||||
@Override
|
||||
public void clearTask(Object params) {
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.ms.api.tool;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.ms.api.common.Ret;
|
||||
import com.ms.api.common.StrObjMap;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class TimeTool {
|
||||
public static String getDateTimeBySeconds(int seconds) {
|
||||
String currentTime = DateUtil.now();
|
||||
// 将当前时间转换为Date类型
|
||||
java.util.Date date = DateUtil.parseDateTime(currentTime);
|
||||
// 将当前时间减去x秒
|
||||
java.util.Date newDate = DateUtil.offsetSecond(date, seconds);
|
||||
return DateUtil.formatDateTime(newDate);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue