blob: 6602bd03fde6582d6c34807373babe2ca5773522 [file] [log] [blame]
guangchao.xu070005a2020-12-07 09:56:40 +08001<template>
2 <u-popup closeable :maskCloseAble="maskCloseAble" mode="bottom" :popup="false" v-model="value" length="auto"
3 :safeAreaInsetBottom="safeAreaInsetBottom" @close="close" :z-index="uZIndex" :border-radius="borderRadius" :closeable="closeable">
4 <view class="u-calendar">
5 <view class="u-calendar__header">
6 <view class="u-calendar__header__text" v-if="!$slots['tooltip']">
7 {{toolTip}}
8 </view>
9 <slot v-else name="tooltip" />
10 </view>
11 <view class="u-calendar__action u-flex u-row-center">
12 <view class="u-calendar__action__icon">
13 <u-icon v-if="changeYear" name="arrow-left-double" :color="yearArrowColor" @click="changeYearHandler(0)"></u-icon>
14 </view>
15 <view class="u-calendar__action__icon">
16 <u-icon v-if="changeMonth" name="arrow-left" :color="monthArrowColor" @click="changeMonthHandler(0)"></u-icon>
17 </view>
18 <view class="u-calendar__action__text">{{ showTitle }}</view>
19 <view class="u-calendar__action__icon">
20 <u-icon v-if="changeMonth" name="arrow-right" :color="monthArrowColor" @click="changeMonthHandler(1)"></u-icon>
21 </view>
22 <view class="u-calendar__action__icon">
23 <u-icon v-if="changeYear" name="arrow-right-double" :color="yearArrowColor" @click="changeYearHandler(1)"></u-icon>
24 </view>
25 </view>
26 <view class="u-calendar__week-day">
27 <view class="u-calendar__week-day__text" v-for="(item, index) in weekDayZh" :key="index">{{item}}</view>
28 </view>
29 <view class="u-calendar__content">
30 <!-- 前置空白部分 -->
31 <block v-for="(item, index) in weekdayArr" :key="index">
32 <view class="u-calendar__content__item"></view>
33 </block>
34 <view class="u-calendar__content__item" :class="{
35 'u-hover-class':openDisAbled(year,month,index+1),
36 'u-calendar__content--start-date': (mode == 'range' && startDate==`${year}-${month}-${index+1}`) || mode== 'date',
37 'u-calendar__content--end-date':(mode== 'range' && endDate==`${year}-${month}-${index+1}`) || mode == 'date'
38 }" :style="{backgroundColor: getColor(index,1)}" v-for="(item, index) in daysArr" :key="index"
39 @tap="dateClick(index)">
40 <view class="u-calendar__content__item__inner" :style="{color: getColor(index,2)}">
41 <view>{{ index + 1 }}</view>
42 </view>
43 <view class="u-calendar__content__item__tips" :style="{color:activeColor}" v-if="mode== 'range' && startDate==`${year}-${month}-${index+1}` && startDate!=endDate">{{startText}}</view>
44 <view class="u-calendar__content__item__tips" :style="{color:activeColor}" v-if="mode== 'range' && endDate==`${year}-${month}-${index+1}`">{{endText}}</view>
45 </view>
46 <view class="u-calendar__content__bg-month">{{month}}</view>
47 </view>
48 <view class="u-calendar__bottom">
49 <view class="u-calendar__bottom__choose">
50 <text>{{mode == 'date' ? activeDate : startDate}}</text>
51 <text v-if="endDate">至{{endDate}}</text>
52 </view>
53 <view class="u-calendar__bottom__btn">
54 <u-button :type="btnType" shape="circle" size="default" @click="btnFix(false)">确定</u-button>
55 </view>
56 </view>
57 </view>
58 </u-popup>
59</template>
60<script>
61 /**
62 * calendar 日历
63 * @description 此组件用于单个选择日期,范围选择日期等,日历被包裹在底部弹起的容器中。
64 * @tutorial http://uviewui.com/components/calendar.html
65 * @property {String} mode 选择日期的模式,date-为单个日期,range-为选择日期范围
66 * @property {Boolean} v-model 布尔值变量,用于控制日历的弹出与收起
67 * @property {Boolean} safe-area-inset-bottom 是否开启底部安全区适配(默认false)
68 * @property {Boolean} change-year 是否显示顶部的切换年份方向的按钮(默认true)
69 * @property {Boolean} change-month 是否显示顶部的切换月份方向的按钮(默认true)
70 * @property {String Number} max-year 可切换的最大年份(默认2050)
71 * @property {String Number} min-year 最小可选日期(默认1950)
72 * @property {String Number} min-date 可切换的最小年份(默认1950-01-01)
73 * @property {String Number} max-date 最大可选日期(默认当前日期)
74 * @property {String Number} 弹窗顶部左右两边的圆角值,单位rpx(默认20)
75 * @property {Boolean} mask-close-able 是否允许通过点击遮罩关闭日历(默认true)
76 * @property {String} month-arrow-color 月份切换按钮箭头颜色(默认#606266)
77 * @property {String} year-arrow-color 年份切换按钮箭头颜色(默认#909399)
78 * @property {String} color 日期字体的默认颜色(默认#303133)
79 * @property {String} active-bg-color 起始/结束日期按钮的背景色(默认#2979ff)
80 * @property {String Number} z-index 弹出时的z-index值(默认10075)
81 * @property {String} active-color 起始/结束日期按钮的字体颜色(默认#ffffff)
82 * @property {String} range-bg-color 起始/结束日期之间的区域的背景颜色(默认rgba(41,121,255,0.13))
83 * @property {String} range-color 选择范围内字体颜色(默认#2979ff)
84 * @property {String} start-text 起始日期底部的提示文字(默认 '开始')
85 * @property {String} end-text 结束日期底部的提示文字(默认 '结束')
86 * @property {String} btn-type 底部确定按钮的主题(默认 'primary')
87 * @property {String} toolTip 顶部提示文字,如设置名为tooltip的slot,此参数将失效(默认 '选择日期')
88 * @property {Boolean} closeable 是否显示右上角的关闭图标(默认true)
89 * @example <u-calendar v-model="show" :mode="mode"></u-calendar>
90 */
91
92 export default {
93 name: 'u-calendar',
94 props: {
95 safeAreaInsetBottom: {
96 type: Boolean,
97 default: false
98 },
99 // 是否允许通过点击遮罩关闭Picker
100 maskCloseAble: {
101 type: Boolean,
102 default: true
103 },
104 // 通过双向绑定控制组件的弹出与收起
105 value: {
106 type: Boolean,
107 default: false
108 },
109 // 弹出的z-index值
110 zIndex: {
111 type: [String, Number],
112 default: 0
113 },
114 // 是否允许切换年份
115 changeYear: {
116 type: Boolean,
117 default: true
118 },
119 // 是否允许切换月份
120 changeMonth: {
121 type: Boolean,
122 default: true
123 },
124 // date-单个日期选择,range-开始日期+结束日期选择
125 mode: {
126 type: String,
127 default: 'date'
128 },
129 // 可切换的最大年份
130 maxYear: {
131 type: [Number, String],
132 default: 2050
133 },
134 // 可切换的最小年份
135 minYear: {
136 type: [Number, String],
137 default: 1950
138 },
139 // 最小可选日期(不在范围内日期禁用不可选)
140 minDate: {
141 type: [Number, String],
142 default: '1950-01-01'
143 },
144 /**
145 * 最大可选日期
146 * 默认最大值为今天,之后的日期不可选
147 * 2030-12-31
148 * */
149 maxDate: {
150 type: [Number, String],
151 default: ''
152 },
153 // 弹窗顶部左右两边的圆角值
154 borderRadius: {
155 type: [String, Number],
156 default: 20
157 },
158 // 月份切换按钮箭头颜色
159 monthArrowColor: {
160 type: String,
161 default: '#606266'
162 },
163 // 年份切换按钮箭头颜色
164 yearArrowColor: {
165 type: String,
166 default: '#909399'
167 },
168 // 默认日期字体颜色
169 color: {
170 type: String,
171 default: '#303133'
172 },
173 // 选中|起始结束日期背景色
174 activeBgColor: {
175 type: String,
176 default: '#2979ff'
177 },
178 // 选中|起始结束日期字体颜色
179 activeColor: {
180 type: String,
181 default: '#ffffff'
182 },
183 // 范围内日期背景色
184 rangeBgColor: {
185 type: String,
186 default: 'rgba(41,121,255,0.13)'
187 },
188 // 范围内日期字体颜色
189 rangeColor: {
190 type: String,
191 default: '#2979ff'
192 },
193 // mode=range时生效,起始日期自定义文案
194 startText: {
195 type: String,
196 default: '开始'
197 },
198 // mode=range时生效,结束日期自定义文案
199 endText: {
200 type: String,
201 default: '结束'
202 },
203 //按钮样式类型
204 btnType: {
205 type: String,
206 default: 'primary'
207 },
208 // 当前选中日期带选中效果
209 isActiveCurrent: {
210 type: Boolean,
211 default: true
212 },
213 // 切换年月是否触发事件 mode=date时生效
214 isChange: {
215 type: Boolean,
216 default: false
217 },
218 // 是否显示右上角的关闭图标
219 closeable: {
220 type: Boolean,
221 default: true
222 },
223 // 顶部的提示文字
224 toolTip: {
225 type: String,
226 default: '选择日期'
227 }
228 },
229 data() {
230 return {
231 // 星期几,值为1-7
232 weekday: 1,
233 weekdayArr:[],
234 // 当前月有多少天
235 days: 0,
236 daysArr:[],
237 showTitle: '',
238 year: 2020,
239 month: 0,
240 day: 0,
241 startYear: 0,
242 startMonth: 0,
243 startDay: 0,
244 endYear: 0,
245 endMonth: 0,
246 endDay: 0,
247 today: '',
248 activeDate: '',
249 startDate: '',
250 endDate: '',
251 isStart: true,
252 min: null,
253 max: null,
254 weekDayZh: ['日', '一', '二', '三', '四', '五', '六']
255 };
256 },
257 computed: {
258 dataChange() {
259 return `${this.mode}-${this.minDate}-${this.maxDate}`;
260 },
261 uZIndex() {
262 // 如果用户有传递z-index值,优先使用
263 return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
264 }
265 },
266 watch: {
267 dataChange(val) {
268 this.init()
269 }
270 },
271 created() {
272 this.init()
273 },
274 methods: {
275 getColor(index, type) {
276 let color = type == 1 ? '' : this.color;
277 let day = index + 1
278 let date = `${this.year}-${this.month}-${day}`
279 let timestamp = new Date(date.replace(/\-/g, '/')).getTime();
280 let start = this.startDate.replace(/\-/g, '/')
281 let end = this.endDate.replace(/\-/g, '/')
282 if ((this.isActiveCurrent && this.activeDate == date) || this.startDate == date || this.endDate == date) {
283 color = type == 1 ? this.activeBgColor : this.activeColor;
284 } else if (this.endDate && timestamp > new Date(start).getTime() && timestamp < new Date(end).getTime()) {
285 color = type == 1 ? this.rangeBgColor : this.rangeColor;
286 }
287 return color;
288 },
289 init() {
290 let now = new Date();
291 this.year = now.getFullYear();
292 this.month = now.getMonth() + 1;
293 this.day = now.getDate();
294 this.today = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`;
295 this.activeDate = this.today;
296 this.min = this.initDate(this.minDate);
297 this.max = this.initDate(this.maxDate || this.today);
298 this.startDate = "";
299 this.startYear = 0;
300 this.startMonth = 0;
301 this.startDay = 0;
302 this.endYear = 0;
303 this.endMonth = 0;
304 this.endDay = 0;
305 this.endDate = "";
306 this.isStart = true;
307 this.changeData();
308 },
309 //日期处理
310 initDate(date) {
311 let fdate = date.split('-');
312 return {
313 year: Number(fdate[0] || 1920),
314 month: Number(fdate[1] || 1),
315 day: Number(fdate[2] || 1)
316 }
317 },
318 openDisAbled: function(year, month, day) {
319 let bool = true;
320 let date = `${year}/${month}/${day}`;
321 // let today = this.today.replace(/\-/g, '/');
322 let min = `${this.min.year}/${this.min.month}/${this.min.day}`;
323 let max = `${this.max.year}/${this.max.month}/${this.max.day}`;
324 let timestamp = new Date(date).getTime();
325 if (timestamp >= new Date(min).getTime() && timestamp <= new Date(max).getTime()) {
326 bool = false;
327 }
328 return bool;
329 },
330 generateArray: function(start, end) {
331 return Array.from(new Array(end + 1).keys()).slice(start);
332 },
333 formatNum: function(num) {
334 return num < 10 ? '0' + num : num + '';
335 },
336 //一个月有多少天
337 getMonthDay(year, month) {
338 let days = new Date(year, month, 0).getDate();
339 return days;
340 },
341 getWeekday(year, month) {
342 let date = new Date(`${year}/${month}/01 00:00:00`);
343 return date.getDay();
344 },
345 checkRange(year) {
346 let overstep = false;
347 if (year < this.minYear || year > this.maxYear) {
348 uni.showToast({
349 title: "日期超出范围啦~",
350 icon: 'none'
351 })
352 overstep = true;
353 }
354 return overstep;
355 },
356 changeMonthHandler(isAdd) {
357 if (isAdd) {
358 let month = this.month + 1;
359 let year = month > 12 ? this.year + 1 : this.year;
360 if (!this.checkRange(year)) {
361 this.month = month > 12 ? 1 : month;
362 this.year = year;
363 this.changeData();
364 }
365
366 } else {
367 let month = this.month - 1;
368 let year = month < 1 ? this.year - 1 : this.year;
369 if (!this.checkRange(year)) {
370 this.month = month < 1 ? 12 : month;
371 this.year = year;
372 this.changeData();
373 }
374 }
375 },
376 changeYearHandler(isAdd) {
377 let year = isAdd ? this.year + 1 : this.year - 1;
378 if (!this.checkRange(year)) {
379 this.year = year;
380 this.changeData();
381 }
382 },
383 changeData() {
384 this.days = this.getMonthDay(this.year, this.month);
385 this.daysArr=this.generateArray(1,this.days)
386 this.weekday = this.getWeekday(this.year, this.month);
387 this.weekdayArr=this.generateArray(1,this.weekday)
388 this.showTitle = `${this.year}年${this.month}月`;
389 if (this.isChange && this.mode == 'date') {
390 this.btnFix(true);
391 }
392 },
393 dateClick: function(day) {
394 day += 1;
395 if (!this.openDisAbled(this.year, this.month, day)) {
396 this.day = day;
397 let date = `${this.year}-${this.month}-${day}`;
398 if (this.mode == 'date') {
399 this.activeDate = date;
400 } else {
401 let compare = new Date(date.replace(/\-/g, '/')).getTime() < new Date(this.startDate.replace(/\-/g, '/')).getTime()
402 if (this.isStart || compare) {
403 this.startDate = date;
404 this.startYear = this.year;
405 this.startMonth = this.month;
406 this.startDay = this.day;
407 this.endYear = 0;
408 this.endMonth = 0;
409 this.endDay = 0;
410 this.endDate = "";
411 this.activeDate = "";
412 this.isStart = false;
413 } else {
414 this.endDate = date;
415 this.endYear = this.year;
416 this.endMonth = this.month;
417 this.endDay = this.day;
418 this.isStart = true;
419 }
420 }
421 }
422 },
423 close() {
424 // 修改通过v-model绑定的父组件变量的值为false,从而隐藏日历弹窗
425 this.$emit('input', false);
426 },
427 getWeekText(date) {
428 date = new Date(`${date.replace(/\-/g, '/')} 00:00:00`);
429 let week = date.getDay();
430 return '星期' + ['日', '一', '二', '三', '四', '五', '六'][week];
431 },
432 btnFix(show) {
433 if (!show) {
434 this.close();
435 }
436 if (this.mode == 'date') {
437 let arr = this.activeDate.split('-')
438 let year = this.isChange ? this.year : Number(arr[0]);
439 let month = this.isChange ? this.month : Number(arr[1]);
440 let day = this.isChange ? this.day : Number(arr[2]);
441 //当前月有多少天
442 let days = this.getMonthDay(year, month);
443 let result = `${year}-${this.formatNum(month)}-${this.formatNum(day)}`;
444 let weekText = this.getWeekText(result);
445 let isToday = false;
446 if (`${year}-${month}-${day}` == this.today) {
447 //今天
448 isToday = true;
449 }
450 this.$emit('change', {
451 year: year,
452 month: month,
453 day: day,
454 days: days,
455 result: result,
456 week: weekText,
457 isToday: isToday,
458 // switch: show //是否是切换年月操作
459 });
460 } else {
461 if (!this.startDate || !this.endDate) return;
462 let startMonth = this.formatNum(this.startMonth);
463 let startDay = this.formatNum(this.startDay);
464 let startDate = `${this.startYear}-${startMonth}-${startDay}`;
465 let startWeek = this.getWeekText(startDate)
466
467 let endMonth = this.formatNum(this.endMonth);
468 let endDay = this.formatNum(this.endDay);
469 let endDate = `${this.endYear}-${endMonth}-${endDay}`;
470 let endWeek = this.getWeekText(endDate);
471 this.$emit('change', {
472 startYear: this.startYear,
473 startMonth: this.startMonth,
474 startDay: this.startDay,
475 startDate: startDate,
476 startWeek: startWeek,
477 endYear: this.endYear,
478 endMonth: this.endMonth,
479 endDay: this.endDay,
480 endDate: endDate,
481 endWeek: endWeek
482 });
483 }
484 }
485 }
486 };
487</script>
488
489<style scoped lang="scss">
490 @import "../../libs/css/style.components.scss";
491
492 .u-calendar {
493 color: $u-content-color;
494
495 &__header {
496 width: 100%;
497 box-sizing: border-box;
498 font-size: 30rpx;
499 background-color: #fff;
500 color: $u-main-color;
501
502 &__text {
503 margin-top: 30rpx;
504 padding: 0 60rpx;
505 @include vue-flex;
506 justify-content: center;
507 align-items: center;
508 }
509 }
510
511 &__action {
512 padding: 40rpx 0 40rpx 0;
513
514 &__icon {
515 margin: 0 16rpx;
516 }
517
518 &__text {
519 padding: 0 16rpx;
520 color: $u-main-color;
521 font-size: 32rpx;
522 line-height: 32rpx;
523 font-weight: bold;
524 }
525 }
526
527 &__week-day {
528 @include vue-flex;
529 align-items: center;
530 justify-content: center;
531 padding: 6px 0;
532 overflow: hidden;
533
534 &__text {
535 flex: 1;
536 text-align: center;
537 }
538 }
539
540 &__content {
541 width: 100%;
542 @include vue-flex;
543 flex-wrap: wrap;
544 padding: 6px 0;
545 box-sizing: border-box;
546 background-color: #fff;
547 position: relative;
548
549 &--end-date {
550 border-top-right-radius: 8rpx;
551 border-bottom-right-radius: 8rpx;
552 }
553
554 &--start-date {
555 border-top-left-radius: 8rpx;
556 border-bottom-left-radius: 8rpx;
557 }
558
559 &__item {
560 width: 14.2857%;
561 @include vue-flex;
562 align-items: center;
563 justify-content: center;
564 padding: 6px 0;
565 overflow: hidden;
566 position: relative;
567 z-index: 2;
568
569 &__inner {
570 height: 84rpx;
571 @include vue-flex;
572 align-items: center;
573 justify-content: center;
574 flex-direction: column;
575 font-size: 32rpx;
576 position: relative;
577 border-radius: 50%;
578
579 &__desc {
580 width: 100%;
581 font-size: 24rpx;
582 line-height: 24rpx;
583 transform: scale(0.75);
584 transform-origin: center center;
585 position: absolute;
586 left: 0;
587 text-align: center;
588 bottom: 2rpx;
589 }
590 }
591
592 &__tips {
593 width: 100%;
594 font-size: 24rpx;
595 line-height: 24rpx;
596 position: absolute;
597 left: 0;
598 transform: scale(0.8);
599 transform-origin: center center;
600 text-align: center;
601 bottom: 8rpx;
602 z-index: 2;
603 }
604 }
605
606 &__bg-month {
607 position: absolute;
608 font-size: 130px;
609 line-height: 130px;
610 left: 50%;
611 top: 50%;
612 transform: translate(-50%, -50%);
613 color: #e4e7ed;
614 z-index: 1;
615 }
616 }
617
618 &__bottom {
619 width: 100%;
620 @include vue-flex;
621 align-items: center;
622 justify-content: center;
623 flex-direction: column;
624 background-color: #fff;
625 padding: 0 40rpx 30rpx;
626 box-sizing: border-box;
627 font-size: 24rpx;
628 color: $u-tips-color;
629
630 &__choose {
631 height: 50rpx;
632 }
633
634 &__btn {
635 width: 100%;
636 }
637 }
638 }
639</style>