uni-datetime-picker.vue 26.1 KB
Newer Older
芊里 已提交
1
<template>
DCloud_JSON's avatar
DCloud_JSON 已提交
2 3
	<view class="uni-date">
		<view class="uni-date-editor" @click="show">
芊里 已提交
4
			<slot>
DCloud_JSON's avatar
DCloud_JSON 已提交
5 6 7
				<view class="uni-date-editor--x" :class="{'uni-date-editor--x__disabled': disabled,
		'uni-date-x--border': border}">
					<view v-if="!isRange" class="uni-date-x uni-date-single">
8
						<uni-icons type="calendar" color="#c0c4cc" size="22"></uni-icons>
9 10
						<input class="uni-date__x-input" type="text" v-model="singleVal"
							:placeholder="singlePlaceholderText" :disabled="true" />
DCloud_JSON's avatar
DCloud_JSON 已提交
11 12
					</view>
					<view v-else class="uni-date-x uni-date-range">
13
						<uni-icons type="calendar" color="#c0c4cc" size="22"></uni-icons>
14 15
						<input class="uni-date__x-input t-c" type="text" v-model="range.startDate"
							:placeholder="startPlaceholderText" :disabled="true" />
DCloud_JSON's avatar
DCloud_JSON 已提交
16 17 18
						<slot>
							<view class="">{{rangeSeparator}}</view>
						</slot>
19 20
						<input class="uni-date__x-input t-c" type="text" v-model="range.endDate"
							:placeholder="endPlaceholderText" :disabled="true" />
DCloud_JSON's avatar
DCloud_JSON 已提交
21
					</view>
study夏羽's avatar
update  
study夏羽 已提交
22
					<view v-if="showClearIcon" class="uni-date__icon-clear" @click.stop="clear">
23
						<uni-icons type="clear" color="#c0c4cc" size="24"></uni-icons>
芊里 已提交
24 25 26 27
					</view>
				</view>
			</slot>
		</view>
DCloud_JSON's avatar
DCloud_JSON 已提交
28 29 30 31

		<view v-show="popup" class="uni-date-mask" @click="close"></view>
		<view v-if="!isPhone" ref="datePicker" v-show="popup" class="uni-date-picker__container">
			<view v-if="!isRange" class="uni-date-single--x" :style="popover">
study夏羽's avatar
update  
study夏羽 已提交
32 33
				<view class="uni-popper__arrow"></view>
				<view v-if="hasTime" class="uni-date-changed popup-x-header">
34 35
					<input class="uni-date__input t-c" type="text" v-model="tempSingleDate"
						:placeholder="selectDateText" />
DCloud_JSON's avatar
DCloud_JSON 已提交
36
					<time-picker type="time" v-model="time" :border="false" :disabled="!tempSingleDate"
37 38 39
						:start="reactStartTime" :end="reactEndTime" :hideSecond="hideSecond" style="width: 100%;">
						<input class="uni-date__input t-c" type="text" v-model="time" :placeholder="selectTimeText"
							:disabled="!tempSingleDate" />
DCloud_JSON's avatar
DCloud_JSON 已提交
40
					</time-picker>
芊里 已提交
41
				</view>
study夏羽's avatar
update  
study夏羽 已提交
42 43 44
				<calendar ref="pcSingle" :showMonth="false" :start-date="caleRange.startDate"
					:end-date="caleRange.endDate" :date="defSingleDate" @change="singleChange"
					style="padding: 0 8px;" />
DCloud_JSON's avatar
DCloud_JSON 已提交
45 46
				<view v-if="hasTime" class="popup-x-footer">
					<!-- <text class="">此刻</text> -->
47
					<text class="confirm" @click="confirmSingleChange">{{okText}}</text>
DCloud_JSON's avatar
DCloud_JSON 已提交
48 49 50 51 52
				</view>
				<view class="uni-date-popper__arrow"></view>
			</view>

			<view v-else class="uni-date-range--x" :style="popover">
study夏羽's avatar
update  
study夏羽 已提交
53 54
				<view class="uni-popper__arrow"></view>
				<view v-if="hasTime" class="popup-x-header uni-date-changed">
DCloud_JSON's avatar
DCloud_JSON 已提交
55 56
					<view class="popup-x-header--datetime">
						<input class="uni-date__input uni-date-range__input" type="text" v-model="tempRange.startDate"
57
							:placeholder="startDateText" />
DCloud_JSON's avatar
DCloud_JSON 已提交
58
						<time-picker type="time" v-model="tempRange.startTime" :start="reactStartTime" :border="false"
59
							:disabled="!tempRange.startDate" :hideSecond="hideSecond">
DCloud_JSON's avatar
DCloud_JSON 已提交
60
							<input class="uni-date__input uni-date-range__input" type="text"
61 62
								v-model="tempRange.startTime" :placeholder="startTimeText"
								:disabled="!tempRange.startDate" />
DCloud_JSON's avatar
DCloud_JSON 已提交
63
						</time-picker>
芊里 已提交
64
					</view>
DCloud_JSON's avatar
DCloud_JSON 已提交
65 66 67
					<uni-icons type="arrowthinright" color="#999" style="line-height: 40px;"></uni-icons>
					<view class="popup-x-header--datetime">
						<input class="uni-date__input uni-date-range__input" type="text" v-model="tempRange.endDate"
68
							:placeholder="endDateText" />
DCloud_JSON's avatar
DCloud_JSON 已提交
69
						<time-picker type="time" v-model="tempRange.endTime" :end="reactEndTime" :border="false"
