uni-datetime-picker.vue 28 KB

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