From 3033ccf3878fae2c204df53be2a283f29f5853ed Mon Sep 17 00:00:00 2001
From: liujie <1793218484@qq.com>
Date: 星期五, 10 十月 2025 18:17:02 +0800
Subject: [PATCH] Merge branch 'master' of http://120.76.84.145:10101/gitblit/r/java/haizhentong

---
 ruoyi-system/src/main/java/com/ruoyi/system/utils/wx/tools/WxAppletTools.java |   47 ++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/utils/wx/tools/WxAppletTools.java b/ruoyi-system/src/main/java/com/ruoyi/system/utils/wx/tools/WxAppletTools.java
index 2298a44..8877ce5 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/utils/wx/tools/WxAppletTools.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/utils/wx/tools/WxAppletTools.java
@@ -43,6 +43,24 @@
     /**
      * 请求参数
      * 属性	类型	默认值	必填	说明
+     * appid	string		是	公众号 appId
+     * secret	string		是	公众号 appSecret
+     * code	string		是	登录时获取的 code
+     * grant_type	string		是	授权类型,此处只需填写 authorization_cod
+     * <p>
+     * 返回值:
+     * <p>
+     * 属性	类型	说明
+     * openid	string	用户唯一标识
+     * session_key	string	会话密钥
+     * unionid	string	用户在开放平台的唯一标识符,若当前小程序已绑定到微信开放平台帐号下会返回,详见 UnionID 机制说明。
+     * errcode	number	错误码
+     * errmsg	string	错误信息
+     */
+    private static final String CODE_2_SESSION_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code";
+    /**
+     * 请求参数
+     * 属性	类型	默认值	必填	说明
      * grant_type	string		是	填写 client_credential
      * appid	string		是	小程序唯一凭证,即 AppID,可在「微信公众平台 - 设置 - 开发设置」页中获得。(需要已经成为开发者,且帐号没有异常状态)
      * secret	string		是	小程序唯一凭证密钥,即 AppSecret,获取方式同 appid
@@ -101,10 +119,33 @@
     }
 
     /**
+     * 微信公众号获取openId
+     * @param resqBody
      * @return
      */
-    public String getAccessToken(String version) {
-        String accessToken = redisService.getCacheObject(ACCESSTOKEN_CACHE_KEY + version);
+    public Code2SessionRespBody getOpenIdByCode2session(Code2SessionResqBody resqBody) {
+        long start = System.currentTimeMillis();
+        String requestUrl = MessageFormat.format(CODE_2_SESSION_URL, wxConfig.getAppId(), wxConfig.getSecretId(), resqBody.getJsCode());
+        long end = System.currentTimeMillis();
+        log.info("code换取sessionKey时间:{}", (end - start));
+        String respBody = wxRestTemplate.getForEntity(requestUrl, String.class).getBody();
+        end = System.currentTimeMillis();
+        log.info("code换取sessionKey时间:{}", (end - start));
+        log.info("Jscode2session:{}", respBody);
+        Code2SessionRespBody code2SessionRespBody = WxJsonUtils.parseObject(respBody, Code2SessionRespBody.class);
+        // 判断有误异常
+        if (StringUtils.hasLength(code2SessionRespBody.getErrorMsg())) {
+            // 抛出错误
+            throw new WxException(code2SessionRespBody.getErrorCode() + ":" + code2SessionRespBody.getErrorMsg());
+        }
+        return code2SessionRespBody;
+    }
+
+    /**
+     * @return
+     */
+    public String getAccessToken() {
+        String accessToken = redisService.getCacheObject(ACCESSTOKEN_CACHE_KEY);
         if (StringUtils.hasLength(accessToken)) {
             return accessToken;
         }
@@ -116,7 +157,7 @@
             // 抛出错误
             throw new WxException(accessTokenRespBody.getErrorCode() + ":" + accessTokenRespBody.getErrorMsg());
         }
-        redisService.setCacheObject(ACCESSTOKEN_CACHE_KEY + version, accessTokenRespBody.getAccessToken(), 7200L, TimeUnit.SECONDS);
+        redisService.setCacheObject(ACCESSTOKEN_CACHE_KEY, accessTokenRespBody.getAccessToken(), 7200L, TimeUnit.SECONDS);
         return accessTokenRespBody.getAccessToken();
     }
 

--
Gitblit v1.7.1