提交 03c67c4d 编写于 作者: 孙喜旺

多企业token获取修改

上级 c2467c8c
......@@ -17,10 +17,10 @@ import java.util.Date;
@Slf4j
@Component
public class WeMetaObjectHandler implements MetaObjectHandler {
private static String CREATE_TIME = "createTime";
private static String CREATE_BY = "createBy";
private static String UPDATE_TIME = "updateTime";
private static String UPDATE_BY = "updateBY";
private static final String CREATE_TIME = "createTime";
private static final String CREATE_BY = "createBy";
private static final String UPDATE_TIME = "updateTime";
private static final String UPDATE_BY = "updateBY";
@Override
public void insertFill(MetaObject metaObject) {
......
......@@ -49,6 +49,10 @@ public interface IWeCorpAccountService extends IService<WeCorpAccount>
*/
String getCustomerChurnNoticeSwitch();
/**
* 根据corpId 获取企业配置信息
* @param corpId
* @return
*/
WeCorpAccount getWeCorpAccount(String corpId);
}
......@@ -2,6 +2,7 @@ package com.linkwechat.wecom.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.linkwechat.common.config.RuoYiConfig;
import com.linkwechat.common.config.WeComeConfig;
import com.linkwechat.common.constant.Constants;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.redis.RedisCache;
......@@ -14,6 +15,7 @@ import com.linkwechat.common.core.domain.entity.WeCorpAccount;
import com.linkwechat.wecom.domain.dto.WeAccessTokenDtoDto;
import com.linkwechat.wecom.service.IWeAccessTokenService;
import com.linkwechat.wecom.service.IWeAppService;
import com.linkwechat.wecom.service.IWeComCorpTokenService;
import com.linkwechat.wecom.service.IWeCorpAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -26,7 +28,7 @@ import java.util.concurrent.TimeUnit;
* @create: 2020-08-26 14:43
**/
@Service
public class WeAccessTokenServiceImpl implements IWeAccessTokenService {
public class WeAccessTokenServiceImpl extends IWeComCorpTokenService implements IWeAccessTokenService {
@Autowired
......@@ -54,8 +56,6 @@ public class WeAccessTokenServiceImpl implements IWeAccessTokenService {
*/
@Override
public String findCommonAccessToken() {
return findAccessToken(WeConstans.WE_COMMON_ACCESS_TOKEN);
}
......@@ -66,8 +66,6 @@ public class WeAccessTokenServiceImpl implements IWeAccessTokenService {
*/
@Override
public String findContactAccessToken() {
return findAccessToken(WeConstans.WE_CONTACT_ACCESS_TOKEN);
}
......@@ -100,8 +98,6 @@ public class WeAccessTokenServiceImpl implements IWeAccessTokenService {
*/
@Override
public String findThirdAppAccessToken(String agentId) {
String token=redisCache.getCacheObject(WeConstans.WE_THIRD_APP_TOKEN+"::"+agentId);
if(StringUtils.isNotEmpty(token)){
......@@ -117,10 +113,7 @@ public class WeAccessTokenServiceImpl implements IWeAccessTokenService {
}
private String findThirdAppAccessToken(WeApp weApp){
if(weApp == null){
throw new WeComException("当前agentId不可用或不存在");
}
......@@ -138,15 +131,10 @@ public class WeAccessTokenServiceImpl implements IWeAccessTokenService {
redisCache.setCacheObject(WeConstans.WE_THIRD_APP_TOKEN+"::"+weApp.getAgentId(),weAccessTokenDtoDto.getAccess_token(),weAccessTokenDtoDto.getExpires_in().intValue(), TimeUnit.SECONDS);
}
return weAccessTokenDtoDto.getAccess_token();
}
private String findAccessToken(String accessTokenKey){
return ruoYiConfig.isStartTenant()?findAccessTokenForTenant(accessTokenKey):findAccessTokenForNoTenant(accessTokenKey);
......@@ -157,55 +145,7 @@ public class WeAccessTokenServiceImpl implements IWeAccessTokenService {
//多租户环境下获取token
private String findAccessTokenForTenant(String accessTokenKey){
WeCorpAccount wxCorpAccount
= SecurityUtils.getLoginUser().getUser().getWeCorpAccount();
if(null == wxCorpAccount){
//返回错误异常,让用户绑定企业id相关信息
throw new WeComException("无可用的corpid和secret");
}
String weAccessToken =redisCache.getCacheObject(accessTokenKey+"::"+wxCorpAccount.getCorpId());
//为空,请求微信服务器同时缓存到redis中
if(StringUtils.isEmpty(weAccessToken)){
String token="";
Long expires_in=null;
if(WeConstans.WE_COMMON_ACCESS_TOKEN.equals(accessTokenKey) || WeConstans.WE_CONTACT_ACCESS_TOKEN.equals(accessTokenKey)){
WeAccessTokenDtoDto weAccessTokenDtoDto = accessTokenClient.getToken(wxCorpAccount.getCorpId(),
WeConstans.WE_COMMON_ACCESS_TOKEN.equals(accessTokenKey) ? wxCorpAccount.getCorpSecret() : wxCorpAccount.getContactSecret());
token=weAccessTokenDtoDto.getAccess_token();
expires_in=weAccessTokenDtoDto.getExpires_in();
}else if(WeConstans.WE_PROVIDER_ACCESS_TOKEN.equals(accessTokenKey)){
WeAccessTokenDtoDto providerToken = accessTokenClient.getProviderToken(wxCorpAccount.getCorpId(), wxCorpAccount.getProviderSecret());
token=providerToken.getProvider_access_token();
expires_in=providerToken.getExpires_in();
}else if (WeConstans.WE_CHAT_ACCESS_TOKEN.equals(accessTokenKey)){
WeAccessTokenDtoDto weAccessTokenDtoDto = accessTokenClient.getToken(wxCorpAccount.getCorpId(),wxCorpAccount.getChatSecret());
token=weAccessTokenDtoDto.getAccess_token();
expires_in=weAccessTokenDtoDto.getExpires_in();
}else if (WeConstans.WE_AGENT_ACCESS_TOKEN.equals(accessTokenKey)){
WeAccessTokenDtoDto weAccessTokenDtoDto = accessTokenClient.getToken(wxCorpAccount.getCorpId(),wxCorpAccount.getAgentSecret());
token=weAccessTokenDtoDto.getAccess_token();
expires_in=weAccessTokenDtoDto.getExpires_in();
}
if(StringUtils.isNotEmpty(token)){
redisCache.setCacheObject(accessTokenKey+"::"+wxCorpAccount.getCorpId(),token,expires_in.intValue(), TimeUnit.SECONDS);
weAccessToken = token;
}
}
return weAccessToken;
return getWeComToken(accessTokenKey);
}
......@@ -272,4 +212,39 @@ public class WeAccessTokenServiceImpl implements IWeAccessTokenService {
}
@Override
protected String getToken(String accessTokenKey , WeCorpAccount wxCorpAccount) {
String weAccessToken =redisCache.getCacheObject(accessTokenKey+"::"+wxCorpAccount.getCorpId());
//为空,请求微信服务器同时缓存到redis中
if(StringUtils.isEmpty(weAccessToken)){
String token="";
Long expires_in=null;
if(WeConstans.WE_COMMON_ACCESS_TOKEN.equals(accessTokenKey) || WeConstans.WE_CONTACT_ACCESS_TOKEN.equals(accessTokenKey)){
WeAccessTokenDtoDto weAccessTokenDtoDto = accessTokenClient.getToken(wxCorpAccount.getCorpId(),
WeConstans.WE_COMMON_ACCESS_TOKEN.equals(accessTokenKey) ? wxCorpAccount.getCorpSecret() : wxCorpAccount.getContactSecret());
token=weAccessTokenDtoDto.getAccess_token();
expires_in=weAccessTokenDtoDto.getExpires_in();
}else if(WeConstans.WE_PROVIDER_ACCESS_TOKEN.equals(accessTokenKey)){
WeAccessTokenDtoDto providerToken = accessTokenClient.getProviderToken(wxCorpAccount.getCorpId(), wxCorpAccount.getProviderSecret());
token=providerToken.getProvider_access_token();
expires_in=providerToken.getExpires_in();
}else if (WeConstans.WE_CHAT_ACCESS_TOKEN.equals(accessTokenKey)){
WeAccessTokenDtoDto weAccessTokenDtoDto = accessTokenClient.getToken(wxCorpAccount.getCorpId(),wxCorpAccount.getChatSecret());
token=weAccessTokenDtoDto.getAccess_token();
expires_in=weAccessTokenDtoDto.getExpires_in();
}else if (WeConstans.WE_AGENT_ACCESS_TOKEN.equals(accessTokenKey)){
WeAccessTokenDtoDto weAccessTokenDtoDto = accessTokenClient.getToken(wxCorpAccount.getCorpId(),wxCorpAccount.getAgentSecret());
token=weAccessTokenDtoDto.getAccess_token();
expires_in=weAccessTokenDtoDto.getExpires_in();
}
if(StringUtils.isNotEmpty(token)){
redisCache.setCacheObject(accessTokenKey+"::"+wxCorpAccount.getCorpId(),token,expires_in.intValue(), TimeUnit.SECONDS);
weAccessToken = token;
}
}
return weAccessToken;
}
}
......@@ -102,6 +102,10 @@ public class WeCorpAccountServiceImpl extends ServiceImpl<WeCorpAccountMapper,We
return noticeSwitch;
}
@Override
public WeCorpAccount getWeCorpAccount(String corpId) {
return this.getOne(new LambdaQueryWrapper<WeCorpAccount>().eq(WeCorpAccount::getCorpId,corpId));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册