diff --git a/agileboot-api/src/main/java/com/agileboot/api/controller/QywxController.java b/agileboot-api/src/main/java/com/agileboot/api/controller/QywxController.java index 888767b..3cc42f4 100644 --- a/agileboot-api/src/main/java/com/agileboot/api/controller/QywxController.java +++ b/agileboot-api/src/main/java/com/agileboot/api/controller/QywxController.java @@ -2,6 +2,7 @@ package com.agileboot.api.controller; 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.error.ErrorCode; 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.authCorpInfo.AuthCorpInfoApplicationService; 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.template.TemplateApplicationService; import com.agileboot.domain.qywx.template.command.UpdateTemplateCommand; @@ -251,6 +253,24 @@ public class QywxController { return "success"; } + /** + * 根据企业ID查询corpid + * @param id 企业ID + * @return corpid字符串,如果不存在则返回null + */ + @GetMapping("/getCorpidById") + public ResponseDTO 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( String msg_signature, diff --git a/agileboot-domain/src/main/java/com/agileboot/domain/common/cache/CacheCenter.java b/agileboot-domain/src/main/java/com/agileboot/domain/common/cache/CacheCenter.java index 11da44a..5c7e847 100644 --- a/agileboot-domain/src/main/java/com/agileboot/domain/common/cache/CacheCenter.java +++ b/agileboot-domain/src/main/java/com/agileboot/domain/common/cache/CacheCenter.java @@ -40,6 +40,8 @@ public class CacheCenter { public static AbstractCaffeineCacheTemplate accessTokenCache; + public static AbstractCaffeineCacheTemplate corpidCache; + @PostConstruct public void init() { GuavaCacheService guavaCache = SpringUtil.getBean(GuavaCacheService.class); @@ -56,6 +58,7 @@ public class CacheCenter { qyUseridCache = caffeineCache.qyUseridCache; dynamicCodeCache = caffeineCache.dynamicCodeCache; accessTokenCache = caffeineCache.accessTokenCache; + corpidCache = caffeineCache.corpidCache; } } diff --git a/agileboot-domain/src/main/java/com/agileboot/domain/common/cache/CaffeineCacheService.java b/agileboot-domain/src/main/java/com/agileboot/domain/common/cache/CaffeineCacheService.java index e78b35e..67b34a7 100644 --- a/agileboot-domain/src/main/java/com/agileboot/domain/common/cache/CaffeineCacheService.java +++ b/agileboot-domain/src/main/java/com/agileboot/domain/common/cache/CaffeineCacheService.java @@ -1,5 +1,6 @@ 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.SysPostService; 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.infrastructure.cache.caffeine.AbstractCaffeineCacheTemplate; import com.agileboot.infrastructure.user.web.SystemLoginUser; + +import cn.hutool.extra.spring.SpringUtil; + import java.io.Serializable; +import java.util.concurrent.TimeUnit; + import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; @@ -23,8 +29,8 @@ public class CaffeineCacheService { // 验证码缓存:5分钟过期和刷新(验证码通常短时间内有效) public AbstractCaffeineCacheTemplate captchaCache = new AbstractCaffeineCacheTemplate( - 5, java.util.concurrent.TimeUnit.MINUTES, - 5, java.util.concurrent.TimeUnit.MINUTES) { + 5, TimeUnit.MINUTES, + 5, TimeUnit.MINUTES) { @Override public String getObjectFromDb(Object id) { // 验证码通常不需要从数据库获取,这里返回null @@ -34,8 +40,8 @@ public class CaffeineCacheService { // 登录用户缓存:1小时过期和刷新(登录态保持) public AbstractCaffeineCacheTemplate loginUserCache = new AbstractCaffeineCacheTemplate( - 12, java.util.concurrent.TimeUnit.HOURS, - 12, java.util.concurrent.TimeUnit.HOURS) { + 12, TimeUnit.HOURS, + 12, TimeUnit.HOURS) { @Override public SystemLoginUser getObjectFromDb(Object id) { // 登录用户信息通常不需要从数据库获取,这里返回null @@ -45,41 +51,41 @@ public class CaffeineCacheService { // 用户信息缓存:2小时过期和刷新(用户信息相对稳定但可能更新) public AbstractCaffeineCacheTemplate userCache = new AbstractCaffeineCacheTemplate( - 12, java.util.concurrent.TimeUnit.HOURS, - 12, java.util.concurrent.TimeUnit.HOURS) { + 12, TimeUnit.HOURS, + 12, TimeUnit.HOURS) { @Override 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); } }; // 角色缓存:24小时过期和刷新(角色信息很少变更) public AbstractCaffeineCacheTemplate roleCache = new AbstractCaffeineCacheTemplate( - 24, java.util.concurrent.TimeUnit.HOURS, - 24, java.util.concurrent.TimeUnit.HOURS) { + 24, TimeUnit.HOURS, + 24, TimeUnit.HOURS) { @Override 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); } }; // 岗位缓存:24小时过期和刷新(岗位信息很少变更) public AbstractCaffeineCacheTemplate postCache = new AbstractCaffeineCacheTemplate( - 24, java.util.concurrent.TimeUnit.HOURS, - 24, java.util.concurrent.TimeUnit.HOURS) { + 24, TimeUnit.HOURS, + 24, TimeUnit.HOURS) { @Override 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); } }; // 企业微信用户ID缓存:12小时过期和刷新(通过API获取,有一定成本) public AbstractCaffeineCacheTemplate qyUseridCache = new AbstractCaffeineCacheTemplate( - 12, java.util.concurrent.TimeUnit.HOURS, - 12, java.util.concurrent.TimeUnit.HOURS) { + 12, TimeUnit.HOURS, + 12, TimeUnit.HOURS) { @Override public String getObjectFromDb(Object id) { // 企业微信用户ID需要通过API获取,这里返回null,由调用方处理 @@ -89,8 +95,8 @@ public class CaffeineCacheService { // 动态码缓存:30分钟过期和刷新(动态码通常短时间内有效) public AbstractCaffeineCacheTemplate dynamicCodeCache = new AbstractCaffeineCacheTemplate( - 30, java.util.concurrent.TimeUnit.MINUTES, - 30, java.util.concurrent.TimeUnit.MINUTES) { + 30, TimeUnit.MINUTES, + 30, TimeUnit.MINUTES) { @Override public String getObjectFromDb(Object id) { // 动态码不需要从数据库获取,这里返回null @@ -100,8 +106,8 @@ public class CaffeineCacheService { // 微信access_token缓存:90分钟过期和刷新(微信access_token有效期为2小时) public AbstractCaffeineCacheTemplate accessTokenCache = new AbstractCaffeineCacheTemplate( - 90, java.util.concurrent.TimeUnit.MINUTES, - 90, java.util.concurrent.TimeUnit.MINUTES) { + 90, TimeUnit.MINUTES, + 90, TimeUnit.MINUTES) { @Override public com.agileboot.domain.wx.WxAccessToken getObjectFromDb(Object id) { // access_token需要通过微信API获取,这里返回null,由调用方处理 @@ -109,6 +115,19 @@ public class CaffeineCacheService { } }; + // 企业ID到corpid缓存:24小时过期和刷新(企业信息相对稳定) + public AbstractCaffeineCacheTemplate corpidCache = new AbstractCaffeineCacheTemplate( + 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 统计信息字符串 @@ -124,6 +143,7 @@ public class CaffeineCacheService { stats.append("QyUserid Cache: ").append(qyUseridCache.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("Corpid Cache: ").append(corpidCache.getStats()).append("\n"); return stats.toString(); } @@ -150,6 +170,8 @@ public class CaffeineCacheService { return dynamicCodeCache; case "accessTokenCache": return accessTokenCache; + case "corpidCache": + return corpidCache; default: return null; } @@ -162,7 +184,7 @@ public class CaffeineCacheService { public String[] getAllCacheNames() { return new String[]{ "captchaCache", "loginUserCache", "userCache", - "roleCache", "postCache", "qyUseridCache", "dynamicCodeCache", "accessTokenCache" + "roleCache", "postCache", "qyUseridCache", "dynamicCodeCache", "accessTokenCache", "corpidCache" }; } } \ No newline at end of file diff --git a/agileboot-domain/src/main/java/com/agileboot/domain/qywx/authCorpInfo/AuthCorpInfoApplicationService.java b/agileboot-domain/src/main/java/com/agileboot/domain/qywx/authCorpInfo/AuthCorpInfoApplicationService.java index 30cf736..3be29a1 100644 --- a/agileboot-domain/src/main/java/com/agileboot/domain/qywx/authCorpInfo/AuthCorpInfoApplicationService.java +++ b/agileboot-domain/src/main/java/com/agileboot/domain/qywx/authCorpInfo/AuthCorpInfoApplicationService.java @@ -76,4 +76,14 @@ public class AuthCorpInfoApplicationService { } 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; + } } \ No newline at end of file