import CALENDAR from './calendar.js' class Calendar { constructor({ date, selected, startDate, endDate, range } = {}) { // 当剿—¥æœŸ this.date = this.getDate(new Date()) // 当å‰åˆå…¥æ—¥æœŸ // æ‰“ç‚¹ä¿¡æ¯ this.selected = selected || []; // 范围开始 this.startDate = startDate // èŒƒå›´ç»“æŸ this.endDate = endDate this.range = range // å¤šé€‰çŠ¶æ€ this.cleanMultipleStatus() // æ¯å‘¨æ—¥æœŸ this.weeks = {} // this._getWeek(this.date.fullDate) } /** * 设置日期 * @param {Object} date */ setDate(date) { this.selectDate = this.getDate(date) this._getWeek(this.selectDate.fullDate) } /** * 清ç†å¤šé€‰çŠ¶æ€ */ cleanMultipleStatus() { this.multipleStatus = { before: '', after: '', data: [] } } /** * é‡ç½®å¼€å§‹æ—¥æœŸ */ resetSatrtDate(startDate) { // 范围开始 this.startDate = startDate } /** * é‡ç½®ç»“æŸæ—¥æœŸ */ resetEndDate(endDate) { // èŒƒå›´ç»“æŸ this.endDate = endDate } /** * 获å–ä»»æ„æ—¶é—´ */ getDate(date, AddDayCount = 0, str = 'day') { if (!date) { date = new Date() } if (typeof date !== 'object') { date = date.replace(/-/g, '/') } const dd = new Date(date) switch (str) { case 'day': dd.setDate(dd.getDate() + AddDayCount) // 获å–AddDayCount天åŽçš„æ—¥æœŸ break case 'month': if (dd.getDate() === 31 && AddDayCount>0) { dd.setDate(dd.getDate() + AddDayCount) } else { const preMonth = dd.getMonth() dd.setMonth(preMonth + AddDayCount) // 获å–AddDayCount天åŽçš„æ—¥æœŸ const nextMonth = dd.getMonth() // å¤„ç† pre åˆ‡æ¢æœˆä»½ç›®æ ‡æœˆä»½ä¸º2æœˆæ²¡æœ‰å½“å‰æ—¥(30 31) 切æ¢é”™è¯¯é—®é¢˜ if(AddDayCount<0 && preMonth!==0 && nextMonth-preMonth>AddDayCount){ dd.setMonth(nextMonth+(nextMonth-preMonth+AddDayCount)) } // å¤„ç† next åˆ‡æ¢æœˆä»½ç›®æ ‡æœˆä»½ä¸º2æœˆæ²¡æœ‰å½“å‰æ—¥(30 31) 切æ¢é”™è¯¯é—®é¢˜ if(AddDayCount>0 && nextMonth-preMonth>AddDayCount){ dd.setMonth(nextMonth-(nextMonth-preMonth-AddDayCount)) } } break case 'year': dd.setFullYear(dd.getFullYear() + AddDayCount) // 获å–AddDayCount天åŽçš„æ—¥æœŸ break } const y = dd.getFullYear() const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获å–当剿œˆä»½çš„æ—¥æœŸï¼Œä¸è¶³10è¡¥0 const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获å–当å‰å‡ å·ï¼Œä¸è¶³10è¡¥0 return { fullDate: y + '-' + m + '-' + d, year: y, month: m, date: d, day: dd.getDay() } } /** * 获å–上月剩余天数 */ _getLastMonthDays(firstDay, full) { let dateArr = [] for (let i = firstDay; i > 0; i--) { const beforeDate = new Date(full.year, full.month - 1, -i + 1).getDate() dateArr.push({ date: beforeDate, month: full.month - 1, lunar: this.getlunar(full.year, full.month - 1, beforeDate), disable: true }) } return dateArr } /** * èŽ·å–æœ¬æœˆå¤©æ•° */ _currentMonthDys(dateData, full) { let dateArr = [] let fullDate = this.date.fullDate for (let i = 1; i <= dateData; i++) { let nowDate = full.year + '-' + (full.month < 10 ? full.month : full.month) + '-' + (i < 10 ? '0' + i : i) // 是å¦ä»Šå¤© let isDay = fullDate === nowDate // èŽ·å–æ‰“ç‚¹ä¿¡æ¯ let info = this.selected && this.selected.find((item) => { if (this.dateEqual(nowDate, item.date)) { return item } }) // 日期ç¦ç”¨ let disableBefore = true let disableAfter = true if (this.startDate) { // let dateCompBefore = this.dateCompare(this.startDate, fullDate) // disableBefore = this.dateCompare(dateCompBefore ? this.startDate : fullDate, nowDate) disableBefore = this.dateCompare(this.startDate, nowDate) } if (this.endDate) { // let dateCompAfter = this.dateCompare(fullDate, this.endDate) // disableAfter = this.dateCompare(nowDate, dateCompAfter ? this.endDate : fullDate) disableAfter = this.dateCompare(nowDate, this.endDate) } let multiples = this.multipleStatus.data let checked = false let multiplesStatus = -1 if (this.range) { if (multiples) { multiplesStatus = multiples.findIndex((item) => { return this.dateEqual(item, nowDate) }) } if (multiplesStatus !== -1) { checked = true } } let data = { fullDate: nowDate, year: full.year, date: i, multiple: this.range ? checked : false, beforeMultiple: this.dateEqual(this.multipleStatus.before, nowDate), afterMultiple: this.dateEqual(this.multipleStatus.after, nowDate), month: full.month, lunar: this.getlunar(full.year, full.month, i), disable: !(disableBefore && disableAfter), isDay } if (info) { data.extraInfo = info } dateArr.push(data) } return dateArr } /** * 获å–下月天数 */ _getNextMonthDays(surplus, full) { let dateArr = [] for (let i = 1; i < surplus + 1; i++) { dateArr.push({ date: i, month: Number(full.month) + 1, lunar: this.getlunar(full.year, Number(full.month) + 1, i), disable: true }) } return dateArr } /** * 获å–当剿—¥æœŸè¯¦æƒ… * @param {Object} date */ getInfo(date) { if (!date) { date = new Date() } const dateInfo = this.canlender.find(item => item.fullDate === this.getDate(date).fullDate) return dateInfo } /** * æ¯”è¾ƒæ—¶é—´å¤§å° */ dateCompare(startDate, endDate) { // è®¡ç®—æˆªæ¢æ—¶é—´ startDate = new Date(startDate.replace('-', '/').replace('-', '/')) // è®¡ç®—è¯¦ç»†é¡¹çš„æˆªæ¢æ—¶é—´ endDate = new Date(endDate.replace('-', '/').replace('-', '/')) if (startDate <= endDate) { return true } else { return false } } /** * 比较时间是å¦ç›¸ç‰ */ dateEqual(before, after) { // è®¡ç®—æˆªæ¢æ—¶é—´ before = new Date(before.replace('-', '/').replace('-', '/')) // è®¡ç®—è¯¦ç»†é¡¹çš„æˆªæ¢æ—¶é—´ after = new Date(after.replace('-', '/').replace('-', '/')) if (before.getTime() - after.getTime() === 0) { return true } else { return false } } /** * èŽ·å–æ—¥æœŸèŒƒå›´å†…所有日期 * @param {Object} begin * @param {Object} end */ geDateAll(begin, end) { var arr = [] var ab = begin.split('-') var ae = end.split('-') var db = new Date() db.setFullYear(ab[0], ab[1] - 1, ab[2]) var de = new Date() de.setFullYear(ae[0], ae[1] - 1, ae[2]) var unixDb = db.getTime() - 24 * 60 * 60 * 1000 var unixDe = de.getTime() - 24 * 60 * 60 * 1000 for (var k = unixDb; k <= unixDe;) { k = k + 24 * 60 * 60 * 1000 arr.push(this.getDate(new Date(parseInt(k))).fullDate) } return arr } /** * 计算阴历日期显示 */ getlunar(year, month, date) { return CALENDAR.solar2lunar(year, month, date) } /** * 设置打点 */ setSelectInfo(data, value) { this.selected = value this._getWeek(data) } /** * 获å–å¤šé€‰çŠ¶æ€ */ setMultiple(fullDate) { let { before, after } = this.multipleStatus if (!this.range) return if (before && after) { this.multipleStatus.before = fullDate this.multipleStatus.after = '' this.multipleStatus.data = [] } else { if (!before) { this.multipleStatus.before = fullDate } else { this.multipleStatus.after = fullDate if (this.dateCompare(this.multipleStatus.before, this.multipleStatus.after)) { this.multipleStatus.data = this.geDateAll(this.multipleStatus.before, this.multipleStatus.after); } else { this.multipleStatus.data = this.geDateAll(this.multipleStatus.after, this.multipleStatus.before); } } } this._getWeek(fullDate) } /** * èŽ·å–æ¯å‘¨æ•°æ® * @param {Object} dateData */ _getWeek(dateData) { const { year, month } = this.getDate(dateData) let firstDay = new Date(year, month - 1, 1).getDay() let currentDay = new Date(year, month, 0).getDate() let dates = { lastMonthDays: this._getLastMonthDays(firstDay, this.getDate(dateData)), // ä¸Šä¸ªæœˆæœ«å°¾å‡ å¤© currentMonthDys: this._currentMonthDys(currentDay, this.getDate(dateData)), // 本月天数 nextMonthDays: [], // ä¸‹ä¸ªæœˆå¼€å§‹å‡ å¤© weeks: [] } let canlender = [] const surplus = 42 - (dates.lastMonthDays.length + dates.currentMonthDys.length) dates.nextMonthDays = this._getNextMonthDays(surplus, this.getDate(dateData)) canlender = canlender.concat(dates.lastMonthDays, dates.currentMonthDys, dates.nextMonthDays) let weeks = {} // 拼接数组 ä¸Šä¸ªæœˆå¼€å§‹å‡ å¤© + 本月天数+ ä¸‹ä¸ªæœˆå¼€å§‹å‡ å¤© for (let i = 0; i < canlender.length; i++) { if (i % 7 === 0) { weeks[parseInt(i / 7)] = new Array(7) } weeks[parseInt(i / 7)][i % 7] = canlender[i] } this.canlender = canlender this.weeks = weeks } //陿€æ–¹æ³• // static init(date) { // if (!this.instance) { // this.instance = new Calendar(date); // } // return this.instance; // } } export default Calendar