uni-calendar.vue 13 KB

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