Просмотр исходного кода

fix: 购物页退出重入时门状态未同步

onShow 新增门状态预查: 退出小程序期间门已关闭→直接走结算流程
提取 handleDoorClosed 公共方法, 门状态轮询和 onShow 共用

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
skyline 5 часов назад
Родитель
Сommit
c146ee5f8d
1 измененных файлов с 25 добавлено и 26 удалено
  1. 25 26
      haha-mp/src/pages/shopping/shopping.vue

+ 25 - 26
haha-mp/src/pages/shopping/shopping.vue

@@ -158,6 +158,16 @@ const cleanupTimers = () => {
   }
 };
 
+const handleDoorClosed = (status: any) => {
+  doorStatus.value = 'closing';
+  currentActivityId.value = status.activityId;
+  logger.log('门已关,购物完成,活动ID:', status.activityId);
+  if (countdownTimer) { clearInterval(countdownTimer); countdownTimer = null; }
+  if (returnCountdownTimer) { clearInterval(returnCountdownTimer); returnCountdownTimer = null; }
+  startReturnCountdown();
+  pollOrderAndNavigate();
+};
+
 onMounted(() => {
   // 标记当前页面轮询为活跃状态
   uni.setStorageSync('shoppingPollingActive', 'true');
@@ -188,20 +198,25 @@ onUnmounted(() => {
 });
 
 // 页面显示时重新激活轮询和倒计时
-onShow(() => {
+onShow(async () => {
   if (doorStatus.value === 'opened') {
     isComponentActive = true;
-    // 重启选购倒计时(从当前剩余秒数继续)
-    if (!countdownTimer) {
-      startCountdown();
-    }
+    // 先查一次当前门状态,避免退出期间门已关上但页面未感知
+    try {
+      const status = await pollDeviceStatus(deviceId, 5000, 1000);
+      if (status.doorStatus === 'close') {
+        // 门已关闭,直接进入结算流程
+        countdown.value = 0;
+        cleanupTimers();
+        handleDoorClosed(status);
+        return;
+      }
+    } catch { /* 查询失败则继续原逻辑 */ }
+    if (!countdownTimer) startCountdown();
     startStatusPolling();
   } else if (doorStatus.value === 'closing') {
     isComponentActive = true;
-    // 重启返回首页倒计时(从当前剩余秒数继续)
-    if (!returnCountdownTimer) {
-      startReturnCountdown();
-    }
+    if (!returnCountdownTimer) startReturnCountdown();
     startStatusPolling();
   }
 });
@@ -253,23 +268,7 @@ const startStatusPolling = async () => {
 
     // 检查门是否关闭
     if (status.doorStatus === 'close') {
-      // 门已关,显示购物完成
-      doorStatus.value = 'closing';
-      currentActivityId.value = status.activityId;
-
-      logger.log('门已关,购物完成,活动ID:', status.activityId);
-
-      // 停止选购倒计时
-      if (countdownTimer) {
-        clearInterval(countdownTimer);
-        countdownTimer = null;
-      }
-
-      // 开始返回首页倒计时
-      startReturnCountdown();
-
-      // 轮询订单信息,获取到后跳转到订单完成页
-      pollOrderAndNavigate();
+      handleDoorClosed(status);
     } else {
       // 继续等待关门,设置延迟后重新轮询
       if (isComponentActive && doorStatus.value === 'opened') {