uni-datetime-picker.vue 28 KB

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