uni-datetime-picker.vue 29 KB

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