vue_zhongyou 1.0.10 → 1.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue_zhongyou",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "keywords": [],
@@ -109,6 +109,18 @@
109
109
  @click="openAddressPicker(field)"
110
110
  />
111
111
 
112
+ <!-- 单个日期时间 -->
113
+ <van-field
114
+ v-else-if="field.type === 'datetime'"
115
+ is-link
116
+ readonly
117
+ :label="field.label"
118
+ :label-width="60"
119
+ :placeholder="field.placeholder || '请选择日期时间'"
120
+ :model-value="getDateTimeText(field)"
121
+ :rules="field.rules || []"
122
+ @click="openDateTime(field)"
123
+ />
112
124
 
113
125
  <!-- 自定义内容 -->
114
126
  <slot v-else :field="field" :value="formData[field.field]" />
@@ -190,6 +202,23 @@
190
202
  @cancel="closeAddressPicker"
191
203
  />
192
204
  </van-popup>
205
+
206
+ <!-- 单个日期时间 -->
207
+ <van-popup v-model:show="dateTimePopup.visible" position="bottom" round>
208
+ <div class="datetime-popup">
209
+ <div class="popup-header">
210
+ <span class="cancel-btn" @click="closeDateTime">取消</span>
211
+ <span class="confirm-btn" @click="confirmDateTime">确定</span>
212
+ </div>
213
+ <nut-date-picker
214
+ v-model="dateTimePopup.selectedTime"
215
+ type="datetime"
216
+ :show-toolbar="false"
217
+ :filter="dateTimeFilter"
218
+ @change="onSingleDateTimeChange"
219
+ />
220
+ </div>
221
+ </van-popup>
193
222
  </template>
194
223
 
195
224
  <script setup>
@@ -254,6 +283,17 @@ const addressPopup = ref({
254
283
  columnsPlaceholder: null
255
284
  })
256
285
 
286
+ // 存储各个datetime字段的选择时间
287
+ const dateTimeValues = ref({})
288
+
289
+ // 单个日期时间弹窗状态
290
+ const dateTimePopup = ref({
291
+ visible: false,
292
+ field: null,
293
+ selectedTime: new Date(),
294
+ currentHour: '8'
295
+ })
296
+
257
297
 
258
298
 
259
299
 
@@ -268,6 +308,7 @@ const getDefaultValue = (type) => {
268
308
  if (type === 'checkbox') return []
269
309
  if (type === 'dateRange') return ['', '']
270
310
  if (type === 'datetimeRange') return ['', '']
311
+ if (type === 'datetime') return ''
271
312
  if (type === 'address') return { province: '', city: '', county: '', code: '' }
272
313
  return ''
273
314
  }
@@ -312,6 +353,11 @@ const handleSubmit = () => {
312
353
  }
313
354
 
