WeAccessTokenServiceImpl.java 3.3 KB
Newer Older
1 2
package com.linkwechat.wecom.service.impl;

水库浪子 已提交
3 4 5 6
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;
7
import com.linkwechat.wecom.client.WeAccessTokenClient;
8
import com.linkwechat.wecom.domain.WeCorpAccount;
水库浪子 已提交
9
import com.linkwechat.wecom.domain.dto.WeAccessTokenDtoDto;
10
import com.linkwechat.wecom.service.IWeAccessTokenService;
11
import com.linkwechat.wecom.service.IWeCorpAccountService;
12 13 14
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

水库浪子 已提交
15 16
import java.util.concurrent.TimeUnit;

17 18
/**
 * @description: 微信token相关接口
水库浪子 已提交
19
 * @author: HaoN
20 21 22 23 24 25 26 27 28 29
 * @create: 2020-08-26 14:43
 **/
@Service
public class WeAccessTokenServiceImpl implements IWeAccessTokenService {


    @Autowired
    private WeAccessTokenClient accessTokenClient;

    @Autowired
30
    private IWeCorpAccountService iWxCorpAccountService;
31

32

33
    @Autowired
水库浪子 已提交
34
    private RedisCache redisCache;
35 36


37 38 39 40 41

    /**
     * 获取accessToken
     */
    @Override
42
    public String findCommonAccessToken() {
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

        return findAccessToken(WeConstans.WE_COMMON_ACCESS_TOKEN);
    }


    /**
     * 获取外部联系人相关accesstoken
     * @return
     */
    @Override
    public String findContactAccessToken() {


        return findAccessToken(WeConstans.WE_CONTACT_ACCESS_TOKEN);
    }


61 62 63 64 65 66 67 68 69 70
    /**
     * 获取服务商相关token
     * @return
     */
    @Override
    public String findProviderAccessToken() {
        return findAccessToken(WeConstans.WE_PROVIDER_ACCESS_TOKEN);
    }


71 72 73
    private String findAccessToken(String accessTokenKey){

        String  weAccessToken =redisCache.getCacheObject(accessTokenKey);
74

75 76
        //为空,请求微信服务器同时缓存到redis中
        if(StringUtils.isEmpty(weAccessToken)){
77 78 79 80 81 82
            WeCorpAccount wxCorpAccount
                    = iWxCorpAccountService.findValidWeCorpAccount();
            if(null == wxCorpAccount){
                //返回错误异常,让用户绑定企业id相关信息
                throw new WeComException("无可用的corpid和secret");
            }
83 84 85 86 87 88 89 90 91 92 93 94 95
            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();
            }
96

97 98
            if(StringUtils.isNotEmpty(token)){
                redisCache.setCacheObject(accessTokenKey,token,expires_in.intValue(), TimeUnit.SECONDS);
99
            }
100

101
        }
102

103
        return weAccessToken;
104 105 106 107
    }


}