feat(企业微信): 添加根据企业ID查询corpid的功能

新增企业ID到corpid的缓存功能,包括在AuthCorpInfoApplicationService中添加查询方法,在CacheCenter和CaffeineCacheService中添加缓存配置,并在QywxController中提供对外接口。该功能用于快速获取企业corpid信息,减少数据库查询压力。
This commit is contained in:
dzq 2025-12-03 15:46:22 +08:00
parent 8dc5994ece
commit 796018aa32
4 changed files with 75 additions and 20 deletions

View File

@ -2,6 +2,7 @@ package com.agileboot.api.controller;
import com.agileboot.api.customize.async.QyAsyncTaskFactory; import com.agileboot.api.customize.async.QyAsyncTaskFactory;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.exception.ApiException; import com.agileboot.common.exception.ApiException;
import com.agileboot.common.exception.error.ErrorCode; import com.agileboot.common.exception.error.ErrorCode;
import com.agileboot.common.utils.weixin.aes.WXBizMsgCrypt; import com.agileboot.common.utils.weixin.aes.WXBizMsgCrypt;
@ -12,6 +13,7 @@ import com.agileboot.domain.qywx.auth.AuthApplicationService;
import com.agileboot.domain.qywx.auth.command.AddAuthCommand; import com.agileboot.domain.qywx.auth.command.AddAuthCommand;
import com.agileboot.domain.qywx.authCorpInfo.AuthCorpInfoApplicationService; import com.agileboot.domain.qywx.authCorpInfo.AuthCorpInfoApplicationService;
import com.agileboot.domain.qywx.authCorpInfo.db.QyAuthCorpInfoEntity; import com.agileboot.domain.qywx.authCorpInfo.db.QyAuthCorpInfoEntity;
import com.agileboot.domain.common.cache.CacheCenter;
import com.agileboot.domain.qywx.message.db.QyMessageEntity; import com.agileboot.domain.qywx.message.db.QyMessageEntity;
import com.agileboot.domain.qywx.template.TemplateApplicationService; import com.agileboot.domain.qywx.template.TemplateApplicationService;
import com.agileboot.domain.qywx.template.command.UpdateTemplateCommand; import com.agileboot.domain.qywx.template.command.UpdateTemplateCommand;
@ -251,6 +253,24 @@ public class QywxController {
return "success"; return "success";
} }
/**
* 根据企业ID查询corpid
* @param id 企业ID
* @return corpid字符串如果不存在则返回null
*/
@GetMapping("/getCorpidById")
public ResponseDTO<String> getCorpidById(@RequestParam Integer id) {
if (id == null || id <= 0) {
// 默认企业
id = 4;
}
String corpid = CacheCenter.corpidCache.get(id.toString());
if (StringUtils.isBlank(corpid)) {
throw new ApiException(ErrorCode.FAILED, "企业ID不存在");
}
return ResponseDTO.ok(corpid);
}
private Element parseXml( private Element parseXml(
String msg_signature, String msg_signature,

View File

@ -40,6 +40,8 @@ public class CacheCenter {
public static AbstractCaffeineCacheTemplate<com.agileboot.domain.wx.WxAccessToken> accessTokenCache; public static AbstractCaffeineCacheTemplate<com.agileboot.domain.wx.WxAccessToken> accessTokenCache;
public static AbstractCaffeineCacheTemplate<String> corpidCache;
@PostConstruct @PostConstruct
public void init() { public void init() {
GuavaCacheService guavaCache = SpringUtil.getBean(GuavaCacheService.class); GuavaCacheService guavaCache = SpringUtil.getBean(GuavaCacheService.class);
@ -56,6 +58,7 @@ public class CacheCenter {
qyUseridCache = caffeineCache.qyUseridCache; qyUseridCache = caffeineCache.qyUseridCache;
dynamicCodeCache = caffeineCache.dynamicCodeCache; dynamicCodeCache = caffeineCache.dynamicCodeCache;
accessTokenCache = caffeineCache.accessTokenCache; accessTokenCache = caffeineCache.accessTokenCache;
corpidCache = caffeineCache.corpidCache;
} }
} }

View File

@ -1,5 +1,6 @@
package com.agileboot.domain.common.cache; package com.agileboot.domain.common.cache;
import com.agileboot.domain.qywx.authCorpInfo.AuthCorpInfoApplicationService;
import com.agileboot.domain.system.post.db.SysPostEntity; import com.agileboot.domain.system.post.db.SysPostEntity;
import com.agileboot.domain.system.post.db.SysPostService; import com.agileboot.domain.system.post.db.SysPostService;
import com.agileboot.domain.system.role.db.SysRoleEntity; import com.agileboot.domain.system.role.db.SysRoleEntity;
@ -8,7 +9,12 @@ import com.agileboot.domain.system.user.db.SysUserEntity;
import com.agileboot.domain.system.user.db.SysUserService; import com.agileboot.domain.system.user.db.SysUserService;
import com.agileboot.infrastructure.cache.caffeine.AbstractCaffeineCacheTemplate; import com.agileboot.infrastructure.cache.caffeine.AbstractCaffeineCacheTemplate;
import com.agileboot.infrastructure.user.web.SystemLoginUser; import com.agileboot.infrastructure.user.web.SystemLoginUser;
import cn.hutool.extra.spring.SpringUtil;
import java.io.Serializable; import java.io.Serializable;
import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -23,8 +29,8 @@ public class CaffeineCacheService {
// 验证码缓存5分钟过期和刷新验证码通常短时间内有效 // 验证码缓存5分钟过期和刷新验证码通常短时间内有效
public AbstractCaffeineCacheTemplate<String> captchaCache = new AbstractCaffeineCacheTemplate<String>( public AbstractCaffeineCacheTemplate<String> captchaCache = new AbstractCaffeineCacheTemplate<String>(
5, java.util.concurrent.TimeUnit.MINUTES, 5, TimeUnit.MINUTES,
5, java.util.concurrent.TimeUnit.MINUTES) { 5, TimeUnit.MINUTES) {
@Override @Override
public String getObjectFromDb(Object id) { public String getObjectFromDb(Object id) {
// 验证码通常不需要从数据库获取这里返回null // 验证码通常不需要从数据库获取这里返回null
@ -34,8 +40,8 @@ public class CaffeineCacheService {
// 登录用户缓存1小时过期和刷新登录态保持 // 登录用户缓存1小时过期和刷新登录态保持
public AbstractCaffeineCacheTemplate<SystemLoginUser> loginUserCache = new AbstractCaffeineCacheTemplate<SystemLoginUser>( public AbstractCaffeineCacheTemplate<SystemLoginUser> loginUserCache = new AbstractCaffeineCacheTemplate<SystemLoginUser>(
12, java.util.concurrent.TimeUnit.HOURS, 12, TimeUnit.HOURS,
12, java.util.concurrent.TimeUnit.HOURS) { 12, TimeUnit.HOURS) {
@Override @Override
public SystemLoginUser getObjectFromDb(Object id) { public SystemLoginUser getObjectFromDb(Object id) {
// 登录用户信息通常不需要从数据库获取这里返回null // 登录用户信息通常不需要从数据库获取这里返回null
@ -45,41 +51,41 @@ public class CaffeineCacheService {
// 用户信息缓存2小时过期和刷新用户信息相对稳定但可能更新 // 用户信息缓存2小时过期和刷新用户信息相对稳定但可能更新
public AbstractCaffeineCacheTemplate<SysUserEntity> userCache = new AbstractCaffeineCacheTemplate<SysUserEntity>( public AbstractCaffeineCacheTemplate<SysUserEntity> userCache = new AbstractCaffeineCacheTemplate<SysUserEntity>(
12, java.util.concurrent.TimeUnit.HOURS, 12, TimeUnit.HOURS,
12, java.util.concurrent.TimeUnit.HOURS) { 12, TimeUnit.HOURS) {
@Override @Override
public SysUserEntity getObjectFromDb(Object id) { public SysUserEntity getObjectFromDb(Object id) {
SysUserService userService = cn.hutool.extra.spring.SpringUtil.getBean(SysUserService.class); SysUserService userService = SpringUtil.getBean(SysUserService.class);
return userService.getById((Serializable) id); return userService.getById((Serializable) id);
} }
}; };
// 角色缓存24小时过期和刷新角色信息很少变更 // 角色缓存24小时过期和刷新角色信息很少变更
public AbstractCaffeineCacheTemplate<SysRoleEntity> roleCache = new AbstractCaffeineCacheTemplate<SysRoleEntity>( public AbstractCaffeineCacheTemplate<SysRoleEntity> roleCache = new AbstractCaffeineCacheTemplate<SysRoleEntity>(
24, java.util.concurrent.TimeUnit.HOURS, 24, TimeUnit.HOURS,
24, java.util.concurrent.TimeUnit.HOURS) { 24, TimeUnit.HOURS) {
@Override @Override
public SysRoleEntity getObjectFromDb(Object id) { public SysRoleEntity getObjectFromDb(Object id) {
SysRoleService roleService = cn.hutool.extra.spring.SpringUtil.getBean(SysRoleService.class); SysRoleService roleService = SpringUtil.getBean(SysRoleService.class);
return roleService.getById((Serializable) id); return roleService.getById((Serializable) id);
} }
}; };
// 岗位缓存24小时过期和刷新岗位信息很少变更 // 岗位缓存24小时过期和刷新岗位信息很少变更
public AbstractCaffeineCacheTemplate<SysPostEntity> postCache = new AbstractCaffeineCacheTemplate<SysPostEntity>( public AbstractCaffeineCacheTemplate<SysPostEntity> postCache = new AbstractCaffeineCacheTemplate<SysPostEntity>(
24, java.util.concurrent.TimeUnit.HOURS, 24, TimeUnit.HOURS,
24, java.util.concurrent.TimeUnit.HOURS) { 24, TimeUnit.HOURS) {
@Override @Override
public SysPostEntity getObjectFromDb(Object id) { public SysPostEntity getObjectFromDb(Object id) {
SysPostService postService = cn.hutool.extra.spring.SpringUtil.getBean(SysPostService.class); SysPostService postService = SpringUtil.getBean(SysPostService.class);
return postService.getById((Serializable) id); return postService.getById((Serializable) id);
} }
}; };
// 企业微信用户ID缓存12小时过期和刷新通过API获取有一定成本 // 企业微信用户ID缓存12小时过期和刷新通过API获取有一定成本
public AbstractCaffeineCacheTemplate<String> qyUseridCache = new AbstractCaffeineCacheTemplate<String>( public AbstractCaffeineCacheTemplate<String> qyUseridCache = new AbstractCaffeineCacheTemplate<String>(
12, java.util.concurrent.TimeUnit.HOURS, 12, TimeUnit.HOURS,
12, java.util.concurrent.TimeUnit.HOURS) { 12, TimeUnit.HOURS) {
@Override @Override
public String getObjectFromDb(Object id) { public String getObjectFromDb(Object id) {
// 企业微信用户ID需要通过API获取这里返回null由调用方处理 // 企业微信用户ID需要通过API获取这里返回null由调用方处理
@ -89,8 +95,8 @@ public class CaffeineCacheService {
// 动态码缓存30分钟过期和刷新动态码通常短时间内有效 // 动态码缓存30分钟过期和刷新动态码通常短时间内有效
public AbstractCaffeineCacheTemplate<String> dynamicCodeCache = new AbstractCaffeineCacheTemplate<String>( public AbstractCaffeineCacheTemplate<String> dynamicCodeCache = new AbstractCaffeineCacheTemplate<String>(
30, java.util.concurrent.TimeUnit.MINUTES, 30, TimeUnit.MINUTES,
30, java.util.concurrent.TimeUnit.MINUTES) { 30, TimeUnit.MINUTES) {
@Override @Override
public String getObjectFromDb(Object id) { public String getObjectFromDb(Object id) {
// 动态码不需要从数据库获取这里返回null // 动态码不需要从数据库获取这里返回null
@ -100,8 +106,8 @@ public class CaffeineCacheService {
// 微信access_token缓存90分钟过期和刷新微信access_token有效期为2小时 // 微信access_token缓存90分钟过期和刷新微信access_token有效期为2小时
public AbstractCaffeineCacheTemplate<com.agileboot.domain.wx.WxAccessToken> accessTokenCache = new AbstractCaffeineCacheTemplate<com.agileboot.domain.wx.WxAccessToken>( public AbstractCaffeineCacheTemplate<com.agileboot.domain.wx.WxAccessToken> accessTokenCache = new AbstractCaffeineCacheTemplate<com.agileboot.domain.wx.WxAccessToken>(
90, java.util.concurrent.TimeUnit.MINUTES, 90, TimeUnit.MINUTES,
90, java.util.concurrent.TimeUnit.MINUTES) { 90, TimeUnit.MINUTES) {
@Override @Override
public com.agileboot.domain.wx.WxAccessToken getObjectFromDb(Object id) { public com.agileboot.domain.wx.WxAccessToken getObjectFromDb(Object id) {
// access_token需要通过微信API获取这里返回null由调用方处理 // access_token需要通过微信API获取这里返回null由调用方处理
@ -109,6 +115,19 @@ public class CaffeineCacheService {
} }
}; };
// 企业ID到corpid缓存24小时过期和刷新企业信息相对稳定
public AbstractCaffeineCacheTemplate<String> corpidCache = new AbstractCaffeineCacheTemplate<String>(
24, TimeUnit.HOURS,
24, TimeUnit.HOURS) {
@Override
public String getObjectFromDb(Object id) {
// 通过AuthCorpInfoApplicationService获取corpid
AuthCorpInfoApplicationService authCorpInfoService =
SpringUtil.getBean(AuthCorpInfoApplicationService.class);
return authCorpInfoService.getCorpidById(Integer.parseInt((String) id));
}
};
/** /**
* 获取缓存统计信息 * 获取缓存统计信息
* @return 统计信息字符串 * @return 统计信息字符串
@ -124,6 +143,7 @@ public class CaffeineCacheService {
stats.append("QyUserid Cache: ").append(qyUseridCache.getStats()).append("\n"); stats.append("QyUserid Cache: ").append(qyUseridCache.getStats()).append("\n");
stats.append("Dynamic Code Cache: ").append(dynamicCodeCache.getStats()).append("\n"); stats.append("Dynamic Code Cache: ").append(dynamicCodeCache.getStats()).append("\n");
stats.append("Access Token Cache: ").append(accessTokenCache.getStats()).append("\n"); stats.append("Access Token Cache: ").append(accessTokenCache.getStats()).append("\n");
stats.append("Corpid Cache: ").append(corpidCache.getStats()).append("\n");
return stats.toString(); return stats.toString();
} }
@ -150,6 +170,8 @@ public class CaffeineCacheService {
return dynamicCodeCache; return dynamicCodeCache;
case "accessTokenCache": case "accessTokenCache":
return accessTokenCache; return accessTokenCache;
case "corpidCache":
return corpidCache;
default: default:
return null; return null;
} }
@ -162,7 +184,7 @@ public class CaffeineCacheService {
public String[] getAllCacheNames() { public String[] getAllCacheNames() {
return new String[]{ return new String[]{
"captchaCache", "loginUserCache", "userCache", "captchaCache", "loginUserCache", "userCache",
"roleCache", "postCache", "qyUseridCache", "dynamicCodeCache", "accessTokenCache" "roleCache", "postCache", "qyUseridCache", "dynamicCodeCache", "accessTokenCache", "corpidCache"
}; };
} }
} }

View File

@ -76,4 +76,14 @@ public class AuthCorpInfoApplicationService {
} }
return new QyAuthCorpBasicInfoDTO(entity); return new QyAuthCorpBasicInfoDTO(entity);
} }
/**
* 根据企业ID查询corpid
* @param id 企业ID
* @return corpid字符串如果不存在则返回null
*/
public String getCorpidById(Integer id) {
QyAuthCorpInfoEntity entity = authCorpInfoService.getById(id);
return entity != null ? entity.getCorpid() : null;
}
} }