1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- package thyyxxk.webserver.scheduled;
- import com.alibaba.fastjson.JSONObject;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Component;
- import thyyxxk.webserver.entity.WeComTokens;
- import thyyxxk.webserver.service.externalhttp.CorpWxSrvc;
- import thyyxxk.webserver.utils.TokenUtil;
- @Slf4j
- @Component
- public class FetchAccessTokenTask {
- private final CorpWxSrvc srvc;
- private static final String USERINFO_SECRET = "wpHuNePfiDyotmpXjy5hUYGF0w8Ks5OPHSQp22z8oBk";
- private static final String CLOCKIN_SECRET = "_DodO23wewvESf-Lt-K46O2OsZ_nwrNhCbGaj0gbFjQ";
- private static final String HRG_SECRET = "xYHIiJldL1cn5-RbsyV2VIq505_22_3JY-U_gYpQ4hE";
- private static volatile boolean TOKEN_SYNCED = false;
- @Autowired
- public FetchAccessTokenTask(CorpWxSrvc srvc) {
- this.srvc = srvc;
- }
- @Scheduled(fixedRate = 6900 * 1000)
- public void getAccessToken() {
- JSONObject json = srvc.fetchAccessToken(USERINFO_SECRET);
- String weComAddressBookToken = json.getString("access_token");
- log.info("获取企业微信通讯录ACCESS_TOKEN: {}", weComAddressBookToken);
- TokenUtil.setWeComAddressBookToken(weComAddressBookToken);
- json = srvc.fetchAccessToken(CLOCKIN_SECRET);
- String weComClockInToken = json.getString("access_token");
- log.info("获取企业微信打卡数据ACCESS_TOKEN: {}", weComClockInToken);
- TokenUtil.setWeComClockInToken(weComClockInToken);
- json = srvc.fetchAccessToken(HRG_SECRET);
- String weComSendMsgToken = json.getString("access_token");
- log.info("获取企业微信紧急消息推送ACCESS_TOKEN: {}", weComSendMsgToken);
- TokenUtil.setWeComSendMsgToken(weComSendMsgToken);
- if (!TOKEN_SYNCED) {
- WeComTokens tokens = new WeComTokens(weComAddressBookToken, weComClockInToken, weComSendMsgToken);
- String updateTokensResult = srvc.syncTokens(tokens);
- log.info("同步线上环境企业微信tokens: {}", updateTokensResult);
- TOKEN_SYNCED = true;
- }
- }
- }
|