APP & program

原生微信小程序实现打卡日历组件

2023-03-02  本文已影响0人  风中凌乱的男子
背景:最近接了个外包,其中有个打卡功能,里面包含了一个日历,甲方要求百分百还原,闲暇之余就撸了一下,下面是效果图 源码地址 https://github.com/chenxuba/xx-calendar 求star ✨
image.png
分析:
想法💡:网上都没一毛一样的组件,那还想个der,自己撸一个啊~
组件我起名:xx-calendar
image.png
{
  "usingComponents": {
      "xx-calendar":"../../components/xx-calendar/xx-calendar"
  }
}
<xx-calendar></xx-calendar>
image.png
<!--components/xx-calendar/xx-calendar.wxml-->
<!-- 头部 -->
<view class="title-wrap">
    <view class="change-date">
        <view class="prev">
            <image src="./prev.png" mode="" />
        </view>
        <view class="year-mouth">
            2023年 1月
        </view>
        <view clstyle="next">
            <image src="./next.png" mode="" />
        </view>
    </view>
    <view class="week">
        <text>日</text>
        <text>一</text>
        <text>二</text>
        <text>三</text>
        <text>四</text>
        <text>五</text>
        <text>六</text>
    </view>
</view>
<!-- 日期 -->
<view class="date-wrap">
    <!-- 上个月日期 -->
    <view class="mouth-date last-mouth">
        <text class="day-text">29</text>
        <text class="day-nongli">十五</text>
        <text class="day-dot"></text>
    </view>
    <view class="mouth-date last-mouth">
        <text class="day-text">30</text>
        <text class="day-nongli">十六</text>
        <text class="day-dot"></text>
    </view>
    <view class="mouth-date last-mouth">
        <text class="day-text">31</text>
        <text class="day-nongli">十七</text>
        <text class="day-dot"></text>
    </view>
    <!-- 当月日期 -->
    <view class="mouth-date current-mouth" wx:for="{{31}}">
       <view class="day-box {{index==0?'active':''}}">
        <text class="day-text {{index==2||index==4||index==6||index==8?'color':''}}">{{index+1}}</text>
        <text class="day-nongli">十八</text>
        <text class=" {{index==2||index==4||index==6||index==8?'day-dot':'not-dot'}}"></text>
       </view>
    </view>
    <!-- 下个月日期 -->
    <view class="mouth-date next-mouth">
        <text class="day-text">1</text>
        <text class="day-nongli">十五</text>
        <text class="day-dot"></text>
    </view>
