From 4b8f0d1cb618b00303502681159b0ad6bc4404a6 Mon Sep 17 00:00:00 2001
From: spring <2396852758@qq.com>
Date: 星期二, 24 三月 2026 14:54:40 +0800
Subject: [PATCH] fix: 真机运行时,日期组件默认当前日期

---
 src/pages/consumablesLogistics/stockReport/index.vue |   48 ++++++++++++++++++++++++++++++++++++------------
 1 files changed, 36 insertions(+), 12 deletions(-)

diff --git a/src/pages/consumablesLogistics/stockReport/index.vue b/src/pages/consumablesLogistics/stockReport/index.vue
index 8dc324f..175c151 100644
--- a/src/pages/consumablesLogistics/stockReport/index.vue
+++ b/src/pages/consumablesLogistics/stockReport/index.vue
@@ -74,15 +74,13 @@
       </view>
       <view v-else class="no-data">鏆傛棤鏁版嵁</view>
     </view>
-    <up-popup :show="showDatePicker" mode="bottom" @close="showDatePicker = false">
-      <up-datetime-picker
-        v-model="dateValue"
-        :mode="datePickerMode"
-        :show="showDatePicker"
-        @confirm="onDateConfirm"
-        @cancel="showDatePicker = false"
-      />
-    </up-popup>
+    <up-datetime-picker
+      v-model="dateValue"
+      :mode="datePickerMode"
+      :show="showDatePicker"
+      @confirm="onDateConfirm"
+      @cancel="showDatePicker = false"
+    />
   </view>
 </template>
 
@@ -90,7 +88,6 @@
 import { ref, reactive, toRefs, computed, watch } from "vue";
 import dayjs from "dayjs";
 import PageHeader from "@/components/PageHeader.vue";
-import { formatDateToYMD } from "@/utils/ruoyi";
 import { onShow, onReachBottom } from "@dcloudio/uni-app";
 import { getConsumablesInReportList, getConsumablesInInAndOutReportList } from "@/api/consumablesLogistics/consumablesIn.js";
 import {
@@ -207,6 +204,23 @@
   getList();
 };
 
+const toPickerTimestamp = (val, target) => {
+  if (!val) return Date.now();
+  let parsed;
+  if (target === "startMonth" || target === "endMonth") {
+    parsed = dayjs(`${val}-01`).valueOf();
+  } else {
+    parsed = dayjs(val).valueOf();
+  }
+  return Number.isNaN(parsed) ? Date.now() : parsed;
+};
+
+const formatPickerDate = (value, isMonth) => {
+  const parsed = dayjs(value);
+  if (!parsed.isValid()) return "";
+  return parsed.format(isMonth ? "YYYY-MM" : "YYYY-MM-DD");
+};
+
 const openDatePicker = (target) => {
   datePickerTarget.value = target;
   let val = "";
@@ -215,13 +229,18 @@
   else if (target === "endMonth") val = searchForm.value.endMonth;
   else if (target === "startDate") val = searchForm.value.startDate;
   else if (target === "endDate") val = searchForm.value.endDate;
-  dateValue.value = val ? new Date(val).getTime() : Date.now();
+  dateValue.value = toPickerTimestamp(val, target);
   showDatePicker.value = true;
 };
 
 const onDateConfirm = (e) => {
   const isMonth = datePickerTarget.value === "startMonth" || datePickerTarget.value === "endMonth";
-  const str = isMonth ? dayjs(e.value).format("YYYY-MM") : formatDateToYMD(e.value);
+  const str = formatPickerDate(e.value, isMonth);
+  if (!str) {
+    showDatePicker.value = false;
+    uni.showToast({ title: "鏃ユ湡鏍煎紡鏃犳晥", icon: "none" });
+    return;
+  }
   if (datePickerTarget.value === "single") {
     searchForm.value.singleDate = str;
     showDatePicker.value = false;
@@ -262,6 +281,9 @@
     searchForm.value.endDate = today.format("YYYY-MM-DD");
     searchForm.value.startDate = today.subtract(6, "day").format("YYYY-MM-DD");
   }
+  if (!datePickerTarget.value) {
+    dateValue.value = toPickerTimestamp(searchForm.value.singleDate, "single");
+  }
 };
 
 watch(
@@ -277,6 +299,8 @@
   fetchStockRecordTypeOptions();
 });
 
+initDefaultDates();
+
 onReachBottom(() => loadMore());
 
 const goBack = () => uni.navigateBack();

--
Gitblit v1.9.3