uni-datetime-picker.vue 28 KB

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