選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

uni-datetime-picker.vue 28KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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. }
  797. .uni-date-x {
  798. display: flex;
  799. flex-direction: row;
  800. align-items: center;
  801. justify-content: center;
  802. border-radius: 4px;
  803. background-color: #fff;
  804. color: #666;
  805. font-size: 14px;
  806. flex: 1;
  807. .icon-calendar {
  808. padding-left: 3px;
  809. }
  810. .range-separator {
  811. height: 35px;
  812. /* #ifndef MP */
  813. padding: 0 2px;
  814. /* #endif */
  815. line-height: 35px;
  816. }
  817. }
  818. .uni-date-x--border {
  819. box-sizing: border-box;
  820. border-radius: 4px;
  821. border: 1px solid #e5e5e5;
  822. }
  823. .uni-date-editor--x {
  824. display: flex;
  825. align-items: center;
  826. position: relative;
  827. }
  828. .uni-date-editor--x .uni-date__icon-clear {
  829. padding-right: 3px;
  830. display: flex;
  831. align-items: center;
  832. /* #ifdef H5 */
  833. cursor: pointer;
  834. /* #endif */
  835. }
  836. .uni-date__x-input {
  837. width: auto;
  838. height: 35px;
  839. /* #ifndef MP */
  840. padding-left: 5px;
  841. /* #endif */
  842. position: relative;
  843. flex: 1;
  844. line-height: 35px;
  845. font-size: 14px;
  846. overflow: hidden;
  847. }
  848. .text-center {
  849. text-align: center;
  850. }
  851. .uni-date__input {
  852. height: 40px;
  853. width: 100%;
  854. line-height: 40px;
  855. font-size: 14px;
  856. }
  857. .uni-date-range__input {
  858. text-align: center;
  859. max-width: 142px;
  860. }
  861. .uni-date-picker__container {
  862. position: relative;
  863. }
  864. .uni-date-mask--pc {
  865. position: fixed;
  866. bottom: 0px;
  867. top: 0px;
  868. left: 0px;
  869. right: 0px;
  870. background-color: rgba(0, 0, 0, 0);
  871. transition-duration: 0.3s;
  872. z-index: 996;
  873. }
  874. .uni-date-single--x {
  875. background-color: #fff;
  876. position: absolute;
  877. top: 0;
  878. z-index: 999;
  879. border: 1px solid #EBEEF5;
  880. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  881. border-radius: 4px;
  882. }
  883. .uni-date-range--x {
  884. background-color: #fff;
  885. position: absolute;
  886. top: 0;
  887. z-index: 999;
  888. border: 1px solid #EBEEF5;
  889. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  890. border-radius: 4px;
  891. }
  892. .uni-date-editor--x__disabled {
  893. opacity: 0.4;
  894. cursor: default;
  895. }
  896. .uni-date-editor--logo {
  897. width: 16px;
  898. height: 16px;
  899. vertical-align: middle;
  900. }
  901. /* 添加时间 */
  902. .popup-x-header {
  903. /* #ifndef APP-NVUE */
  904. display: flex;
  905. /* #endif */
  906. flex-direction: row;
  907. }
  908. .popup-x-header--datetime {
  909. /* #ifndef APP-NVUE */
  910. display: flex;
  911. /* #endif */
  912. flex-direction: row;
  913. flex: 1;
  914. }
  915. .popup-x-body {
  916. display: flex;
  917. }
  918. .popup-x-footer {
  919. padding: 0 15px;
  920. border-top-color: #F1F1F1;
  921. border-top-style: solid;
  922. border-top-width: 1px;
  923. line-height: 40px;
  924. text-align: right;
  925. color: #666;
  926. }
  927. .popup-x-footer text:hover {
  928. color: $uni-primary;
  929. cursor: pointer;
  930. opacity: 0.8;
  931. }
  932. .popup-x-footer .confirm-text {
  933. margin-left: 20px;
  934. color: $uni-primary;
  935. }
  936. .uni-date-changed {
  937. text-align: center;
  938. color: #333;
  939. border-bottom-color: #F1F1F1;
  940. border-bottom-style: solid;
  941. border-bottom-width: 1px;
  942. }
  943. .uni-date-changed--time text {
  944. height: 50px;
  945. line-height: 50px;
  946. }
  947. .uni-date-changed .uni-date-changed--time {
  948. flex: 1;
  949. }
  950. .uni-date-changed--time-date {
  951. color: #333;
  952. opacity: 0.6;
  953. }
  954. .mr-50 {
  955. margin-right: 50px;
  956. }
  957. /* picker 弹出层通用的指示小三角, todo:扩展至上下左右方向定位 */
  958. .uni-popper__arrow,
  959. .uni-popper__arrow::after {
  960. position: absolute;
  961. display: block;
  962. width: 0;
  963. height: 0;
  964. border: 6px solid transparent;
  965. border-top-width: 0;
  966. }
  967. .uni-popper__arrow {
  968. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  969. top: -6px;
  970. left: 10%;
  971. margin-right: 3px;
  972. border-bottom-color: #EBEEF5;
  973. }
  974. .uni-popper__arrow::after {
  975. content: " ";
  976. top: 1px;
  977. margin-left: -6px;
  978. border-bottom-color: #fff;
  979. }
  980. </style>