| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493 |
- <template>
- <view class="calendar-wrapper" @touchstart="touchStart" @touchend="touchEnd">
- <view class="header" v-if="headerBar">
- <view class="pre" v-if="formatNum(m-1) == 0" @click="changeMonth('pre')">{{formatNum(y-1) +'年'+formatNum(12)+'月'}}</view>
- <view class="pre" v-else @click="changeMonth('pre')">{{y+'年'+formatNum(m-1)+'月'}}</view>
- <view>{{y+'年'+formatNum(m)+'月'}}</view>
- <view class="next" v-if="formatNum(m+1) == 13" @click="changeMonth('next')">{{formatNum(y+1)+'年'+formatNum(m)+'月'}}</view>
- <view class="next" v-else @click="changeMonth('next')">{{y+'年'+formatNum(m+1)+'月'}}</view>
- </view>
- <view class="week">
- <view class="week-day" v-for="(item, index) in weekDay" :key="index">{{ item }}</view>
- </view>
- <view :class="{ hide: !monthOpen }" class="content" :style="{ height: height }">
- <view :style="{ top: positionTop + 'rpx' }" class="days">
- <view class="item" v-for="(item, index) in dates" :key="index">
- <view
- class="day"
- @click="selectOne(item, $event)"
- :class="{
- choose: choose == `${item.year}-${item.month}-${item.date}`&&item.isCurM,
- today: isToday(item.year, item.month, item.date),
- isWorkDay:item.isCurM
- }"
- >
- <!-- isWorkDay: isWorkDay(item.year, item.month, item.date) -->
- <!-- nolm: !item.isCurM, -->
- {{ Number(item.date) }}
- </view>
- <view class="markDay" v-if="(isMarkDay(item.year, item.month, item.date)&&item.isCurM)"></view>
- <!-- <view class="today-text" v-if="isToday(item.year, item.month, item.date)">今</view> -->
- </view>
- </view>
- </view>
- <image src="https://i.loli.net/2020/07/16/2MmZsucVTlRjSwK.png" mode="scaleToFill" v-if="collapsible" @click="toggle" class="weektoggle" :class="{ down: monthOpen }"></image>
- </view>
- </template>
- <script>
- export default {
- props: {
- // 星期几为第一天(0为星期日)
- weekstart: {
- type: Number,
- default: 1
- },
- // 默认选中日期
- itemData: {
- type: String,
- default: ''
- },
- // 标记的日期
- markDays: {
- type: Array,
- default: ()=>{
- return [];
- }
- },
- //是否展示月份切换按钮
- headerBar:{
- type: Boolean,
- default: true
- },
- // 是否展开
- open: {
- type: Boolean,
- default: true
- },
- //是否可收缩
- collapsible:{
- type: Boolean,
- default: false
- },
- //未来日期是否不可点击
- disabledAfter: {
- type: Boolean,
- default: false
- }
- },
- data() {
- return {
- weektext: ['日', '一', '二', '三', '四', '五', '六'],
- y: new Date().getFullYear(), // 年
- m: new Date().getMonth() + 1, // 月
- dates: [], // 当前月的日期数据
- positionTop: 0,
- monthOpen: true,
- choose: '',
- touchStartX: 0, // 触屏起始点x
- touchStartY: 0, // 触屏起始点y
- };
- },
- created() {
- if(this.itemData){
- let arr = this.itemData.split('-')
- // this.dates = this.monthDay(arr[0], arr[1]);
- this.y = Number(arr[0])
- this.m = Number(arr[1])
- }
- this.dates = this.monthDay(this.y, this.m);
- !this.open && this.toggle();
- },
- mounted() {
- if(this.itemData){
- this.choose = this.itemData
- }else{
- this.choose = this.getToday().date;
- }
-
- console.log('%cren-calendar.vue line:134 today', 'color: white; background-color: #007acc;', this.choose);
- // this.$emit('onDayClick', this.choose);
- },
- computed: {
- // 顶部星期栏
- weekDay() {
- return this.weektext.slice(this.weekstart).concat(this.weektext.slice(0, this.weekstart));
- },
- height() {
- return (((this.dates.length / 7) * 80) + 100) + 'rpx';
- }
- },
- methods: {
- formatNum(num) {
- let res = Number(num);
- return res < 10 ? '0' + res : res;
- },
- getToday() {
- let date = new Date();
- let y = date.getFullYear();
- let m = date.getMonth();
- let d = date.getDate();
- let week = new Date().getDay();
- let weekText = ['日', '一', '二', '三', '四', '五', '六'];
- let formatWeek = '星期' + weekText[week];
- let today = {
- date: y + '-' + this.formatNum(m + 1) + '-' + this.formatNum(d),
- week: formatWeek
- };
- return today;
- },
- // 获取当前月份数据
- monthDay(y, month) {
- let dates = [];
- let m = Number(month);
- let firstDayOfMonth = new Date(y, m - 1, 1).getDay(); // 当月第一天星期几
- let lastDateOfMonth = new Date(y, m, 0).getDate(); // 当月最后一天
- let lastDayOfLastMonth = new Date(y, m - 1, 0).getDate(); // 上一月的最后一天
- let weekstart = this.weekstart == 7 ? 0 : this.weekstart;
- let startDay = (() => {
- // 周初有几天是上个月的
- if (firstDayOfMonth == weekstart) {
- return 0;
- } else if (firstDayOfMonth > weekstart) {
- return firstDayOfMonth - weekstart;
- } else {
- return 7 - weekstart + firstDayOfMonth;
- }
- })();
- let endDay = 7 - ((startDay + lastDateOfMonth) % 7); // 结束还有几天是下个月的
- for (let i = 1; i <= startDay; i++) {
- dates.push({
- date: this.formatNum(lastDayOfLastMonth - startDay + i),
- day: weekstart + i - 1 || 7,
- month: m - 1 >= 0 ? this.formatNum(m - 1) : 12,
- year: m - 1 >= 0 ? y : y - 1
- });
- }
- for (let j = 1; j <= lastDateOfMonth; j++) {
- dates.push({
- date: this.formatNum(j),
- day: (j % 7) + firstDayOfMonth - 1 || 7,
- month: this.formatNum(m),
- year: y,
- isCurM: true //是否当前月份
- });
- }
- for (let k = 1; k <= endDay; k++) {
- dates.push({
- date: this.formatNum(k),
- day: (lastDateOfMonth + startDay + weekstart + k - 1) % 7 || 7,
- month: m + 1 <= 11 ? this.formatNum(m + 1) : 0,
- year: m + 1 <= 11 ? y : y + 1
- });
- }
- console.log(dates);
- return dates;
- },
- isWorkDay(y, m, d) {
- //是否工作日
- let ymd = `${y}/${m}/${d}`;
- let formatDY = new Date(ymd.replace(/-/g, '/'));
- let week = formatDY.getDay();
- if (week == 0 || week == 6) {
- return false;
- } else {
- return true;
- }
- },
- isFutureDay(y, m, d) {
- //是否未来日期
- let ymd = `${y}/${m}/${d}`;
- let formatDY = new Date(ymd.replace(/-/g, '/'));
- let showTime = formatDY.getTime();
- let curTime = new Date().getTime();
- if (showTime > curTime) {
- return true;
- } else {
- return false;
- }
- },
- // 标记日期
- isMarkDay(y, m, d) {
- let flag = false;
- for (let i = 0; i < this.markDays.length; i++) {
- let dy = `${y}-${m}-${d}`;
- if (this.markDays[i] == dy) {
- flag = true;
- break;
- }
- }
- return flag;
- },
- isToday(y, m, d) {
- let checkD = y + '-' + m + '-' + d;
- let today = this.getToday().date;
- if (checkD == today) {
- return true;
- } else {
- return false;
- }
- },
- // 展开收起
- toggle() {
- this.monthOpen = !this.monthOpen;
- if (this.monthOpen) {
- this.positionTop = 0;
- } else {
- let index = -1;
- this.dates.forEach((i, x) => {
- this.isToday(i.year, i.month, i.date) && (index = x);
- });
- this.positionTop = -((Math.ceil((index + 1) / 7) || 1) - 1) * 80;
- }
- },
- // 点击回调
- selectOne(i, event) {
- let date = `${i.year}-${i.month}-${i.date}`;
- let time = `${i.month}.${i.date}`
- let selectD = new Date(date).getTime();
- let curTime = new Date().getTime();
- let week = new Date(date).getDay();
- let weekText = ['日', '一', '二', '三', '四', '五', '六'];
- let formatWeek = '周' + weekText[week];
- let response = {
- date: date,
- time: time,
- week: formatWeek
- };
- if (!i.isCurM) {
- // console.log('不在当前月范围内');
- return false;
- }
- if (selectD > curTime) {
- if (this.disabledAfter) {
- console.log('未来日期不可选');
- return false;
- } else {
- this.choose = date;
- this.$emit('onDayClick', response);
- }
- } else {
- this.choose = date;
- this.$emit('onDayClick', response);
- }
- console.log(response);
- },
- //改变年月
- changYearMonth(y, m) {
- this.dates = this.monthDay(y, m);
- this.y = y;
- this.m = m;
- },
- changeMonth(type){
- if(type=='pre'){
- if (this.m + 1 == 2) {
- this.m = 12;
- this.y = this.y - 1;
- } else {
- this.m = this.m - 1;
- }
- let time = this.y + '-' + this.formatNum(this.m) + '-' + '01'
- this.$emit('onDateClick', time);
- }else{
- if (this.m + 1 == 13) {
- this.m = 1;
- this.y = this.y + 1;
- } else {
- this.m = this.m + 1;
- }
- let time = this.y + '-' + this.formatNum(this.m) + '-' + '01'
- this.$emit('onDateClick', time);
- }
- this.dates = this.monthDay(this.y, this.m);
- },
- /**
- * 触摸开始
- **/
- touchStart(e) {
- console.log("触摸开始")
- this.touchStartX = e.touches[0].clientX;
- this.touchStartY = e.touches[0].clientY;
- },
- /**
- * 触摸结束
- **/
- touchEnd(e) {
- console.log("触摸结束")
- let deltaX = e.changedTouches[0].clientX - this.touchStartX;
- let deltaY = e.changedTouches[0].clientY - this.touchStartY;
- console.log('%cren-calendar.vue line:320 deltaX', 'color: white; background-color: #26bfa5;', deltaX,deltaY);
- if (Math.abs(deltaX) > 50 && Math.abs(deltaX) > Math.abs(deltaY)) {
- if (deltaX >= 0) {
- console.log("左滑")
- this.changeMonth('pre')
- } else {
- console.log("右滑")
- this.changeMonth('next')
- }
- } else if(Math.abs(deltaY) > 50&& Math.abs(deltaX) < Math.abs(deltaY)) {
- if (deltaY < 0) {
- console.log("上滑")
- } else {
- console.log("下滑")
- }
- } else {
- console.log("可能是误触!")
- }
- },
- }
- };
- </script>
- <style lang="scss" scoped>
- .calendar-wrapper {
- color: #bbb7b7;
- font-size: 28rpx;
- text-align: center;
- background-color: #fff;
- padding-bottom: 10rpx;
-
- .header{
- display: flex;
- align-items: center;
- justify-content: space-between;
- width: 100vw;
- height: 88rpx;
- color: #000;
- font-size: 32rpx;
- // font-weight: bold;
- border-bottom: 2rpx solid #f2f2f2;
- .pre,.next{
- color: #00000088;
- font-size: 28rpx;
- font-weight: normal;
- padding: 8rpx 15rpx;
- border-radius: 10rpx;
- // border: 2rpx solid #dcdfe6;
- }
- .pre{
- margin-right: 30rpx;
- }
- .next{
- margin-left: 30rpx;
- }
- }
- .week {
- display: flex;
- align-items: center;
- height: 80rpx;
- line-height: 80rpx;
- border-bottom: 1rpx solid rgba(255, 255, 255, 0.2);
- view {
- flex: 1;
- }
- }
- .content {
- position: relative;
- overflow: hidden;
- transition: height 0.4s ease;
- .days {
- height: 100%;
- transition: top 0.3s;
- display: flex;
- align-items: center;
- flex-wrap: wrap;
- position: relative;
- overflow-y: auto;
- .item {
- position: relative;
- display: flex;
- justify-content: center;
- align-items: center;
- height: 80rpx;
- line-height: 80rpx;
- width: calc(100% / 7);
- .day {
- font-style: normal;
- display: inline-block;
- vertical-align: middle;
- width: 60rpx;
- height: 60rpx;
- line-height: 60rpx;
- overflow: hidden;
- border-radius: 60rpx;
- &.choose {
- background-color: #836BFF;
- color: #fff;
- }
- &.nolm {
- color: #fff;
- opacity: 0.3;
- }
- }
- .isWorkDay {
- color: #42464a;
- }
- .notSigned {
- font-style: normal;
- width: 8rpx;
- height: 8rpx;
- background: #fa7268;
- border-radius: 10rpx;
- position: absolute;
- left: 50%;
- bottom: 0;
- pointer-events: none;
- }
- .today {
- color: #fff;
- background-color: #836BFF88;
- }
- .workDay {
- font-style: normal;
- width: 8rpx;
- height: 8rpx;
- background: #4d7df9;
- border-radius: 10rpx;
- position: absolute;
- left: 50%;
- bottom: 0;
- pointer-events: none;
- }
- .markDay{
- font-style: normal;
- width: 6px;
- height: 6px;
- background: #31CA8A;
- border-radius: 10rpx;
- position: absolute;
- // left: 50%;
- bottom: -10%;
- pointer-events: none;
- }
- }
- }
- }
- .hide {
- height: 80rpx !important;
- }
- .weektoggle {
- width: 85rpx;
- height: 32rpx;
- position: relative;
- bottom: -42rpx;
- &.down {
- transform: rotate(180deg);
- bottom: 0;
- }
- }
- }
- </style>
|