feat(WorkOrderEdit): 将作业周期选择改为日期范围选择器

重构日期选择组件,将单个日期输入改为开始和结束日期分开选择
添加日期范围验证逻辑,确保结束日期不小于开始日期
更新表单数据结构以支持日期范围存储
This commit is contained in:
liangbin 2026-01-16 14:06:19 +08:00
parent 7d1b906aae
commit 158a652db7
1 changed files with 98 additions and 24 deletions

View File

@ -18,34 +18,65 @@
<view class="FormItem"> <view class="FormItem">
<view class="FormLableBox mustBox">作业周期</view> <view class="FormLableBox mustBox">作业周期</view>
<view class="FormValueBox"> <view class="FormValueBox">
<view class="date-picker-box"> <view class="date-range-box">
<u-input v-model="formData.period" placeholder="请选择作业周期" readonly @focus="showTimeBox = true"></u-input> <view class="date-item">
<u-button style="margin-left: 5rpx;width: 80rpx;" type="primary" size="small" @click="showTimeBox = true">选择日期</u-button> <u-input v-model="startDateText" placeholder="开始日期" readonly>
<template #suffix>
<u-button type="primary" size="small" @click="showStartDatePicker = true">选择日期</u-button>
</template>
</u-input>
<up-datetime-picker :show="showStartDatePicker" v-model="startDate" mode="date"
:minDate="minTimestamp" @confirm="HandleStartDateConfirm"
@cancel="showStartDatePicker = false"></up-datetime-picker>
</view>
<view class="date-separator"></view>
<view class="date-item">
<u-input v-model="endDateText" placeholder="结束日期" readonly>
<template #suffix>
<u-button type="primary" size="small" @click="showEndDatePicker = true">选择日期</u-button>
</template>
</u-input>
<up-datetime-picker :show="showEndDatePicker" v-model="endDate" mode="date"
:minDate="minTimestamp" @confirm="HandleEndDateConfirm"
@cancel="showEndDatePicker = false"></up-datetime-picker>
</view>
</view> </view>
<u-calendar :show="showTimeBox" mode="range" :minDate="minDate"
@confirm="HandlePeriodConfirm" @close="HandleCloseTimeBox" ></u-calendar>
</view> </view>
</view> </view>
</view> </view>
</view> </view>
</template> </template>
<script setup> <script setup>
import { ref, onMounted, defineExpose } from "vue"; import { ref, computed, onMounted, defineExpose } from "vue";
import dayjs from 'dayjs';
// //
let mapEntity = null; let mapEntity = null;
// //
let currentMarker = null; let currentMarker = null;
const showTimeBox = ref(false); // const showStartDatePicker = ref(false); //
const minDate = ref('1900-01-01'); // const showEndDatePicker = ref(false); //
const startDate = ref(null); //
const endDate = ref(null); //
const startDateText = ref(''); //
const endDateText = ref(''); //
const minTimestamp = ref(dayjs('2020-01-01').valueOf()); //
//
const endDateMinTimestamp = computed(() => {
if (startDate.value) {
return startDate.value;
}
return minTimestamp.value;
});
// //
const formData = ref({ const formData = ref({
ProjectName: "", // ProjectName: "", //
Location: "", // Location: "", //
SpecificAddress: "", // SpecificAddress: "", //
period: "", // period: [], // [, ]
}); });
// //
@ -58,22 +89,56 @@ onMounted(() => {
initTiandituMap(); initTiandituMap();
}); });
// //
const HandlePeriodConfirm = (dates) => { const HandleStartDateConfirm = (e) => {
console.log('选中的日期范围:', dates); console.log('选中的开始日期:', e);
if (dates && dates.length > 0) {
const startDate = dates[0]; // 使
const endDate = dates[dates.length - 1] || dates[0]; const selectedDate = (e.value && dayjs(e.value).isValid()) ? e.value : minTimestamp.value;
formData.value.period = `${startDate}${endDate}`;
console.log('作业周期:', formData.value.period); startDateText.value = dayjs(selectedDate).format('YYYY-MM-DD');
} startDate.value = selectedDate;
showTimeBox.value = false; endDateText.value = '';
endDate.value = null;
showStartDatePicker.value = false;
UpdatePeriod();
}; };
// //
const HandleCloseTimeBox = () => { const HandleEndDateConfirm = (e) => {
showTimeBox.value = false; console.log('选中的结束日期:', e);
formData.value.period = "";
// 使
const selectedDate = (e.value && dayjs(e.value).isValid()) ? e.value : minTimestamp.value;
//
if (startDate.value && dayjs(selectedDate).isBefore(dayjs(startDate.value))) {
uni.showToast({
title: '结束日期不能小于开始日期',
icon: 'none',
duration: 2000
});
return;
}
endDateText.value = dayjs(selectedDate).format('YYYY-MM-DD');
endDate.value = selectedDate;
showEndDatePicker.value = false;
UpdatePeriod();
};
//
const UpdatePeriod = () => {
if (startDateText.value && endDateText.value) {
formData.value.period = [startDateText.value, endDateText.value];
} else if (startDateText.value) {
formData.value.period = [startDateText.value, ''];
} else if (endDateText.value) {
formData.value.period = ['', endDateText.value];
} else {
formData.value.period = [];
}
console.log('作业周期:', formData.value.period);
}; };
// //
@ -167,11 +232,20 @@ const initTiandituMap = () => {
font-size: 30rpx; font-size: 30rpx;
margin-top: 20rpx; margin-top: 20rpx;
.date-picker-box { .date-range-box {
display: flex; display: flex;
align-items: center; align-items: center;
gap: 20rpx; gap: 20rpx;
flex: 1; flex: 1;
.date-item {
flex: 1;
}
.date-separator {
color: #999;
font-size: 28rpx;
}
} }
} }
} }