index.js
5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* *
@Author: drfu*
@Description: 显示法定节假日班/休情况
@Date: 2020-10-12 14:29:45*
* @Last Modified by: drfu
* @Last Modified time: 2020-10-16 17:34:13
*/
import { holidays, festival } from './holidays-map'
import { dateUtil, getCalendarData, logger } from '../../utils/index'
/**
* 当前是否在休假期内
* @param {object} { year, month }
* @param {object} { start, end, current }
* @returns
*/
function inHolidays({ year, month }, { start, end, current }) {
const getTimeStamp = dateUtil.getTimeStamp
const startTimestamp = getTimeStamp({
year,
month,
date: start
})
const endTimestamp = getTimeStamp({
year,
month,
date: end
})
const currentDateTimestamp = getTimeStamp({
year,
month,
date: current
})
if (
currentDateTimestamp >= startTimestamp &&
currentDateTimestamp <= endTimestamp
) {
return true
}
return false
}
function addSpecialFestival(date, component) {
const { convertlLunar2Solar, convertSolarLunar } = component.calendar || {}
const lunarDateInfo = convertSolarLunar(date)
const { lYear, lMonth } = lunarDateInfo || {}
// 春节
const info = {
type: 'festival',
name: '除夕',
label: '除夕'
}
if (lMonth === 12) {
if (!festival.lunar['12']) festival.lunar['12'] = {}
if (convertlLunar2Solar(`${lYear}-12-30`) === -1) {
festival.lunar['12']['29'] = info
} else {
festival.lunar['12']['30'] = info
}
}
}
/**
* 是否匹配到节日
* @param {object} [dateInfo={}]
* @param {object} [component={}]
* @returns {object|boolean} 匹配到的节日数据或者false
*/
function hasFestivalDate(dateInfo = {}, component = {}) {
const { month, date } = dateInfo
let festivalDate = festival.solar[month] && festival.solar[month][date]
if (!festivalDate) {
const { convertSolarLunar } = component.calendar || {}
const lunarDateInfo = convertSolarLunar(dateInfo)
const { lMonth, lDay } = lunarDateInfo
festivalDate = festival.lunar[lMonth] && festival.lunar[lMonth][lDay]
if (!festivalDate) {
const festivalOfMonth = festival.lunar[lMonth] || {}
const festivalDateKey = Object.keys(festivalOfMonth).find(item =>
item.match(new RegExp(`\\b${lDay}\\b`))
)
if (!festivalDateKey) {
festivalDate = false
} else {
const festivalInfo = festival.lunar[lMonth][festivalDateKey]
if (!festivalInfo) {
festivalDate = false
} else {
const { condition } = festivalInfo
if (typeof condition === 'function') {
festivalDate = condition(lunarDateInfo)
} else {
festivalDate = false
}
}
}
}
}
return festivalDate
}
export default () => {
return {
name: 'holidays',
beforeRender(calendarData = {}, calendarConfig = {}, component) {
let { dates = [] } = calendarData
if (calendarConfig.showHolidays || calendarConfig.showFestival) {
dates = dates.map(d => {
let item = { ...d }
const { year, month, date } = item
const holidaysOfMonth =
(holidays[year] && holidays[year][month]) || {}
const holidayDate = holidaysOfMonth[date]
if (holidayDate) {
item = {
...item,
...holidayDate
}
} else {
const holidayKeys = Object.keys(holidaysOfMonth).filter(item =>
item.includes('-')
)
let target = ''
for (let v of holidayKeys) {
const [start, end] = v.split('-')
if (+d.date >= +start && +d.date <= +end) {
target = v
break
}
}
const [start, end] = target.split('-')
const isInHolidays = inHolidays(
{
year,
month
},
{
start,
end,
current: date
}
)
if (isInHolidays) {
item = {
...item,
...holidaysOfMonth[target]
}
} else if (calendarConfig.showFestival) {
const { convertSolarLunar, convertlLunar2Solar } =
component.calendar || {}
if (
typeof convertSolarLunar !== 'function' ||
typeof convertlLunar2Solar !== 'function'
) {
return logger.warn(
'农历节日显示需要引入农历插件(/component/v2/plugins/solarLunar)'
)
}
addSpecialFestival(item, component)
const festivalDate = hasFestivalDate(item, component)
if (festivalDate) {
item = {
...item,
...festivalDate
}
}
}
}
return item
})
}
return {
calendarData: {
...calendarData,
dates: dates
},
calendarConfig
}
},
methods(component) {
return {
getHolidaysOfCurrentYear() {
const calendar = getCalendarData('calendar', component)
const { curYear } = calendar
return this.methods(component).getHolidaysOfYear(curYear)
},
getHolidaysOfYear(year) {
if (!year) return logger.warn('getHolidaysOfCurrentYear() 入参错误')
if (!holidays[year]) {
logger.warn('未匹配到当前年份节假日信息,请自行补充')
return {
err: 'not match'
}
}
return holidays[year]
}
}
}
}
}