GetAccessToken.java
3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.uccc.number.job;
import com.alibaba.fastjson.JSONObject;
import com.uccc.number.domain.WebsiteConfig;
import com.uccc.number.service.NumberService;
import com.uccc.number.service.WebsiteService;
import com.uccc.number.utils.HTTPHelper;
import com.uccc.number.utils.MD5Utils;
import com.uccc.pretty.common.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.LinkedHashMap;
import static com.uccc.pretty.constants.ResultEnum.RESULT_OK;
/**
* Created by bert on 2021-10-27 10:01
*/
@Component
@EnableScheduling
public class GetAccessToken {
private Logger logger = LoggerFactory.getLogger(GetAccessToken.class);
@Autowired
private WebsiteService websiteService;
@Value("${third.api.url}")
private String thirdApiUrl;
@Value("${third.user.code}")
private String userCode;
@Value("${third.secret.key}")
private String secretKey;
@Value("${job.run}")
private String jobRun;
@Scheduled(cron = "0 0 * * * ?")
private void doJob() throws Exception {
logger.info("Start Do Job...");
if (jobRun.equals("0")) {
logger.info("can not run job for:{}", jobRun);
}
/**
* 定时1小时获取一次token
*/
JSONObject jsonBody = new JSONObject();
jsonBody.put("userCode", userCode);
jsonBody.put("secretKey", MD5Utils.stringToMD5(userCode+secretKey));
// 构建请求头
LinkedHashMap<String, String> header = new LinkedHashMap<>();
header.put("Accept", "*/*");
header.put("Content-Type", "application/json");
try {
String result = HTTPHelper.sendPOST(thirdApiUrl + "channelModule/getAccessToken", jsonBody.toJSONString(), header, "UTF-8");
if (result.contains("success")) {
logger.info("currentToken:{}", result);
JSONObject resJson = JSONObject.parseObject(result);
String data = resJson.getString("data");
JSONObject dataJson = JSONObject.parseObject(data);
String token = dataJson.getString("access_Token");
if (!StringUtils.isEmpty(token)) {
WebsiteConfig websiteConfig = new WebsiteConfig();
websiteConfig.setId(6L);
websiteConfig.setValue(token);
websiteConfig.setName("access_token");
Result insertRes = websiteService.updateConfig(websiteConfig);
if (insertRes.getCode() == RESULT_OK.getCode()) {
logger.info("get token success:{}", token);
}else {
logger.info("get token err.");
}
}
}else {
logger.error("get token err:{}", result);
}
} catch (Exception e) {
logger.error("get token err:{}", e.fillInStackTrace());
}
logger.info("end Do Job...");
}
}