uni-calendar.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. <template>
  2. <view class="uni-calendar">
  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 v-if="title" class="uni-calendar__title">
  8. {{ title }}
  9. </view>
  10. <view v-if="!insert && showCancelAndOk" class="uni-calendar__header uni-calendar--fixed-top">
  11. <view class="uni-calendar__header-btn-box" @click="close">
  12. <text class="uni-calendar__header-text uni-calendar--fixed-width">{{ cancelText }}</text>
  13. </view>
  14. <view class="uni-calendar__header-btn-box" @click="confirm">
  15. <text class="uni-calendar__header-text uni-calendar--fixed-width">{{ okText }}</text>
  16. </view>
  17. </view>
  18. <view class="uni-calendar__header">
  19. <view class="uni-calendar__header-btn-box" @click.stop="pre">
  20. <view class="uni-calendar__header-btn uni-calendar--left"></view>
  21. </view>
  22. <picker mode="date" :value="date" fields="month" @change="bindDateChange">
  23. <text class="uni-calendar__header-text">{{ (nowDate.year || '') + ' / ' + (nowDate.month || '') }}</text>
  24. </picker>
  25. <view class="uni-calendar__header-btn-box" @click.stop="next">
  26. <view class="uni-calendar__header-btn uni-calendar--right"></view>
  27. </view>
  28. <text class="uni-calendar__backtoday" @click="backToday">{{ todayText }}</text>
  29. </view>
  30. <view class="uni-calendar__box" @touchstart="startTouch" @touchmove="moveTouch" @touchend="endTouch">
  31. <view v-if="showMonth" class="uni-calendar__box-bg">
  32. <text class="uni-calendar__box-bg-text">{{ nowDate.month }}</text>
  33. </view>
  34. <view class="uni-calendar__weeks">
  35. <view class="uni-calendar__weeks-day">
  36. <text class="uni-calendar__weeks-day-text">{{ SUNText }}</text>
  37. </view>
  38. <view class="uni-calendar__weeks-day">
  39. <text class="uni-calendar__weeks-day-text">{{ monText }}</text>
  40. </view>
  41. <view class="uni-calendar__weeks-day">
  42. <text class="uni-calendar__weeks-day-text">{{ TUEText }}</text>
  43. </view>
  44. <view class="uni-calendar__weeks-day">
  45. <text class="uni-calendar__weeks-day-text">{{ WEDText }}</text>
  46. </view>
  47. <view class="uni-calendar__weeks-day">
  48. <text class="uni-calendar__weeks-day-text">{{ THUText }}</text>
  49. </view>
  50. <view class="uni-calendar__weeks-day">
  51. <text class="uni-calendar__weeks-day-text">{{ FRIText }}</text>
  52. </view>
  53. <view class="uni-calendar__weeks-day">
  54. <text class="uni-calendar__weeks-day-text">{{ SATText }}</text>
  55. </view>
  56. </view>
  57. <view class="uni-calendar__weeks" v-for="(item, weekIndex) in weeks" :key="weekIndex">
  58. <view class="uni-calendar__weeks-item" v-for="(weeks, weeksIndex) in item" :key="weeksIndex">
  59. <calendar-item class="uni-calendar-item--hook" :weeks="weeks" :calendar="calendar" :selected="selected"
  60. :lunar="lunar" @change="choiceDate"></calendar-item>
  61. </view>
  62. </view>
  63. <view @click="confirm">
  64. <slot name="btn"></slot>
  65. </view>
  66. </view>
  67. </view>
  68. </view>
  69. </template>
  70. <script>
  71. import Calendar from './util.js';
  72. import CalendarItem from './uni-calendar-item.vue'
  73. import { initVueI18n } from '@dcloudio/uni-i18n'
  74. import i18nMessages from './i18n/index.js'
  75. const { t } = initVueI18n(i18nMessages)
  76. /**
  77. * Calendar 日历
  78. * @description 日历组件可以查看日期,选择任意范围内的日期,打点操作。常用场景如:酒店日期预订、火车机票选择购买日期、上下班打卡等
  79. * @tutorial https://ext.dcloud.net.cn/plugin?id=56
  80. * @property {String} date 自定义当前时间,默认为今天
  81. * @property {Boolean} lunar 显示农历
  82. * @property {String} startDate 日期选择范围-开始日期
  83. * @property {String} endDate 日期选择范围-结束日期
  84. * @property {Boolean} range 范围选择
  85. * @property {Boolean} insert = [true|false] 插入模式,默认为false
  86. * @value true 弹窗模式
  87. * @value false 插入模式
  88. * @property {Boolean} clearDate = [true|false] 弹窗模式是否清空上次选择内容
  89. * @property {Array} selected 打点,期待格式[{date: '2019-06-27', info: '签到', data: { custom: '自定义信息', name: '自定义消息头',xxx:xxx... }}]
  90. * @property {Boolean} showMonth 是否选择月份为背景
  91. * @event {Function} change 日期改变,`insert :ture` 时生效
  92. * @event {Function} confirm 确认选择`insert :false` 时生效
  93. * @event {Function} monthSwitch 切换月份时触发
  94. * @example <uni-calendar :insert="true":lunar="true" :start-date="'2019-3-2'":end-date="'2019-5-20'"@change="change" />
  95. */
  96. export default {
  97. components: {
  98. CalendarItem
  99. },
  100. emits: ['close', 'confirm', 'change', 'monthSwitch'],
  101. props: {
  102. date: {
  103. type: String,
  104. default: ''
  105. },
  106. selected: {
  107. type: Array,
  108. default() {
  109. return []
  110. }
  111. },
  112. lunar: {
  113. type: Boolean,
  114. default: false
  115. },
  116. startDate: {
  117. type: String,
  118. default: ''
  119. },
  120. endDate: {
  121. type: String,
  122. default: ''
  123. },
  124. range: {
  125. type: Boolean,
  126. default: false
  127. },
  128. insert: {
  129. type: Boolean,
  130. default: true
  131. },
  132. showMonth: {
  133. type: Boolean,
  134. default: true
  135. },
  136. clearDate: {
  137. type: Boolean,
  138. default: true
  139. },
  140. closeOnClickMask: {
  141. type: Boolean,
  142. default: true
  143. },
  144. showCancelAndOk: {
  145. type: Boolean,
  146. default: true
  147. },
  148. title: {
  149. type: String,
  150. default: ''
  151. },
  152. nextDay: {
  153. type: Boolean,
  154. default: false
  155. }
  156. },
  157. data() {
  158. return {
  159. show: false,
  160. weeks: [],
  161. calendar: {},
  162. nowDate: '',
  163. aniMaskShow: false,
  164. moveX: 0,
  165. startX: 0,
  166. }
  167. },
  168. computed: {
  169. /**
  170. * for i18n
  171. */
  172. okText() {
  173. return t("uni-calender.ok")
  174. },
  175. cancelText() {
  176. return t("uni-calender.cancel")
  177. },
  178. todayText() {
  179. return t("uni-calender.today")
  180. },
  181. monText() {
  182. return t("uni-calender.MON")
  183. },
  184. TUEText() {
  185. return t("uni-calender.TUE")
  186. },
  187. WEDText() {
  188. return t("uni-calender.WED")
  189. },
  190. THUText() {
  191. return t("uni-calender.THU")
  192. },
  193. FRIText() {
  194. return t("uni-calender.FRI")
  195. },
  196. SATText() {
  197. return t("uni-calender.SAT")
  198. },
  199. SUNText() {
  200. return t("uni-calender.SUN")
  201. },
  202. },
  203. watch: {
  204. date(newVal) {
  205. // this.cale.setDate(newVal)
  206. this.init(newVal)
  207. },
  208. startDate(val) {
  209. this.cale.resetSatrtDate(val)
  210. this.cale.setDate(this.nowDate.fullDate)
  211. this.weeks = this.cale.weeks
  212. },
  213. endDate(val) {
  214. this.cale.resetEndDate(val)
  215. this.cale.setDate(this.nowDate.fullDate)
  216. this.weeks = this.cale.weeks
  217. },
  218. selected(newVal) {
  219. this.cale.setSelectInfo(this.nowDate.fullDate, newVal)
  220. this.weeks = this.cale.weeks
  221. }
  222. },
  223. created() {
  224. this.cale = new Calendar({
  225. selected: this.selected,
  226. startDate: this.startDate,
  227. endDate: this.endDate,
  228. range: this.range,
  229. })
  230. this.init(this.date)
  231. },
  232. methods: {
  233. startTouch(e) {
  234. this.startX = e.changedTouches[0].clientX;
  235. },
  236. moveTouch(e) {
  237. this.moveX = e.changedTouches[0].clientX - this.startX;
  238. },
  239. endTouch(e) {
  240. if (Math.abs(this.moveX) > 90) {
  241. // 滑动距离超过90px时才进行切换
  242. uni.vibrateShort()
  243. if (this.moveX > 0) {
  244. // 向右滑动
  245. if (this.headerTab === 0) {
  246. this.next()
  247. } else {
  248. this.pre()
  249. }
  250. } else {
  251. // 向左滑动
  252. if (this.headerTab === 1) {
  253. this.pre()
  254. } else {
  255. this.next()
  256. }
  257. }
  258. this.startX = 0; // 重置起始位置
  259. this.moveX = 0; // 重置移动距离
  260. }
  261. },
  262. // 取消穿透
  263. clean() {
  264. // console.log('click')
  265. if (this.closeOnClickMask)
  266. this.close()
  267. },
  268. bindDateChange(e) {
  269. const value = e.detail.value + '-1'
  270. this.setDate(value)
  271. const { year, month } = this.cale.getDate(value)
  272. this.$emit('monthSwitch', {
  273. year,
  274. month
  275. })
  276. },
  277. /**
  278. * 初始化日期显示
  279. * @param {Object} date
  280. */
  281. init(date) {
  282. this.cale.setDate(date)
  283. this.weeks = this.cale.weeks
  284. this.nowDate = this.calendar = this.cale.getInfo(date)
  285. },
  286. /**
  287. * 打开日历弹窗
  288. */
  289. open() {
  290. // 弹窗模式并且清理数据
  291. if (this.clearDate && !this.insert) {
  292. this.cale.cleanMultipleStatus()
  293. // this.cale.setDate(this.date)
  294. this.init(this.date)
  295. }
  296. this.show = true
  297. this.$nextTick(() => {
  298. setTimeout(() => {
  299. this.aniMaskShow = true
  300. this.backToday()
  301. }, 50)
  302. })
  303. },
  304. /**
  305. * 关闭日历弹窗
  306. */
  307. close() {
  308. this.aniMaskShow = false
  309. this.$nextTick(() => {
  310. setTimeout(() => {
  311. this.show = false
  312. this.$emit('close')
  313. }, 300)
  314. })
  315. },
  316. /**
  317. * 确认按钮
  318. */
  319. confirm() {
  320. this.setEmit('confirm')
  321. this.close()
  322. },
  323. /**
  324. * 变化触发
  325. */
  326. change() {
  327. if (!this.insert) return
  328. this.setEmit('change')
  329. },
  330. /**
  331. * 选择月份触发
  332. */
  333. monthSwitch() {
  334. let {
  335. year,
  336. month
  337. } = this.nowDate
  338. this.$emit('monthSwitch', {
  339. year,
  340. month: Number(month)
  341. })
  342. },
  343. /**
  344. * 派发事件
  345. * @param {Object} name
  346. */
  347. setEmit(name) {
  348. let {
  349. year,
  350. month,
  351. date,
  352. fullDate,
  353. lunar,
  354. extraInfo
  355. } = this.calendar
  356. this.$emit(name, {
  357. range: this.cale.multipleStatus,
  358. year,
  359. month,
  360. date,
  361. fulldate: fullDate,
  362. lunar,
  363. extraInfo: extraInfo || {}
  364. })
  365. },
  366. /**
  367. * 选择天触发
  368. * @param {Object} weeks
  369. */
  370. choiceDate(weeks) {
  371. if (weeks.disable) {
  372. // const { month,year } = this.calendar
  373. // const clickmonth = weeks.month
  374. // const clickyear = weeks.lunar.cYear
  375. // console.log(month,year,clickmonth,clickyear)
  376. // if (month > clickmonth) {
  377. // if(year === clickyear || year < clickyear){
  378. // this.pre()
  379. // }else{
  380. // this.next()
  381. // }
  382. // } else {
  383. // this.next()
  384. // }
  385. return
  386. }
  387. uni.vibrateShort()
  388. this.calendar = weeks
  389. // 设置多选
  390. this.cale.setMultiple(this.calendar.fullDate)
  391. this.weeks = this.cale.weeks
  392. this.change()
  393. },
  394. /**
  395. * 回到今天
  396. */
  397. backToday() {
  398. const nowYearMonth = `${this.nowDate.year}-${this.nowDate.month}`
  399. const dateObj = new Date()
  400. if (this.nextDay) {
  401. dateObj.setDate(dateObj.getDate() + 1)
  402. }
  403. const date = this.cale.getDate(dateObj)
  404. const todayYearMonth = `${date.year}-${date.month}`
  405. this.init(date.fullDate)
  406. if (nowYearMonth !== todayYearMonth) {
  407. this.monthSwitch()
  408. }
  409. this.change()
  410. },
  411. /**
  412. * 上个月
  413. */
  414. pre() {
  415. const preDate = this.cale.getDate(this.nowDate.fullDate, -1, 'month').fullDate
  416. this.setDate(preDate)
  417. this.monthSwitch()
  418. },
  419. /**
  420. * 下个月
  421. */
  422. next() {
  423. const nextDate = this.cale.getDate(this.nowDate.fullDate, +1, 'month').fullDate
  424. this.setDate(nextDate)
  425. this.monthSwitch()
  426. },
  427. /**
  428. * 设置日期
  429. * @param {Object} date
  430. */
  431. setDate(date) {
  432. this.cale.setDate(date)
  433. this.weeks = this.cale.weeks
  434. this.nowDate = this.cale.getInfo(date)
  435. }
  436. }
  437. }
  438. </script>
  439. <style lang="scss" scoped>
  440. $uni-bg-color-mask: rgba($color: #000000, $alpha: 0.4);
  441. $uni-border-color: #EDEDED;
  442. $uni-text-color: #333;
  443. $uni-bg-color-hover: #f1f1f1;
  444. $uni-font-size-base: 14px;
  445. $uni-text-color-placeholder: #808080;
  446. $uni-color-subtitle: #555555;
  447. $uni-text-color-grey: #999;
  448. .uni-calendar {
  449. /* #ifndef APP-NVUE */
  450. display: flex;
  451. /* #endif */
  452. flex-direction: column;
  453. }
  454. .uni-calendar__mask {
  455. position: fixed;
  456. bottom: 0;
  457. top: 0;
  458. left: 0;
  459. right: 0;
  460. background-color: $uni-bg-color-mask;
  461. transition-property: opacity;
  462. transition-duration: 0.3s;
  463. opacity: 0;
  464. /* #ifndef APP-NVUE */
  465. z-index: 99;
  466. /* #endif */
  467. }
  468. .uni-calendar--mask-show {
  469. opacity: 1
  470. }
  471. .uni-calendar--fixed {
  472. position: fixed;
  473. /* #ifdef APP-NVUE */
  474. bottom: 0;
  475. /* #endif */
  476. left: 0;
  477. right: 0;
  478. transition-property: transform;
  479. transition-duration: 0.3s;
  480. transform: translateY(460px);
  481. /* #ifndef APP-NVUE */
  482. bottom: calc(var(--window-bottom));
  483. z-index: 99;
  484. /* #endif */
  485. }
  486. .uni-calendar--ani-show {
  487. transform: translateY(0);
  488. }
  489. .uni-calendar__content {
  490. background-color: #fff;
  491. border-radius: 16rpx 16rpx 0 0;
  492. padding-bottom: 40rpx;
  493. }
  494. .uni-calendar__header {
  495. position: relative;
  496. /* #ifndef APP-NVUE */
  497. display: flex;
  498. /* #endif */
  499. flex-direction: row;
  500. justify-content: center;
  501. align-items: center;
  502. height: 50px;
  503. border-bottom-color: $uni-border-color;
  504. border-bottom-style: solid;
  505. border-bottom-width: 1px;
  506. }
  507. .uni-calendar__title {
  508. font-size: 16px;
  509. color: $uni-text-color;
  510. padding: 24rpx 0 0 24rpx;
  511. }
  512. .uni-calendar--fixed-top {
  513. /* #ifndef APP-NVUE */
  514. display: flex;
  515. /* #endif */
  516. flex-direction: row;
  517. justify-content: space-between;
  518. // border-top-color: $uni-border-color;
  519. // border-top-style: solid;
  520. // border-top-width: 1px;
  521. }
  522. .uni-calendar--fixed-width {
  523. width: 50px;
  524. }
  525. .uni-calendar__backtoday {
  526. position: absolute;
  527. right: 0;
  528. top: 25rpx;
  529. padding: 0 5px;
  530. padding-left: 10px;
  531. height: 25px;
  532. line-height: 25px;
  533. font-size: 12px;
  534. border-top-left-radius: 25px;
  535. border-bottom-left-radius: 25px;
  536. color: $uni-text-color;
  537. background-color: $uni-bg-color-hover;
  538. }
  539. .uni-calendar__header-text {
  540. text-align: center;
  541. width: 100px;
  542. font-size: $uni-font-size-base;
  543. color: $uni-text-color;
  544. }
  545. .uni-calendar__header-btn-box {
  546. /* #ifndef APP-NVUE */
  547. display: flex;
  548. /* #endif */
  549. flex-direction: row;
  550. align-items: center;
  551. justify-content: center;
  552. width: 50px;
  553. height: 50px;
  554. }
  555. .uni-calendar__header-btn {
  556. width: 10px;
  557. height: 10px;
  558. border-left-color: $uni-text-color-placeholder;
  559. border-left-style: solid;
  560. border-left-width: 2px;
  561. border-top-color: $uni-color-subtitle;
  562. border-top-style: solid;
  563. border-top-width: 2px;
  564. }
  565. .uni-calendar--left {
  566. transform: rotate(-45deg);
  567. }
  568. .uni-calendar--right {
  569. transform: rotate(135deg);
  570. }
  571. .uni-calendar__weeks {
  572. position: relative;
  573. /* #ifndef APP-NVUE */
  574. display: flex;
  575. /* #endif */
  576. flex-direction: row;
  577. background-color: #F0F0F0;
  578. margin: 24rpx;
  579. border-radius: 16rpx;
  580. }
  581. .uni-calendar__weeks-item {
  582. background: #FFFFFF;
  583. flex: 1;
  584. }
  585. .uni-calendar__weeks-day {
  586. flex: 1;
  587. /* #ifndef APP-NVUE */
  588. display: flex;
  589. /* #endif */
  590. flex-direction: column;
  591. justify-content: center;
  592. align-items: center;
  593. height: 45px;
  594. border-bottom-color: #F5F5F5;
  595. border-bottom-style: solid;
  596. border-bottom-width: 1px;
  597. }
  598. .uni-calendar__weeks-day-text {
  599. font-size: 14px;
  600. }
  601. .uni-calendar__box {
  602. position: relative;
  603. }
  604. .uni-calendar__box-bg {
  605. /* #ifndef APP-NVUE */
  606. display: flex;
  607. /* #endif */
  608. justify-content: center;
  609. align-items: center;
  610. position: absolute;
  611. top: 0;
  612. left: 0;
  613. right: 0;
  614. bottom: 0;
  615. }
  616. .uni-calendar__box-bg-text {
  617. font-size: 200px;
  618. font-weight: bold;
  619. color: $uni-text-color-grey;
  620. opacity: 0.1;
  621. text-align: center;
  622. /* #ifndef APP-NVUE */
  623. line-height: 1;
  624. /* #endif */
  625. }
  626. </style>