Browse Source

✨ feat: 增加face_code

Pchen. 7 months ago
parent
commit
2a672f540e

+ 29 - 1
apis/Lepao/Account/AddAccount.js

@@ -11,8 +11,29 @@ class AddAccount extends API {
         this.setMethod('POST')
         this.setMethod('POST')
     }
     }
 
 
+    // 生成 6 位数字 + 字母混合码
+    async generateCode() {
+        try {
+            const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
+            let code = ""
+            for (let i = 0; i < 6; i++) {
+                code += chars.charAt(Math.floor(Math.random() * chars.length))
+            }
+
+            let sql = 'SELECT id FROM lepao_face WHERE face_code = ?'
+            let rows = await db.query(sql, [code])
+            if (!rows)
+                throw new Error('数据库错误,请稍后再试')
+            if (rows.length > 0)
+                return await this.generateCode()
+            return code
+        } catch (error) {
+            throw error
+        }
+    }
+
     async onRequest(req, res) {
     async onRequest(req, res) {
-        let { uuid, session, student_num, email, id, area, max_distance, min_distance, auto_time, auto_run, notes } = req.body
+        let { uuid, session, student_num, email, id, area, auto_time, auto_run, notes } = req.body
 
 
         if ([uuid, session, student_num, email, auto_time].some(value => value === '' || value === null || value === undefined))
         if ([uuid, session, student_num, email, auto_time].some(value => value === '' || value === null || value === undefined))
             return res.json({
             return res.json({
@@ -50,8 +71,15 @@ class AddAccount extends API {
                 r = await db.query(sql, [uuid, email, area, auto_time, auto_run, time, notes ?? '', countRows[0].id])
                 r = await db.query(sql, [uuid, email, area, auto_time, auto_run, time, notes ?? '', countRows[0].id])
             }
             }
             else {
             else {
+                const face_code = await this.generateCode()
+
                 sql = 'INSERT INTO lepao_account (student_num, email, area, auto_time, auto_run, create_user, create_time, notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?)'
                 sql = 'INSERT INTO lepao_account (student_num, email, area, auto_time, auto_run, create_user, create_time, notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?)'
                 r = await db.query(sql, [student_num, email, area, auto_time, auto_run, uuid, time, notes ?? ''])
                 r = await db.query(sql, [student_num, email, area, auto_time, auto_run, uuid, time, notes ?? ''])
+
+                let faceSql = 'INSERT INTO lepao_face (student_num, face_code) VALUES (?, ?)'
+                let faceRows = await db.query(faceSql, [student_num, face_code])
+                if (!faceRows || faceRows.affectedRows !== 1)
+                    return res.json({ ...BaseStdResponse.ERR, msg: '添加乐跑账号失败!数据库错误' })
             }
             }
         } else {
         } else {
             sql = 'UPDATE lepao_account SET student_num = ?, email = ?, area = ?, auto_time = ?, auto_run = ?, notes = ? WHERE id = ?'
             sql = 'UPDATE lepao_account SET student_num = ?, email = ?, area = ?, auto_time = ?, auto_run = ?, notes = ? WHERE id = ?'

+ 8 - 1
apis/Lepao/Account/Admin/GetAccountList.js

@@ -70,13 +70,20 @@ class GetAccountList extends API {
                     l.auto_run,
                     l.auto_run,
                     l.auto_time,
                     l.auto_time,
                     l.state,
                     l.state,
-                    l.notes
+                    l.notes,
+                    f.face_code,
+                    f.state AS face_state,
+                    f.url AS face_url
                 FROM 
                 FROM 
                     lepao_account l
                     lepao_account l
                 LEFT JOIN 
                 LEFT JOIN 
                     users u
                     users u
                 ON 
                 ON 
                     l.create_user = u.uuid
                     l.create_user = u.uuid
+                LEFT JOIN 
+                    lepao_face f 
+                ON
+                    l.student_num = f.student_num
                 WHERE 
                 WHERE 
                     1 = 1
                     1 = 1
             `
             `

+ 5 - 4
apis/Lepao/Account/GetAccount.js

@@ -61,13 +61,14 @@ class GetAccount extends API {
                     l.email,
                     l.email,
                     l.auto_run,
                     l.auto_run,
                     l.notes,
                     l.notes,
-                    u.username AS create_user
+                    f.face_code,
+                    f.state AS face_state
                 FROM 
                 FROM 
                     lepao_account l
                     lepao_account l
                 LEFT JOIN 
                 LEFT JOIN 
-                    users u 
-                ON 
-                    l.create_user = u.uuid
+                    lepao_face f 
+                ON
+                    l.student_num = f.student_num
                 WHERE 
                 WHERE 
                     l.create_user = ?
                     l.create_user = ?
             `
             `

+ 3 - 2
apis/Upload/UploadFaceVideo.js

@@ -94,10 +94,11 @@ class UploadFaceVideo extends API {
             }
             }
 
 
             const videoPath = `/uploads/faces/${student_num}/${req.file.filename}`
             const videoPath = `/uploads/faces/${student_num}/${req.file.filename}`
+            const videoUrl = `${url}/uploads/faces/${student_num}/${req.file.filename}`
             const time = new Date().getTime()
             const time = new Date().getTime()
 
 
-            let sql = 'INSERT INTO lepao_face (student_num, video_path, create_time, state) VALUES (?, ?, ?, ?)'
-            let rows = await db.query(sql, [student_num, videoPath, time, 1])
+            let sql = 'UPDATE lepao_face SET video_path = ?, create_time = ?, state = ?, url = ? WHERE student_num = ?'
+            let rows = await db.query(sql, [videoPath, time, 1, videoUrl, student_num])
             if (!rows || rows.affectedRows !== 1)
             if (!rows || rows.affectedRows !== 1)
                 return res.json({
                 return res.json({
                     ...BaseStdResponse.ERR,
                     ...BaseStdResponse.ERR,

+ 53 - 0
test.js

@@ -0,0 +1,53 @@
+const db = require("./plugin/DataBase/db");
+
+// 生成 6 位数字 + 字母混合码
+function generateCode() {
+  const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; // 全部大写,避免大小写重复
+  let code = "";
+  for (let i = 0; i < 6; i++) {
+    code += chars.charAt(Math.floor(Math.random() * chars.length));
+  }
+  return code;
+}
+
+// 生成唯一的 code
+async function generateUniqueCode(existingCodes) {
+  let code;
+  do {
+    code = generateCode();
+  } while (existingCodes.has(code)); // 查重
+  return code;
+}
+
+async function updateFaceCodes() {
+  try {
+    // 查询所有账号
+    const accounts = await db.query("SELECT student_num FROM lepao_account");
+
+    // 收集已有的 face_code
+    const usedCodes = new Set();
+
+    for (let account of accounts) {
+      // 如果已有 code,就覆盖(如果你想保留,可以加判断)
+      const code = await generateUniqueCode(usedCodes);
+      usedCodes.add(code);
+
+      // 更新数据库
+      let sql = 'INSERT INTO lepao_face (face_code, student_num ) VALUES (?, ?)'
+      await db.query(sql, [
+        code,
+        account.student_num,
+      ]);
+
+      console.log(`账号ID: ${account.student_num} 更新 face_code 为: ${code}`);
+    }
+
+    console.log("所有 face_code 更新完成!");
+  } catch (err) {
+    console.error("执行出错:", err);
+  } finally {
+    db.end && db.end();
+  }
+}
+
+updateFaceCodes();