get-calendar-data.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * @Author: drfu*
  3. * @Description: 获取日历数据
  4. * @Date: 2020-10-08 21:22:09*
  5. * @Last Modified by: drfu
  6. * @Last Modified time: 2020-10-11 13:42:37
  7. * */
  8. import { getCalendarData, logger, getCalendarConfig } from '../../utils/index'
  9. function wrapDateWithLunar(dates = [], convertFn) {
  10. const datesWithLunar = JSON.parse(JSON.stringify(dates)).map(date => ({
  11. ...date,
  12. lunar: convertFn(date)
  13. }))
  14. return datesWithLunar
  15. }
  16. export default () => {
  17. return {
  18. name: 'getData',
  19. methods(component) {
  20. return {
  21. getCurrentYM: () => {
  22. const { curYear, curMonth } = getCalendarData('calendar', component)
  23. return {
  24. year: curYear,
  25. month: curMonth
  26. }
  27. },
  28. getSelectedDates: (options = {}) => {
  29. const dates =
  30. getCalendarData('calendar.selectedDates', component) || []
  31. const config = getCalendarConfig(component) || {}
  32. if (options.lunar && !config.showLunar) {
  33. const injectedFns = component.calendar || {}
  34. if (typeof injectedFns.convertSolarLunar === 'function') {
  35. return wrapDateWithLunar(dates, injectedFns.convertSolarLunar)
  36. } else {
  37. logger.warn('获取农历信息需引入农历插件')
  38. }
  39. } else {
  40. return dates
  41. }
  42. },
  43. getCalendarDates: (options = {}) => {
  44. const config = getCalendarConfig(component) || {}
  45. const dates = getCalendarData('calendar.dates', component)
  46. if (options.lunar && !config.showLunar) {
  47. const injectedFns = component.calendar || {}
  48. if (typeof injectedFns.convertSolarLunar === 'function') {
  49. return wrapDateWithLunar(dates, injectedFns.convertSolarLunar)
  50. } else {
  51. logger.warn('获取农历信息需引入农历插件')
  52. }
  53. } else {
  54. return dates
  55. }
  56. },
  57. getCalendarAllData: () => {
  58. return {
  59. data: getCalendarData('calendar', component) || {},
  60. config: getCalendarConfig(component) || {}
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }