uni-datetime-picker.vue 28 KB

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