70
							:disabled="!tempRange.endDate" :hideSecond="hideSecond">
DCloud_JSON's avatar
DCloud_JSON 已提交
71
							<input class="uni-date__input uni-date-range__input" type="text" v-model="tempRange.endTime"
72
								:placeholder="endTimeText" :disabled="!tempRange.endDate" />
DCloud_JSON's avatar
DCloud_JSON 已提交
73
						</time-picker>
芊里 已提交
74 75
					</view>
				</view>
DCloud_JSON's avatar
DCloud_JSON 已提交
76
				<view class="popup-x-body">
study夏羽's avatar
update  
study夏羽 已提交
77 78 79 80 81 82 83
					<calendar ref="left" :showMonth="false" :start-date="caleRange.startDate"
						:end-date="caleRange.endDate" :range="true" @change="leftChange" :pleStatus="endMultipleStatus"
						@firstEnterCale="updateRightCale" @monthSwitch="leftMonthSwitch" style="padding: 0 8px;" />
					<calendar ref="right" :showMonth="false" :start-date="caleRange.startDate"
						:end-date="caleRange.endDate" :range="true" @change="rightChange"
						:pleStatus="startMultipleStatus" @firstEnterCale="updateLeftCale"
						@monthSwitch="rightMonthSwitch" style="padding: 0 8px;border-left: 1px solid #F1F1F1;" />
DCloud_JSON's avatar
DCloud_JSON 已提交
84 85
				</view>
				<view v-if="hasTime" class="popup-x-footer">
86 87
					<text class="" @click="clear">{{clearText}}</text>
					<text class="confirm" @click="confirmRangeChange">{{okText}}</text>
DCloud_JSON's avatar
DCloud_JSON 已提交
88
				</view>
芊里 已提交
89 90
			</view>
		</view>
study夏羽's avatar
update  
study夏羽 已提交
91
		<calendar v-show="isPhone" ref="mobile" :clearDate="false" :date="defSingleDate" :defTime="reactMobDefTime"
DCloud_JSON's avatar
DCloud_JSON 已提交
92 93
			:start-date="caleRange.startDate" :end-date="caleRange.endDate" :selectableTimes="mobSelectableTime"
			:pleStatus="endMultipleStatus" :showMonth="false" :range="isRange" :typeHasTime="hasTime" :insert="false"
94
			:hideSecond="hideSecond" @confirm="mobileChange" @maskClose="close" />
芊里 已提交
95 96 97 98 99
	</view>
</template>
<script>
	/**
	 * DatetimePicker 时间选择器
DCloud_JSON's avatar
DCloud_JSON 已提交
100 101 102
	 * @description 同时支持 PC 和移动端使用日历选择日期和日期范围
	 * @tutorial https://ext.dcloud.net.cn/plugin?id=3962
	 * @property {String} type 选择器类型
study夏羽's avatar
update  
study夏羽 已提交
103
	 * @property {String|Number|Array|Date} value 绑定值
DCloud_JSON's avatar
DCloud_JSON 已提交
104 105
	 * @property {String} placeholder 单选择时的占位内容
	 * @property {String} start 起始时间
study夏羽's avatar
update  
study夏羽 已提交
106
	 * @property {String} end 终止时间
DCloud_JSON's avatar
DCloud_JSON 已提交
107 108 109 110 111 112 113
	 * @property {String} start-placeholder 范围选择时开始日期的占位内容
	 * @property {String} end-placeholder 范围选择时结束日期的占位内容
	 * @property {String} range-separator 选择范围时的分隔符
	 * @property {Boolean} border = [true|false] 是否有边框
	 * @property {Boolean} disabled = [true|false] 是否禁用
	 * @property {Boolean} clearIcon = [true|false] 是否显示清除按钮(仅PC端适用)
	 * @event {Function} change 确定日期时触发的事件
study夏羽's avatar
update  
study夏羽 已提交
114 115 116
	 * @event {Function} show 打开弹出层
	 * @event {Function} close 关闭弹出层
	 * @event {Function} clear 清除上次选中的状态和值
DCloud_JSON's avatar
DCloud_JSON 已提交
117
	 **/
118 119 120 121 122 123
	import calendar from './calendar.vue'
	import timePicker from './time-picker.vue'
	import {
		initVueI18n
	} from '@dcloudio/uni-i18n'
	import messages from './i18n/index.js'
124
	let t = null
