lsc 1 deň pred
rodič
commit
4eea009517
1 zmenil súbory, kde vykonal 88 pridanie a 1 odobranie
  1. 88 1
      cocoflow.js

+ 88 - 1
cocoflow.js

@@ -28,7 +28,7 @@ const { WECHAT_APPID, WECHAT_SECRET } = { WECHAT_APPID: "wx2d69589899b7ecd6", WE
 //   WECHAT_APPID: "wx3a8dd28881c2c41f",
 //   WECHAT_SECRET: "e6c81745345f44251e44bc7a3b837687",
 // }; 
-// //自己
+//自己
 // const { WECHAT_APPID, WECHAT_SECRET } = {
 //   WECHAT_APPID: "wx3a8dd28881c2c41f",
 //   WECHAT_SECRET: "e6c81745345f44251e44bc7a3b837687",
@@ -93,6 +93,93 @@ router.route("/wechat-callback").all(async (req, res) => {
   }
 });
 
+// 通过openid获取微信用户信息
+router.route("/wechat-user-info").post(async (req, res) => {
+  const { openid, access_token } = req.body;
+  
+  if (!openid || !access_token) {
+    return res.status(400).json({
+      success: false,
+      error: "缺少必要参数",
+      message: "openid 和 access_token 都是必需的"
+    });
+  }
+  
+  try {
+    // 使用openid和access_token获取用户信息
+    const userInfoResponse = await axios.get(
+      `https://api.weixin.qq.com/sns/userinfo?access_token=${access_token}&openid=${openid}&lang=zh_CN`
+    );
+    
+    res.status(200).json({
+      success: true,
+      data: userInfoResponse.data,
+      status: userInfoResponse.status,
+      statusText: userInfoResponse.statusText
+    });
+  } catch (error) {
+    console.log("获取用户信息失败:", error);
+    res.status(500).json({
+      success: false,
+      error: "获取用户信息失败",
+      message: error.message
+    });
+  }
+});
+
+// 获取微信用户信息的完整流程(包含获取access_token)
+router.route("/wechat-get-user-info").post(async (req, res) => {
+  const { code } = req.body;
+  
+  if (!code) {
+    return res.status(400).json({
+      success: false,
+      error: "缺少授权码",
+      message: "code 参数是必需的"
+    });
+  }
+  
+  try {
+    // 第一步:使用授权码换取 access_token 和 openid
+    const tokenResponse = await axios.get(
+      `https://api.weixin.qq.com/sns/oauth2/access_token?appid=${WECHAT_APPID}&secret=${WECHAT_SECRET}&code=${code}&grant_type=authorization_code`
+    );
+    
+    const { access_token, openid } = tokenResponse.data;
+    
+    if (!access_token || !openid) {
+      return res.status(400).json({
+        success: false,
+        error: "获取access_token失败",
+        message: tokenResponse.data.errmsg || "微信API返回错误"
+      });
+    }
+    
+    // 第二步:使用access_token和openid获取用户信息
+    const userInfoResponse = await axios.get(
+      `https://api.weixin.qq.com/sns/userinfo?access_token=${access_token}&openid=${openid}&lang=zh_CN`
+    );
+    
+    res.status(200).json({
+      success: true,
+      data: {
+        access_token,
+        openid,
+        userInfo: userInfoResponse.data
+      },
+      status: userInfoResponse.status,
+      statusText: userInfoResponse.statusText
+    });
+  } catch (error) {
+    console.log("获取用户信息失败:", error);
+    res.status(500).json({
+      success: false,
+      error: "获取用户信息失败",
+      message: error.message
+    });
+  }
+});
+
 
 //深圳电教馆注册
 router.route("/wechat-register").post(function(req, res, next) {