Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

uni-datetime-picker.vue 28KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. <template>
  2. <view class="uni-date">
  3. <view class="uni-date-editor" @click="show">
  4. <slot>
  5. <view class="uni-date-editor--x"
  6. :class="{'uni-date-editor--x__disabled': disabled,'uni-date-x--border': border}">
  7. <view v-if="!isRange" class="uni-date-x uni-date-single">
  8. <uni-icons class="icon-calendar" type="calendar" color="#c0c4cc" size="22"></uni-icons>
  9. <view class="uni-date__x-input">{{ displayValue || singlePlaceholderText }}</view>
  10. </view>
  11. <view v-else class="uni-date-x uni-date-range">
  12. <uni-icons class="icon-calendar" type="calendar" color="#c0c4cc" size="22"></uni-icons>
  13. <view class="uni-date__x-input text-center">
  14. {{ displayRangeValue.startDate || startPlaceholderText }}
  15. </view>
  16. <view class="range-separator">{{rangeSeparator}}</view>
  17. <view class="uni-date__x-input text-center">
  18. {{ displayRangeValue.endDate || endPlaceholderText }}
  19. </view>
  20. </view>
  21. <view v-if="showClearIcon" class="uni-date__icon-clear" @click.stop="clear">
  22. <uni-icons type="clear" color="#c0c4cc" size="22"></uni-icons>
  23. </view>
  24. </view>
  25. </slot>
  26. </view>
  27. <view v-show="pickerVisible" class="uni-date-mask--pc" @click="close"></view>
  28. <view v-if="!isPhone" v-show="pickerVisible" ref="datePicker" class="uni-date-picker__container">
  29. <view v-if="!isRange" class="uni-date-single--x" :style="pickerPositionStyle">
  30. <view class="uni-popper__arrow"></view>
  31. <view v-if="hasTime" class="uni-date-changed popup-x-header">
  32. <input class="uni-date__input text-center" type="text" v-model="inputDate"
  33. :placeholder="selectDateText" />
  34. <time-picker type="time" v-model="pickerTime" :border="false" :disabled="!inputDate"
  35. :start="timepickerStartTime" :end="timepickerEndTime" :hideSecond="hideSecond"
  36. style="width: 100%;">
  37. <input class="uni-date__input text-center" type="text" v-model="pickerTime"
  38. :placeholder="selectTimeText" :disabled="!inputDate" />
  39. </time-picker>
  40. </view>
  41. <Calendar ref="pcSingle" :showMonth="false" :start-date="calendarRange.startDate"
  42. :end-date="calendarRange.endDate" :date="calendarDate" @change="singleChange"
  43. :default-value="defaultValue" style="padding: 0 8px;" />
  44. <view v-if="hasTime" class="popup-x-footer">
  45. <text class="confirm-text" @click="confirmSingleChange">{{okText}}</text>
  46. </view>
  47. </view>
  48. <view v-else class="uni-date-range--x" :style="pickerPositionStyle">
  49. <view class="uni-popper__arrow"></view>
  50. <view v-if="hasTime" class="popup-x-header uni-date-changed">
  51. <view class="popup-x-header--datetime">
  52. <input class="uni-date__input uni-date-range__input" type="text" v-model="tempRange.startDate"
  53. :placeholder="startDateText" />
  54. <time-picker type="time" v-model="tempRange.startTime" :start="timepickerStartTime"
  55. :border="false" :disabled="!tempRange.startDate" :hideSecond="hideSecond">
  56. <input class="uni-date__input uni-date-range__input" type="text"
  57. v-model="tempRange.startTime" :placeholder="startTimeText"
  58. :disabled="!tempRange.startDate" />
  59. </time-picker>
  60. </view>
  61. <uni-icons type="arrowthinright" color="#999" style="line-height: 40px;"></uni-icons>
  62. <view class="popup-x-header--datetime">
  63. <input class="uni-date__input uni-date-range__input" type="text" v-model="tempRange.endDate"
  64. :placeholder="endDateText" />
  65. <time-picker type="time" v-model="tempRange.endTime" :end="timepickerEndTime" :border="false"
  66. :disabled="!tempRange.endDate" :hideSecond="hideSecond">
  67. <input class="uni-date__input uni-date-range__input" type="text" v-model="tempRange.endTime"
  68. :placeholder="endTimeText" :disabled="!tempRange.endDate" />
  69. </time-picker>
  70. </view>
  71. </view>
  72. <view class="popup-x-body">
  73. <Calendar ref="left" :showMonth="false" :start-date="calendarRange.startDate"
  74. :end-date="calendarRange.endDate" :range="true" :pleStatus="endMultipleStatus"
  75. @change="leftChange" @firstEnterCale="updateRightCale" style="padding: 0 8px;" />
  76. <Calendar ref="right" :showMonth="false" :start-date="calendarRange.startDate"
  77. :end-date="calendarRange.endDate" :range="true" @change="rightChange"
  78. :pleStatus="startMultipleStatus" @firstEnterCale="updateLeftCale"
  79. style="padding: 0 8px;border-left: 1px solid #F1F1F1;" />
  80. </view>
  81. <view v-if="hasTime" class="popup-x-footer">
  82. <text @click="clear">{{clearText}}</text>
  83. <text class="confirm-text" @click="confirmRangeChange">{{okText}}</text>
  84. </view>
  85. </view>
  86. </view>
  87. <Calendar v-if="isPhone" ref="mobile" :clearDate="false" :date="calendarDate" :defTime="mobileCalendarTime"
  88. :start-date="calendarRange.startDate" :end-date="calendarRange.endDate" :selectableTimes="mobSelectableTime"
  89. :startPlaceholder="startPlaceholder" :endPlaceholder="endPlaceholder" :default-value="defaultValue"
  90. :pleStatus="endMultipleStatus" :showMonth="false" :range="isRange" :hasTime="hasTime" :insert="false"
  91. :hideSecond="hideSecond" @confirm="mobileChange" @maskClose="close" />
  92. </view>
  93. </template>
  94. <script>
  95. /**
  96. * DatetimePicker 时间选择器
  97. * @description 同时支持 PC 和移动端使用日历选择日期和日期范围
  98. * @tutorial https://ext.dcloud.net.cn/plugin?id=3962
  99. * @property {String} type 选择器类型
  100. * @property {String|Number|Array|Date} value 绑定值
  101. * @property {String} placeholder 单选择时的占位内容
  102. * @property {String} start 起始时间
  103. * @property {String} end 终止时间
  104. * @property {String} start-placeholder 范围选择时开始日期的占位内容
  105. * @property {String} end-placeholder 范围选择时结束日期的占位内容
  106. * @property {String} range-separator 选择范围时的分隔符
  107. * @property {Boolean} border = [true|false] 是否有边框
  108. * @property {Boolean} disabled = [true|false] 是否禁用
  109. * @property {Boolean} clearIcon = [true|false] 是否显示清除按钮(仅PC端适用)
  110. * @property {[String} defaultValue 选择器打开时默认显示的时间
  111. * @event {Function} change 确定日期时触发的事件
  112. * @event {Function} maskClick 点击遮罩层触发的事件
  113. * @event {Function} show 打开弹出层
  114. * @event {Function} close 关闭弹出层
  115. * @event {Function} clear 清除上次选中的状态和值
  116. **/
  117. import Calendar from './calendar.vue'
  118. import TimePicker from './time-picker.vue'
  119. import {
  120. initVueI18n
  121. } from '@dcloudio/uni-i18n'
  122. import i18nMessages from './i18n/index.js'
  123. import {
  124. getDateTime,
  125. getDate,
  126. getTime,
  127. getDefaultSecond,
  128. dateCompare,
  129. checkDate,
  130. fixIosDateFormat
  131. } from './util'
  132. export default {
  133. name: 'UniDatetimePicker',
  134. options: {
  135. virtualHost: true
  136. },
  137. components: {
  138. Calendar,
  139. TimePicker
  140. },
  141. data() {
  142. return {
  143. isRange: false,
  144. hasTime: false,
  145. displayValue: '',
  146. inputDate: '',
  147. calendarDate: '',
  148. pickerTime: '',
  149. calendarRange: {
  150. startDate: '',
  151. startTime: '',
  152. endDate: '',
  153. endTime: ''
  154. },
  155. displayRangeValue: {
  156. startDate: '',
  157. endDate: '',
  158. },
  159. tempRange: {
  160. startDate: '',
  161. startTime: '',
  162. endDate: '',
  163. endTime: ''
  164. },
  165. // 左右日历同步数据
  166. startMultipleStatus: {
  167. before: '',
  168. after: '',
  169. data: [],
  170. fulldate: ''
  171. },
  172. endMultipleStatus: {
  173. before: '',
  174. after: '',
  175. data: [],
  176. fulldate: ''
  177. },
  178. pickerVisible: false,
  179. pickerPositionStyle: null,
  180. isEmitValue: false,
  181. isPhone: false,
  182. isFirstShow: true,
  183. i18nT: () => {}
  184. }
  185. },
  186. props: {
  187. type: {
  188. type: String,
  189. default: 'datetime'
  190. },
  191. value: {
  192. type: [String, Number, Array, Date],
  193. default: ''
  194. },
  195. modelValue: {
  196. type: [String, Number, Array, Date],
  197. default: ''
  198. },
  199. start: {
  200. type: [Number, String],
  201. default: ''
  202. },
  203. end: {
  204. type: [Number, String],
  205. default: ''
  206. },
  207. returnType: {
  208. type: String,
  209. default: 'string'
  210. },
  211. placeholder: {
  212. type: String,
  213. default: ''
  214. },
  215. startPlaceholder: {
  216. type: String,
  217. default: ''
  218. },
  219. endPlaceholder: {
  220. type: String,
  221. default: ''
  222. },
  223. rangeSeparator: {
  224. type: String,
  225. default: '-'
  226. },
  227. border: {
  228. type: [Boolean],
  229. default: true
  230. },
  231. disabled: {
  232. type: [Boolean],
  233. default: false
  234. },
  235. clearIcon: {
  236. type: [Boolean],
  237. default: true
  238. },
  239. hideSecond: {
  240. type: [Boolean],
  241. default: false
  242. },
  243. defaultValue: {
  244. type: [String, Object, Array],
  245. default: ''
  246. }
  247. },
  248. watch: {
  249. type: {
  250. immediate: true,
  251. handler(newVal) {
  252. this.hasTime = newVal.indexOf('time') !== -1
  253. this.isRange = newVal.indexOf('range') !== -1
  254. }
  255. },
  256. // #ifndef VUE3
  257. value: {
  258. immediate: true,
  259. handler(newVal) {
  260. if (this.isEmitValue) {
  261. this.isEmitValue = false
  262. return
  263. }
  264. this.initPicker(newVal)
  265. }
  266. },
  267. // #endif
  268. // #ifdef VUE3
  269. modelValue: {
  270. immediate: true,
  271. handler(newVal) {
  272. if (this.isEmitValue) {
  273. this.isEmitValue = false
  274. return
  275. }
  276. this.initPicker(newVal)
  277. }
  278. },
  279. // #endif
  280. start: {
  281. immediate: true,
  282. handler(newVal) {
  283. if (!newVal) return
  284. this.calendarRange.startDate = getDate(newVal)
  285. if (this.hasTime) {
  286. this.calendarRange.startTime = getTime(newVal)
  287. }
  288. }
  289. },
  290. end: {
  291. immediate: true,
  292. handler(newVal) {
  293. if (!newVal) return
  294. this.calendarRange.endDate = getDate(newVal)
  295. if (this.hasTime) {
  296. this.calendarRange.endTime = getTime(newVal, this.hideSecond)
  297. }
  298. }
  299. },
  300. },
  301. computed: {
  302. timepickerStartTime() {
  303. const activeDate = this.isRange ? this.tempRange.startDate : this.inputDate
  304. return activeDate === this.calendarRange.startDate ? this.calendarRange.startTime : ''
  305. },
  306. timepickerEndTime() {
  307. const activeDate = this.isRange ? this.tempRange.endDate : this.inputDate
  308. return activeDate === this.calendarRange.endDate ? this.calendarRange.endTime : ''
  309. },
  310. mobileCalendarTime() {
  311. const timeRange = {
  312. start: this.tempRange.startTime,
  313. end: this.tempRange.endTime
  314. }
  315. return this.isRange ? timeRange : this.pickerTime
  316. },
  317. mobSelectableTime() {
  318. return {
  319. start: this.calendarRange.startTime,
  320. end: this.calendarRange.endTime
  321. }
  322. },
  323. datePopupWidth() {
  324. // todo
  325. return this.isRange ? 653 : 301
  326. },
  327. /**
  328. * for i18n
  329. */
  330. singlePlaceholderText() {
  331. return this.placeholder || (this.type === 'date' ? this.selectDateText : this.selectDateTimeText)
  332. },
  333. startPlaceholderText() {
  334. return this.startPlaceholder || this.startDateText
  335. },
  336. endPlaceholderText() {
  337. return this.endPlaceholder || this.endDateText
  338. },
  339. selectDateText() {
  340. return this.i18nT("uni-datetime-picker.selectDate")
  341. },
  342. selectDateTimeText() {
  343. return this.i18nT("uni-datetime-picker.selectDateTime")
  344. },
  345. selectTimeText() {
  346. return this.i18nT("uni-datetime-picker.selectTime")
  347. },
  348. startDateText() {
  349. return this.startPlaceholder || this.i18nT("uni-datetime-picker.startDate")
  350. },
  351. startTimeText() {
  352. return this.i18nT("uni-datetime-picker.startTime")
  353. },
  354. endDateText() {
  355. return this.endPlaceholder || this.i18nT("uni-datetime-picker.endDate")
  356. },
  357. endTimeText() {
  358. return this.i18nT("uni-datetime-picker.endTime")
  359. },
  360. okText() {
  361. return this.i18nT("uni-datetime-picker.ok")
  362. },
  363. clearText() {
  364. return this.i18nT("uni-datetime-picker.clear")
  365. },
  366. showClearIcon() {
  367. return this.clearIcon && !this.disabled && (this.displayValue || (this.displayRangeValue.startDate && this
  368. .displayRangeValue.endDate))
  369. }
  370. },
  371. created() {
  372. this.initI18nT()
  373. this.platform()
  374. },
  375. methods: {
  376. initI18nT() {
  377. const vueI18n = initVueI18n(i18nMessages)
  378. this.i18nT = vueI18n.t
  379. },
  380. initPicker(newVal) {
  381. if ((!newVal && !this.defaultValue) || Array.isArray(newVal) && !newVal.length) {
  382. this.$nextTick(() => {
  383. this.clear(false)
  384. })
  385. return
  386. }
  387. if (!Array.isArray(newVal) && !this.isRange) {
  388. if (newVal) {
  389. this.displayValue = this.inputDate = this.calendarDate = getDate(newVal)
  390. if (this.hasTime) {
  391. this.pickerTime = getTime(newVal, this.hideSecond)
  392. this.displayValue = `${this.displayValue} ${this.pickerTime}`
  393. }
  394. } else if (this.defaultValue) {
  395. this.inputDate = this.calendarDate = getDate(this.defaultValue)
  396. if (this.hasTime) {
  397. this.pickerTime = getTime(this.defaultValue, this.hideSecond)
  398. }
  399. }
  400. } else {
  401. const [before, after] = newVal
  402. if (!before && !after) return
  403. const beforeDate = getDate(before)
  404. const beforeTime = getTime(before, this.hideSecond)
  405. const afterDate = getDate(after)
  406. const afterTime = getTime(after, this.hideSecond)
  407. const startDate = beforeDate
  408. const endDate = afterDate
  409. this.displayRangeValue.startDate = this.tempRange.startDate = startDate
  410. this.displayRangeValue.endDate = this.tempRange.endDate = endDate
  411. if (this.hasTime) {
  412. this.displayRangeValue.startDate = `${beforeDate} ${beforeTime}`
  413. this.displayRangeValue.endDate = `${afterDate} ${afterTime}`
  414. this.tempRange.startTime = beforeTime
  415. this.tempRange.endTime = afterTime
  416. }
  417. const defaultRange = {
  418. before: beforeDate,
  419. after: afterDate
  420. }
  421. this.startMultipleStatus = Object.assign({}, this.startMultipleStatus, defaultRange, {
  422. which: 'right'
  423. })
  424. this.endMultipleStatus = Object.assign({}, this.endMultipleStatus, defaultRange, {
  425. which: 'left'
  426. })
  427. }
  428. },
  429. updateLeftCale(e) {
  430. const left = this.$refs.left
  431. // 设置范围选
  432. left.cale.setHoverMultiple(e.after)
  433. left.setDate(this.$refs.left.nowDate.fullDate)
  434. },
  435. updateRightCale(e) {
  436. const right = this.$refs.right
  437. // 设置范围选
  438. right.cale.setHoverMultiple(e.after)
  439. right.setDate(this.$refs.right.nowDate.fullDate)
  440. },
  441. platform() {
  442. const {
  443. windowWidth
  444. } = uni.getSystemInfoSync()
  445. this.isPhone = windowWidth <= 500
  446. this.windowWidth = windowWidth
  447. },
  448. show() {
  449. this.$emit('show')
  450. if (this.disabled) {
  451. return
  452. }
  453. this.platform()
  454. if (this.isPhone) {
  455. this.$refs.mobile.open()
  456. return
  457. }
  458. this.pickerPositionStyle = {
  459. top: '10px'
  460. }
  461. const dateEditor = uni.createSelectorQuery().in(this).select(".uni-date-editor")
  462. dateEditor.boundingClientRect(rect => {
  463. if (this.windowWidth - rect.left < this.datePopupWidth) {
  464. this.pickerPositionStyle.right = 0
  465. }
  466. }).exec()
  467. setTimeout(() => {
  468. this.pickerVisible = !this.pickerVisible
  469. if (!this.isPhone && this.isRange && this.isFirstShow) {
  470. this.isFirstShow = false
  471. const {
  472. startDate,
  473. endDate
  474. } = this.calendarRange
  475. if (startDate && endDate) {
  476. if (this.diffDate(startDate, endDate) < 30) {
  477. this.$refs.right.changeMonth('pre')
  478. }
  479. } else {
  480. this.$refs.right.changeMonth('next')
  481. this.$refs.right.cale.lastHover = false
  482. }
  483. }
  484. }, 50)
  485. },
  486. close() {
  487. this.$emit('close')
  488. setTimeout(() => {
  489. this.pickerVisible = false
  490. this.$emit('maskClick', this.value)
  491. this.$refs.mobile && this.$refs.mobile.close()
  492. }, 20)
  493. },
  494. setEmit(value) {
  495. if (this.returnType === "timestamp" || this.returnType === "date") {
  496. if (!Array.isArray(value)) {
  497. if (!this.hasTime) {
  498. value = value + ' ' + '00:00:00'
  499. }
  500. value = this.createTimestamp(value)
  501. if (this.returnType === "date") {
  502. value = new Date(value)
  503. }
  504. } else {
  505. if (!this.hasTime) {
  506. value[0] = value[0] + ' ' + '00:00:00'
  507. value[1] = value[1] + ' ' + '00:00:00'
  508. }
  509. value[0] = this.createTimestamp(value[0])
  510. value[1] = this.createTimestamp(value[1])
  511. if (this.returnType === "date") {
  512. value[0] = new Date(value[0])
  513. value[1] = new Date(value[1])
  514. }
  515. }
  516. }
  517. this.$emit('update:modelValue', value)
  518. this.$emit('input', value)
  519. this.$emit('change', value)
  520. this.isEmitValue = true
  521. },
  522. createTimestamp(date) {
  523. date = fixIosDateFormat(date)
  524. return Date.parse(new Date(date))
  525. },
  526. singleChange(e) {
  527. this.calendarDate = this.inputDate = e.fulldate
  528. if (this.hasTime) return
  529. this.confirmSingleChange()
  530. },
  531. confirmSingleChange() {
  532. if (!checkDate(this.inputDate)) {
  533. const now = new Date()
  534. this.calendarDate = this.inputDate = getDate(now)
  535. this.pickerTime = getTime(now, this.hideSecond)
  536. }
  537. let startLaterInputDate = false
  538. let startDate, startTime
  539. if (this.start) {
  540. let startString = this.start
  541. if (typeof this.start === 'number') {
  542. startString = getDateTime(this.start, this.hideSecond)
  543. }
  544. [startDate, startTime] = startString.split(' ')
  545. if (this.start && !dateCompare(startDate, this.inputDate)) {
  546. startLaterInputDate = true
  547. this.inputDate = startDate
  548. }
  549. }
  550. let endEarlierInputDate = false
  551. let endDate, endTime
  552. if (this.end) {
  553. let endString = this.end
  554. if (typeof this.end === 'number') {
  555. endString = getDateTime(this.end, this.hideSecond)
  556. }
  557. [endDate, endTime] = endString.split(' ')
  558. if (this.end && !dateCompare(this.inputDate, endDate)) {
  559. endEarlierInputDate = true
  560. this.inputDate = endDate
  561. }
  562. }
  563. if (this.hasTime) {
  564. if (startLaterInputDate) {
  565. this.pickerTime = startTime || getDefaultSecond(this.hideSecond)
  566. }
  567. if (endEarlierInputDate) {
  568. this.pickerTime = endTime || getDefaultSecond(this.hideSecond)
  569. }
  570. if (!this.pickerTime) {
  571. this.pickerTime = getTime(Date.now(), this.hideSecond)
  572. }
  573. this.displayValue = `${this.inputDate} ${this.pickerTime}`
  574. } else {
  575. this.displayValue = this.inputDate
  576. }
  577. this.setEmit(this.displayValue)
  578. this.pickerVisible = false
  579. },
  580. leftChange(e) {
  581. const {
  582. before,
  583. after
  584. } = e.range
  585. this.rangeChange(before, after)
  586. const obj = {
  587. before: e.range.before,
  588. after: e.range.after,
  589. data: e.range.data,
  590. fulldate: e.fulldate
  591. }
  592. this.startMultipleStatus = Object.assign({}, this.startMultipleStatus, obj)
  593. },
  594. rightChange(e) {
  595. const {
  596. before,
  597. after
  598. } = e.range
  599. this.rangeChange(before, after)
  600. const obj = {
  601. before: e.range.before,
  602. after: e.range.after,
  603. data: e.range.data,
  604. fulldate: e.fulldate
  605. }
  606. this.endMultipleStatus = Object.assign({}, this.endMultipleStatus, obj)
  607. },
  608. mobileChange(e) {
  609. this.$emit('close')
  610. if (this.isRange) {
  611. const {
  612. before,
  613. after
  614. } = e.range
  615. if (!before || !after) {
  616. return
  617. }
  618. this.handleStartAndEnd(before, after, true)
  619. if (this.hasTime) {
  620. const {
  621. startTime,
  622. endTime
  623. } = e.timeRange
  624. this.tempRange.startTime = startTime
  625. this.tempRange.endTime = endTime
  626. }
  627. this.confirmRangeChange()
  628. } else {
  629. if (this.hasTime) {
  630. this.displayValue = e.fulldate + ' ' + e.time
  631. } else {
  632. this.displayValue = e.fulldate
  633. }
  634. this.setEmit(this.displayValue)
  635. }
  636. this.$refs.mobile.close()
  637. },
  638. rangeChange(before, after) {
  639. if (!(before && after)) return
  640. this.handleStartAndEnd(before, after, true)
  641. if (this.hasTime) return
  642. this.confirmRangeChange()
  643. },
  644. confirmRangeChange() {
  645. if (!this.tempRange.startDate || !this.tempRange.endDate) {
  646. this.pickerVisible = false
  647. return
  648. }
  649. if (!checkDate(this.tempRange.startDate)) {
  650. this.tempRange.startDate = getDate(Date.now())
  651. }
  652. if (!checkDate(this.tempRange.endDate)) {
  653. this.tempRange.endDate = getDate(Date.now())
  654. }
  655. let start, end
  656. let startDateLaterRangeStartDate = false
  657. let startDateLaterRangeEndDate = false
  658. let startDate, startTime
  659. if (this.start) {
  660. let startString = this.start
  661. if (typeof this.start === 'number') {
  662. startString = getDateTime(this.start, this.hideSecond)
  663. }
  664. [startDate, startTime] = startString.split(' ')
  665. if (this.start && !dateCompare(this.start, this.tempRange.startDate)) {
  666. startDateLaterRangeStartDate = true
  667. this.tempRange.startDate = startDate
  668. }
  669. if (this.start && !dateCompare(this.start, this.tempRange.endDate)) {
  670. startDateLaterRangeEndDate = true
  671. this.tempRange.endDate = startDate
  672. }
  673. }
  674. let endDateEarlierRangeStartDate = false
  675. let endDateEarlierRangeEndDate = false
  676. let endDate, endTime
  677. if (this.end) {
  678. let endString = this.end
  679. if (typeof this.end === 'number') {
  680. endString = getDateTime(this.end, this.hideSecond)
  681. }
  682. [endDate, endTime] = endString.split(' ')
  683. if (this.end && !dateCompare(this.tempRange.startDate, this.end)) {
  684. endDateEarlierRangeStartDate = true
  685. this.tempRange.startDate = endDate
  686. }
  687. if (this.end && !dateCompare(this.tempRange.endDate, this.end)) {
  688. endDateEarlierRangeEndDate = true
  689. this.tempRange.endDate = endDate
  690. }
  691. }
  692. if (!this.hasTime) {
  693. start = this.displayRangeValue.startDate = this.tempRange.startDate
  694. end = this.displayRangeValue.endDate = this.tempRange.endDate
  695. } else {
  696. if (startDateLaterRangeStartDate) {
  697. this.tempRange.startTime = startTime || getDefaultSecond(this.hideSecond)
  698. } else if (endDateEarlierRangeStartDate) {
  699. this.tempRange.startTime = endTime || getDefaultSecond(this.hideSecond)
  700. }
  701. if (!this.tempRange.startTime) {
  702. this.tempRange.startTime = getTime(Date.now(), this.hideSecond)
  703. }
  704. if (startDateLaterRangeEndDate) {
  705. this.tempRange.endTime = startTime || getDefaultSecond(this.hideSecond)
  706. } else if (endDateEarlierRangeEndDate) {
  707. this.tempRange.endTime = endTime || getDefaultSecond(this.hideSecond)
  708. }
  709. if (!this.tempRange.endTime) {
  710. this.tempRange.endTime = getTime(Date.now(), this.hideSecond)
  711. }
  712. start = this.displayRangeValue.startDate = `${this.tempRange.startDate} ${this.tempRange.startTime}`
  713. end = this.displayRangeValue.endDate = `${this.tempRange.endDate} ${this.tempRange.endTime}`
  714. }
  715. if (!dateCompare(start, end)) {
  716. [start, end] = [end, start]
  717. }
  718. this.displayRangeValue.startDate = start
  719. this.displayRangeValue.endDate = end
  720. const displayRange = [start, end]
  721. this.setEmit(displayRange)
  722. this.pickerVisible = false
  723. },
  724. handleStartAndEnd(before, after, temp = false) {
  725. if (!(before && after)) return
  726. const type = temp ? 'tempRange' : 'range'
  727. const isStartEarlierEnd = dateCompare(before, after)
  728. this[type].startDate = isStartEarlierEnd ? before : after
  729. this[type].endDate = isStartEarlierEnd ? after : before
  730. },
  731. /**
  732. * 比较时间大小
  733. */
  734. dateCompare(startDate, endDate) {
  735. // 计算截止时间
  736. startDate = new Date(startDate.replace('-', '/').replace('-', '/'))
  737. // 计算详细项的截止时间
  738. endDate = new Date(endDate.replace('-', '/').replace('-', '/'))
  739. return startDate <= endDate
  740. },
  741. /**
  742. * 比较时间差
  743. */
  744. diffDate(startDate, endDate) {
  745. // 计算截止时间
  746. startDate = new Date(startDate.replace('-', '/').replace('-', '/'))
  747. // 计算详细项的截止时间
  748. endDate = new Date(endDate.replace('-', '/').replace('-', '/'))
  749. const diff = (endDate - startDate) / (24 * 60 * 60 * 1000)
  750. return Math.abs(diff)
  751. },
  752. clear(needEmit = true) {
  753. if (!this.isRange) {
  754. this.displayValue = ''
  755. this.inputDate = ''
  756. this.pickerTime = ''
  757. if (this.isPhone) {
  758. this.$refs.mobile && this.$refs.mobile.clearCalender()
  759. } else {
  760. this.$refs.pcSingle && this.$refs.pcSingle.clearCalender()
  761. }
  762. if (needEmit) {
  763. this.$emit('change', '')
  764. this.$emit('input', '')
  765. this.$emit('update:modelValue', '')
  766. }
  767. } else {
  768. this.displayRangeValue.startDate = ''
  769. this.displayRangeValue.endDate = ''
  770. this.tempRange.startDate = ''
  771. this.tempRange.startTime = ''
  772. this.tempRange.endDate = ''
  773. this.tempRange.endTime = ''
  774. if (this.isPhone) {
  775. this.$refs.mobile && this.$refs.mobile.clearCalender()
  776. } else {
  777. this.$refs.left && this.$refs.left.clearCalender()
  778. this.$refs.right && this.$refs.right.clearCalender()
  779. this.$refs.right && this.$refs.right.changeMonth('next')
  780. }
  781. if (needEmit) {
  782. this.$emit('change', [])
  783. this.$emit('input', [])
  784. this.$emit('update:modelValue', [])
  785. }
  786. }
  787. }
  788. }
  789. }
  790. </script>
  791. <style lang="scss">
  792. $uni-primary: #007aff !default;
  793. .uni-date {
  794. width: 100%;
  795. flex: 1;
  796. font-family: "Microsoft Yahei";
  797. }
  798. .uni-date-x {
  799. display: flex;
  800. flex-direction: row;
  801. align-items: center;
  802. justify-content: center;
  803. border-radius: 4px;
  804. background-color: #fff;
  805. color: #666;
  806. font-size: 14px;
  807. flex: 1;
  808. .icon-calendar {
  809. padding-left: 3px;
  810. }
  811. .range-separator {
  812. height: 35px;
  813. /* #ifndef MP */
  814. padding: 0 2px;
  815. /* #endif */
  816. line-height: 35px;
  817. }
  818. }
  819. .uni-date-x--border {
  820. box-sizing: border-box;
  821. border-radius: 4px;
  822. border: 1px solid #e5e5e5;
  823. }
  824. .uni-date-editor--x {
  825. display: flex;
  826. align-items: center;
  827. position: relative;
  828. }
  829. .uni-date-editor--x .uni-date__icon-clear {
  830. padding-right: 3px;
  831. display: flex;
  832. align-items: center;
  833. /* #ifdef H5 */
  834. cursor: pointer;
  835. /* #endif */
  836. }
  837. .uni-date__x-input {
  838. width: auto;
  839. height: 35px;
  840. /* #ifndef MP */
  841. padding-left: 5px;
  842. /* #endif */
  843. position: relative;
  844. flex: 1;
  845. line-height: 35px;
  846. font-size: 14px;
  847. overflow: hidden;
  848. }
  849. .text-center {
  850. text-align: center;
  851. }
  852. .uni-date__input {
  853. height: 40px;
  854. width: 100%;
  855. line-height: 40px;
  856. font-size: 14px;
  857. }
  858. .uni-date-range__input {
  859. text-align: center;
  860. max-width: 142px;
  861. }
  862. .uni-date-picker__container {
  863. position: relative;
  864. }
  865. .uni-date-mask--pc {
  866. position: fixed;
  867. bottom: 0px;
  868. top: 0px;
  869. left: 0px;
  870. right: 0px;
  871. background-color: rgba(0, 0, 0, 0);
  872. transition-duration: 0.3s;
  873. z-index: 996;
  874. }
  875. .uni-date-single--x {
  876. background-color: #fff;
  877. position: absolute;
  878. top: 0;
  879. z-index: 999;
  880. border: 1px solid #EBEEF5;
  881. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  882. border-radius: 4px;
  883. }
  884. .uni-date-range--x {
  885. background-color: #fff;
  886. position: absolute;
  887. top: 0;
  888. z-index: 999;
  889. border: 1px solid #EBEEF5;
  890. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  891. border-radius: 4px;
  892. }
  893. .uni-date-editor--x__disabled {
  894. opacity: 0.4;
  895. cursor: default;
  896. }
  897. .uni-date-editor--logo {
  898. width: 16px;
  899. height: 16px;
  900. vertical-align: middle;
  901. }
  902. /* 添加时间 */
  903. .popup-x-header {
  904. /* #ifndef APP-NVUE */
  905. display: flex;
  906. /* #endif */
  907. flex-direction: row;
  908. }
  909. .popup-x-header--datetime {
  910. /* #ifndef APP-NVUE */
  911. display: flex;
  912. /* #endif */
  913. flex-direction: row;
  914. flex: 1;
  915. }
  916. .popup-x-body {
  917. display: flex;
  918. }
  919. .popup-x-footer {
  920. padding: 0 15px;
  921. border-top-color: #F1F1F1;
  922. border-top-style: solid;
  923. border-top-width: 1px;
  924. line-height: 40px;
  925. text-align: right;
  926. color: #666;
  927. }
  928. .popup-x-footer text:hover {
  929. color: $uni-primary;
  930. cursor: pointer;
  931. opacity: 0.8;
  932. }
  933. .popup-x-footer .confirm-text {
  934. margin-left: 20px;
  935. color: $uni-primary;
  936. }
  937. .uni-date-changed {
  938. text-align: center;
  939. color: #333;
  940. border-bottom-color: #F1F1F1;
  941. border-bottom-style: solid;
  942. border-bottom-width: 1px;
  943. }
  944. .uni-date-changed--time text {
  945. height: 50px;
  946. line-height: 50px;
  947. }
  948. .uni-date-changed .uni-date-changed--time {
  949. flex: 1;
  950. }
  951. .uni-date-changed--time-date {
  952. color: #333;
  953. opacity: 0.6;
  954. }
  955. .mr-50 {
  956. margin-right: 50px;
  957. }
  958. /* picker 弹出层通用的指示小三角, todo:扩展至上下左右方向定位 */
  959. .uni-popper__arrow,
  960. .uni-popper__arrow::after {
  961. position: absolute;
  962. display: block;
  963. width: 0;
  964. height: 0;
  965. border: 6px solid transparent;
  966. border-top-width: 0;
  967. }
  968. .uni-popper__arrow {
  969. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  970. top: -6px;
  971. left: 10%;
  972. margin-right: 3px;
  973. border-bottom-color: #EBEEF5;
  974. }
  975. .uni-popper__arrow::after {
  976. content: " ";
  977. top: 1px;
  978. margin-left: -6px;
  979. border-bottom-color: #fff;
  980. }
  981. </style>