From 6739faebc8bac0aa168a6f61573466b1c80bbecd Mon Sep 17 00:00:00 2001
From: zhangwencui <1064582902@qq.com>
Date: 星期五, 12 六月 2026 11:12:27 +0800
Subject: [PATCH] 打卡增加夜班逻辑

---
 src/pages/humanResources/attendance/checkin.vue |   95 +++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 90 insertions(+), 5 deletions(-)

diff --git a/src/pages/humanResources/attendance/checkin.vue b/src/pages/humanResources/attendance/checkin.vue
index cc113a1..ab93ed9 100644
--- a/src/pages/humanResources/attendance/checkin.vue
+++ b/src/pages/humanResources/attendance/checkin.vue
@@ -132,10 +132,12 @@
         } else {
           noNeedCheckIn.value = false;
         }
+        updateInCheckRange();
       } else {
         // 椤甸潰鏄剧ず鈥滄棤闇�鎵撳崱鈥�
         todayRecord.value = {};
         noNeedCheckIn.value = true;
+        updateInCheckRange();
       }
     });
   };
@@ -158,21 +160,104 @@
     const m = String(now.getMinutes()).padStart(2, "0");
     const s = String(now.getSeconds()).padStart(2, "0");
     nowTime.value = `${Y}-${M}-${D} ${h}:${m}:${s}`;
+    updateInCheckRange();
   };
 
   // 浠婃棩鏃ユ湡
   const todayStr = computed(() => nowTime.value.slice(0, 10));
+
+  const parseHmToMinutes = hm => {
+    if (!hm || typeof hm !== "string") return null;
+    const [hStr, mStr] = hm.split(":");
+    const h = Number(hStr);
+    const m = Number(mStr);
+    if (!Number.isFinite(h) || !Number.isFinite(m)) return null;
+    if (h < 0 || h > 23 || m < 0 || m > 59) return null;
+    return h * 60 + m;
+  };
+
+  const parseYmdToDate = ymd => {
+    if (!ymd || typeof ymd !== "string") return null;
+    const [yStr, mStr, dStr] = ymd.slice(0, 10).split("-");
+    const y = Number(yStr);
+    const m = Number(mStr);
+    const d = Number(dStr);
+    if (!Number.isFinite(y) || !Number.isFinite(m) || !Number.isFinite(d)) {
+      return null;
+    }
+    return new Date(y, m - 1, d, 0, 0, 0, 0);
+  };
+
+  const addMinutes = (date, minutes) => {
+    return new Date(date.getTime() + minutes * 60 * 1000);
+  };
+
+  const buildShiftWindow = () => {
+    const startAtMinutes = parseHmToMinutes(todayRecord.value?.startAt);
+    const endAtMinutes = parseHmToMinutes(todayRecord.value?.endAt);
+    if (startAtMinutes === null || endAtMinutes === null) return null;
+
+    const baseYmd = todayRecord.value?.date
+      ? String(todayRecord.value.date).slice(0, 10)
+      : todayStr.value;
+    const baseDate = parseYmdToDate(baseYmd);
+    if (!baseDate) return null;
+
+    const startDateTime = addMinutes(baseDate, startAtMinutes);
+    const crossDay = startAtMinutes > endAtMinutes;
+    const endBase = crossDay ? addMinutes(baseDate, 24 * 60) : baseDate;
+    const endDateTime = addMinutes(endBase, endAtMinutes);
+
+    return { startDateTime, endDateTime };
+  };
+
+  const updateInCheckRange = () => {
+    if (noNeedCheckIn.value) {
+      inCheckRange.value = true;
+      return;
+    }
+    const window = buildShiftWindow();
+    if (!window) {
+      inCheckRange.value = true;
+      return;
+    }
+    const now = new Date();
+    const checkInEarlyMinutes = 120;
+    const checkOutLateMinutes = 720;
+    const needAction = (() => {
+      if (todayRecord.value?.workEndAt) return "done";
+      if (todayRecord.value?.workStartAt) return "checkOut";
+      const distToStart = Math.abs(
+        now.getTime() - window.startDateTime.getTime()
+      );
+      const distToEnd = Math.abs(now.getTime() - window.endDateTime.getTime());
+      return distToEnd < distToStart ? "checkOut" : "checkIn";
+    })();
+    const start = addMinutes(window.startDateTime, -checkInEarlyMinutes);
+    const end = addMinutes(
+      window.endDateTime,
+      needAction === "checkOut" ? checkOutLateMinutes : 0
+    );
+    inCheckRange.value = now >= start && now <= end;
+  };
 
   // 鎵撳崱鎸夐挳鏂囨湰
   const checkInOutText = computed(() => {
     if (noNeedCheckIn.value) {
       return "鏃犻渶鎵撳崱";
     }
-    if (!todayRecord.value || !todayRecord.value.workStartAt) {
-      return "涓婄彮鎵撳崱";
-    }
     if (!todayRecord.value.workEndAt) {
-      return "涓嬬彮鎵撳崱";
+      if (todayRecord.value.workStartAt) {
+        return "涓嬬彮鎵撳崱";
+      }
+      const window = buildShiftWindow();
+      if (!window) return "涓婄彮鎵撳崱";
+      const now = new Date();
+      const distToStart = Math.abs(
+        now.getTime() - window.startDateTime.getTime()
+      );
+      const distToEnd = Math.abs(now.getTime() - window.endDateTime.getTime());
+      return distToEnd < distToStart ? "涓嬬彮鎵撳崱" : "涓婄彮鎵撳崱";
     }
     return "宸叉墦鍗�";
   });
@@ -799,4 +884,4 @@
   .attendance-records {
     animation-delay: 0.2s;
   }
-</style>
\ No newline at end of file
+</style>

--
Gitblit v1.9.3