|
|
@@ -1,177 +0,0 @@
|
|
|
-package com.dderp.business.service.flycat;
|
|
|
-
|
|
|
-import com.dderp.common.api.flycat.IncomePlatformService;
|
|
|
-import com.dderp.common.base.BaseService;
|
|
|
-import com.dderp.common.datas.RedisKeys;
|
|
|
-import com.dderp.common.entity.invoke.douyin.DouyinTokenRequest;
|
|
|
-import com.dderp.common.entity.invoke.douyin.DouyinTokenResponse;
|
|
|
-import com.dderp.common.http.HttpTools;
|
|
|
-import com.dySweetFishPlugin.redis.RedisService;
|
|
|
-import com.dySweetFishPlugin.tool.lang.ThreadFactoryWithNamePrefix;
|
|
|
-import com.sweetfish.convert.json.JsonConvert;
|
|
|
-import com.sweetfish.service.Local;
|
|
|
-import com.sweetfish.util.AnyValue;
|
|
|
-import com.sweetfish.util.AutoLoad;
|
|
|
-import com.sweetfish.util.ResourceType;
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
-
|
|
|
-import javax.annotation.Resource;
|
|
|
-import java.time.Instant;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.concurrent.*;
|
|
|
-
|
|
|
-@AutoLoad(false)
|
|
|
-@Local
|
|
|
-@ResourceType(IncomePlatformService.class)
|
|
|
-public class IncomePlatformServiceImpl extends BaseService implements IncomePlatformService {
|
|
|
-
|
|
|
- @Resource
|
|
|
- private RedisService redisService;
|
|
|
-
|
|
|
- //抖音来客token线程池
|
|
|
- private ScheduledThreadPoolExecutor dyAccessTokenThread;
|
|
|
- private String runEnvironment;
|
|
|
-
|
|
|
- @Resource
|
|
|
- private JsonConvert jsonConvert;
|
|
|
-
|
|
|
- @Resource(name = "property.DYLK.appId")
|
|
|
- private String dyAppId;
|
|
|
-
|
|
|
- @Resource(name = "property.DYLK.appSecret")
|
|
|
- private String dyAppSecret;
|
|
|
-
|
|
|
- @Resource(name = "property.DYLK.reqUrl")
|
|
|
- private String dyReqUrl;
|
|
|
-
|
|
|
-
|
|
|
- @Override
|
|
|
- public void start(AnyValue config) {
|
|
|
- boolean master = false;
|
|
|
- AnyValue masterValue = config.getAnyValue("master");
|
|
|
- if (masterValue != null) {
|
|
|
- master = masterValue.getBoolValue("value");
|
|
|
- }
|
|
|
-
|
|
|
- AnyValue environmentValue = config.getAnyValue("environment");
|
|
|
- if (environmentValue != null) {
|
|
|
- runEnvironment = environmentValue.getValue("value");
|
|
|
- } else {
|
|
|
- runEnvironment = "dev";
|
|
|
- }
|
|
|
-
|
|
|
- if (master) {
|
|
|
- if (StringUtils.equalsIgnoreCase(runEnvironment, "dev")) {
|
|
|
- //开发环境
|
|
|
- dyAccessTokenThread = null;
|
|
|
- } else {
|
|
|
- //启动的时候10分钟之后获取,然后每隔1个半小时刷新一次token
|
|
|
- //为什么10分钟,因为企点是每10分钟发送ticket,没有ticket的情况通过ticket是换不到开发商token的
|
|
|
- dyAccessTokenThread = new ScheduledThreadPoolExecutor(1,
|
|
|
- new ThreadFactoryWithNamePrefix("[抖音Access_Token线程池]"));
|
|
|
-
|
|
|
- long tokenInitialDelay = 30;
|
|
|
-
|
|
|
- //判断上次刷新token的时间,
|
|
|
- if (redisService.exists(RedisKeys.KEY_DOUYIN_LASTTOKENDATE)) {
|
|
|
- long lastTime = Long.parseLong(redisService.get(RedisKeys.KEY_DOUYIN_LASTTOKENDATE));
|
|
|
- int diffSeconds = (int) ((System.currentTimeMillis() - lastTime) / 1000);
|
|
|
-
|
|
|
- if (diffSeconds <= 4800) {
|
|
|
- tokenInitialDelay = 4800 - diffSeconds;
|
|
|
- if (tokenInitialDelay < 30) {
|
|
|
- tokenInitialDelay = 30;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- dyAccessTokenThread.scheduleWithFixedDelay(() -> {
|
|
|
- scanRefreshToken();
|
|
|
- //写入当前时间
|
|
|
- redisService.set(RedisKeys.KEY_DOUYIN_LASTTOKENDATE, String.valueOf(Date.from(Instant.now()).getTime()));
|
|
|
- }, tokenInitialDelay, 5400, TimeUnit.SECONDS);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void destroy(AnyValue config) {
|
|
|
- if (dyAccessTokenThread != null) {
|
|
|
- dyAccessTokenThread.shutdownNow();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void scanRefreshToken() {
|
|
|
- DouyinTokenResponse dyToken = null;
|
|
|
- //抖音有可能获取不到token,这里循环获取,直到有值?
|
|
|
- do {
|
|
|
- dyToken = this.requestAccessToken(dyAppId, dyAppSecret);
|
|
|
-
|
|
|
- } while ((dyToken == null) || (StringUtils.isEmpty(dyToken.getData().getAccess_token())));
|
|
|
-
|
|
|
- redisService.set(RedisKeys.KEY_DOUYIN_ACCESSTOKEN, dyToken.getData().getAccess_token());
|
|
|
- logger.info("抖音服务刷新token:" + dyToken.getData().getAccess_token());
|
|
|
- }
|
|
|
-
|
|
|
- private DouyinTokenResponse requestAccessToken(String appId, String appSecret) {
|
|
|
- DouyinTokenRequest tokenRequest = new DouyinTokenRequest.Builder()
|
|
|
- .client_key(appId)
|
|
|
- .client_secret(appSecret)
|
|
|
- .build();
|
|
|
- Map<String, String> header = new HashMap<>();
|
|
|
- header.put("Content-Type", "application/json");
|
|
|
-
|
|
|
- try {
|
|
|
- String responseStr = HttpTools.postHttpContentAsync(dyReqUrl + "/oauth/client_token/", header,
|
|
|
- jsonConvert.convertTo(tokenRequest)).get(5, TimeUnit.SECONDS);
|
|
|
-
|
|
|
- return jsonConvert.convertFrom(DouyinTokenResponse.class, responseStr);
|
|
|
- } catch (InterruptedException | ExecutionException | TimeoutException e) {
|
|
|
- logger.error(e.getMessage(), e);
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- private String getAccessToken() {
|
|
|
- String token = redisService.get(RedisKeys.KEY_DOUYIN_ACCESSTOKEN);
|
|
|
- if (StringUtils.isBlank(token)) {
|
|
|
- //token不存在,直接刷新一下
|
|
|
- this.scanRefreshToken();
|
|
|
- token = redisService.get(RedisKeys.KEY_DOUYIN_ACCESSTOKEN);
|
|
|
- }
|
|
|
-
|
|
|
- return token;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String dyPostInvoker(String subUrl, String body) {
|
|
|
- Map<String, String> header = new HashMap<>();
|
|
|
- header.put("access-token", getAccessToken());
|
|
|
- header.put("log_id", String.valueOf(new Date().getTime()));
|
|
|
- header.put("Content-Type", "application/json");
|
|
|
-
|
|
|
- try {
|
|
|
- return HttpTools.postHttpContentAsync(dyReqUrl + subUrl, header, body).get(20, TimeUnit.SECONDS);
|
|
|
- } catch (InterruptedException | ExecutionException | TimeoutException e) {
|
|
|
- logger.error("请求抖音来客接口[" + subUrl + "]失败", e);
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String dyGetInvoker(String subUrl, String body) {
|
|
|
- Map<String, String> header = new HashMap<>();
|
|
|
- header.put("access-token", getAccessToken());
|
|
|
- header.put("log_id", String.valueOf(new Date().getTime()));
|
|
|
- header.put("Content-Type", "application/json");
|
|
|
-
|
|
|
- try {
|
|
|
- return HttpTools.getHttpContentAsync(dyReqUrl + subUrl, header, body).get(20, TimeUnit.SECONDS);
|
|
|
- } catch (InterruptedException | ExecutionException | TimeoutException e) {
|
|
|
- logger.error("请求抖音来客接口[" + subUrl + "]失败", e);
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-}
|