FetchAccessTokenTask.java 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package thyyxxk.webserver.scheduled;
  2. import com.alibaba.fastjson.JSONObject;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.scheduling.annotation.Scheduled;
  6. import org.springframework.stereotype.Component;
  7. import thyyxxk.webserver.entity.WeComTokens;
  8. import thyyxxk.webserver.service.externalhttp.CorpWxSrvc;
  9. import thyyxxk.webserver.utils.TokenUtil;
  10. @Slf4j
  11. @Component
  12. public class FetchAccessTokenTask {
  13. private final CorpWxSrvc srvc;
  14. private static final String USERINFO_SECRET = "wpHuNePfiDyotmpXjy5hUYGF0w8Ks5OPHSQp22z8oBk";
  15. private static final String CLOCKIN_SECRET = "_DodO23wewvESf-Lt-K46O2OsZ_nwrNhCbGaj0gbFjQ";
  16. private static final String HRG_SECRET = "xYHIiJldL1cn5-RbsyV2VIq505_22_3JY-U_gYpQ4hE";
  17. private static volatile boolean TOKEN_SYNCED = false;
  18. @Autowired
  19. public FetchAccessTokenTask(CorpWxSrvc srvc) {
  20. this.srvc = srvc;
  21. }
  22. @Scheduled(fixedRate = 6900 * 1000)
  23. public void getAccessToken() {
  24. JSONObject json = srvc.fetchAccessToken(USERINFO_SECRET);
  25. String weComAddressBookToken = json.getString("access_token");
  26. log.info("获取企业微信通讯录ACCESS_TOKEN: {}", weComAddressBookToken);
  27. TokenUtil.setWeComAddressBookToken(weComAddressBookToken);
  28. json = srvc.fetchAccessToken(CLOCKIN_SECRET);
  29. String weComClockInToken = json.getString("access_token");
  30. log.info("获取企业微信打卡数据ACCESS_TOKEN: {}", weComClockInToken);
  31. TokenUtil.setWeComClockInToken(weComClockInToken);
  32. json = srvc.fetchAccessToken(HRG_SECRET);
  33. String weComSendMsgToken = json.getString("access_token");
  34. log.info("获取企业微信紧急消息推送ACCESS_TOKEN: {}", weComSendMsgToken);
  35. TokenUtil.setWeComSendMsgToken(weComSendMsgToken);
  36. if (!TOKEN_SYNCED) {
  37. WeComTokens tokens = new WeComTokens(weComAddressBookToken, weComClockInToken, weComSendMsgToken);
  38. String updateTokensResult = srvc.syncTokens(tokens);
  39. log.info("同步线上环境企业微信tokens: {}", updateTokensResult);
  40. TOKEN_SYNCED = true;
  41. }
  42. }
  43. }