Browse Source

✨ feat: 限制乐跑过程中发起乐跑

Pchen. 7 months ago
parent
commit
3d08dd76e6
2 changed files with 20 additions and 3 deletions
  1. 7 0
      apis/Lepao/SingleRun.js
  2. 13 3
      lib/Lepao/Lepao.js

+ 7 - 0
apis/Lepao/SingleRun.js

@@ -43,6 +43,13 @@ class SingleRun extends API {
                 msg: '该账号当天已乐跑成功!请勿重复乐跑'
             })
 
+        const isProgress = await Redis.get(`lepaoProgress:${account}`)
+        if (isProgress)
+            return res.json({
+                ...BaseStdResponse.ERR,
+                msg: '该账号已进入乐跑任务队列,请等待乐跑完成后再进行乐跑操作'
+            })
+
         let selectSql = 'SELECT create_user FROM lepao_account WHERE student_num = ?'
         let selectRows = await db.query(selectSql, [student_num])
         if (!selectRows || selectRows.length === 0)

+ 13 - 3
lib/Lepao/Lepao.js

@@ -98,6 +98,14 @@ class Lepao {
             const isSuccess = await Redis.get(`lepaoSuccess:${account}`)
             if (isSuccess)
                 throw new Error('该账号当天已存在成功乐跑记录')
+            const isProgress = await Redis.get(`lepaoProgress:${account}`)
+            if (isProgress)
+                throw new Error('该账号已进入乐跑任务队列,请等待乐跑完成后再进行乐跑操作')
+
+            //已开始乐跑,存入Redis
+            await Redis.set(`lepaoProgress:${account}`, account, {
+                EX: 120
+            })
 
             const userPermissionSql = 'SELECT vip, lepao_count FROM users WHERE uuid = ?'
             const userPermissionData = await db.query(userPermissionSql, [uuid])
@@ -127,7 +135,7 @@ class Lepao {
             let run_end_time = Math.floor(Date.now() / 1000) - 300 // 提前5分钟
             let hour = new Date().getHours()
 
-            if(hour < 7)
+            if (hour < 7)
                 throw new Error('当前不在有效乐跑时间范围内。RunForge支持乐跑时间段为7:00~24:00')
 
             if (hour >= 22) {
@@ -165,7 +173,7 @@ class Lepao {
                 const { data } = ossRes
                 this.logger.info(`${account}上传OSS记录返回结果: ${JSON.stringify(data)}`)
                 if (!data || data.code !== 200 || !data.oss_path || !data.point_data) {
-                    if(data.code == -200) {
+                    if (data.code == -200) {
                         this.logger.info(`${account}分配打卡点数量不足,重新执行乐跑流程`)
                         return this.beginLepao(uuid, account, token, uid, school_id, state)
                     }
@@ -231,7 +239,7 @@ class Lepao {
                     this.logger.warn(`${account}乐跑失败,原因: ${data.data.record_failed_reason}`)
 
                     // 已存在记录也存redis
-                    if(data.data.record_failed_reason === '当天关联成绩次数已达到上限')
+                    if (data.data.record_failed_reason === '当天关联成绩次数已达到上限')
                         await this.writeRedis(account)
 
                     await this.sendFailEmail(account, data.data.record_failed_reason)
@@ -252,6 +260,8 @@ class Lepao {
         } catch (error) {
             this.logger.error(`${account}乐跑流程异常: ${error.stack || error.message}`)
             await this.sendFailEmail(account, error.message || '未知错误,请尝试重新登录')
+        } finally {
+            await Redis.del(`lepaoProgress:${account}`)
         }
     }