com.zopen.wechat.mp.task.WechatMpTask Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zopen-ato-starter Show documentation
Show all versions of zopen-ato-starter Show documentation
Alibaba Tencent And Others For Spring Boot.
package com.zopen.wechat.mp.task;
import com.zcj.util.UtilString;
import com.zopen.ato.properties.WechatMpProperties;
import com.zopen.wechat.mp.service.WechatMpAccessTokenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component("atoWechatTask")
@EnableScheduling
public class WechatMpTask {
private static final Logger logger = LoggerFactory.getLogger(WechatMpTask.class);
@Autowired
private WechatMpAccessTokenService wechatMpAccessTokenService;
@Autowired
private WechatMpProperties wechatMpProperties;
@Scheduled(fixedRate = 1 * 60 * 60 * 1000)
private void refreshAccessToken() {
logger.info("开始刷新所有 access_token");
for (WechatInfo wechatInfo : WechatInfo.getOpenIdInfoMap().values()) {
try {
wechatMpAccessTokenService.init(wechatInfo.getAppId(), wechatInfo.getAppSecret());
} catch (Exception e) {
logger.warn("刷新 access_token 失败,app_id[{}],错误原因[{}]", wechatInfo.getAppId(), e.getMessage());
}
}
String appId = wechatMpProperties.getAppId();
String appSecret = wechatMpProperties.getAppSecret();
if (UtilString.isNotBlank(appId) && UtilString.isNotBlank(appSecret)) {
if (WechatInfo.getOpenIdInfoMap().get(appId) == null) {
wechatMpAccessTokenService.init(appId, appSecret);
}
}
logger.info("刷新所有 access_token 结束");
}
}