index.js 5.7 KB

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