uni-datetime-picker.vue 28 KB

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