WeAccessTokenServiceImpl.java
3.31 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
package com.linkwechat.wecom.service.impl;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.redis.RedisCache;
import com.linkwechat.common.exception.wecom.WeComException;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.client.WeAccessTokenClient;
import com.linkwechat.wecom.domain.WeCorpAccount;
import com.linkwechat.wecom.domain.dto.WeAccessTokenDtoDto;
import com.linkwechat.wecom.service.IWeAccessTokenService;
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;
/**
* 获取accessToken
*/
@Override
public String findCommonAccessToken() {
return findAccessToken(WeConstans.WE_COMMON_ACCESS_TOKEN);
}
/**
* 获取外部联系人相关accesstoken
* @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);
}
private String findAccessToken(String accessTokenKey){
String weAccessToken =redisCache.getCacheObject(accessTokenKey);
//为空,请求微信服务器同时缓存到redis中
if(StringUtils.isEmpty(weAccessToken)){
WeCorpAccount wxCorpAccount
= iWxCorpAccountService.findValidWeCorpAccount();
if(null == wxCorpAccount){
//返回错误异常,让用户绑定企业id相关信息
throw new WeComException("无可用的corpid和secret");
}
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();
}
if(StringUtils.isNotEmpty(token)){
redisCache.setCacheObject(accessTokenKey,token,expires_in.intValue(), TimeUnit.SECONDS);
}
}
return weAccessToken;
}
}