lsc 6 months ago
parent
commit
621c75fe06
3 changed files with 56 additions and 2 deletions
  1. 1 1
      app.js
  2. 1 1
      mysql.js
  3. 54 0
      pbl.js

+ 1 - 1
app.js

@@ -64,5 +64,5 @@ app.all('/download', function (req, res, next) {
 
 // START THE SERVER
 // =============================================================================
-app.listen(port);
+app.listen(port, '0.0.0.0');
 console.log('app happens on port ' + port);

+ 1 - 1
mysql.js

@@ -15,7 +15,7 @@ us.mysqlconnection = function(host, database) {
             host: host, //数据库地址
             // host: '123.58.32.151', //数据库地址
             user: "root", //用户名
-            password: "cocorobo", //密码
+            password: host == '172.16.12.7' ? "cocorobo" : "root", //密码
             database: database, //数据库名称
             // port: 20007 //端口
             port: port //端口

+ 54 - 0
pbl.js

@@ -17,6 +17,7 @@ const _mysqluser = ["172.16.12.5", "cocorobouser"]; //edu數據庫信息
 const _getmysqlLabor2 = ["172.16.12.7", "pbl"]; //edu數據庫信息
 const _getmysqlLabor = ["172.16.12.7", "pbl"]; //edu數據庫信息
 const _getmysqluser = ["172.16.12.7", "cocorobouser"]; //edu數據庫信息
+const _localhost = ["127.0.0.1", "pbl"]; //edu數據庫信息
 var crypto = require("crypto");
 var https = require("https");
 const { Pool } = require('pg');
@@ -82,6 +83,33 @@ async function findMatchingContents(discipline, content) {
     }
 }
 
+async function findMatchingContentsPage2(discipline, content, grade, page) {
+    const contentVector = await calculateVectors(content);
+    const formattedContentVector = contentVector.length > 0 ? `[` + contentVector.join(', ') + `]` : null;
+    const offset = ((page || 1) - 1) * 5;
+    const client = await pool.connect();
+    try {
+        // 使用余弦相似度进行内容向量匹配
+        const query = `
+            SELECT id, content,
+                   1 - (content_vector <#> $1::vector) AS contentSimilarity
+            FROM (select id, content, content_vector from knowledgefiles as a 
+            where ($2 = '' or a.parent_arr && (SELECT ARRAY_AGG(id)::text[] FROM directory_files WHERE folder_name = ANY(string_to_array($2, ',')))) 
+            AND ($3 = '' or a.parent_arr && (SELECT ARRAY_AGG(id)::text[] FROM directory_files WHERE folder_name = ANY(string_to_array($3, ','))))) as a
+            ORDER BY contentSimilarity DESC
+            LIMIT 5 OFFSET $4;
+        `;
+
+        const result = await client.query(query, [formattedContentVector, discipline, grade, offset]);
+        return result.rows; // 返回匹配结果
+    } catch (error) {
+        console.error('Error finding matching contents:', error);
+        return [];
+    } finally {
+        client.release(); // 确保释放数据库连接
+    }
+}
+
 async function findMatchingContentsPage(discipline, content, page) {
     const contentVector = await calculateVectors(content);
     const formattedContentVector = contentVector.length > 0 ? `[` + contentVector.join(', ') + `]` : null;
@@ -123,6 +151,14 @@ router.route("/findMatchingContentsPage").all(async (req, res, next) => {
     res.end(JSON.stringify(titleMatches));
 });
 
+/*检索文件*/
+router.route("/findMatchingContentsPage2").all(async (req, res, next) => {
+    var json = queryString(req.url);
+    console.log(json["type"], json["string"])
+    const titleMatches = await findMatchingContentsPage2(json["type"], json["string"], json["grade"], json["page"]);
+    res.end(JSON.stringify(titleMatches));
+});
+
 
 //統壹處理區域
 router.use(async function(req, res, next) {
@@ -8568,6 +8604,11 @@ router.route('/getChatHistory').all((req,res,next)=>{
 	getmysql(req, res, "getChatHistory", json["uid"], json["n"], json["page"], json["num"]); //用户id,会话名称,页码,页数
 })
 
+//本地调用
+router.route("/localPost").all((req,res,next)=>{
+	postmysqlLocal(req, res); 
+})
+
 function generateAPIKey(username) {
     let timeStamp = new Date().getTime();
 
@@ -8689,4 +8730,17 @@ usergetmysql = function(req, res, functionname, ...p) {
     }); //
 };
 
+//post存儲過程處理
+postmysqlLocal = function(req, res) {
+    //mode的處理
+    if (req.body[0]) {
+        p = Object.values(req.body[0]);
+        p.unshift(_localhost[0], _localhost[1]);
+        //執行存儲過程
+        mysql.usselect(p, function(ret) {
+            res.end(JSON.stringify(ret));
+        });
+    }
+};
+
 module.exports = router;