wangchaoxu 1 year ago
commit 48974cd1c2

@ -5,7 +5,7 @@ curl --location --request POST 'localhost:8080/base/cloud/spi' \
--data-raw '{
"appId":"7264840234423027259",
"authId":"4463798",
"method":"getMoveHistoryProductList",
"method":"testTime",
"data":"{\"moveShopSettingId\":6}"
}'

@ -0,0 +1,35 @@
package com.ms.api;
import cn.hutool.core.date.DateUtil;
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.SPIBaseService;
import com.ms.api.dto.ItemDTO;
import com.ms.api.paas.StorageService;
import com.ms.api.tool.TimeTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@ExtensionService("testTime")
@Slf4j
public class TestTimeService extends SPIBaseService implements ExtensionServiceHandler<ItemDTO, String> {
@Override
public BaseResponse<String> handle(BaseRequest<ItemDTO> req) {
// String currentTime = DateUtil.now();
//
// // 将当前时间转换为Date类型
// java.util.Date date = DateUtil.parseDateTime(currentTime);
//
// // 将当前时间减去5秒
// java.util.Date newDate = DateUtil.offsetSecond(date, -3600);
//
// String newTime = DateUtil.formatDateTime(newDate);
// System.out.println("当前时间:" + currentTime);
System.out.println("减去5秒后的时间" + TimeTool.getDateTimeBySeconds(-3600));
return R.ok("");
}
}

@ -24,4 +24,6 @@ public interface ShopService {
int updateByPrimaryKey(Shop record);
List<Shop> getShopByIds(List<String> shopIds);
List<Shop> getExpiresTokenShops(String gmtExpireStart,String gmtExpireEnd);
}

@ -67,6 +67,11 @@ public class ShopServiceImpl implements ShopService {
return shopList;
}
@Override
public List<Shop> getExpiresTokenShops(String gmtExpireStart,String gmtExpireEnd) {
return shopMapper.getExpiresTokenShops(gmtExpireStart,gmtExpireEnd);
}
}

@ -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);
}
}

@ -1,5 +1,6 @@
package com.ms.dal.mapper;
import java.util.Date;
import java.util.List;
import com.ms.dal.entity.Shop;
@ -25,4 +26,6 @@ public interface ShopMapper {
int updateByPrimaryKey(Shop record);
List<Shop> getShopByIds(@Param("shopIds") List<String> shopIds);
List<Shop> getExpiresTokenShops(String gmtExpireStart,String gmtExpireEnd);
}

@ -151,4 +151,10 @@
gmt_modified = #{gmtModified,jdbcType=TIMESTAMP}
where shop_id = #{shopId,jdbcType=BIGINT}
</update>
<select id="getExpiresTokenShops" parameterType="java.lang.Long" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from shop
where gmt_expire &lt; #{gmtExpireStart,jdbcType=TIMESTAMP} and gmt_expire &gt; #{gmtExpireEnd,jdbcType=TIMESTAMP}
</select>
</mapper>

Loading…
Cancel
Save