Browse Source

🐞 fix: 修复乐跑结束不返还多扣次数的问题

Pchen. 1 month ago
parent
commit
6f43a0b03b
1 changed files with 20 additions and 8 deletions
  1. 20 8
      lib/Lepao/Worker.js

+ 20 - 8
lib/Lepao/Worker.js

@@ -30,6 +30,8 @@ class Worker {
         this.maxRetry = 3
         this.timeout = 15000
         this._lepaoRecordPathColumn = null
+        // 预扣标记需覆盖“跑步+同步+重试”窗口,避免结算返还时标记已过期
+        this.lepaoBalanceMarkerTtlSec = 24 * 3600
     }
 
     roundKm(n) {
@@ -46,6 +48,12 @@ class Worker {
         return new Promise((r) => setTimeout(r, ms))
     }
 
+    getLepaoBalanceBaseKey(req, ctx) {
+        const stableTraceId = req?.traceId || ctx?.traceId
+        if (stableTraceId) return String(stableTraceId)
+        return `${ctx?.taskId || req?.account || req?.uuid || 'unknown'}`
+    }
+
     async markLoginExpired(account) {
         if (!account) return
         try {
@@ -354,7 +362,8 @@ class Worker {
                     {
                         account: req.account,
                         uuid: userData?.create_user,
-                        amountKm: deductedKm
+                        amountKm: deductedKm,
+                        traceId
                     },
                     ctx
                 )
@@ -480,7 +489,8 @@ class Worker {
                         {
                             account: req.account,
                             uuid: userData?.create_user,
-                            amountKm: deductedKm
+                            amountKm: deductedKm,
+                            traceId
                         },
                         ctx
                     )
@@ -564,7 +574,8 @@ class Worker {
                     {
                         account,
                         uuid,
-                        amountKm: this.roundKm(deductedKm || targetKm || 0)
+                        amountKm: this.roundKm(deductedKm || targetKm || 0),
+                        traceId
                     },
                     { ...ctx, taskId: `${ctx?.taskId || traceId}:finalize_refund_full` }
                 )
@@ -598,7 +609,8 @@ class Worker {
                     {
                         account,
                         uuid,
-                        amountKm: refundKm
+                        amountKm: refundKm,
+                        traceId
                     },
                     { ...ctx, taskId: `${ctx?.taskId || traceId}:finalize_refund_over` }
                 )
@@ -775,7 +787,7 @@ class Worker {
                 throw new Error('扣减乐跑公里失败:amountKm 无效')
             }
 
-            const baseKey = `${ctx?.taskId || ctx?.traceId || account || uuid}`
+            const baseKey = this.getLepaoBalanceBaseKey(req, ctx)
             const consumeKey = jkesRedisKeys.consume(baseKey)
             const existed = await Redis.get(consumeKey)
             if (existed) {
@@ -791,7 +803,7 @@ class Worker {
             }
             this.logger.info(`${account || uuid}扣减乐跑公里完成`)
 
-            await Redis.set(consumeKey, '1', { EX: 3600 })
+            await Redis.set(consumeKey, '1', { EX: this.lepaoBalanceMarkerTtlSec })
             return true
         })
 
@@ -804,7 +816,7 @@ class Worker {
             }
             if (!(amountKm > 0)) return true
 
-            const baseKey = `${ctx?.taskId || ctx?.traceId || account || uuid}`
+            const baseKey = this.getLepaoBalanceBaseKey(req, ctx)
             const consumeKey = jkesRedisKeys.consume(baseKey)
             const refundKey = jkesRedisKeys.refund(baseKey)
 
@@ -824,7 +836,7 @@ class Worker {
                 throw new Error('返还乐跑公里失败:数据库更新失败')
             }
             this.logger.info(`${account || uuid}返还乐跑公里完成`)
-            await Redis.set(refundKey, '1', { EX: 3600 })
+            await Redis.set(refundKey, '1', { EX: this.lepaoBalanceMarkerTtlSec })
             return true
         })