index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. /**
  42. * 是否匹配到节日
  43. * @param {object} [dateInfo={}]
  44. * @param {object} [component={}]
  45. * @returns
  46. */
  47. function hasFestivalDate(dateInfo = {}, component = {}) {
  48. const { month, date } = dateInfo
  49. let festivalDate = festival.solar[month] && festival.solar[month][date]
  50. if (!festivalDate) {
  51. const { convertSolarLunar } = component.calendar || {}
  52. if (typeof convertSolarLunar === 'function') {
  53. const { lMonth, lDay } = convertSolarLunar(dateInfo)
  54. festivalDate = festival.lunar[lMonth] && festival.lunar[lMonth][lDay]
  55. } else {
  56. logger.warn(
  57. '农历节日显示需要引入农历插件(/component/v2/plugins/solarLunar)'
  58. )
  59. }
  60. }
  61. return festivalDate
  62. }
  63. export default () => {
  64. return {
  65. name: 'holidays',
  66. beforeRender(calendarData = {}, calendarConfig = {}, component) {
  67. let { dates = [] } = calendarData
  68. if (calendarConfig.showHolidays || calendarConfig.showFestival) {
  69. dates = dates.map(d => {
  70. let item = { ...d }
  71. const { year, month, date } = item
  72. const hasHolidaysOfThisMonth =
  73. (holidays[year] && holidays[year][month]) || {}
  74. const holidayDate = hasHolidaysOfThisMonth[date]
  75. if (holidayDate) {
  76. item = {
  77. ...item,
  78. ...holidayDate
  79. }
  80. } else {
  81. const holidayKeys = Object.keys(
  82. hasHolidaysOfThisMonth
  83. ).filter(item => item.includes('-'))
  84. let target = ''
  85. for (let v of holidayKeys) {
  86. const [start, end] = v.split('-')
  87. if (+d.date >= +start && +d.date <= +end) {
  88. target = v
  89. break
  90. }
  91. }
  92. const [start, end] = target.split('-')
  93. const isInHolidays = inHolidays(
  94. {
  95. year,
  96. month
  97. },
  98. {
  99. start,
  100. end,
  101. current: date
  102. }
  103. )
  104. if (isInHolidays) {
  105. item = {
  106. ...item,
  107. ...hasHolidaysOfThisMonth[target]
  108. }
  109. } else if (calendarConfig.showFestival) {
  110. const festivalDate = hasFestivalDate(item, component)
  111. if (festivalDate) {
  112. item = {
  113. ...item,
  114. ...festivalDate
  115. }
  116. }
  117. }
  118. }
  119. return item
  120. })
  121. }
  122. return {
  123. calendarData: {
  124. ...calendarData,
  125. dates: dates
  126. },
  127. calendarConfig
  128. }
  129. },
  130. methods(component) {
  131. return {
  132. getHolidaysOfCurrentYear() {
  133. const calendar = getCalendarData('calendar', component)
  134. const { curYear } = calendar
  135. return this.methods(component).getHolidaysOfYear(curYear)
  136. },
  137. getHolidaysOfYear(year) {
  138. if (!year) return logger.warn('getHolidaysOfCurrentYear() 入参错误')
  139. if (!holidays[year]) {
  140. logger.warn('未匹配到当前年份节假日信息,请自行补充')
  141. return {
  142. err: 'not match'
  143. }
  144. }
  145. return holidays[year]
  146. }
  147. }
  148. }
  149. }
  150. }