uni-datetime-picker.vue 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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. this.$refs.right.cale.lastHover = false
  473. }
  474. }
  475. }, 50)
  476. },
  477. close() {
  478. setTimeout(() => {
  479. this.pickerVisible = false
  480. this.$emit('maskClick', this.value)
  481. this.$refs.mobile && this.$refs.mobile.close()
  482. }, 20)
  483. },
  484. setEmit(value) {
  485. if (this.returnType === "timestamp" || this.returnType === "date") {
  486. if (!Array.isArray(value)) {
  487. if (!this.hasTime) {
  488. value = value + ' ' + '00:00:00'
  489. }
  490. value = this.createTimestamp(value)
  491. if (this.returnType === "date") {
  492. value = new Date(value)
  493. }
  494. } else {
  495. if (!this.hasTime) {
  496. value[0] = value[0] + ' ' + '00:00:00'
  497. value[1] = value[1] + ' ' + '00:00:00'
  498. }
  499. value[0] = this.createTimestamp(value[0])
  500. value[1] = this.createTimestamp(value[1])
  501. if (this.returnType === "date") {
  502. value[0] = new Date(value[0])
  503. value[1] = new Date(value[1])
  504. }
  505. }
  506. }
  507. this.$emit('update:modelValue', value)
  508. this.$emit('input', value)
  509. this.$emit('change', value)
  510. this.isEmitValue = true
  511. },
  512. createTimestamp(date) {
  513. date = fixIosDateFormat(date)
  514. return Date.parse(new Date(date))
  515. },
  516. singleChange(e) {
  517. this.calendarDate = this.inputDate = e.fulldate
  518. if (this.hasTime) return
  519. this.confirmSingleChange()
  520. },
  521. confirmSingleChange() {
  522. if(!checkDate(this.inputDate)){
  523. const now = new Date()
  524. this.calendarDate = this.inputDate = getDate(now)
  525. this.pickerTime = getTime(now, this.hideSecond)
  526. }
  527. let startLaterInputDate = false
  528. let startDate, startTime
  529. if(this.start) {
  530. let startString = this.start
  531. if(typeof this.start === 'number'){
  532. startString = getDateTime(this.start, this.hideSecond)
  533. }
  534. [startDate, startTime] = startString.split(' ')
  535. if(this.start && !dateCompare(startDate, this.inputDate)) {
  536. startLaterInputDate = true
  537. this.inputDate = startDate
  538. }
  539. }
  540. let endEarlierInputDate = false
  541. let endDate, endTime
  542. if(this.end) {
  543. let endString = this.end
  544. if(typeof this.end === 'number'){
  545. endString = getDateTime(this.end, this.hideSecond)
  546. }
  547. [endDate, endTime] = endString.split(' ')
  548. if(this.end && !dateCompare(this.inputDate, endDate)) {
  549. endEarlierInputDate = true
  550. this.inputDate = endDate
  551. }
  552. }
  553. if (this.hasTime) {
  554. if(startLaterInputDate){
  555. this.pickerTime = startTime || getDefaultSecond(this.hideSecond)
  556. }
  557. if(endEarlierInputDate){
  558. this.pickerTime = endTime || getDefaultSecond(this.hideSecond)
  559. }
  560. if(!this.pickerTime){
  561. this.pickerTime = getTime(Date.now(), this.hideSecond)
  562. }
  563. this.displayValue = `${this.inputDate} ${this.pickerTime}`
  564. } else {
  565. this.displayValue = this.inputDate
  566. }
  567. this.setEmit(this.displayValue)
  568. this.pickerVisible = false
  569. },
  570. leftChange(e) {
  571. const {
  572. before,
  573. after
  574. } = e.range
  575. this.rangeChange(before, after)
  576. const obj = {
  577. before: e.range.before,
  578. after: e.range.after,
  579. data: e.range.data,
  580. fulldate: e.fulldate
  581. }
  582. this.startMultipleStatus = Object.assign({}, this.startMultipleStatus, obj)
  583. },
  584. rightChange(e) {
  585. const {
  586. before,
  587. after
  588. } = e.range
  589. this.rangeChange(before, after)
  590. const obj = {
  591. before: e.range.before,
  592. after: e.range.after,
  593. data: e.range.data,
  594. fulldate: e.fulldate
  595. }
  596. this.endMultipleStatus = Object.assign({}, this.endMultipleStatus, obj)
  597. },
  598. mobileChange(e) {
  599. if (this.isRange) {
  600. const {before, after} = e.range
  601. if(!before || !after){
  602. return
  603. }
  604. this.handleStartAndEnd(before, after, true)
  605. if (this.hasTime) {
  606. const {
  607. startTime,
  608. endTime
  609. } = e.timeRange
  610. this.tempRange.startTime = startTime
  611. this.tempRange.endTime = endTime
  612. }
  613. this.confirmRangeChange()
  614. } else {
  615. if (this.hasTime) {
  616. this.displayValue = e.fulldate + ' ' + e.time
  617. } else {
  618. this.displayValue = e.fulldate
  619. }
  620. this.setEmit(this.displayValue)
  621. }
  622. this.$refs.mobile.close()
  623. },
  624. rangeChange(before, after) {
  625. if (!(before && after)) return
  626. this.handleStartAndEnd(before, after, true)
  627. if (this.hasTime) return
  628. this.confirmRangeChange()
  629. },
  630. confirmRangeChange() {
  631. if (!this.tempRange.startDate || !this.tempRange.endDate) {
  632. this.pickerVisible = false
  633. return
  634. }
  635. if(!checkDate(this.tempRange.startDate)){
  636. this.tempRange.startDate = getDate(Date.now())
  637. }
  638. if(!checkDate(this.tempRange.endDate)){
  639. this.tempRange.endDate = getDate(Date.now())
  640. }
  641. let start, end
  642. let startDateLaterRangeStartDate = false
  643. let startDateLaterRangeEndDate = false
  644. let startDate, startTime
  645. if(this.start) {
  646. let startString = this.start
  647. if(typeof this.start === 'number'){
  648. startString = getDateTime(this.start, this.hideSecond)
  649. }
  650. [startDate,startTime] = startString.split(' ')
  651. if(this.start && !dateCompare(this.start, this.tempRange.startDate)) {
  652. startDateLaterRangeStartDate = true
  653. this.tempRange.startDate = startDate
  654. }
  655. if(this.start && !dateCompare(this.start, this.tempRange.endDate)) {
  656. startDateLaterRangeEndDate = true
  657. this.tempRange.endDate = startDate
  658. }
  659. }
  660. let endDateEarlierRangeStartDate = false
  661. let endDateEarlierRangeEndDate = false
  662. let endDate, endTime
  663. if(this.end) {
  664. let endString = this.end
  665. if(typeof this.end === 'number'){
  666. endString = getDateTime(this.end, this.hideSecond)
  667. }
  668. [endDate,endTime] = endString.split(' ')
  669. if(this.end && !dateCompare(this.tempRange.startDate, this.end)) {
  670. endDateEarlierRangeStartDate = true
  671. this.tempRange.startDate = endDate
  672. }
  673. if(this.end && !dateCompare(this.tempRange.endDate, this.end)) {
  674. endDateEarlierRangeEndDate = true
  675. this.tempRange.endDate = endDate
  676. }
  677. }
  678. if (!this.hasTime) {
  679. start = this.displayRangeValue.startDate = this.tempRange.startDate
  680. end = this.displayRangeValue.endDate = this.tempRange.endDate
  681. } else {
  682. if(startDateLaterRangeStartDate){
  683. this.tempRange.startTime = startTime || getDefaultSecond(this.hideSecond)
  684. }else if(endDateEarlierRangeStartDate){
  685. this.tempRange.startTime = endTime || getDefaultSecond(this.hideSecond)
  686. }
  687. if(!this.tempRange.startTime){
  688. this.tempRange.startTime = getTime(Date.now(), this.hideSecond)
  689. }
  690. if(startDateLaterRangeEndDate){
  691. this.tempRange.endTime = startTime || getDefaultSecond(this.hideSecond)
  692. }else if(endDateEarlierRangeEndDate){
  693. this.tempRange.endTime = endTime || getDefaultSecond(this.hideSecond)
  694. }
  695. if(!this.tempRange.endTime){
  696. this.tempRange.endTime = getTime(Date.now(), this.hideSecond)
  697. }
  698. start = this.displayRangeValue.startDate = `${this.tempRange.startDate} ${this.tempRange.startTime}`
  699. end = this.displayRangeValue.endDate = `${this.tempRange.endDate} ${this.tempRange.endTime}`
  700. }
  701. if(!dateCompare(start,end)){
  702. [start, end] = [end, start]
  703. }
  704. this.displayRangeValue.startDate = start
  705. this.displayRangeValue.endDate = end
  706. const displayRange = [start, end]
  707. this.setEmit(displayRange)
  708. this.pickerVisible = false
  709. },
  710. handleStartAndEnd(before, after, temp = false) {
  711. if (!(before && after)) return
  712. const type = temp ? 'tempRange' : 'range'
  713. const isStartEarlierEnd = dateCompare(before, after)
  714. this[type].startDate = isStartEarlierEnd ? before : after
  715. this[type].endDate = isStartEarlierEnd ? after : before
  716. },
  717. /**
  718. * 比较时间大小
  719. */
  720. dateCompare(startDate, endDate) {
  721. // 计算截止时间
  722. startDate = new Date(startDate.replace('-', '/').replace('-', '/'))
  723. // 计算详细项的截止时间
  724. endDate = new Date(endDate.replace('-', '/').replace('-', '/'))
  725. return startDate <= endDate
  726. },
  727. /**
  728. * 比较时间差
  729. */
  730. diffDate(startDate, endDate) {
  731. // 计算截止时间
  732. startDate = new Date(startDate.replace('-', '/').replace('-', '/'))
  733. // 计算详细项的截止时间
  734. endDate = new Date(endDate.replace('-', '/').replace('-', '/'))
  735. const diff = (endDate - startDate) / (24 * 60 * 60 * 1000)
  736. return Math.abs(diff)
  737. },
  738. clear(needEmit = true) {
  739. if (!this.isRange) {
  740. this.displayValue = ''
  741. this.inputDate = ''
  742. this.pickerTime = ''
  743. if (this.isPhone) {
  744. this.$refs.mobile && this.$refs.mobile.clearCalender()
  745. } else {
  746. this.$refs.pcSingle && this.$refs.pcSingle.clearCalender()
  747. }
  748. if (needEmit) {
  749. this.$emit('change', '')
  750. this.$emit('input', '')
  751. this.$emit('update:modelValue', '')
  752. }
  753. } else {
  754. this.displayRangeValue.startDate = ''
  755. this.displayRangeValue.endDate = ''
  756. this.tempRange.startDate = ''
  757. this.tempRange.startTime = ''
  758. this.tempRange.endDate = ''
  759. this.tempRange.endTime = ''
  760. if (this.isPhone) {
  761. this.$refs.mobile && this.$refs.mobile.clearCalender()
  762. } else {
  763. this.$refs.left && this.$refs.left.clearCalender()
  764. this.$refs.right && this.$refs.right.clearCalender()
  765. this.$refs.right && this.$refs.right.changeMonth('next')
  766. }
  767. if (needEmit) {
  768. this.$emit('change', [])
  769. this.$emit('input', [])
  770. this.$emit('update:modelValue', [])
  771. }
  772. }
  773. }
  774. }
  775. }
  776. </script>
  777. <style lang="scss">
  778. $uni-primary: #007aff !default;
  779. .uni-date {
  780. width: 100%;
  781. flex: 1;
  782. }
  783. .uni-date-x {
  784. display: flex;
  785. flex-direction: row;
  786. align-items: center;
  787. justify-content: center;
  788. border-radius: 4px;
  789. background-color: #fff;
  790. color: #666;
  791. font-size: 14px;
  792. flex: 1;
  793. .icon-calendar{
  794. padding-left: 3px;
  795. }
  796. .range-separator{
  797. height: 35px;
  798. /* #ifndef MP */
  799. padding: 0 2px;
  800. /* #endif */
  801. line-height: 35px;
  802. }
  803. }
  804. .uni-date-x--border {
  805. box-sizing: border-box;
  806. border-radius: 4px;
  807. border: 1px solid #e5e5e5;
  808. }
  809. .uni-date-editor--x {
  810. display: flex;
  811. align-items: center;
  812. position: relative;
  813. }
  814. .uni-date-editor--x .uni-date__icon-clear {
  815. padding-right: 3px;
  816. display: flex;
  817. align-items: center;
  818. /* #ifdef H5 */
  819. cursor: pointer;
  820. /* #endif */
  821. }
  822. .uni-date__x-input {
  823. width: auto;
  824. height: 35px;
  825. /* #ifndef MP */
  826. padding-left: 5px;
  827. /* #endif */
  828. position: relative;
  829. flex: 1;
  830. line-height: 35px;
  831. font-size: 14px;
  832. overflow: hidden;
  833. }
  834. .text-center {
  835. text-align: center;
  836. }
  837. .uni-date__input {
  838. height: 40px;
  839. width: 100%;
  840. line-height: 40px;
  841. font-size: 14px;
  842. }
  843. .uni-date-range__input {
  844. text-align: center;
  845. max-width: 142px;
  846. }
  847. .uni-date-picker__container {
  848. position: relative;
  849. }
  850. .uni-date-mask--pc {
  851. position: fixed;
  852. bottom: 0px;
  853. top: 0px;
  854. left: 0px;
  855. right: 0px;
  856. background-color: rgba(0, 0, 0, 0);
  857. transition-duration: 0.3s;
  858. z-index: 996;
  859. }
  860. .uni-date-single--x {
  861. background-color: #fff;
  862. position: absolute;
  863. top: 0;
  864. z-index: 999;
  865. border: 1px solid #EBEEF5;
  866. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  867. border-radius: 4px;
  868. }
  869. .uni-date-range--x {
  870. background-color: #fff;
  871. position: absolute;
  872. top: 0;
  873. z-index: 999;
  874. border: 1px solid #EBEEF5;
  875. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  876. border-radius: 4px;
  877. }
  878. .uni-date-editor--x__disabled {
  879. opacity: 0.4;
  880. cursor: default;
  881. }
  882. .uni-date-editor--logo {
  883. width: 16px;
  884. height: 16px;
  885. vertical-align: middle;
  886. }
  887. /* 添加时间 */
  888. .popup-x-header {
  889. /* #ifndef APP-NVUE */
  890. display: flex;
  891. /* #endif */
  892. flex-direction: row;
  893. }
  894. .popup-x-header--datetime {
  895. /* #ifndef APP-NVUE */
  896. display: flex;
  897. /* #endif */
  898. flex-direction: row;
  899. flex: 1;
  900. }
  901. .popup-x-body {
  902. display: flex;
  903. }
  904. .popup-x-footer {
  905. padding: 0 15px;
  906. border-top-color: #F1F1F1;
  907. border-top-style: solid;
  908. border-top-width: 1px;
  909. line-height: 40px;
  910. text-align: right;
  911. color: #666;
  912. }
  913. .popup-x-footer text:hover {
  914. color: $uni-primary;
  915. cursor: pointer;
  916. opacity: 0.8;
  917. }
  918. .popup-x-footer .confirm-text {
  919. margin-left: 20px;
  920. color: $uni-primary;
  921. }
  922. .uni-date-changed {
  923. text-align: center;
  924. color: #333;
  925. border-bottom-color: #F1F1F1;
  926. border-bottom-style: solid;
  927. border-bottom-width: 1px;
  928. }
  929. .uni-date-changed--time text {
  930. height: 50px;
  931. line-height: 50px;
  932. }
  933. .uni-date-changed .uni-date-changed--time {
  934. flex: 1;
  935. }
  936. .uni-date-changed--time-date {
  937. color: #333;
  938. opacity: 0.6;
  939. }
  940. .mr-50 {
  941. margin-right: 50px;
  942. }
  943. /* picker 弹出层通用的指示小三角, todo:扩展至上下左右方向定位 */
  944. .uni-popper__arrow,
  945. .uni-popper__arrow::after {
  946. position: absolute;
  947. display: block;
  948. width: 0;
  949. height: 0;
  950. border: 6px solid transparent;
  951. border-top-width: 0;
  952. }
  953. .uni-popper__arrow {
  954. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  955. top: -6px;
  956. left: 10%;
  957. margin-right: 3px;
  958. border-bottom-color: #EBEEF5;
  959. }
  960. .uni-popper__arrow::after {
  961. content: " ";
  962. top: 1px;
  963. margin-left: -6px;
  964. border-bottom-color: #fff;
  965. }
  966. </style>