|
@@ -10,6 +10,7 @@ import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
import top.husj.husj_wx.properties.WeChatProperties;
|
|
import top.husj.husj_wx.properties.WeChatProperties;
|
|
|
|
|
+import top.husj.husj_wx.service.AccessTokenService;
|
|
|
import top.husj.husj_wx.utils.AccessTokenUtil;
|
|
import top.husj.husj_wx.utils.AccessTokenUtil;
|
|
|
import top.husj.husj_wx.utils.EmailUtil;
|
|
import top.husj.husj_wx.utils.EmailUtil;
|
|
|
|
|
|
|
@@ -17,61 +18,11 @@ import top.husj.husj_wx.utils.EmailUtil;
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
public class AccessTokenConfig {
|
|
public class AccessTokenConfig {
|
|
|
@Autowired
|
|
@Autowired
|
|
|
- private RestTemplate restTemplate;
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private WeChatProperties weChatProperties;
|
|
|
|
|
|
|
+ private AccessTokenService accessTokenService;
|
|
|
|
|
|
|
|
@PostConstruct
|
|
@PostConstruct
|
|
|
public void initAccessToken() throws MessagingException {
|
|
public void initAccessToken() throws MessagingException {
|
|
|
log.info("应用启动,正在获取初始access_token...");
|
|
log.info("应用启动,正在获取初始access_token...");
|
|
|
- refreshAccessToken();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 每小时自动刷新access_token
|
|
|
|
|
- * fixedRate = 3600000 表示每3600000毫秒(1小时)执行一次
|
|
|
|
|
- */
|
|
|
|
|
- @Scheduled(fixedRate = 3600000)
|
|
|
|
|
- public void scheduledRefreshAccessToken() {
|
|
|
|
|
- log.info("定时任务触发,正在刷新access_token...");
|
|
|
|
|
- try {
|
|
|
|
|
- refreshAccessToken();
|
|
|
|
|
- } catch (MessagingException e) {
|
|
|
|
|
- log.error("定时刷新access_token失败: ", e);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 刷新access_token的核心逻辑
|
|
|
|
|
- */
|
|
|
|
|
- private void refreshAccessToken() throws MessagingException {
|
|
|
|
|
- String appId = weChatProperties.getAppid();
|
|
|
|
|
- String appSecret = weChatProperties.getAppsecret();
|
|
|
|
|
- try {
|
|
|
|
|
- String url = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",
|
|
|
|
|
- appId, appSecret);
|
|
|
|
|
-
|
|
|
|
|
- String response = restTemplate.getForObject(url, String.class);
|
|
|
|
|
- JSONObject jsonResponse = JSONObject.parseObject(response);
|
|
|
|
|
-
|
|
|
|
|
- if (jsonResponse.containsKey("access_token")) {
|
|
|
|
|
- String accessToken = jsonResponse.getString("access_token");
|
|
|
|
|
- AccessTokenUtil.setAccessToken(accessToken);
|
|
|
|
|
- log.info("成功获取access_token: {}", accessToken);
|
|
|
|
|
- } else {
|
|
|
|
|
- String errorMsg = jsonResponse.getString("errmsg");
|
|
|
|
|
- log.error("获取access_token失败: {}", errorMsg);
|
|
|
|
|
- throw new RuntimeException("获取access_token失败: " + errorMsg);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- log.error("获取access_token时发生异常: ", e);
|
|
|
|
|
- String host = "smtp.030208.xyz";
|
|
|
|
|
- String port = "465";
|
|
|
|
|
- String username = "husj@030208.xyz";
|
|
|
|
|
- String password = "15629747218hsjH";
|
|
|
|
|
- EmailUtil emailUtil = new EmailUtil(host, port, username, password);
|
|
|
|
|
- emailUtil.sendTextEmail("807244836@qq.com", "微信公众号token获取失败请检查", "微信公众号token获取失败请检查");
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ accessTokenService.refreshAccessToken();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|