xuhy
2026-01-06 3d8e6a16f98bc01d3cee3a2733fa9a4eff8db2c7
ruoyi-system/src/main/java/com/ruoyi/system/utils/wx/tools/WxAppletTools.java
@@ -1,6 +1,7 @@
package com.ruoyi.system.utils.wx.tools;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.system.utils.wx.body.resp.AccessTokenRespBody;
import com.ruoyi.system.utils.wx.body.resp.Code2SessionRespBody;
@@ -22,6 +23,7 @@
@Slf4j
public class WxAppletTools {
    private final static String ACCESSTOKEN_CACHE_KEY = "accessToken";
    private final static String ACCESS_TOKEN_OFFICE_CACHE_KEY = "accessTokenOffice";
    /**
     * 请求参数
     * 属性   类型   默认值   必填   说明
@@ -77,12 +79,12 @@
    public static String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}";
    private WeixinProperties wxConfig;
    private RestTemplate wxRestTemplate;
    private RedisService redisService;
    private RedisCache redisCache;
    public WxAppletTools(RestTemplate wxRestTemplate, WeixinProperties wxConfig, RedisService redisService) {
    public WxAppletTools(RestTemplate wxRestTemplate, WeixinProperties wxConfig, RedisCache redisCache) {
        this.wxRestTemplate = wxRestTemplate;
        this.wxConfig = wxConfig;
        this.redisService = redisService;
        this.redisCache = redisCache;
    }
    /**
@@ -125,7 +127,7 @@
     */
    public Code2SessionRespBody getOpenIdByCode2session(Code2SessionResqBody resqBody) {
        long start = System.currentTimeMillis();
        String requestUrl = MessageFormat.format(CODE_2_SESSION_URL, wxConfig.getAppId(), wxConfig.getSecretId(), resqBody.getJsCode());
        String requestUrl = MessageFormat.format(CODE_2_SESSION_URL, wxConfig.getOfficeAppId(), wxConfig.getOfficeSecretId(), resqBody.getJsCode());
        long end = System.currentTimeMillis();
        log.info("code换取sessionKey时间:{}", (end - start));
        String respBody = wxRestTemplate.getForEntity(requestUrl, String.class).getBody();
@@ -145,7 +147,7 @@
     * @return
     */
    public String getAccessToken() {
        String accessToken = redisService.getCacheObject(ACCESSTOKEN_CACHE_KEY);
        String accessToken = redisCache.getCacheObject(ACCESSTOKEN_CACHE_KEY);
        if (StringUtils.hasLength(accessToken)) {
            return accessToken;
        }
@@ -157,7 +159,27 @@
            // 抛出错误
            throw new WxException(accessTokenRespBody.getErrorCode() + ":" + accessTokenRespBody.getErrorMsg());
        }
        redisService.setCacheObject(ACCESSTOKEN_CACHE_KEY, accessTokenRespBody.getAccessToken(), 7200L, TimeUnit.SECONDS);
        redisCache.setCacheObject(ACCESSTOKEN_CACHE_KEY, accessTokenRespBody.getAccessToken(), 7200, TimeUnit.SECONDS);
        return accessTokenRespBody.getAccessToken();
    }
    /**
     * @return
     */
    public String getAccessTokenOffice() {
        String accessToken = redisCache.getCacheObject(ACCESS_TOKEN_OFFICE_CACHE_KEY);
        if (StringUtils.hasLength(accessToken)) {
            return accessToken;
        }
        String requestUrl = MessageFormat.format(ACCESS_TOKEN_URL, wxConfig.getOfficeAppId(), wxConfig.getOfficeSecretId());
        String respBody = wxRestTemplate.getForEntity(requestUrl, String.class).getBody();
        AccessTokenRespBody accessTokenRespBody = WxJsonUtils.parseObject(respBody, AccessTokenRespBody.class);
        // 判断有误异常
        if (StringUtils.hasLength(accessTokenRespBody.getErrorMsg())) {
            // 抛出错误
            throw new WxException(accessTokenRespBody.getErrorCode() + ":" + accessTokenRespBody.getErrorMsg());
        }
        redisCache.setCacheObject(ACCESS_TOKEN_OFFICE_CACHE_KEY, accessTokenRespBody.getAccessToken(), 7200, TimeUnit.SECONDS);
        return accessTokenRespBody.getAccessToken();
    }