diff --git a/pages/template/calendar/calendar.uts b/pages/template/calendar/calendar.uts index 80ddbf23d2a1422a3dc4f527f08c4d44fa1bddd8..0950683b88c73d73fa3b19373a39f363cc014b8f 100644 --- a/pages/template/calendar/calendar.uts +++ b/pages/template/calendar/calendar.uts @@ -43,8 +43,8 @@ const lunarYears = [ 0x0d520, 0x0daa0, 0x16aa6, 0x056d0, 0x04ae0, 0x0a9d4, 0x0a2d0, 0x0d150, 0x0f252, 0x0d520 ] -// ['月','正','一','二','三','四','五','六','七','八','九','十','冬','腊']; -const N_STR_3 = ["\u6708","\u6b63", "\u4e8c", "\u4e09", "\u56db", "\u4e94", "\u516d", "\u4e03", "\u516b", "\u4e5d", "\u5341", "\u51ac", "\u814a"] +// ['月','正','一','二','三','四','五','六','七','八','九','十','冬','腊']; +const N_STR_3 = ["\u6708", "\u6b63", "\u4e8c", "\u4e09", "\u56db", "\u4e94", "\u516d", "\u4e03", "\u516b", "\u4e5d", "\u5341", "\u51ac", "\u814a"] // ['日','一','二','三','四','五','六','七','八','九','十'] const N_STR_1 = ["\u65e5", "\u4e00", "\u4e8c", "\u4e09", "\u56db", "\u4e94", "\u516d", "\u4e03", "\u516b", "\u4e5d", "\u5341"] @@ -82,6 +82,8 @@ export type LunarInfoType = { export class Lunar { + private lunarYearDaysMap = new Map() + private lunarMonthDaysMap = new Map() constructor() { } /** * 传入农历数字月份返回汉语通俗表示法 @@ -90,10 +92,6 @@ export class Lunar { * @eg:let cnMonth = calendar.toChinaMonth(12) ;//cnMonth='腊月' */ toChinaMonth(m : number, leap : boolean = false) : string { // 月 => \u6708 - // if (m > 12 || m < 1) { return '' } // 若参数错误 返回-1 - // let s = N_STR_3[m - 1] - // s += '\u6708'// 加上月字 - // return s return leap ? (N_STR_3[4] + N_STR_3[m] + N_STR_3[0]) : (N_STR_3[m] + N_STR_3[0]); } @@ -148,27 +146,43 @@ export class Lunar { // 某年份农历各月天数 lunarMonthDays(year : number) : number[] { - let lunarYear = lunarYears[year - 1900]; - let monthDays : number[] = []; - for (let i = 4; i < 16; i++) { - let monthDay = (lunarYear >> i & 0x1) != 0 ? 30 : 29; - monthDays.push(monthDay); - } - monthDays.reverse(); - // 添加闰月 - let leapM = this.leapMonth(year); - if (leapM > 0) monthDays.splice(leapM, 0, leapDays(year)); - return monthDays; + + let monthDays = this.lunarMonthDaysMap.get(year) + if (monthDays != null) { + return monthDays + } + + monthDays = []; + + let lunarYear = lunarYears[year - 1900]; + + for (let i = 15; i >= 4; i--) { + let monthDay = (lunarYear >> i & 0x1) != 0 ? 30 : 29; + monthDays.push(monthDay); + } + + // 添加闰月 + let leapM = this.leapMonth(year); + + if (leapM > 0) monthDays.splice(leapM, 0, leapDays(year)); + this.lunarMonthDaysMap.set(year, monthDays) + + return monthDays; } // 某年农历天数 lunarYearDays(year : number) : number { + if (this.lunarYearDaysMap.has(year)) { + return this.lunarYearDaysMap.get(year)! + } let num = 0; this.lunarMonthDays(year).forEach(item => { num += item; }); + this.lunarYearDaysMap.set(year, num) return num; + } @@ -188,8 +202,8 @@ export class Lunar { let isLeap = moonDay.isLeap // 计算农历日期 - const IMonthCn = this.toChinaMonth(lMonth, isLeap) - + const IMonthCn = this.toChinaMonth(lMonth, isLeap) + let IDayCn = lDay == 1 ? IMonthCn : this.toChinaDay(lDay) // 是否今天 @@ -280,6 +294,4 @@ export class Lunar { return info } - - } \ No newline at end of file diff --git a/pages/template/calendar/calendar.uvue b/pages/template/calendar/calendar.uvue index 36f5996a7ff3c3994b9387b532b33092c8ca37b7..f786b99b7db0e396f614b5536b5c5dc3732b9078 100644 --- a/pages/template/calendar/calendar.uvue +++ b/pages/template/calendar/calendar.uvue @@ -167,9 +167,10 @@ drawWeek(weeks : Array>, time : string) { const start_time = Date.now() const refs = this.$refs['draw-weeks'] as INode - let ctx = refs.getDrawableContext() - const width = refs.getBoundingClientRect().width - const height = refs.getBoundingClientRect().height + let ctx = refs.getDrawableContext() + const dom = refs.getBoundingClientRect() + const width = dom.width + const height = dom.height let week_len = weeks.length const one_width = width / weeks[0].length const one_height = height / week_len diff --git a/pages/template/calendar/index.uts b/pages/template/calendar/index.uts index f70ff65ea70f75c29aa6b893322eb00c52af382e..0ac53ff43216c6bbcd89b599acb5ddba84f26044 100644 --- a/pages/template/calendar/index.uts +++ b/pages/template/calendar/index.uts @@ -12,8 +12,11 @@ export type DateType = { data ?: LunarInfoType } -export class Calendar { - constructor() { } +export class Calendar { + private lunar:Lunar + constructor() { + this.lunar =new Lunar() + } getDateInfo(time : string = '') : DateType { const nowDate = this.getDate(time) @@ -121,8 +124,7 @@ export class Calendar { * 计算阴历日期显示 */ getlunar(year : number, month : number, date : number) : LunarInfoType { - const lunar = new Lunar() - return lunar.solar2lunar(year, month, date) + return this.lunar.solar2lunar(year, month, date) }