const imgurl = require("../../utils/imgurl"); Component({ /** * 组件的属性列表 */ properties: { activityStyle: String, allFlag:Boolean, }, /** * 组件的初始数据 */ data: { week: ['日', '一', '二', '三', '四', '五', '六'], dateLsit: [], year: new Date().getFullYear(), month: new Date().getMonth() + 1, yearFlgh: new Date().getFullYear(),//当前年 monthFlgh: new Date().getMonth() + 1,//当前月 dey: new Date().getDate(), thendata: [], //当前这一周的数组 xiala: imgurl.concealBnt.url, switchFlag:true, }, /** * 组件的方法列表 */ methods: { switchs(){ let temp = this.data.switchFlag this.setData({ switchFlag:!temp }) console.log(this.data.switchFlag) }, showDate(year, month) { //刷新日历 let thenDey = this.getThenWeek(year, month) //每个月第一天星期几 let thenMonth = this.getDays(year, month) //获取每月有多少天 let lastMonth = this.getLastMonth(year, month) //上月最后一天是多少天 let tempDey = [] let tmepArr = [] for (let i = 0; i < 42; i++) { //初始化日历数组 tempDey.splice(i, 0, "") } for (let i = 0; i < thenMonth; i++) { //获取当前选中月份天数 tmepArr.splice(i, 0, { style: "0", value: i + 1, y:year, m: month }) } tempDey.splice(thenDey, thenMonth, ...tmepArr) for (let i = 0; i < thenDey; i++) { //添加上一个月的天数 tempDey.splice(i, 1, { style: "1", value: lastMonth - (thenDey - 1), y: year, m: month-1 }) lastMonth++ } for (let i = 0; i < 42 - (thenDey + thenMonth); i++) { //设置下一个月 tempDey.splice(thenDey + thenMonth + i, 1, { style: "2", value: i + 1, y: year, m: month+1 }) } this.setData({ dateLsit: tempDey }) console.log(tempDey,"?") }, handle(e) { //点击号数 let item = e.currentTarget.dataset.item console.log(item) if (item.style == '1') { this.front() } else if (item.style == '2') { this.behind() } }, getDays(year, month) { //获取每月有多少天 let tempDateArr = [] let thenDate = new Date(year, month, 0).getDate() return thenDate }, getLastMonth(year, month) { //上一个月最后一天是多少号 if (month == 1) { month = 12 year = year - 1 } else { month = month - 1 } let tempDateArr = [] let thenDate = new Date(year, month, 0).getDate() return thenDate }, getThenWeek(year, month) { //获取每个月第一天是星期几 if (month == 1) { month = 12 year = year - 1 } else { month = month - 1 } let date = new Date() date.setFullYear(year) date.setMonth(month) date.setDate(1) return date.getDay() }, front() { //前面 if (this.data.month == 1) { let temp = this.data.switchFlag this.setData({ switchFlag: false }) this.setData({ year: this.data.year - 1, month: 12 }) this.showDate(this.data.year, this.data.month) } else { this.setData({ month: this.data.month - 1 }) this.showDate(this.data.year, this.data.month) } }, behind() { //后面 let temp = this.data.switchFlag this.setData({ switchFlag: false }) if (this.data.month == 12) { this.setData({ year: this.data.year + 1, month: 1 }) this.showDate(this.data.year, this.data.month) } else { this.setData({ month: this.data.month + 1 }) this.showDate(this.data.year, this.data.month) } }, }, lifetimes: { attached: function() { // 在组件实例进入页面节点树时执行 let thenDey = new Date().getDay() //今天是周几 let thenDate = new Date().getDate() //今天是多少号 let tempArr = [] if (thenDey == 0) { for (let i = 0; i < 7; i++) { tempArr.splice(i, 1, { thenFalg: "1", value: thenDate, style:"0", y: this.data.year, m:this.data.month }) thenDate++ } } else { let temp = new Date().getDay() //今天是周几 let tmpeDey = new Date().getDate() //今天是多少号 for (let i = 0; i <= temp; i++) { tempArr.splice(i, 0, { style: "0", value: thenDate - thenDey, y: this.data.year, m: this.data.month} ) thenDey-- } for (let i = temp + 1; i < 7; i++) { tmpeDey++ tempArr.splice(i, 0, { style: "0", value: tmpeDey, y: this.data.year, m: this.data.month} ) } } this.setData({ thendata: tempArr }) console.log(this.data.thendata, "?????") this.showDate(this.data.year, this.data.month) }, detached: function() { // 在组件实例被从页面节点树移除时执行 }, }, })