Browse Source

✨ feat: 乐跑记录支持类型与公开标识

为乐跑任务链路补充 runMode 并落库 run_mode,同时引入 public_id 供前端查询详情,避免暴露自增主键并兼容管理端查询。

Made-with: Cursor
Pchen0 1 week ago
parent
commit
50f41add7a

+ 2 - 1
apis/Corn/StartAutoLepao.js

@@ -68,7 +68,8 @@ class StartAutoLepao extends API {
                     type: 'lepao.startRun',
                     data: {
                         taskId,
-                        account: student_num
+                        account: student_num,
+                        runMode: 'auto'
                     },
                     retry: 0
                 }

+ 2 - 1
apis/Corn/StartLepao.js

@@ -65,7 +65,8 @@ class StartLepao extends API {
                         type: 'lepao.startRun',
                         data: {
                             taskId,
-                            account: student_num
+                            account: student_num,
+                            runMode: 'auto'
                         },
                         retry: 0
                     }

+ 3 - 2
apis/Lepao/Record/Admin/GetLepaoRecords.js

@@ -52,9 +52,10 @@ class AdminGetLepaoRecords extends API {
 
         let sql = `
                 SELECT 
-                    r.id,
+                    r.public_id,
                     r.time,
                     r.result,
+                    r.run_mode,
                     r.lepao_account,
                     a.name,
                     a.user_avatar,
@@ -120,7 +121,7 @@ class AdminGetLepaoRecords extends API {
         }
 
         sql += `
-            ORDER BY id DESC
+            ORDER BY r.id DESC
             LIMIT ? OFFSET ?;
         `
         params.push(String(pagesize), String(offset))

+ 18 - 5
apis/Lepao/Record/Admin/GetRecordDetail.js

@@ -15,10 +15,15 @@ class AdminGetRecordDetail extends API {
         let {
             uuid,
             session,
-            id
+            id,
+            public_id
         } = req.query
 
-        if ([uuid, session, id].some(value => value === '' || value === null || value === undefined))
+        if ([uuid, session].some(value => value === '' || value === null || value === undefined))
+            return res.json({
+                ...BaseStdResponse.MISSING_PARAMETER
+            })
+        if ((id === '' || id === null || id === undefined) && (public_id === '' || public_id === null || public_id === undefined))
             return res.json({
                 ...BaseStdResponse.MISSING_PARAMETER
             })
@@ -39,8 +44,10 @@ class AdminGetRecordDetail extends API {
 
         let sql = `
                 SELECT 
+                    r.public_id,
                     r.time,
                     r.result,
+                    r.run_mode,
                     r.lepao_account,
                     r.point_data,
                     r.path_id,
@@ -56,10 +63,16 @@ class AdminGetRecordDetail extends API {
                     path_data p
                 ON 
                     r.path_id = p.id
-                WHERE 
-                    r.id = ?
             `
-        let rows = await db.query(sql, [id])
+        const params = []
+        if (public_id !== '' && public_id !== null && public_id !== undefined) {
+            sql += ' WHERE r.public_id = ?'
+            params.push(public_id)
+        } else {
+            sql += ' WHERE r.id = ?'
+            params.push(id)
+        }
+        let rows = await db.query(sql, params)
 
         if (!rows)
             return res.json({

+ 3 - 2
apis/Lepao/Record/GetLepaoRecords.js

@@ -44,10 +44,11 @@ class GetLepaoRecords extends API {
 
         let sql = `
                 SELECT 
-                    r.id,
+                    r.public_id,
                     r.uuid,
                     r.time,
                     r.result,
+                    r.run_mode,
                     r.lepao_account,
                     a.name,
                     a.user_avatar
@@ -100,7 +101,7 @@ class GetLepaoRecords extends API {
         }
 
         sql += `
-            ORDER BY id DESC
+            ORDER BY r.id DESC
             LIMIT ? OFFSET ?;
         `
         params.push(String(pagesize), String(offset))

+ 20 - 4
apis/Lepao/Record/GetRecordDetail.js

@@ -15,10 +15,15 @@ class GetRecordDetail extends API {
         let {
             uuid,
             session,
-            id
+            id,
+            public_id
         } = req.query
 
-        if ([uuid, session, id].some(value => value === '' || value === null || value === undefined))
+        if ([uuid, session].some(value => value === '' || value === null || value === undefined))
+            return res.json({
+                ...BaseStdResponse.MISSING_PARAMETER
+            })
+        if ((id === '' || id === null || id === undefined) && (public_id === '' || public_id === null || public_id === undefined))
             return res.json({
                 ...BaseStdResponse.MISSING_PARAMETER
             })
@@ -31,8 +36,10 @@ class GetRecordDetail extends API {
 
         let sql = `
                 SELECT 
+                    r.public_id,
                     r.time,
                     r.result,
+                    r.run_mode,
                     r.lepao_account,
                     r.point_data,
                     a.name,
@@ -48,9 +55,18 @@ class GetRecordDetail extends API {
                 ON 
                     r.path_id = p.id
                 WHERE 
-                    (r.uuid = ? OR (a.create_user IS NOT NULL AND a.create_user = ?)) AND r.id = ?
+                    (r.uuid = ? OR (a.create_user IS NOT NULL AND a.create_user = ?))
             `
-        let rows = await db.query(sql, [uuid, uuid, id])
+        const params = [uuid, uuid]
+        if (public_id !== '' && public_id !== null && public_id !== undefined) {
+            sql += ' AND r.public_id = ?'
+            params.push(public_id)
+        } else {
+            sql += ' AND r.id = ?'
+            params.push(id)
+        }
+
+        let rows = await db.query(sql, params)
 
         if (!rows)
             return res.json({

+ 2 - 1
apis/Lepao/SingleRun.js

@@ -99,7 +99,8 @@ class SingleRun extends API {
                     type: 'lepao.startRun',
                     data: {
                         taskId,
-                        account: student_num
+                        account: student_num,
+                        runMode: 'manual'
                     },
                     retry: 0
                 }

+ 7 - 4
lib/Lepao/Worker.js

@@ -13,6 +13,7 @@ const {
     selectCheckpoints,
     generateCadence
 } = require('../../plugin/Lepao/Path')
+const { v4: uuidv4 } = require('uuid')
 const {
     dataEncrypt,
     dataDecrypt,
@@ -142,12 +143,14 @@ class Worker {
         }
     }
 
-    async addLepaoRecord(uuid, account, result, pathId, pointData) {
+    async addLepaoRecord(uuid, account, result, pathId, pointData, runMode = 'auto') {
         if (!uuid || !account || !result || !pathId) return
         try {
             const time = Date.now()
-            const sql = 'INSERT INTO lepao_record (uuid, time, lepao_account, result, path_id, point_data, state) VALUES (?, ?, ?, ?, ?, ?, ?)'
-            await db.query(sql, [uuid, time, account, result, pathId, JSON.stringify(pointData || []), 1])
+            const publicId = uuidv4()
+            const safeRunMode = runMode === 'manual' ? 'manual' : 'auto'
+            const sql = 'INSERT INTO lepao_record (public_id, uuid, time, lepao_account, result, path_id, point_data, state, run_mode) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)'
+            await db.query(sql, [publicId, uuid, time, account, result, pathId, JSON.stringify(pointData || []), 1, safeRunMode])
         } catch (error) {
             this.logger.error(`写入乐跑记录失败: ${error.stack || error}`)
         }
@@ -564,7 +567,7 @@ class Worker {
 
                 // 绑定接口有返回即入库
                 if (bindRes && bindRes.data) {
-                    await this.addLepaoRecord(userData?.create_user, req.account, bindRes.data, pathId, pointData)
+                    await this.addLepaoRecord(userData?.create_user, req.account, bindRes.data, pathId, pointData, req.runMode)
                 }
 
                 const runResult = this.isRunSuccess(bindRes)

+ 2 - 1
plugin/mq/enqueueLepaoStartRun.js

@@ -14,7 +14,8 @@ async function enqueueLepaoStartRun(studentNum, logger) {
             type: 'lepao.startRun',
             data: {
                 taskId,
-                account: studentNum
+                account: studentNum,
+                runMode: 'auto'
             },
             retry: 0
         })