</view>
/* components/xx-calendar/xx-calendar.wxss */
/* 头部样式start */
.title-wrap{
    background-color: #fff;
    padding-top: 20rpx;
    border-bottom: 1px solid #D4DBDC;
    padding-bottom: 10rpx;
}
.change-date{
    display: flex;
    justify-content: center;
    align-items: center;
}
.change-date image{
    width: 50rpx;
    height: 50rpx;
    transform: scale(1.5);
    display: flex;
}
.year-mouth{
    margin: 0 60rpx;
}
.week{
    display: flex;
    justify-content: space-between;
    font-size: 24rpx;
    margin-top: 30rpx;
    color: #1F1F1F;
}
.week text{
    flex: 1;
    text-align: center;
}
/* 头部样式end */
/* 日期区域样式start */
.date-wrap{
    height: 500rpx;
    background-color: #F8F9F9;
    display: flex;
    flex-wrap: wrap;
}
.mouth-date{
    display: flex;
    font-size: 24rpx;
    flex-direction: column;
    align-items: center;
    width: calc(100% / 7);
    border-bottom: 1rpx solid rgb(229, 234, 235);
    padding-top: 10rpx;
}
.last-mouth text,.next-mouth text{
    opacity: 0;
}
.mouth-date .day{
    display: flex;
    flex-direction: column;
    align-items: center;
    color: #1F1F1F;

}
.mouth-date  .day-nongli{
    font-size: 18rpx;
    color: #888888;
    margin-bottom: 6rpx;
}
.mouth-date .color{
    color: #8096F0;
}
.mouth-date  .day-dot{
    width: 8rpx;
    height: 8rpx;
    border-radius: 50%;
    background-color: #8096F0;
}
.mouth-date .day-box{
    border-radius: 10rpx;
    width: 80rpx;
    padding-top: 6rpx;
    display: flex;
    flex-direction: column;
    align-items: center;
}
.mouth-date .active{
    background-color: #A1D7EE;
}
.mouth-date .active text{
    color: #fff;
}
.mouth-date .active .day-text{
    font-weight: 600;
}
.not-dot{
    width: 8rpx;
    height: 8rpx;
}
image.png
data:{
   year: new Date().getFullYear(),
   month: new Date().getMonth() + 1,
   weeksArr: ['日', '一', '二', '三', '四', '五', '六'],
}
image.png
image.png
data:{
        nowMonth: new Date().getMonth() + 1, //本月是几月
        nowDay: new Date().getDate(), //本月当天的日期
        lastMonthDays: [], //上一个月
        nowMonthDays: [], //本月 
        nextMonthDays: [], //下一个月
}
 /**
     * 组件的方法列表
     */
    methods: {
        /** 获取上个月日期 */
        getLastMonthDays(year,month){},
       /** 获取当月日期 */
        getNowMonthDays(year, month) {},
        /** 获取下个月日期 */
        getNextMonthDays(year, month) {},
    }
 /**
     * 组件的方法列表
     */
    methods: {
         //获取当月天数
        getThisMonthDays(year, month) {
            return new Date(year, month, 0).getDate();
        },
        /** 总方法 */
        //创建日期
        createDays(year, month) {
            this.getLastMonthDays(year, month)
            this.getNowMonthDays(year, month)
            this.getNextMonthDays(year, month)
        },
        /** 获取上个月日期 */
        getLastMonthDays(year, month) {},
       /** 获取当月日期 */
        getNowMonthDays(year, month) {},
        /** 获取下个月日期 */
        getNextMonthDays(year, month) {},
    },
    ready(){
        let { year,month } = this.data
        this.createDays(year,month)
    }

/** 获取上个月日期 */
        getLastMonthDays(year, month) {
            let nowMonthFirstDays = new Date(year, month - 1, 1).getDay()
            let lastMonthDays = []
            if (nowMonthFirstDays) { //判断当月的第一天是不是星期天
                //上个月显示多少天
                let lastMonthNums = month - 1 < 0 ? this.getThisMonthDays(year - 1, 12) : this.getThisMonthDays(year, month - 1); //判断是否会跨年
                //上个月从几号开始显示
                for (let i = lastMonthNums - nowMonthFirstDays + 1; i <= lastMonthNums; i++) {
                    let time = new Date(year, month - 2, i).toLocaleDateString() //对应的时间
                    lastMonthDays.push({
                        date: i, //几号
                        week: this.data.weeksArr[new Date(year, month - 2, i).getDay()], //星期几
                        time,
                        isNowMonthDay: ''
                    });
                }
            }
            this.setData({
                lastMonthDays
            })
            console.log(lastMonthDays);
        },
image.png
image.png
 getNowMonthDays(year, month) {
            let {
                nowMonth,
                nowDay
            } = this.data
            let nowMonthDays = []
            let days = this.getThisMonthDays(year, month); //获取当月的天数
            for (let i = 1; i <= days; i++) {
                let d = new Date(year, month - 1, i)
                let years = d.getFullYear()
                let months = d.getMonth() + 1
                let day = d.getDate()
                let time = `${years+'/'+months +'/'+day}` // 2023/3/3
                nowMonthDays.push({
                    date: i, //几号
                    week: this.data.weeksArr[new Date(year, month - 1, i).getDay()], //星期几
                    time,
                    color: false, //为已打卡日期样式做准备
                    day, //后面会改成农历
                    isNowMonthDay: (month == nowMonth && i == nowDay) ? "isNowMonthDay" : ""
                });
            }
            this.setData({
                nowMonthDays
            })
            console.log(nowMonthDays);
        },
