lsc 4 днів тому
батько
коміт
38c3caf7c5
4 змінених файлів з 90 додано та 2 видалено
  1. 2 0
      app.js
  2. 30 1
      mongo.js
  3. 1 1
      package.json
  4. 57 0
      weixin.js

+ 2 - 0
app.js

@@ -5,6 +5,7 @@ var bodyParser = require('body-parser');
 var request = require("request");
 const edurouter = require('./pbl');
 const mongo = require('./mongo');
+const weixin = require('./weixin');
 const baoantoken = require('./baoantoken')
 const morgan = require('morgan');
 var path = require("path");
@@ -45,6 +46,7 @@ app.use(cors({
 // all of our routes will be prefixed with /api
 app.use('/api/pbl', edurouter);
 app.use('/api/mongo', mongo);
+app.use('/api/weixin', weixin);
 app.use('/api/bat/getToken', async function (req, res, next) {
     let ticket = req.query.ticket
     await baoantoken.getToken(ticket, res)

+ 30 - 1
mongo.js

@@ -136,7 +136,36 @@ updateUserData("mongodb://root:usestudio-1@123.58.32.151:11641?authSource=admin"
 });
 */
 
-//查找有无学校
+router.route('/updateUserData2').all((req, res, next) => {
+    let today = new Date();
+    let date = today.getFullYear() + '-' + (today.getMonth() + 1).toString().padStart(2, '0') + '-' + today.getDate().toString().padStart(2, '0');
+    let data = req.body[0];
+    let decryptedData = {};
+    for (let key in data) {
+        decryptedData[key] = decodeURIComponent(data[key]);
+    }
+    updateUserData("mongodb://root:usestudio-1@172.16.9.240:27017?authSource=admin", "cocolog", "applog", {
+        [date]: {
+            [decryptedData.userid]: 
+            {
+                userid: decryptedData.username,
+                accountNumber: decryptedData.accountNumber,
+                org: decryptedData.org,
+                school: decryptedData.school,
+                browser: decryptedData.browser,
+                operation: [
+                    {
+                        useTime: decryptedData.userTime,
+                        loadTime: decryptedData.loadTime,
+                        object: decryptedData.object,
+                        status: decryptedData.status,
+                    }
+                ]
+            }
+        }
+    }, res);
+});
+
 router.route('/updateUserData').all((req, res, next) => {
     let today = new Date();
     let date = today.getFullYear() + '-' + (today.getMonth() + 1).toString().padStart(2, '0') + '-' + today.getDate().toString().padStart(2, '0');

+ 1 - 1
package.json

@@ -218,7 +218,7 @@
       "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.17.tgz",
       "version": "2.1.17"
     },
-    "mongodb": "^4.0.1",
+    "mongodb": "^6.15.0",
     "morgan": "^1.9.1",
     "ms": {
       "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=",

+ 57 - 0
weixin.js

@@ -0,0 +1,57 @@
+const express = require("express");
+const axios = require("axios"); // 用于发起网络请求
+const router = express.Router();
+
+// 假设已经有了微信的 AppID 和 AppSecret
+
+// const { WECHAT_APPID, WECHAT_SECRET } = { WECHAT_APPID: "wx2d69589899b7ecd6", WECHAT_SECRET: "99fd14315d0b41375be4d4a17c830001" }; //cocorobo公众号
+// const { WECHAT_APPID, WECHAT_SECRET } = {
+//   WECHAT_APPID: "wx3a8dd28881c2c41f",
+//   WECHAT_SECRET: "e6c81745345f44251e44bc7a3b837687",
+// }; //自己
+const { WECHAT_APPID, WECHAT_SECRET } = {
+    WECHAT_APPID: "wxe9d7fff3c659445f",
+    WECHAT_SECRET: "8224f665f09fd5a79f5720676a142792",
+  }; 
+
+
+// const WECHAT_REDIRECT_URI = "https://liyuan.cocorobo.cn/#/login"; // 微信授权后的回调地址
+
+// 获取微信登录二维码的接口
+// router.route("/wechat-login-qrcode").all((req, res) => {
+//   try {
+//     // 这里应该是调用微信 API 来获取授权 URL,并转换为二维码
+//     const randomState = Math.random().toString(36).substring(2); // 生成随机状态
+//     const wechatUrl = `https://open.weixin.qq.com/connect/qrconnect?appid=${WECHAT_APPID}&redirect_uri=${encodeURIComponent(
+//       WECHAT_REDIRECT_URI
+//     )}&response_type=code&scope=snsapi_login&state=${randomState}#wechat_redirect`;
+//     // const wechatUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${WECHAT_APPID}&redirect_uri=${encodeURIComponent(WECHAT_REDIRECT_URI)}&response_type=code&scope=snsapi_base&state=${randomState}#wechat_redirect`
+//     // 这里简化处理,直接返回 URL
+//     res.json({ url: wechatUrl });
+//   } catch (error) {
+//     res.status(500).send("服务器错误");
+//   }
+// });
+
+// 微信回调接口,也就是用户扫码之后在手机上点击同意之后,需要进行重定向的目标URL
+router.route("/wechat-callback").all(async (req, res) => {
+  const code = req.query.code; // 获取微信回调返回的授权码
+  try {
+    // 使用授权码换取 access_token
+    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 accessToken = tokenResponse.data.access_token;
+    const openId = tokenResponse.data.openid;
+
+    // 这里可以根据 accessToken 和 openId 获取用户信息,并进行登录处理
+
+    // res.send('登录成功');
+    res.status(200).json({ openId: openId });
+  } catch (error) {
+    res.status(500).send("授权失败");
+  }
+});
+
+
+module.exports = router;