index.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { dateUtil } from '../../utils/index'
  2. import convertSolarLunar from './convertSolarLunar'
  3. function getDateRow2Dict(dateInfo) {
  4. if (!dateInfo) return dateInfo
  5. if (typeof dateInfo === 'string' && dateInfo.includes('-')) {
  6. dateInfo = dateUtil.transformDateRow2Dict(dateInfo)
  7. }
  8. return dateInfo
  9. }
  10. export default () => {
  11. return {
  12. name: 'convertSolarLunar',
  13. beforeRender(calendarData = {}, calendarConfig = {}) {
  14. let { dates = [], selectedDates = [] } = calendarData
  15. if (calendarConfig.showLunar) {
  16. dates = dates.map(dataInfo => {
  17. const { year, month, date } = dataInfo
  18. return {
  19. ...dataInfo,
  20. lunar: convertSolarLunar.solar2lunar(year, month, date)
  21. }
  22. })
  23. selectedDates = selectedDates.map(dataInfo => {
  24. const { year, month, date } = dataInfo
  25. return {
  26. ...dataInfo,
  27. lunar: convertSolarLunar.solar2lunar(year, month, date)
  28. }
  29. })
  30. }
  31. return {
  32. calendarData: {
  33. ...calendarData,
  34. dates: dates,
  35. selectedDates: selectedDates
  36. },
  37. calendarConfig
  38. }
  39. },
  40. methods() {
  41. return {
  42. convertSolarLunar: dateInfo => {
  43. dateInfo = getDateRow2Dict(dateInfo)
  44. if (!dateInfo) return dateInfo
  45. const { year, month, date } = dateInfo
  46. return convertSolarLunar.solar2lunar(year, month, date)
  47. },
  48. convertlLunar2Solar: (dateInfo, isLeapMonth) => {
  49. dateInfo = getDateRow2Dict(dateInfo)
  50. if (!dateInfo) return dateInfo
  51. const { year, month, date } = dateInfo
  52. return convertSolarLunar.lunar2solar(year, month, date, isLeapMonth)
  53. }
  54. }
  55. }
  56. }
  57. }