WeAccessTokenServiceImpl.java
6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package com.linkwechat.wecom.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.linkwechat.common.constant.Constants;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.domain.entity.SysUser;
import com.linkwechat.common.core.domain.model.LoginUser;
import com.linkwechat.common.core.redis.RedisCache;
import com.linkwechat.common.exception.wecom.WeComException;
import com.linkwechat.common.utils.SecurityUtils;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.client.WeAccessTokenClient;
import com.linkwechat.wecom.domain.WeApp;
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.IWeCorpAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
/**
* @description: 微信token相关接口
* @author: HaoN
* @create: 2020-08-26 14:43
**/
@Service
public class WeAccessTokenServiceImpl implements IWeAccessTokenService {
@Autowired
private WeAccessTokenClient accessTokenClient;
@Autowired
private IWeCorpAccountService iWxCorpAccountService;
@Autowired
private RedisCache redisCache;
@Autowired
private IWeAppService iWeAppService;
/**
* 获取通用accessToken
*/
@Override
public String findCommonAccessToken() {
return findAccessToken(WeConstans.WE_COMMON_ACCESS_TOKEN);
}
/**
* 获取外部联系人相关token
* @return
*/
@Override
public String findContactAccessToken() {
return findAccessToken(WeConstans.WE_CONTACT_ACCESS_TOKEN);
}
/**
* 获取服务商相关token
* @return
*/
@Override
public String findProviderAccessToken() {
return findAccessToken(WeConstans.WE_PROVIDER_ACCESS_TOKEN);
}
/**
* 会话存档相关token
* @return
*/
@Override
public String findChatAccessToken() {
return findAccessToken(WeConstans.WE_CHAT_ACCESS_TOKEN);
}
/**
* 获取第三方应用所需要的token
* @param agentId
* @return
*/
@Override
public String findThirdAppAccessToken(String agentId) {
String token=redisCache.getCacheObject(WeConstans.WE_THIRD_APP_TOKEN+"::"+agentId);
if(StringUtils.isNotEmpty(token)){
return token;
}
WeApp weApp = iWeAppService.getOne(new LambdaQueryWrapper<WeApp>()
.eq(WeApp::getAgentId, agentId)
.eq(WeApp::getDelFlag, Constants.NORMAL_CODE)
.eq(WeApp::getStatus, Constants.NORMAL_CODE));
if(weApp == null){
throw new WeComException("当前agentId不可用或不存在");
}
WeCorpAccount wxCorpAccount
= iWxCorpAccountService.findValidWeCorpAccount();
if(wxCorpAccount == null){
throw new WeComException("无可用的corpid和secret");
}
WeAccessTokenDtoDto weAccessTokenDtoDto
= accessTokenClient.getToken(wxCorpAccount.getCorpId(),weApp.getAgentSecret());
token=weAccessTokenDtoDto.getAccess_token();
if(StringUtils.isNotEmpty(token)){
redisCache.setCacheObject(WeConstans.WE_THIRD_APP_TOKEN+"::"+agentId,token,weAccessTokenDtoDto.getExpires_in().intValue(), TimeUnit.SECONDS);
}
return token;
}
private String findAccessToken(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;
}
/**
* 清空redis中的相关token
*/
@Override
public void removeToken(WeCorpAccount wxCorpAccount) {
redisCache.deleteObject(WeConstans.WE_COMMON_ACCESS_TOKEN+"::"+wxCorpAccount.getAgentId());
redisCache.deleteObject(WeConstans.WE_CONTACT_ACCESS_TOKEN+"::"+wxCorpAccount.getAgentId());
redisCache.deleteObject(WeConstans.WE_PROVIDER_ACCESS_TOKEN+"::"+wxCorpAccount.getAgentId());
}
}