From 640d1ebf2b738440ab16f8e8954bfeed1472a3b3 Mon Sep 17 00:00:00 2001
From: 44323 <443237572@qq.com>
Date: 星期五, 29 十二月 2023 17:59:26 +0800
Subject: [PATCH] 接口所有代码

---
 guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/weChat/WeChatUtil.java |   56 ++++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 46 insertions(+), 10 deletions(-)

diff --git a/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/weChat/WeChatUtil.java b/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/weChat/WeChatUtil.java
index 7b08e00..bcd3495 100644
--- a/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/weChat/WeChatUtil.java
+++ b/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/weChat/WeChatUtil.java
@@ -7,6 +7,9 @@
 import com.stylefeng.guns.modular.system.util.UUIDUtil;
 import com.stylefeng.guns.modular.system.util.httpClinet.HttpClientUtil;
 import com.stylefeng.guns.modular.system.util.httpClinet.HttpResult;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.Response;
 import org.apache.commons.codec.digest.DigestUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -57,9 +60,10 @@
 
     @Autowired
     private RedisUtil redisUtil;
-
-
-
+    // 获取微信用户access_token的接口地址
+    private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token";
+    // 获取微信用户信息的接口地址
+    private static final String USER_INFO_URL = "https://api.weixin.qq.com/sns/userinfo";
     {
         new Thread(new Runnable() {
             @Override
@@ -95,24 +99,34 @@
      * @return
      */
     public Map<String, Object> code2Session(String jscode) throws Exception{
-        System.err.println("看看调用奥");
-        System.err.println("jsCode"+"========="+jscode);
         String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + wxAppletsAppid + "&secret=" + wxAppletsAppSecret
                 + "&js_code=" + jscode + "&grant_type=authorization_code";
+//        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+wxAppletsAppid+"&secret="+wxAppletsAppSecret+"&code="+jscode+"&grant_type=authorization_code";
         HttpResult httpResult = httpClientUtil.pushHttpRequset("GET", url, null, null, "form");
         if(null == httpResult || httpResult.getCode() != 200){
             return null;
         }
         JSONObject jsonObject = JSON.parseObject(httpResult.getData());
-        System.err.println("返回json串"+jsonObject);
         int errcode = jsonObject.getIntValue("errcode");
         Map<String, Object> map = new HashMap<>();
-        map.put("errcode", errcode);
-        System.err.println("返回code"+errcode);
+        System.err.println("调用返回"+jsonObject);
+        System.err.println("响应结果"+httpResult);
         if(errcode == 0){//成功
+            Map<String, Object> userInfo = getUserInfo(jsonObject.getString("session_key"), jsonObject.getString("openid"));
+            System.err.println(userInfo);
+            // 用户名
+//            String nickname = userInfo.get("nickname").toString();
+//            // 用户头像
+//            String headimgurl = userInfo.get("headimgurl").toString();
+//            System.err.println("用户名:"+nickname);
+//            System.err.println("头像:"+headimgurl);
             map.put("openid", jsonObject.getString("openid"));
             map.put("sessionKey", jsonObject.getString("session_key"));
             map.put("unionid", jsonObject.getString("unionid"));
+            String wxAppletsAccessToken = getWxAppletsAccessToken();
+            System.err.println("看看accesstoken:"+wxAppletsAccessToken);
+            Map<String, Object> openid = getUserInfo(wxAppletsAccessToken, jsonObject.getString("openid"));
+            System.err.println("用户信息:"+ openid);
             return map;
         }
         if(errcode == -1){//系统繁忙,此时请开发者稍候再试
@@ -145,6 +159,7 @@
         if(httpResult.getCode() != 200){
             return "";
         }
+        System.err.println("获取AAAAAAAAAAAAAAAAAAAAAAAAAA:"+httpResult);
         JSONObject jsonObject = JSON.parseObject(httpResult.getData());
         return jsonObject.getString("access_token");
     }
@@ -191,8 +206,6 @@
         map.put("signature", signature);
         return  map;
     }
-
-
 
     /**
      * 网站应用登录
@@ -301,6 +314,29 @@
         return map;
     }
 
+    /**
+     * 获取微信用户的access_token
+     *
+     * @param code 微信小程序登录时获取的code
+     * @return 包含access_token和openid的Map
+     * @throws Exception
+     */
+    public  Map<String, String> getAccessToken(String code) throws Exception {
+        OkHttpClient client = new OkHttpClient();
+        String url = ACCESS_TOKEN_URL + "?appid=" +  wxAppletsAppid+ "&secret=" + wxAppletsAppSecret + "&code=" + code
+                + "&grant_type=authorization_code";
+        Request request = new Request.Builder().url(url).get().build();
+        Response response = client.newCall(request).execute();
+        String responseStr = response.body().string();
+        System.err.println("响应结果"+responseStr);
+        JSONObject jsonObject = JSON.parseObject(responseStr);
+        String accessToken = jsonObject.getString("access_token");
+        String openid = jsonObject.getString("openid");
+        Map<String, String> map = new HashMap<>();
+        map.put("access_token", accessToken);
+        map.put("openid", openid);
+        return map;
+    }
 
     /***
      * 获取acess_token (公众号)

--
Gitblit v1.7.1