314
355
  const handleReset = () => {
356
+ // 清除保存的datetime值
357
+ Object.keys(dateTimeValues.value).forEach(key => {
358
+ delete dateTimeValues.value[key]
359
+ })
360
+
315
361
  initializeFormData()
316
362
  emit('update:modelValue', snapshot())
317
363
  emit('reset', snapshot())
@@ -441,27 +487,39 @@ const confirmDateTimeRange = () => {
441
487
  }
442
488
 
443
489
  // 日期时间选择器筛选函数
444
- const startTimeFilter = (type, options) => {
490
+ // 通用时间过滤函数,根据类型(开始时间/结束时间)和当前小时值过滤选项
491
+ const timeFilter = (type, options, timeType) => {
445
492
  if (type === 'hour') {
446
493
  return options.filter(option => ['08', '12', '13', '17'].includes(option.value))
447
494
  } else if (type === 'minute') {
448
- // 当小时位是'08'或者'12'时,分钟只保留'00'
449
- if (['12','13'].includes(dateTimeRangePopup.value.currentStartHour)) {
495
+ // 获取对应时间类型的当前小时值
496
+ const currentHour = timeType === 'start'
497
+ ? dateTimeRangePopup.value.currentStartHour
498
+ : dateTimeRangePopup.value.currentEndHour
499
+
500
+ // 当小时位是'12'或者'13'时,分钟只保留'00'
501
+ if (['12','13'].includes(currentHour)) {
450
502
  return options.filter(option => ['00'].includes(option.value))
451
- }else{
503
+ } else {
452
504
  return options.filter(option => ['30'].includes(option.value))
453
505
  }
454
506
  }
455
507
  return options
456
508
  }
457
- const endTimeFilter = (type, options) => {
509
+
510
+ // 开始时间和结束时间的过滤器包装函数
511
+ const startTimeFilter = (type, options) => timeFilter(type, options, 'start')
512
+ const endTimeFilter = (type, options) => timeFilter(type, options, 'end')
513
+
514
+ // 单个日期时间的过滤函数
515
+ const dateTimeFilter = (type, options) => {
458
516
  if (type === 'hour') {
459
517
  return options.filter(option => ['08', '12', '13', '17'].includes(option.value))
460
518
  } else if (type === 'minute') {
461
- // 当小时位是'08'或者'12'时,分钟只保留'00'
462
- if (['12','13'].includes(dateTimeRangePopup.value.currentEndHour)) {
519
+ // 当小时位是'12'或者'13'时,分钟只保留'00'
520
+ if (['12','13'].includes(dateTimePopup.value.currentHour)) {
463
521
  return options.filter(option => ['00'].includes(option.value))
464
- }else{
522
+ } else {
465
523
  return options.filter(option => ['30'].includes(option.value))
466
524
  }
467
525
  }
@@ -558,6 +616,84 @@ const getAddressText = (field) => {
558
616
  return parts.join('')
559
617
  }
560
618
 
619
+ // 单个日期时间
620
+ const openDateTime = (field) => {
621
+ dateTimePopup.value.visible = true
622
+ dateTimePopup.value.field = field
623
+
624
+ // 检查是否已有该字段的保存值
625
+ const savedTime = dateTimeValues.value[field.field]
626
+
627
+ // 如果已经有保存的时间,则使用保存的时间,否则设置默认时间为当天8:30
628
+ if (savedTime) {
629
+ dateTimePopup.value.selectedTime = new Date(savedTime)
630
+ } else {
631
+ const today = new Date()
632
+ const defaultTime = new Date(today)
633
+ defaultTime.setHours(8, 30, 0, 0)
634
+
635
+ // 将field的时间转换为日期时间格式
636
+ dateTimePopup.value.selectedTime = typeof field.defaultValue === 'string' ? reverseFormatDateTime(field.defaultValue) : defaultTime
637
+ }
638
+
639
+ // 更新当前小时值
640
+ const hours = dateTimePopup.value.selectedTime.getHours()
641
+ dateTimePopup.value.currentHour = hours.toString().padStart(2, '0')
642
+ }
643
+
644
+ const closeDateTime = () => {
645
+ dateTimePopup.value.visible = false
646
+ }
647
+
648
+ const confirmDateTime = () => {
649
+ if (!dateTimePopup.value.selectedTime) {
650
+ return
651
+ }
652
+
653
+ // 格式化日期时间
654
+ const dateTimeStr = formatDateTime(dateTimePopup.value.selectedTime)
655
+
656
+ // 保存当前字段的时间值
657
+ if (dateTimePopup.value.field) {
658
+ dateTimeValues.value[dateTimePopup.value.field.field] = new Date(dateTimePopup.value.selectedTime)
659
+
660
+ // 更新表单数据
661
+ updateFieldValue(dateTimePopup.value.field.field, dateTimeStr)
662
+ }
663
+
664
+ // 关闭弹窗
665
+ closeDateTime()
666
+ }
667
+
668
+ const getDateTimeText = (field) => {
669
+ const value = formData[field.field]
670
+ return value || ''
671
+ }
672
+
673
+ const onSingleDateTimeChange = (params) => {
674
+ // 更新当前选择的时间
675
+ if (params && params.selectedValue) {
676
+ const [year, month, day, hour, minute] = params.selectedValue
677
+ const newDate = new Date(year, month - 1, day, hour, minute)
678
+ dateTimePopup.value.selectedTime = newDate
679
+
680
+ // 更新当前选择的小时值
681
+ if (params && params.selectedValue && params.columnIndex === 3) {
682
+ // 更新小时值
683
+ dateTimePopup.value.currentHour = params.selectedValue[3];
684
+
685
+ // 根据小时值设置默认分钟值
686
+ if (['08', '17'].includes(params.selectedValue[3])) {
687
+ // 小时为08或17时,分钟设为30
688
+ dateTimePopup.value.selectedTime.setMinutes(30)
689
+ } else if (['12', '13'].includes(params.selectedValue[3])) {
690
+ // 小时为12或13时,分钟设为00
691
+ dateTimePopup.value.selectedTime.setMinutes(0)
692
+ }
693
+ }
694
+ }
695
+ }
696
+
561
697
 
562
698
 
563
699
  </script>
@@ -630,6 +766,30 @@ const getAddressText = (field) => {
630
766
  }
631
767
  }
632
768
 
769
+ .datetime-popup {
770
+ .popup-header {
771
+ display: flex;
772
+ justify-content: space-between;
773
+ align-items: center;
774
+ padding: 16px;
775
+ border-bottom: 1px solid #f0f0f0;
776
+
777
+ .cancel-btn, .confirm-btn {
778
+ font-size: 16px;
779
+ padding: 4px 8px;
780
+ cursor: pointer;
781
+ }
782
+
783
+ .cancel-btn {
784
+ color: #666;
785
+ }
786
+
787
+ .confirm-btn {
788
+ color: #1989fa;
789
+ }
790
+ }
791
+ }
792
+
633
793
  :deep(.van-cell) {
634
794
  padding: 8px;
635
795
  }