芊里 已提交
125 126 127

	export default {
		name: 'UniDatetimePicker',
128 129 130
		options: {
			virtualHost: true
		},
芊里 已提交
131
		components: {
DCloud_JSON's avatar
DCloud_JSON 已提交
132 133
			calendar,
			timePicker
芊里 已提交
134
		},
135 136 137 138 139 140 141 142 143 144
		inject: {
			form: {
				from: 'uniForm',
				default: null
			},
			formItem: {
				from: 'uniFormItem',
				default: null
			},
		},
芊里 已提交
145 146
		data() {
			return {
DCloud_JSON's avatar
DCloud_JSON 已提交
147 148 149 150 151 152 153
				isRange: false,
				hasTime: false,
				mobileRange: false,
				// 单选
				singleVal: '',
				tempSingleDate: '',
				defSingleDate: '',
芊里 已提交
154
				time: '',
DCloud_JSON's avatar
DCloud_JSON 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
				// 范围选
				caleRange: {
					startDate: '',
					startTime: '',
					endDate: '',
					endTime: ''
				},
				range: {
					startDate: '',
					// startTime: '',
					endDate: '',
					// endTime: ''
				},
				tempRange: {
					startDate: '',
					startTime: '',
					endDate: '',
					endTime: ''
				},
				// 左右日历同步数据
				startMultipleStatus: {
					before: '',
					after: '',
					data: [],
					fulldate: ''
				},
				endMultipleStatus: {
					before: '',
					after: '',
					data: [],
					fulldate: ''
				},
				visible: false,
				popup: false,
DCloud_JSON's avatar
DCloud_JSON 已提交
189 190
				popover: null,
				isEmitValue: false,
DCloud_JSON's avatar
DCloud_JSON 已提交
191
				isPhone: false,
192
				isFirstShow: true,
芊里 已提交
193 194 195 196 197 198 199 200
			}
		},
		props: {
			type: {
				type: String,
				default: 'datetime'
			},
			value: {
DCloud_JSON's avatar
DCloud_JSON 已提交
201
				type: [String, Number, Array, Date],
芊里 已提交
202
				default: ''
203 204 205 206
			},
			modelValue: {
				type: [String, Number, Array, Date],
				default: ''
芊里 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219
			},
			start: {
				type: [Number, String],
				default: ''
			},
			end: {
				type: [Number, String],
				default: ''
			},
			returnType: {
				type: String,
				default: 'string'
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
220 221
			placeholder: {
				type: String,
222
				default: ''
DCloud_JSON's avatar
DCloud_JSON 已提交
223 224 225
			},
			startPlaceholder: {
				type: String,
226
				default: ''
DCloud_JSON's avatar
DCloud_JSON 已提交
227 228 229
			},
			endPlaceholder: {
				type: String,
230
				default: ''
DCloud_JSON's avatar
DCloud_JSON 已提交
231 232 233 234
			},
			rangeSeparator: {
				type: String,
				default: '-'
芊里 已提交
235 236
			},
			border: {
DCloud_JSON's avatar
DCloud_JSON 已提交
237
				type: [Boolean],
芊里 已提交
238 239
				default: true
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
240 241
			disabled: {
				type: [Boolean],
芊里 已提交
242
				default: false
DCloud_JSON's avatar
DCloud_JSON 已提交
243 244 245 246
			},
			clearIcon: {
				type: [Boolean],
				default: true
study夏羽's avatar
update  
study夏羽 已提交
247 248 249 250
			},
			hideSecond: {
				type: [Boolean],
				default: false
芊里 已提交
251 252 253
			}
		},
		watch: {
DCloud_JSON's avatar
DCloud_JSON 已提交
254 255
			type: {
				immediate: true,
芊里 已提交
256
				handler(newVal, oldVal) {
DCloud_JSON's avatar
DCloud_JSON 已提交
257 258
					if (newVal.indexOf('time') !== -1) {
						this.hasTime = true
259 260
					} else {
						this.hasTime = false
芊里 已提交
261
					}
DCloud_JSON's avatar
DCloud_JSON 已提交
262 263
					if (newVal.indexOf('range') !== -1) {
						this.isRange = true
芊里 已提交
264
					} else {
DCloud_JSON's avatar
DCloud_JSON 已提交
265
						this.isRange = false
芊里 已提交
266 267 268
					}
				}
			},
study夏羽's avatar
update  
study夏羽 已提交
269
			// #ifndef VUE3
DCloud_JSON's avatar
DCloud_JSON 已提交
270 271
			value: {
				immediate: true,
DCloud_JSON's avatar
DCloud_JSON 已提交
272 273 274 275
				handler(newVal, oldVal) {
					if (this.isEmitValue) {
						this.isEmitValue = false
						return
芊里 已提交
276
					}
DCloud_JSON's avatar
DCloud_JSON 已提交
277
					this.initPicker(newVal)
芊里 已提交
278 279
				}
			},
study夏羽's avatar
update  
study夏羽 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292
			// #endif
			// #ifdef VUE3
			modelValue: {
				immediate: true,
				handler(newVal, oldVal) {
					if (this.isEmitValue) {
						this.isEmitValue = false
						return
					}
					this.initPicker(newVal)
				}
			},
			// #endif
DCloud_JSON's avatar
DCloud_JSON 已提交
293 294 295 296 297 298 299 300 301 302 303
			start: {
				immediate: true,
				handler(newVal, oldVal) {
					if (!newVal) return
					const {
						defDate,
						defTime
					} = this.parseDate(newVal)
					this.caleRange.startDate = defDate
					if (this.hasTime) {
						this.caleRange.startTime = defTime
芊里 已提交
304 305 306
					}
				}
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
307 308 309 310 311 312 313 314 315 316 317
			end: {
				immediate: true,
				handler(newVal, oldVal) {
					if (!newVal) return
					const {
						defDate,
						defTime
					} = this.parseDate(newVal)
					this.caleRange.endDate = defDate
					if (this.hasTime) {
						this.caleRange.endTime = defTime
芊里 已提交
318 319 320
					}
				}
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
321 322 323 324 325 326 327 328 329 330 331 332
		},
		computed: {
			reactStartTime() {
				const activeDate = this.isRange ? this.tempRange.startDate : this.tempSingleDate
				const res = activeDate === this.caleRange.startDate ? this.caleRange.startTime : ''
				return res
			},
			reactEndTime() {
				const activeDate = this.isRange ? this.tempRange.endDate : this.tempSingleDate
				const res = activeDate === this.caleRange.endDate ? this.caleRange.endTime : ''
				return res
			},
study夏羽's avatar
update  
study夏羽 已提交
333
			reactMobDefTime() {
334
				const times = {
DCloud_JSON's avatar
DCloud_JSON 已提交
335 336
					start: this.tempRange.startTime,
					end: this.tempRange.endTime
337 338
				}
				return this.isRange ? times : this.time
DCloud_JSON's avatar
DCloud_JSON 已提交
339 340 341 342 343 344 345 346 347 348
			},
			mobSelectableTime() {
				return {
					start: this.caleRange.startTime,
					end: this.caleRange.endTime
				}
			},
			datePopupWidth() {
				// todo
				return this.isRange ? 653 : 301
349 350 351 352 353 354
			},

			/**
			 * for i18n
			 */
			singlePlaceholderText() {
355 356
				return this.placeholder || (this.type === 'date' ? this.selectDateText : t(
					"uni-datetime-picker.selectDateTime"))
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
			},
			startPlaceholderText() {
				return this.startPlaceholder || this.startDateText
			},
			endPlaceholderText() {
				return this.endPlaceholder || this.endDateText
			},
			selectDateText() {
				return t("uni-datetime-picker.selectDate")
			},
			selectTimeText() {
				return t("uni-datetime-picker.selectTime")
			},
			startDateText() {
				return this.startPlaceholder || t("uni-datetime-picker.startDate")
			},
			startTimeText() {
				return t("uni-datetime-picker.startTime")
			},
			endDateText() {
				return this.endPlaceholder || t("uni-datetime-picker.endDate")
			},
			endTimeText() {
				return t("uni-datetime-picker.endTime")
			},
			okText() {
				return t("uni-datetime-picker.ok")
			},
			clearText() {
				return t("uni-datetime-picker.clear")
study夏羽's avatar
update  
study夏羽 已提交
387 388 389 390 391 392 393 394 395 396
			},
			showClearIcon() {
				const {
					clearIcon,
					disabled,
					singleVal,
					range
				} = this
				const bool = clearIcon && !disabled && (singleVal || (range.startDate && range.endDate))
				return bool
DCloud_JSON's avatar
DCloud_JSON 已提交
397 398
			}
		},
399
		created() {
400 401 402 403
			if(!t) {
				const  vueI18n = initVueI18n(messages)
				t = vueI18n.t
			}
404
		},
DCloud_JSON's avatar
DCloud_JSON 已提交
405
		mounted() {
study夏羽's avatar
update  
study夏羽 已提交
406
			this.platform()
DCloud_JSON's avatar
DCloud_JSON 已提交
407 408 409 410 411
		},
		methods: {
			initPicker(newVal) {
				if (!newVal || Array.isArray(newVal) && !newVal.length) {
					this.$nextTick(() => {
412 413
						this.clear(false)
					})
DCloud_JSON's avatar
DCloud_JSON 已提交
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
					return
				}
				if (!Array.isArray(newVal) && !this.isRange) {
					const {
						defDate,
						defTime
					} = this.parseDate(newVal)
					this.singleVal = defDate
					this.tempSingleDate = defDate
					this.defSingleDate = defDate
					if (this.hasTime) {
						this.singleVal = defDate + ' ' + defTime
						this.time = defTime
					}
				} else {
					const [before, after] = newVal
					if (!before && !after) return
					const defBefore = this.parseDate(before)
					const defAfter = this.parseDate(after)
					const startDate = defBefore.defDate
					const endDate = defAfter.defDate
					this.range.startDate = this.tempRange.startDate = startDate
					this.range.endDate = this.tempRange.endDate = endDate

					if (this.hasTime) {
						this.range.startDate = defBefore.defDate + ' ' + defBefore.defTime
						this.range.endDate = defAfter.defDate + ' ' + defAfter.defTime
						this.tempRange.startTime = defBefore.defTime
						this.tempRange.endTime = defAfter.defTime
					}
					const defaultRange = {
						before: defBefore.defDate,
						after: defAfter.defDate
					}
					this.startMultipleStatus = Object.assign({}, this.startMultipleStatus, defaultRange, {
						which: 'right'
					})
					this.endMultipleStatus = Object.assign({}, this.endMultipleStatus, defaultRange, {
						which: 'left'
study夏羽's avatar
update  
study夏羽 已提交
453
					})
DCloud_JSON's avatar
DCloud_JSON 已提交
454
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
455 456 457 458 459 460 461 462 463 464 465 466 467
			},
			updateLeftCale(e) {
				const left = this.$refs.left
				// 设置范围选
				left.cale.setHoverMultiple(e.after)
				left.setDate(this.$refs.left.nowDate.fullDate)
			},
			updateRightCale(e) {
				const right = this.$refs.right
				// 设置范围选
				right.cale.setHoverMultiple(e.after)
				right.setDate(this.$refs.right.nowDate.fullDate)
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
468 469 470 471
			platform() {
				const systemInfo = uni.getSystemInfoSync()
				this.isPhone = systemInfo.windowWidth <= 500
				this.windowWidth = systemInfo.windowWidth
DCloud_JSON's avatar
DCloud_JSON 已提交
472 473 474 475
			},
			show(event) {
				if (this.disabled) {
					return
DCloud_JSON's avatar
DCloud_JSON 已提交
476
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
477 478 479 480 481 482 483 484 485 486 487 488
				this.platform()
				if (this.isPhone) {
					this.$refs.mobile.open()
					return
				}
				this.popover = {
					top: '10px'
				}
				const dateEditor = uni.createSelectorQuery().in(this).select(".uni-date-editor")
				dateEditor.boundingClientRect(rect => {
					if (this.windowWidth - rect.left < this.datePopupWidth) {
						this.popover.right = 0
芊里 已提交
489
					}
DCloud_JSON's avatar
DCloud_JSON 已提交
490
				}).exec()
DCloud_JSON's avatar
DCloud_JSON 已提交
491
				setTimeout(() => {
DCloud_JSON's avatar
DCloud_JSON 已提交
492
					this.popup = !this.popup
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
					if (!this.isPhone && this.isRange && this.isFirstShow) {
						this.isFirstShow = false
						const {
							startDate,
							endDate
						} = this.range
						if (startDate && endDate) {
							if (this.diffDate(startDate, endDate) < 30) {
								this.$refs.right.next()
							}
						} else {
							this.$refs.right.next()
							this.$refs.right.cale.lastHover = false
						}
					}

study夏羽's avatar
update  
study夏羽 已提交
509
				}, 50)
DCloud_JSON's avatar
DCloud_JSON 已提交
510 511
			},

DCloud_JSON's avatar
DCloud_JSON 已提交
512
			close() {
DCloud_JSON's avatar
DCloud_JSON 已提交
513 514
				setTimeout(() => {
					this.popup = false
515 516
					this.$emit('maskClick', this.value)
					this.$refs.mobile.close()
DCloud_JSON's avatar
DCloud_JSON 已提交
517 518
				}, 20)
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
519
			setEmit(value) {
DCloud_JSON's avatar
DCloud_JSON 已提交
520 521 522 523 524
				if (this.returnType === "timestamp" || this.returnType === "date") {
					if (!Array.isArray(value)) {
						if (!this.hasTime) {
							value = value + ' ' + '00:00:00'
						}
DCloud_JSON's avatar
DCloud_JSON 已提交
525 526 527 528
						value = this.createTimestamp(value)
						if (this.returnType === "date") {
							value = new Date(value)
						}
芊里 已提交
529
					} else {
DCloud_JSON's avatar
DCloud_JSON 已提交
530 531 532 533 534
						if (!this.hasTime) {
							value[0] = value[0] + ' ' + '00:00:00'
							value[1] = value[1] + ' ' + '00:00:00'
						}
						value[0] = this.createTimestamp(value[0])
DCloud_JSON's avatar
DCloud_JSON 已提交
535 536 537 538
						value[1] = this.createTimestamp(value[1])
						if (this.returnType === "date") {
							value[0] = new Date(value[0])
							value[1] = new Date(value[1])
DCloud_JSON's avatar
DCloud_JSON 已提交
539
						}
芊里 已提交
540 541
					}
				}
542 543


DCloud_JSON's avatar
DCloud_JSON 已提交
544
				this.$emit('change', value)
DCloud_JSON's avatar
DCloud_JSON 已提交
545
				this.$emit('input', value)
546
				this.$emit('update:modelValue', value)
DCloud_JSON's avatar
DCloud_JSON 已提交
547
				this.isEmitValue = true
芊里 已提交
548
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
549 550 551
			createTimestamp(date) {
				date = this.fixIosDateFormat(date)
				return Date.parse(new Date(date))
芊里 已提交
552
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
553 554 555 556
			singleChange(e) {
				this.tempSingleDate = e.fulldate
				if (this.hasTime) return
				this.confirmSingleChange()
芊里 已提交
557 558
			},

DCloud_JSON's avatar
DCloud_JSON 已提交
559 560 561 562
			confirmSingleChange() {
				if (!this.tempSingleDate) {
					this.popup = false
					return
芊里 已提交
563
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
564 565
				if (this.hasTime) {
					this.singleVal = this.tempSingleDate + ' ' + (this.time ? this.time : '00:00:00')
芊里 已提交
566
				} else {
DCloud_JSON's avatar
DCloud_JSON 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
					this.singleVal = this.tempSingleDate
				}
				this.setEmit(this.singleVal)
				this.popup = false
			},

			leftChange(e) {
				const {
					before,
					after
				} = e.range
				this.rangeChange(before, after)
				const obj = {
					before: e.range.before,
					after: e.range.after,
					data: e.range.data,
					fulldate: e.fulldate
				}
				this.startMultipleStatus = Object.assign({}, this.startMultipleStatus, obj)
			},

			rightChange(e) {
				const {
					before,
					after
				} = e.range
				this.rangeChange(before, after)
				const obj = {
					before: e.range.before,
					after: e.range.after,
					data: e.range.data,
					fulldate: e.fulldate
				}
				this.endMultipleStatus = Object.assign({}, this.endMultipleStatus, obj)
			},

			mobileChange(e) {
				if (this.isRange) {
					const {
						before,
						after
					} = e.range
					this.handleStartAndEnd(before, after, true)
					if (this.hasTime) {
						const {
							startTime,
							endTime
						} = e.timeRange
						this.tempRange.startTime = startTime
						this.tempRange.endTime = endTime
					}
					this.confirmRangeChange()
芊里 已提交
619

DCloud_JSON's avatar
DCloud_JSON 已提交
620 621 622
				} else {
					if (this.hasTime) {
						this.singleVal = e.fulldate + ' ' + e.time
芊里 已提交
623
					} else {
DCloud_JSON's avatar
DCloud_JSON 已提交
624
						this.singleVal = e.fulldate
芊里 已提交
625
					}
DCloud_JSON's avatar
DCloud_JSON 已提交
626
					this.setEmit(this.singleVal)
芊里 已提交
627
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
628 629
				this.$refs.mobile.close()
			},
芊里 已提交
630

DCloud_JSON's avatar
DCloud_JSON 已提交
631 632 633 634 635
			rangeChange(before, after) {
				if (!(before && after)) return
				this.handleStartAndEnd(before, after, true)
				if (this.hasTime) return
				this.confirmRangeChange()
芊里 已提交
636 637
			},

DCloud_JSON's avatar
DCloud_JSON 已提交
638 639 640 641
			confirmRangeChange() {
				if (!this.tempRange.startDate && !this.tempRange.endDate) {
					this.popup = false
					return
芊里 已提交
642
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
				let start, end
				if (!this.hasTime) {
					start = this.range.startDate = this.tempRange.startDate
					end = this.range.endDate = this.tempRange.endDate
				} else {
					start = this.range.startDate = this.tempRange.startDate + ' ' +
						(this.tempRange.startTime ? this.tempRange.startTime : '00:00:00')
					end = this.range.endDate = this.tempRange.endDate + ' ' +
						(this.tempRange.endTime ? this.tempRange.endTime : '00:00:00')
				}
				const displayRange = [start, end]
				this.setEmit(displayRange)
				this.popup = false
			},

			handleStartAndEnd(before, after, temp = false) {
				if (!(before && after)) return
				const type = temp ? 'tempRange' : 'range'
				if (this.dateCompare(before, after)) {
					this[type].startDate = before
					this[type].endDate = after
				} else {
					this[type].startDate = after
					this[type].endDate = before
芊里 已提交
667 668 669 670
				}
			},

			/**
DCloud_JSON's avatar
DCloud_JSON 已提交
671
			 * 比较时间大小
芊里 已提交
672
			 */
DCloud_JSON's avatar
DCloud_JSON 已提交
673 674 675 676 677 678 679
			dateCompare(startDate, endDate) {
				// 计算截止时间
				startDate = new Date(startDate.replace('-', '/').replace('-', '/'))
				// 计算详细项的截止时间
				endDate = new Date(endDate.replace('-', '/').replace('-', '/'))
				if (startDate <= endDate) {
					return true
芊里 已提交
680
				} else {
DCloud_JSON's avatar
DCloud_JSON 已提交
681
					return false
芊里 已提交
682 683 684 685
				}
			},

			/**
DCloud_JSON's avatar
DCloud_JSON 已提交
686
			 * 比较时间差
芊里 已提交
687
			 */
DCloud_JSON's avatar
DCloud_JSON 已提交
688 689 690 691 692 693 694 695 696 697 698 699
			diffDate(startDate, endDate) {
				// 计算截止时间
				startDate = new Date(startDate.replace('-', '/').replace('-', '/'))
				// 计算详细项的截止时间
				endDate = new Date(endDate.replace('-', '/').replace('-', '/'))
				const diff = (endDate - startDate) / (24 * 60 * 60 * 1000)
				return Math.abs(diff)
			},

			clear(needEmit = true) {
				if (!this.isRange) {
					this.singleVal = ''
700 701
					this.tempSingleDate = ''
					this.time = ''
DCloud_JSON's avatar
DCloud_JSON 已提交
702
					if (this.isPhone) {
study夏羽's avatar
update  
study夏羽 已提交
703
						this.$refs.mobile && this.$refs.mobile.clearCalender()
DCloud_JSON's avatar
DCloud_JSON 已提交
704
					} else {
study夏羽's avatar
update  
study夏羽 已提交
705
						this.$refs.pcSingle && this.$refs.pcSingle.clearCalender()
DCloud_JSON's avatar
DCloud_JSON 已提交
706
					}
DCloud_JSON's avatar
DCloud_JSON 已提交
707
					if (needEmit) {
708 709 710 711 712 713 714 715 716
						// 校验规则
						// if(this.form  && this.formItem){
						// 	const {
						// 		validateTrigger
						// 	} = this.form
						// 	if (validateTrigger === 'blur') {
						// 		this.formItem.onFieldChange()
						// 	}
						// }
DCloud_JSON's avatar
DCloud_JSON 已提交
717
						this.$emit('change', '')
718
						this.$emit('input', '')
719
						this.$emit('update:modelValue', '')
芊里 已提交
720
					}
DCloud_JSON's avatar
DCloud_JSON 已提交
721 722 723
				} else {
					this.range.startDate = ''
					this.range.endDate = ''
study夏羽's avatar
update  
study夏羽 已提交
724 725 726 727
					this.tempRange.startDate = ''
					this.tempRange.startTime = ''
					this.tempRange.endDate = ''
					this.tempRange.endTime = ''
DCloud_JSON's avatar
DCloud_JSON 已提交
728
					if (this.isPhone) {
study夏羽's avatar
update  
study夏羽 已提交
729
						this.$refs.mobile && this.$refs.mobile.clearCalender()
DCloud_JSON's avatar
DCloud_JSON 已提交
730
					} else {
study夏羽's avatar
update  
study夏羽 已提交
731 732 733
						this.$refs.left && this.$refs.left.clearCalender()
						this.$refs.right && this.$refs.right.clearCalender()
						this.$refs.right && this.$refs.right.next()
DCloud_JSON's avatar
DCloud_JSON 已提交
734
					}
DCloud_JSON's avatar
DCloud_JSON 已提交
735 736
					if (needEmit) {
						this.$emit('change', [])
737
						this.$emit('input', [])
738
						this.$emit('update:modelValue', [])
芊里 已提交
739 740 741 742
					}
				}
			},

DCloud_JSON's avatar
DCloud_JSON 已提交
743 744 745 746 747 748 749 750 751 752
			parseDate(date) {
				date = this.fixIosDateFormat(date)
				const defVal = new Date(date)
				const year = defVal.getFullYear()
				const month = defVal.getMonth() + 1
				const day = defVal.getDate()
				const hour = defVal.getHours()
				const minute = defVal.getMinutes()
				const second = defVal.getSeconds()
				const defDate = year + '-' + this.lessTen(month) + '-' + this.lessTen(day)
study夏羽's avatar
update  
study夏羽 已提交
753 754
				const defTime = this.lessTen(hour) + ':' + this.lessTen(minute) + (this.hideSecond ? '' : (':' + this
					.lessTen(second)))
DCloud_JSON's avatar
DCloud_JSON 已提交
755 756 757
				return {
					defDate,
					defTime
芊里 已提交
758 759 760
				}
			},

DCloud_JSON's avatar
DCloud_JSON 已提交
761 762
			lessTen(item) {
				return item < 10 ? '0' + item : item
芊里 已提交
763 764 765 766 767 768 769 770 771 772
			},

			//兼容 iOS、safari 日期格式
			fixIosDateFormat(value) {
				if (typeof value === 'string') {
					value = value.replace(/-/g, '/')
				}
				return value
			},

DCloud_JSON's avatar
DCloud_JSON 已提交
773 774
			leftMonthSwitch(e) {
				// console.log('leftMonthSwitch 返回:', e)
芊里 已提交
775
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
776 777
			rightMonthSwitch(e) {
				// console.log('rightMonthSwitch 返回:', e)
芊里 已提交
778 779 780 781 782
			}
		}
	}
</script>

783 784
<style lang="scss">
	$uni-primary: #007aff !default;
785

786 787 788 789 790 791
	.uni-date {
		/* #ifndef APP-NVUE */
		width: 100%;
		/* #endif */
		flex: 1;
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
792 793 794 795 796 797 798 799 800 801
	.uni-date-x {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		padding: 0 10px;
		border-radius: 4px;
		background-color: #fff;
		color: #666;
		font-size: 14px;
802
		flex: 1;
芊里 已提交
803 804
	}

DCloud_JSON's avatar
DCloud_JSON 已提交
805 806 807
	.uni-date-x--border {
		box-sizing: border-box;
		border-radius: 4px;
808
		border: 1px solid #e5e5e5;
芊里 已提交
809 810
	}

DCloud_JSON's avatar
DCloud_JSON 已提交
811
	.uni-date-editor--x {
812 813
		display: flex;
		align-items: center;
DCloud_JSON's avatar
DCloud_JSON 已提交
814
		position: relative;
芊里 已提交
815 816
	}

DCloud_JSON's avatar
DCloud_JSON 已提交
817
	.uni-date-editor--x .uni-date__icon-clear {
818 819 820
		padding: 0 5px;
		display: flex;
		align-items: center;
DCloud_JSON's avatar
DCloud_JSON 已提交
821
		/* #ifdef H5 */
芊里 已提交
822 823 824 825
		cursor: pointer;
		/* #endif */
	}

826 827
	.uni-date__x-input {
		padding: 0 8px;
828 829 830 831 832 833 834
		/* #ifndef APP-NVUE */
		width: auto;
		/* #endif */
		position: relative;
		overflow: hidden;
		flex: 1;
		line-height: 1;
835
		font-size: 14px;
836
		height: 35px;
837 838 839 840
	}

	.t-c {
		text-align: center;
841 842
	}

DCloud_JSON's avatar
DCloud_JSON 已提交
843 844 845 846
	.uni-date__input {
		height: 40px;
		width: 100%;
		line-height: 40px;
芊里 已提交
847 848 849
		font-size: 14px;
	}

DCloud_JSON's avatar
DCloud_JSON 已提交
850
	.uni-date-range__input {
851 852
		text-align: center;
		max-width: 142px;
芊里 已提交
853 854
	}

DCloud_JSON's avatar
DCloud_JSON 已提交
855 856 857 858 859 860 861 862 863 864
	.uni-date-picker__container {
		position: relative;
		/* 		position: fixed;
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
		box-sizing: border-box;
		z-index: 996;
		font-size: 14px; */
芊里 已提交
865 866
	}

DCloud_JSON's avatar
DCloud_JSON 已提交
867
	.uni-date-mask {
芊里 已提交
868 869 870 871 872
		position: fixed;
		bottom: 0px;
		top: 0px;
		left: 0px;
		right: 0px;
DCloud_JSON's avatar
DCloud_JSON 已提交
873
		background-color: rgba(0, 0, 0, 0);
芊里 已提交
874
		transition-duration: 0.3s;
DCloud_JSON's avatar
DCloud_JSON 已提交
875
		z-index: 996;
芊里 已提交
876 877
	}

DCloud_JSON's avatar
DCloud_JSON 已提交
878 879
	.uni-date-single--x {
		/* padding: 0 8px; */
880
		background-color: #fff;
DCloud_JSON's avatar
DCloud_JSON 已提交
881 882 883
		position: absolute;
		top: 0;
		z-index: 999;
study夏羽's avatar
update  
study夏羽 已提交
884 885
		border: 1px solid #EBEEF5;
		box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
DCloud_JSON's avatar
DCloud_JSON 已提交
886 887 888 889
		border-radius: 4px;
	}

	.uni-date-range--x {
890
		/* padding: 0 8px; */
芊里 已提交
891
		background-color: #fff;
DCloud_JSON's avatar
DCloud_JSON 已提交
892 893
		position: absolute;
		top: 0;
芊里 已提交
894
		z-index: 999;
study夏羽's avatar
update  
study夏羽 已提交
895 896
		border: 1px solid #EBEEF5;
		box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
DCloud_JSON's avatar
DCloud_JSON 已提交
897
		border-radius: 4px;
芊里 已提交
898 899
	}

DCloud_JSON's avatar
DCloud_JSON 已提交
900 901 902
	.uni-date-editor--x__disabled {
		opacity: 0.4;
		cursor: default;
芊里 已提交
903 904
	}

DCloud_JSON's avatar
DCloud_JSON 已提交
905 906 907 908 909
	.uni-date-editor--logo {
		width: 16px;
		height: 16px;
		vertical-align: middle;
	}
芊里 已提交
910

DCloud_JSON's avatar
DCloud_JSON 已提交
911 912
	/* 添加时间 */
	.popup-x-header {
芊里 已提交
913
		/* #ifndef APP-NVUE */
DCloud_JSON's avatar
DCloud_JSON 已提交
914
		display: flex;
芊里 已提交
915
		/* #endif */
DCloud_JSON's avatar
DCloud_JSON 已提交
916 917
		flex-direction: row;
		/* justify-content: space-between; */
芊里 已提交
918 919
	}

DCloud_JSON's avatar
DCloud_JSON 已提交
920
	.popup-x-header--datetime {
芊里 已提交
921
		/* #ifndef APP-NVUE */
DCloud_JSON's avatar
DCloud_JSON 已提交
922
		display: flex;
芊里 已提交
923
		/* #endif */
DCloud_JSON's avatar
DCloud_JSON 已提交
924 925
		flex-direction: row;
		flex: 1;
芊里 已提交
926 927
	}

DCloud_JSON's avatar
DCloud_JSON 已提交
928 929 930
	.popup-x-body {
		display: flex;
	}
芊里 已提交
931

DCloud_JSON's avatar
DCloud_JSON 已提交
932 933 934 935 936
	.popup-x-footer {
		padding: 0 15px;
		border-top-color: #F1F1F1;
		border-top-style: solid;
		border-top-width: 1px;
study夏羽's avatar
update  
study夏羽 已提交
937
		/* background-color: #fff; */
DCloud_JSON's avatar
DCloud_JSON 已提交
938 939 940
		line-height: 40px;
		text-align: right;
		color: #666;
芊里 已提交
941 942
	}

DCloud_JSON's avatar
DCloud_JSON 已提交
943
	.popup-x-footer text:hover {
944
		color: $uni-primary;
DCloud_JSON's avatar
DCloud_JSON 已提交
945 946
		cursor: pointer;
		opacity: 0.8;
芊里 已提交
947 948
	}

DCloud_JSON's avatar
DCloud_JSON 已提交
949 950
	.popup-x-footer .confirm {
		margin-left: 20px;
951
		color: $uni-primary;
芊里 已提交
952 953
	}

DCloud_JSON's avatar
DCloud_JSON 已提交
954
	.uni-date-changed {
study夏羽's avatar
update  
study夏羽 已提交
955
		/* background-color: #fff; */
DCloud_JSON's avatar
DCloud_JSON 已提交
956 957 958 959 960 961
		text-align: center;
		color: #333;
		border-bottom-color: #F1F1F1;
		border-bottom-style: solid;
		border-bottom-width: 1px;
		/* padding: 0 50px; */
芊里 已提交
962 963
	}

DCloud_JSON's avatar
DCloud_JSON 已提交
964 965 966 967
	.uni-date-changed--time text {
		/* padding: 0 20px; */
		height: 50px;
		line-height: 50px;
芊里 已提交
968 969
	}

DCloud_JSON's avatar
DCloud_JSON 已提交
970 971 972
	.uni-date-changed .uni-date-changed--time {
		/* display: flex; */
		flex: 1;
芊里 已提交
973 974
	}

DCloud_JSON's avatar
DCloud_JSON 已提交
975 976 977
	.uni-date-changed--time-date {
		color: #333;
		opacity: 0.6;
芊里 已提交
978 979
	}

DCloud_JSON's avatar
DCloud_JSON 已提交
980 981
	.mr-50 {
		margin-right: 50px;
芊里 已提交
982
	}
983

study夏羽's avatar
update  
study夏羽 已提交
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
	/* picker 弹出层通用的指示小三角, todo:扩展至上下左右方向定位 */
	.uni-popper__arrow,
	.uni-popper__arrow::after {
		position: absolute;
		display: block;
		width: 0;
		height: 0;
		border-color: transparent;
		border-style: solid;
		border-width: 6px;
	}

	.uni-popper__arrow {
		filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
		top: -6px;
		left: 10%;
		margin-right: 3px;
		border-top-width: 0;
		border-bottom-color: #EBEEF5;
	}

	.uni-popper__arrow::after {
		content: " ";
		top: 1px;
		margin-left: -6px;
		border-top-width: 0;
		border-bottom-color: #fff;
1011
	}
1012
</style>