IWxAuthServiceImpl.java
2.04 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
package com.linkwechat.wecom.service.impl;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.redis.RedisCache;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.domain.weixin.dto.WxAuthUserInfoDto;
import com.linkwechat.wecom.domain.weixin.dto.WxTokenDto;
import com.linkwechat.wecom.service.IWxAuthService;
import com.linkwechat.wecom.wxclient.WxAuthClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
/**
* @author danmo
* @description
* @date 2021/4/5 22:45
**/
@Slf4j
@Service
public class IWxAuthServiceImpl implements IWxAuthService {
@Autowired
private WxAuthClient wxAuthClient;
@Autowired
private RedisCache redisCache;
@Value("${weixin.appid}")
private String appId;
@Value("${weixin.secret}")
private String secret;
private final String grantType = "authorization_code";
@Override
public WxTokenDto getToken(String code,String openId) {
WxTokenDto authToken;
if (StringUtils.isNotEmpty(openId)){
authToken =redisCache.getCacheObject(WeConstans.WX_AUTH_ACCESS_TOKEN+":"+ openId);
}else {
authToken = wxAuthClient.getAuthToken(appId, secret, code, grantType);
if(authToken != null && StringUtils.isNotEmpty(authToken.getAccessToken())){
redisCache.setCacheObject(WeConstans.WX_AUTH_ACCESS_TOKEN+":"+ authToken.getOpenId(),
authToken, authToken.getExpiresIn(), TimeUnit.SECONDS);
redisCache.setCacheObject(WeConstans.WX_AUTH_REFRESH_ACCESS_TOKEN+":"+ authToken.getOpenId(),
authToken.getRefreshToken(), 30, TimeUnit.DAYS);
}
}
return authToken;
}
@Override
public WxAuthUserInfoDto getUserInfo(String openId, String lang) {
return wxAuthClient.getUserInfo(openId, lang);
}
}