image.png
/** 获取下个月日期 */
        getNextMonthDays(year, month) {
            let {
                lastMonthDays,
                nowMonthDays,
            } = this.data
            let nextMonthDays = []
            let nextMonthNums = (lastMonthDays.length + nowMonthDays.length) > 35 ? 42 - (lastMonthDays.length + nowMonthDays.length) : 35 - (lastMonthDays.length + nowMonthDays.length) //下个月显示多少天
            let nowYear = (parseInt(month) + 1) > 12 ? year + 1 : year //下一个月的年份
            let nowMonth = (parseInt(month) + 1) > 12 ? 1 : parseInt(month) + 1 //下一个月的月份
            if (nextMonthNums) { //判断当前天数是否大于零
                for (let i = 1; i <= nextMonthNums; i++) {
                    let time = new Date(year, month - 1, i).toLocaleDateString()
                    nextMonthDays.push({
                        date: i, //几号
                        week: this.data.weeksArr[new Date(nowYear, nowMonth - 1, i).getDay()], //星期几
                        time,
                        isNowMonthDay: ''
                    });
                }
            }
            this.setData({
                nextMonthDays
            })
            console.log(nextMonthDays)
        },
image.png
image.png
image.png
import calendarFormatter from "./index";
 getNowMonthDays(year, month) {
            let {
                nowMonth,
                nowDay
            } = this.data
            let nowMonthDays = []
            let days = this.getThisMonthDays(year, month); //获取当月的天数
            for (let i = 1; i <= days; i++) {
                let d = new Date(year, month - 1, i)
                let years = d.getFullYear()
                let months = d.getMonth() + 1
                let day2 = d.getDate()
                let time = `${years+'/'+months +'/'+day2}` // 2023/3/3
                let timer = time.replace(/\//g, "-")
                let timer2 = timer.split('-')
                var day = calendarFormatter.solar2lunar(timer2[0], timer2[1], timer2[2]);
                let newdate
                if (day.IDayCn == '初一') {
                    newdate = day.IMonthCn
                } else {
                    newdate = day.IDayCn
                }
                nowMonthDays.push({
                    date: i, //几号
                    week: this.data.weeksArr[new Date(year, month - 1, i).getDay()], //星期几
                    time,
                    color: false,
                    day: newdate,
                    isNowMonthDay: (month == nowMonth && i == nowDay) ? "isNowMonthDay" : ""
                });
            }
            this.setData({
                nowMonthDays
            })
            console.log(nowMonthDays);
        },
嗯~实现了
image.png
image.png
/** 切换月份 */
        changeMonthFun(e){
            let {
                year,
                month
            } = this.data
            let type = e.currentTarget.dataset.type //类型
            if (type == 'prev') { //上一个月
                year = month - 1 > 0 ? year : year - 1
                month = month - 1 > 0 ? month - 1 : 12
            } else { //next 下个月
                year = (parseInt(month) + 1) > 12 ? year + 1 : year
                month = (parseInt(month) + 1) > 12 ? 1 : parseInt(month) + 1
            }
            this.setData({
                year,
                month,
            })
            this.createDays(year, month)
        }
image.png
image.png
image.png
selectDate(e){
            let type = e.currentTarget.dataset.type //选择的时间类型
            let index = e.currentTarget.dataset.index //选择的下标
            let date = e.currentTarget.dataset.item.time //选择的下标
            let selectDate = date.replace(/\//g, "-")
            console.log("选择的时间", selectDate)
            // 自定义事件,父组件调用,回调 选择的时间selectDate
            this.triggerEvent('selectDate', selectDate)
             //将选择的时间类型的 isNowMonthDay 全改为''
             this.data[type]?.forEach(item => {
                item.isNowMonthDay = ''
            })
            this.data[type]?.forEach((item, idx) => {
                if (index == idx) {
                    item.isNowMonthDay = (item.time == new Date().toLocaleDateString() ? "isNowMonthDay" : "isNotNowMonthDay"); //判断当前选中的日期是否是当前时间
                } else {
                    item.isNowMonthDay = ''
                }
            })
            this.setData({
                [type]: this.data[type],
            })
        },
image.png
  /**
     * 组件的属性列表
     */
    properties: {
        use_date_arr:{
            type:Array,
            value:[]
        }
    },
 this.data.use_date_arr.forEach(ele => {
                ele = ele.replace(/\-/g, "/")
                nowMonthDays.forEach(item => {
                    if (ele == item.time) {
                        console.log(item);
                        item.color = true
                    }
                })
            })
image.png
data: {
    use_date_arr:['2023-3-1','2023-3-2','2023-3-3','2023-3-5','2023-3-8']
  },
<xx-calendar use_date_arr="{{use_date_arr}}"></xx-calendar>
image.png
image.png
image.png
上一篇 下一篇

猜你喜欢

热点阅读