feat(企业微信): 添加根据企业ID查询corpid的功能
新增企业ID到corpid的缓存功能,包括在AuthCorpInfoApplicationService中添加查询方法,在CacheCenter和CaffeineCacheService中添加缓存配置,并在QywxController中提供对外接口。该功能用于快速获取企业corpid信息,减少数据库查询压力。
This commit is contained in:
parent
8dc5994ece
commit
796018aa32
|
|
@ -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<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(
|
||||
String msg_signature,
|
||||
|
|
|
|||
|
|
@ -40,6 +40,8 @@ public class CacheCenter {
|
|||
|
||||
public static AbstractCaffeineCacheTemplate<com.agileboot.domain.wx.WxAccessToken> accessTokenCache;
|
||||
|
||||
public static AbstractCaffeineCacheTemplate<String> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<String> captchaCache = new AbstractCaffeineCacheTemplate<String>(
|
||||
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<SystemLoginUser> loginUserCache = new AbstractCaffeineCacheTemplate<SystemLoginUser>(
|
||||
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<SysUserEntity> userCache = new AbstractCaffeineCacheTemplate<SysUserEntity>(
|
||||
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<SysRoleEntity> roleCache = new AbstractCaffeineCacheTemplate<SysRoleEntity>(
|
||||
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<SysPostEntity> postCache = new AbstractCaffeineCacheTemplate<SysPostEntity>(
|
||||
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<String> qyUseridCache = new AbstractCaffeineCacheTemplate<String>(
|
||||
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<String> dynamicCodeCache = new AbstractCaffeineCacheTemplate<String>(
|
||||
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<com.agileboot.domain.wx.WxAccessToken> accessTokenCache = new AbstractCaffeineCacheTemplate<com.agileboot.domain.wx.WxAccessToken>(
|
||||
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<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 统计信息字符串
|
||||
|
|
@ -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"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue