|
@@ -1,6 +1,7 @@
|
|
|
package com.qqflow.engine.domain.ewechat.service;
|
|
package com.qqflow.engine.domain.ewechat.service;
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.qqflow.engine.common.service.RedissonService;
|
|
|
import com.qqflow.engine.domain.ewechat.api.EnterpriseWechatApi;
|
|
import com.qqflow.engine.domain.ewechat.api.EnterpriseWechatApi;
|
|
|
import com.qqflow.engine.domain.ewechat.config.EnterpriseWechatProperties;
|
|
import com.qqflow.engine.domain.ewechat.config.EnterpriseWechatProperties;
|
|
|
import com.qqflow.engine.domain.ewechat.mapper.EnterpriseWechatCorpMapper;
|
|
import com.qqflow.engine.domain.ewechat.mapper.EnterpriseWechatCorpMapper;
|
|
@@ -14,6 +15,7 @@ import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
+import java.time.Duration;
|
|
|
import java.util.Collections;
|
|
import java.util.Collections;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
@@ -31,6 +33,11 @@ public class EnterpriseWechatMsgService {
|
|
|
private final EnterpriseWechatCorpMapper wechatCorpMapper;
|
|
private final EnterpriseWechatCorpMapper wechatCorpMapper;
|
|
|
private final SysUserMapper sysUserMapper;
|
|
private final SysUserMapper sysUserMapper;
|
|
|
private final SysUserRoleMapper sysUserRoleMapper;
|
|
private final SysUserRoleMapper sysUserRoleMapper;
|
|
|
|
|
+ private final RedissonService redissonService;
|
|
|
|
|
+
|
|
|
|
|
+ private static final String ACCESS_TOKEN_CACHE_KEY = "wecom:access_token:";
|
|
|
|
|
+ /** access_token 有效期 7200 秒,缓存 7000 秒留余量 */
|
|
|
|
|
+ private static final Duration ACCESS_TOKEN_TTL = Duration.ofSeconds(7000);
|
|
|
|
|
|
|
|
/** 给指定企微用户ID列表发送文本消息 */
|
|
/** 给指定企微用户ID列表发送文本消息 */
|
|
|
public void sendTextToUsers(List<String> wecomUserIds, String content) {
|
|
public void sendTextToUsers(List<String> wecomUserIds, String content) {
|
|
@@ -76,7 +83,7 @@ public class EnterpriseWechatMsgService {
|
|
|
.collect(Collectors.toList());
|
|
.collect(Collectors.toList());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /** 获取企业 access_token(供同步服务等复用) */
|
|
|
|
|
|
|
+ /** 获取企业 access_token(Redis 缓存,TTL 7000s) */
|
|
|
public String getCorpAccessToken() {
|
|
public String getCorpAccessToken() {
|
|
|
List<EnterpriseWechatCorp> corps = wechatCorpMapper.selectList(
|
|
List<EnterpriseWechatCorp> corps = wechatCorpMapper.selectList(
|
|
|
new LambdaQueryWrapper<EnterpriseWechatCorp>()
|
|
new LambdaQueryWrapper<EnterpriseWechatCorp>()
|
|
@@ -85,8 +92,15 @@ public class EnterpriseWechatMsgService {
|
|
|
.last("LIMIT 1"));
|
|
.last("LIMIT 1"));
|
|
|
if (corps.isEmpty()) return null;
|
|
if (corps.isEmpty()) return null;
|
|
|
EnterpriseWechatCorp corp = corps.get(0);
|
|
EnterpriseWechatCorp corp = corps.get(0);
|
|
|
|
|
+
|
|
|
|
|
+ String cacheKey = ACCESS_TOKEN_CACHE_KEY + corp.getCorpId();
|
|
|
|
|
+ String cached = redissonService.get(cacheKey);
|
|
|
|
|
+ if (cached != null) return cached;
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
- return enterpriseWechatApi.getAccessToken(corp.getCorpId(), corp.getPermanentCode()).getAccessToken();
|
|
|
|
|
|
|
+ String token = enterpriseWechatApi.getAccessToken(corp.getCorpId(), corp.getPermanentCode()).getAccessToken();
|
|
|
|
|
+ redissonService.set(cacheKey, token, ACCESS_TOKEN_TTL);
|
|
|
|
|
+ return token;
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
log.error("[企微消息] 获取 access_token 失败, corpId={}", corp.getCorpId(), e);
|
|
log.error("[企微消息] 获取 access_token 失败, corpId={}", corp.getCorpId(), e);
|
|
|
return null;
|
|
return null;
|