uni-datetime-picker.vue 32 KB

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