uni-calendar.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. <template>
  2. <view class="uni-calendar" @mouseleave="leaveCale">
  3. <view v-if="!insert&&show" class="uni-calendar__mask" :class="{'uni-calendar--mask-show':aniMaskShow}"
  4. @click="clean"></view>
  5. <view v-if="insert || show" class="uni-calendar__content"
  6. :class="{'uni-calendar--fixed':!insert,'uni-calendar--ani-show':aniMaskShow}">
  7. <view class="uni-calendar__header">
  8. <view v-if="left" class="uni-calendar__header-btn-box" @click.stop="pre">
  9. <view class="uni-calendar__header-btn uni-calendar--left"></view>
  10. </view>
  11. <picker mode="date" :value="date" fields="month" @change="bindDateChange">
  12. <text
  13. class="uni-calendar__header-text">{{ (nowDate.year||'') +'年'+( nowDate.month||'') +'月'}}</text>
  14. </picker>
  15. <view v-if="right" class="uni-calendar__header-btn-box" @click.stop="next">
  16. <view class="uni-calendar__header-btn uni-calendar--right"></view>
  17. </view>
  18. <!-- <text class="uni-calendar__backtoday" @click="backtoday">回到今天</text> -->
  19. </view>
  20. <view class="uni-calendar__box">
  21. <view v-if="showMonth" class="uni-calendar__box-bg">
  22. <text class="uni-calendar__box-bg-text">{{nowDate.month}}</text>
  23. </view>
  24. <view class="uni-calendar__weeks">
  25. <view class="uni-calendar__weeks-day">
  26. <text class="uni-calendar__weeks-day-text">日</text>
  27. </view>
  28. <view class="uni-calendar__weeks-day">
  29. <text class="uni-calendar__weeks-day-text">一</text>
  30. </view>
  31. <view class="uni-calendar__weeks-day">
  32. <text class="uni-calendar__weeks-day-text">二</text>
  33. </view>
  34. <view class="uni-calendar__weeks-day">
  35. <text class="uni-calendar__weeks-day-text">三</text>
  36. </view>
  37. <view class="uni-calendar__weeks-day">
  38. <text class="uni-calendar__weeks-day-text">四</text>
  39. </view>
  40. <view class="uni-calendar__weeks-day">
  41. <text class="uni-calendar__weeks-day-text">五</text>
  42. </view>
  43. <view class="uni-calendar__weeks-day">
  44. <text class="uni-calendar__weeks-day-text">六</text>
  45. </view>
  46. </view>
  47. <view class="uni-calendar__weeks" v-for="(item,weekIndex) in weeks" :key="weekIndex">
  48. <view class="uni-calendar__weeks-item" v-for="(weeks,weeksIndex) in item" :key="weeksIndex">
  49. <calendar-item class="uni-calendar-item--hook" :weeks="weeks" :calendar="calendar"
  50. :selected="selected" :lunar="lunar" :checkHover="range" @change="choiceDate"
  51. @handleMouse="handleMouse">
  52. </calendar-item>
  53. </view>
  54. </view>
  55. </view>
  56. <view v-if="!insert && !range && typeHasTime" class="uni-date-changed uni-calendar--fixed-top"
  57. style="padding: 0 40px;">
  58. <text class="uni-date-changed--time-date">{{tempSingleDate ? tempSingleDate : '选择日期'}}</text>
  59. <time-picker type="time" :start="reactStartTime" :end="reactEndTime" v-model="time" :disabled="!tempSingleDate"
  60. :border="false" class="time-picker-style">
  61. </time-picker>
  62. </view>
  63. <view v-if="!insert && range && typeHasTime" class="uni-date-changed uni-calendar--fixed-top">
  64. <view class="uni-date-changed--time-start">
  65. <text class="uni-date-changed--time-date">{{tempRange.before ? tempRange.before : '开始日期'}}</text>
  66. <time-picker type="time" :start="reactStartTime" v-model="timeRange.startTime"
  67. :border="false" :disabled="!tempRange.before" class="time-picker-style">
  68. </time-picker>
  69. </view>
  70. <uni-icons type="arrowthinright" color="#999" style="line-height: 50px;"></uni-icons>
  71. <view class="uni-date-changed--time-end">
  72. <text class="uni-date-changed--time-date">{{tempRange.after ? tempRange.after : '结束日期'}}</text>
  73. <time-picker type="time" :end="reactEndTime" v-model="timeRange.endTime" :border="false"
  74. :disabled="!tempRange.after" class="time-picker-style">
  75. </time-picker>
  76. </view>
  77. </view>
  78. <view v-if="!insert" class="uni-date-changed uni-calendar__header" @click="confirm">
  79. <!-- <view class="uni-calendar__header-btn-box" @click="close">
  80. <text class="uni-calendar__header-text uni-calendar--fixed-width">取消</text>
  81. </view> -->
  82. <view class="uni-calendar__header-btn-box">
  83. <text class="uni-calendar__button-text uni-calendar--fixed-width">确定</text>
  84. </view>
  85. </view>
  86. </view>
  87. </view>
  88. </template>
  89. <script>
  90. import Calendar from './util.js';
  91. import calendarItem from './uni-calendar-item.vue'
  92. import timePicker from './time-picker/components/time-picker/time-picker.vue'
  93. /**
  94. * Calendar 日历
  95. * @description 日历组件可以查看日期,选择任意范围内的日期,打点操作。常用场景如:酒店日期预订、火车机票选择购买日期、上下班打卡等
  96. * @tutorial https://ext.dcloud.net.cn/plugin?id=56
  97. * @property {String} date 自定义当前时间,默认为今天
  98. * @property {Boolean} lunar 显示农历
  99. * @property {String} startDate 日期选择范围-开始日期
  100. * @property {String} endDate 日期选择范围-结束日期
  101. * @property {Boolean} range 范围选择
  102. * @property {Boolean} insert = [true|false] 插入模式,默认为false
  103. * @value true 弹窗模式
  104. * @value false 插入模式
  105. * @property {Boolean} clearDate = [true|false] 弹窗模式是否清空上次选择内容
  106. * @property {Array} selected 打点,期待格式[{date: '2019-06-27', info: '签到', data: { custom: '自定义信息', name: '自定义消息头',xxx:xxx... }}]
  107. * @property {Boolean} showMonth 是否选择月份为背景
  108. * @event {Function} change 日期改变,`insert :ture` 时生效
  109. * @event {Function} confirm 确认选择`insert :false` 时生效
  110. * @event {Function} monthSwitch 切换月份时触发
  111. * @example <uni-calendar :insert="true":lunar="true" :start-date="'2019-3-2'":end-date="'2019-5-20'"@change="change" />
  112. */
  113. export default {
  114. components: {
  115. calendarItem,
  116. timePicker
  117. },
  118. props: {
  119. date: {
  120. type: String,
  121. default: ''
  122. },
  123. defTime: {
  124. type: [String, Object],
  125. default: ''
  126. },
  127. selectableTimes: {
  128. type: [Object],
  129. default () {
  130. return {}
  131. }
  132. },
  133. selected: {
  134. type: Array,
  135. default () {
  136. return []
  137. }
  138. },
  139. lunar: {
  140. type: Boolean,
  141. default: false
  142. },
  143. startDate: {
  144. type: String,
  145. default: ''
  146. },
  147. endDate: {
  148. type: String,
  149. default: ''
  150. },
  151. range: {
  152. type: Boolean,
  153. default: false
  154. },
  155. typeHasTime: {
  156. type: Boolean,
  157. default: false
  158. },
  159. insert: {
  160. type: Boolean,
  161. default: true
  162. },
  163. showMonth: {
  164. type: Boolean,
  165. default: true
  166. },
  167. clearDate: {
  168. type: Boolean,
  169. default: true
  170. },
  171. left: {
  172. type: Boolean,
  173. default: true
  174. },
  175. right: {
  176. type: Boolean,
  177. default: true
  178. },
  179. checkHover: {
  180. type: Boolean,
  181. default: true
  182. },
  183. pleStatus: {
  184. type: Object,
  185. default () {
  186. return {
  187. before: '',
  188. after: '',
  189. data: [],
  190. fulldate: ''
  191. }
  192. }
  193. }
  194. },
  195. data() {
  196. return {
  197. show: false,
  198. weeks: [],
  199. calendar: {},
  200. nowDate: '',
  201. aniMaskShow: false,
  202. firstEnter: true,
  203. time: this.defTime ? this.defTime : '',
  204. timeRange: {
  205. startTime: this.defTime.start ? this.defTime.start : '',
  206. endTime: this.defTime.end ? this.defTime.end : ''
  207. },
  208. tempSingleDate: this.date,
  209. tempRange: {
  210. before: '',
  211. after: ''
  212. }
  213. }
  214. },
  215. watch: {
  216. date(newVal, oldVal) {
  217. // this.cale.setDate(newVal)
  218. this.init(newVal)
  219. },
  220. startDate(val) {
  221. this.cale.resetSatrtDate(val)
  222. },
  223. endDate(val) {
  224. this.cale.resetEndDate(val)
  225. },
  226. selected(newVal) {
  227. this.cale.setSelectInfo(this.nowDate.fullDate, newVal)
  228. this.weeks = this.cale.weeks
  229. },
  230. // tempRange: {
  231. // immediate: true,
  232. // handler(newVal, oldVal) {debugger
  233. // if (!oldVal) return
  234. // if (!newVal.before) this.timeRange.startTime = ''
  235. // if (!newVal.after) this.timeRange.endTime = ''
  236. // }
  237. // },
  238. pleStatus: {
  239. immediate: true,
  240. handler(newVal, oldVal) {
  241. const {
  242. before,
  243. after,
  244. fulldate,
  245. which
  246. } = newVal
  247. this.tempRange.before = before
  248. this.tempRange.after = after
  249. setTimeout(() => {
  250. if (fulldate) {
  251. this.cale.setHoverMultiple(fulldate)
  252. if (before && after) {
  253. this.cale.lastHover = true
  254. if (this.rangeWithinMonth(after, before)) return
  255. this.setDate(before)
  256. }
  257. if (!before && !after) {
  258. this.cale.setMultiple(fulldate)
  259. this.setDate(this.nowDate.fullDate)
  260. this.calendar.fullDate = ''
  261. this.cale.lastHover = false
  262. }
  263. } else {
  264. this.cale.setDefaultMultiple(before, after)
  265. if (which === 'left') {
  266. this.setDate(before)
  267. } else {
  268. this.setDate(after)
  269. }
  270. this.cale.lastHover = true
  271. }
  272. }, 16)
  273. }
  274. }
  275. },
  276. computed: {
  277. reactStartTime() {
  278. const activeDate = this.range ? this.tempRange.before : this.calendar.fullDate
  279. const res = activeDate === this.startDate ? this.selectableTimes.start : ''
  280. return res
  281. },
  282. reactEndTime() {
  283. const activeDate = this.range ? this.tempRange.after : this.calendar.fullDate
  284. const res = activeDate === this.endDate ? this.selectableTimes.end : ''
  285. return res
  286. }
  287. },
  288. created() {
  289. // 获取日历方法实例
  290. this.cale = new Calendar({
  291. // date: new Date(),
  292. selected: this.selected,
  293. startDate: this.startDate,
  294. endDate: this.endDate,
  295. range: this.range,
  296. // multipleStatus: this.pleStatus
  297. })
  298. // 选中某一天
  299. // this.cale.setDate(this.date)
  300. this.init(this.date)
  301. // this.setDay
  302. },
  303. methods: {
  304. leaveCale() {
  305. this.firstEnter = true
  306. },
  307. handleMouse(weeks) {
  308. if (weeks.disable) return
  309. if (this.cale.lastHover) return
  310. let {
  311. before,
  312. after
  313. } = this.cale.multipleStatus
  314. if (!before) return
  315. this.calendar = weeks
  316. // 设置范围选
  317. this.cale.setHoverMultiple(this.calendar.fullDate)
  318. this.weeks = this.cale.weeks
  319. // hover时,进入一个日历,更新另一个
  320. if (this.firstEnter) {
  321. this.$emit('firstEnterCale', this.cale.multipleStatus)
  322. this.firstEnter = false
  323. }
  324. },
  325. rangeWithinMonth(A, B) {
  326. const [yearA, monthA] = A.split('-')
  327. const [yearB, monthB] = B.split('-')
  328. return yearA === yearB && monthA === monthB
  329. },
  330. // 取消穿透
  331. clean() {
  332. this.close()
  333. },
  334. bindDateChange(e) {
  335. const value = e.detail.value + '-1'
  336. console.log(this.cale.getDate(value));
  337. this.init(value)
  338. },
  339. /**
  340. * 初始化日期显示
  341. * @param {Object} date
  342. */
  343. init(date) {
  344. this.cale.setDate(date)
  345. this.weeks = this.cale.weeks
  346. this.nowDate = this.calendar = this.cale.getInfo(date)
  347. },
  348. /**
  349. * 打开日历弹窗
  350. */
  351. open() {
  352. // 弹窗模式并且清理数据
  353. if (this.clearDate && !this.insert) {
  354. this.cale.cleanMultipleStatus()
  355. // this.cale.setDate(this.date)
  356. this.init(this.date)
  357. }
  358. this.show = true
  359. this.$nextTick(() => {
  360. setTimeout(() => {
  361. this.aniMaskShow = true
  362. }, 50)
  363. })
  364. },
  365. /**
  366. * 关闭日历弹窗
  367. */
  368. close() {
  369. this.aniMaskShow = false
  370. this.$nextTick(() => {
  371. setTimeout(() => {
  372. this.show = false
  373. this.$emit('close')
  374. }, 300)
  375. })
  376. },
  377. /**
  378. * 确认按钮
  379. */
  380. confirm() {
  381. this.setEmit('confirm')
  382. this.close()
  383. },
  384. /**
  385. * 变化触发
  386. */
  387. change() {
  388. if (!this.insert) return
  389. this.setEmit('change')
  390. },
  391. /**
  392. * 选择月份触发
  393. */
  394. monthSwitch() {
  395. let {
  396. year,
  397. month
  398. } = this.nowDate
  399. this.$emit('monthSwitch', {
  400. year,
  401. month: Number(month)
  402. })
  403. },
  404. /**
  405. * 派发事件
  406. * @param {Object} name
  407. */
  408. setEmit(name) {
  409. let {
  410. year,
  411. month,
  412. date,
  413. fullDate,
  414. lunar,
  415. extraInfo
  416. } = this.calendar
  417. this.$emit(name, {
  418. range: this.cale.multipleStatus,
  419. year,
  420. month,
  421. date,
  422. time: this.time,
  423. timeRange: this.timeRange,
  424. fulldate: fullDate,
  425. lunar,
  426. extraInfo: extraInfo || {}
  427. })
  428. },
  429. /**
  430. * 选择天触发
  431. * @param {Object} weeks
  432. */
  433. choiceDate(weeks) {
  434. if (weeks.disable) return
  435. this.calendar = weeks
  436. // 设置多选
  437. this.cale.setMultiple(this.calendar.fullDate, true)
  438. this.weeks = this.cale.weeks
  439. this.tempSingleDate = this.calendar.fullDate
  440. this.tempRange.before = this.cale.multipleStatus.before
  441. this.tempRange.after = this.cale.multipleStatus.after
  442. this.change()
  443. },
  444. /**
  445. * 回到今天
  446. */
  447. backtoday() {
  448. console.log(this.cale.getDate(new Date()).fullDate);
  449. let date = this.cale.getDate(new Date()).fullDate
  450. // this.cale.setDate(date)
  451. this.init(date)
  452. this.change()
  453. },
  454. /**
  455. * 比较时间大小
  456. */
  457. dateCompare(startDate, endDate) {
  458. // 计算截止时间
  459. startDate = new Date(startDate.replace('-', '/').replace('-', '/'))
  460. // 计算详细项的截止时间
  461. endDate = new Date(endDate.replace('-', '/').replace('-', '/'))
  462. if (startDate <= endDate) {
  463. return true
  464. } else {
  465. return false
  466. }
  467. },
  468. /**
  469. * 上个月
  470. */
  471. pre() {
  472. const preDate = this.cale.getDate(this.nowDate.fullDate, -1, 'month').fullDate
  473. this.setDate(preDate)
  474. this.monthSwitch()
  475. },
  476. /**
  477. * 下个月
  478. */
  479. next() {
  480. const nextDate = this.cale.getDate(this.nowDate.fullDate, +1, 'month').fullDate
  481. this.setDate(nextDate)
  482. this.monthSwitch()
  483. },
  484. /**
  485. * 设置日期
  486. * @param {Object} date
  487. */
  488. setDate(date) {
  489. this.cale.setDate(date)
  490. this.weeks = this.cale.weeks
  491. this.nowDate = this.cale.getInfo(date)
  492. }
  493. }
  494. }
  495. </script>
  496. <style lang="scss" scoped>
  497. .uni-calendar {
  498. /* #ifndef APP-NVUE */
  499. display: flex;
  500. /* #endif */
  501. flex-direction: column;
  502. }
  503. .uni-calendar__mask {
  504. position: fixed;
  505. bottom: 0;
  506. top: 0;
  507. left: 0;
  508. right: 0;
  509. background-color: $uni-bg-color-mask;
  510. transition-property: opacity;
  511. transition-duration: 0.3s;
  512. opacity: 0;
  513. /* #ifndef APP-NVUE */
  514. z-index: 99;
  515. /* #endif */
  516. }
  517. .uni-calendar--mask-show {
  518. opacity: 1
  519. }
  520. .uni-calendar--fixed {
  521. position: fixed;
  522. bottom: 0;
  523. left: 0;
  524. right: 0;
  525. transition-property: transform;
  526. transition-duration: 0.3s;
  527. transform: translateY(460px);
  528. /* #ifndef APP-NVUE */
  529. z-index: 99;
  530. /* #endif */
  531. }
  532. .uni-calendar--ani-show {
  533. transform: translateY(0);
  534. }
  535. .uni-calendar__content {
  536. background-color: #fff;
  537. }
  538. .uni-calendar__header {
  539. position: relative;
  540. /* #ifndef APP-NVUE */
  541. display: flex;
  542. /* #endif */
  543. flex-direction: row;
  544. justify-content: center;
  545. align-items: center;
  546. height: 50px;
  547. // border-bottom-color: $uni-border-color;
  548. // border-bottom-style: solid;
  549. // border-bottom-width: 1px;
  550. }
  551. .uni-calendar--fixed-top {
  552. /* #ifndef APP-NVUE */
  553. display: flex;
  554. /* #endif */
  555. flex-direction: row;
  556. justify-content: space-between;
  557. border-top-color: $uni-border-color;
  558. border-top-style: solid;
  559. border-top-width: 1px;
  560. }
  561. .uni-calendar--fixed-width {
  562. width: 50px;
  563. // padding: 0 15px;
  564. }
  565. .uni-calendar__backtoday {
  566. position: absolute;
  567. right: 0;
  568. top: 25rpx;
  569. padding: 0 5px;
  570. padding-left: 10px;
  571. height: 25px;
  572. line-height: 25px;
  573. font-size: 12px;
  574. border-top-left-radius: 25px;
  575. border-bottom-left-radius: 25px;
  576. color: $uni-text-color;
  577. background-color: $uni-bg-color-hover;
  578. }
  579. .uni-calendar__header-text {
  580. text-align: center;
  581. width: 100px;
  582. font-size: $uni-font-size-base;
  583. color: $uni-text-color;
  584. }
  585. .uni-calendar__button-text {
  586. text-align: center;
  587. width: 100px;
  588. font-size: $uni-font-size-base;
  589. color: #007aff;
  590. letter-spacing: 3px;
  591. }
  592. .uni-calendar__header-btn-box {
  593. /* #ifndef APP-NVUE */
  594. display: flex;
  595. /* #endif */
  596. flex-direction: row;
  597. align-items: center;
  598. justify-content: center;
  599. width: 50px;
  600. height: 50px;
  601. }
  602. .uni-calendar__header-btn {
  603. width: 10px;
  604. height: 10px;
  605. border-left-color: $uni-text-color-placeholder;
  606. border-left-style: solid;
  607. border-left-width: 2px;
  608. border-top-color: $uni-color-subtitle;
  609. border-top-style: solid;
  610. border-top-width: 2px;
  611. }
  612. .uni-calendar--left {
  613. transform: rotate(-45deg);
  614. }
  615. .uni-calendar--right {
  616. transform: rotate(135deg);
  617. }
  618. .uni-calendar__weeks {
  619. position: relative;
  620. /* #ifndef APP-NVUE */
  621. display: flex;
  622. /* #endif */
  623. flex-direction: row;
  624. }
  625. .uni-calendar__weeks-item {
  626. flex: 1;
  627. }
  628. .uni-calendar__weeks-day {
  629. flex: 1;
  630. /* #ifndef APP-NVUE */
  631. display: flex;
  632. /* #endif */
  633. flex-direction: column;
  634. justify-content: center;
  635. align-items: center;
  636. height: 45px;
  637. border-bottom-color: #F5F5F5;
  638. border-bottom-style: solid;
  639. border-bottom-width: 1px;
  640. }
  641. .uni-calendar__weeks-day-text {
  642. font-size: 14px;
  643. }
  644. .uni-calendar__box {
  645. position: relative;
  646. }
  647. .uni-calendar__box-bg {
  648. /* #ifndef APP-NVUE */
  649. display: flex;
  650. /* #endif */
  651. justify-content: center;
  652. align-items: center;
  653. position: absolute;
  654. top: 0;
  655. left: 0;
  656. right: 0;
  657. bottom: 0;
  658. }
  659. .uni-calendar__box-bg-text {
  660. font-size: 200px;
  661. font-weight: bold;
  662. color: $uni-text-color-grey;
  663. opacity: 0.1;
  664. text-align: center;
  665. /* #ifndef APP-NVUE */
  666. line-height: 1;
  667. /* #endif */
  668. }
  669. .uni-date-changed {
  670. padding: 0 10px;
  671. // line-height: 50px;
  672. text-align: center;
  673. color: #333;
  674. border-top-color: $uni-border-color;
  675. border-top-style: solid;
  676. border-top-width: 1px;
  677. }
  678. .uni-date-changed--time text {
  679. // padding: 0 20px;
  680. // height: 50px;
  681. line-height: 50px;
  682. }
  683. .uni-date-changed {
  684. flex: 1;
  685. }
  686. .uni-date-changed--time {
  687. display: flex;
  688. flex: 1;
  689. }
  690. .uni-date-changed--time-start {
  691. display: flex;
  692. justify-content: right;
  693. align-items: center;
  694. // flex: 1;
  695. }
  696. .uni-date-changed--time-end {
  697. display: flex;
  698. justify-content: left;
  699. align-items: center;
  700. // flex: 1;
  701. }
  702. .uni-date-changed--time-date {
  703. color: #999;
  704. line-height: 50px;
  705. // opacity: 0.6;
  706. }
  707. .time-picker-style {
  708. width: 62px;
  709. display: flex;
  710. justify-content: center;
  711. align-items: center
  712. }
  713. .mr-10 {
  714. margin-right: 10px;
  715. }
  716. </style>