uni-datetime-picker.vue 25 KB

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