123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- <template>
- <view class="calendar__wrap">
- <view class="header">
- <view class="current-date">{{ currentDate }}</view>
- </view>
- <view class="body">
- <view class="weeks">
- <view class="week__item" v-for="week in weeks" :key="week">{{ week }}</view>
- </view>
- <view class="day__list">
- <view class="day__item" v-for="(item, index) in dateData" :key="index">
- <view class="checked-box" :class="[checksClass]" v-if="item === 'checked'">
- <text class="checked iconfavor" v-if="!checksIcon"></text>
- <image v-else :src="checksIcon" mode="aspectFit"></image>
- <view class="check_text" v-if="checkTextShow">{{ dayLoad(index) }}</view>
- </view>
- <text class="current-box" v-else>{{ item }}</text>
- </view>
- </view>
- </view>
- <view class="header" v-if="nextMounth">
- <view class="current-date">{{ currentDate1 }}</view>
- </view>
- <view class="body" v-if="nextMounth">
- <view class="day__list">
- <view class="day__item" v-for="(item, index) in dateData1" :key="index">
- <view class="checked-box" :class="[checksClass]" v-if="item === 'checked'">
- <text class="checked iconfavor" v-if="!checksIcon"></text>
- <image v-else :src="checksIcon" mode="aspectFit"></image>
- <view class="check_text" v-if="checkTextShow">{{ dayLoad(index) }}</view>
- </view>
- <text class="current-box" v-else>{{ item }}</text>
- </view>
- </view>
- </view>
- </view>
- </template>
- <script>
- export default {
- props: {
- //选中的日期数据
- checks: {
- type: Array,
- default() {
- // 例子[1,2,3,4],表示本月1,2,3,4号4天被选中
- return [];
- }
- },
- nextMounth: {
- type: Boolean,
- default: false
- },
- // 选中物品的样式类
- checksClass: {
- type: String,
- default: ''
- },
- // 选中时图标
- checksIcon: {
- type: String
- },
- // 选中时是否显示文字
- checkTextShow: {
- type: Boolean,
- default: false
- },
- // 表示当前日期的样式
- actionClass: {
- type: String,
- default: ''
- }
- },
- data() {
- const { year, month, day } = this.getDate();
- const dateData = this.getDateData(year, month);
- const dateData1 = this.getDateData(year, month + 1);
- const noDay = new Date(year, month - 1, 1).getDay();
- const noDay1 = new Date(year, month, 1).getDay();
- return {
- year,
- month,
- day,
- dateData,
- dateData1,
- noDay, //本月1号是星期几
- noDay1, //下个月1号是星期几
- weeks: ['日', '一', '二', '三', '四', '五', '六'],
- };
- },
- computed: {
- // 获取当前日期
- currentDate() {
- return `${this.year}-${this.format(this.month)}`;
- },
- // 获取下个月日期
- currentDate1() {
- return `${this.year}-${this.format(this.month + 1)}`;
- }
- },
- watch: {
- checks(val) {
- console.log(val,"123456");
- const { year, month } = this.getDate();
- const dateData = this.getDateData(year, month);
- const dateData1 = this.getDateData1(year, month + 1);
- // 保存当前月份信息
- this.dateData = dateData;
- this.dateData1 = dateData1;
- }
- },
- methods: {
- // 重新计算时间
- dayLoad: function(value) {
- if(this.nextMounth) {
- return value + 1 - this.noDay1;
- }else {
- return value + 1 - this.noDay;
- }
- },
- // 获取当前日期
- getDate(current) {
- const date = current ? new Date(current) : new Date();
- const year = date.getFullYear();
- // 月份值默认从0开始
- const month = date.getMonth() + 1;
- const day = date.getDate();
- return {
- year,
- month,
- day
- };
- },
- // 日期处理
- getDateData(year, month) {
- // 新增月份时需要减少1个月
- const date = new Date(year, month - 1, 1);
- const firstDayWeek = date.getDay();
- let data
- if(this.nextMounth) {
- data = [...this.getEmptys(firstDayWeek), ...this.getDays1(firstDayWeek)];
- }else {
- data = [...this.getEmptys(firstDayWeek), ...this.getDays(firstDayWeek)];
- }
- const day = new Date().getDate();
- if(this.nextMounth) {
- const cd = data.indexOf(day);
- let del = (cd - cd%7);
- for(let i=0;i<del;i++) {
- data.shift()
- }
- }
- console.log(data);
- return data;
- },
- // 下个月日期处理
- getDateData1(year, month) {
- // 新增月份时需要减少1个月
- const date = new Date(year, month - 1, 1);
- const firstDayWeek = date.getDay();
- let data
- if(this.nextMounth) {
- data = [...this.getEmptys(firstDayWeek), ...this.getDays(firstDayWeek)];
- }else {
- data = [...this.getEmptys(firstDayWeek), ...this.getDays1(firstDayWeek)];
- }
-
- data.length = 14
- return data;
- },
- // 查询日期列表有几个空格
- getEmptys(count) {
- let arr = [];
- if (count) {
- for (let i = 0; i < count; i++) {
- arr.push('');
- }
- }
- return arr;
- },
- getLastDay() {
- let { year, month } = this.getDate();
- month += 1;
- if (month > 11) {
- year += 1;
- month = 1;
- }
- let firstDayTimeStamp = new Date(`${year}/${month}/1`).getTime();
- let oneDayTimeStamp = 24 * 60 * 60 * 1000;
- let lastDay = new Date(firstDayTimeStamp - oneDayTimeStamp).getDate();
- return lastDay;
- },
- getDays() {
- const lastDay = this.getLastDay();
- const days = [];
- for (let i = 1; i <= lastDay; i++) {
- days.push(this.checks.includes(i) ? 'checked' : i);
- }
- return days;
- },
- getDays1() {
- const lastDay = this.getLastDay();
- const days = [];
- for (let i = 1; i <= lastDay; i++) {
- days.push(this.checks.includes(i) ? 'checked' : i);
- }
- return days;
- },
- format(num) {
- return num < 10 ? `0${num}` : num;
- }
- }
- };
- </script>
- <style lang="scss" scoped>
- .calendar__wrap {
- background-color: #fff;
- color: $uni-text-color;
- .header {
- padding: 0 24rpx;
- .current-date {
- text-align: center;
- font-size: $font-lg + 2rpx;
- // border-bottom: 2rpx solid #eee;
- padding: 32rpx 0;
- }
- }
- .body {
- .weeks {
- display: flex;
- font-size: $font-lg;
- padding: 10rpx 0;
- background-color: #f4f7ff;
- .week__item {
- flex: 1;
- text-align: center;
- }
- }
- .day__list {
- display: flex;
- flex-wrap: wrap;
- .day__item {
- display: flex;
- justify-content: center;
- width: 14.285%;
- text-align: center;
- padding: 30rpx 0;
- font-size: 34rpx;
- color: $font-color-light;
- .checked-box,
- .current-box {
- display: flex;
- align-items: center;
- justify-content: center;
- border-radius: 100%;
- box-sizing: border-box;
- position: relative;
- }
- image,
- .checked-box,
- .current-box,
- .check_text {
- width: 56rpx;
- height: 56rpx;
- line-height: 56rpx;
- }
- image,
- .check_text {
- position: absolute;
- top: 0;
- left: 0;
- }
- .checked {
- width: 56rpx;
- height: 56rpx;
- border-radius: 6rpx;
- font-size: 40rpx;
- background-color: #fbed66;
- color: #fff;
- }
- .current {
- padding: 18rpx;
- background-color: #fbed66;
- color: #fff;
- font-size: 28rpx;
- }
- }
- }
- }
- }
- </style>
|