event.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { getAssetUrl } from '@/api/asset'
  2. import { getProgram as programApi } from '@/api/program'
  3. import { getSchedule as scheduleApi } from '@/api/calendar'
  4. import { EventTarget } from '@/constant'
  5. const cacheMap = {}
  6. function getProgramInst (id) {
  7. return programApi(id, { custom: true, background: true }).then(({ data }) => data)
  8. }
  9. function getScheduleInst (id) {
  10. return scheduleApi(id, { custom: true, background: true })
  11. }
  12. function getProgramImage ({ img }) {
  13. return getAssetUrl(img)
  14. }
  15. function getScheduleImage ({ events }) {
  16. return getImage(EventTarget.PROGRAM, events[0].programId)
  17. }
  18. function getCache (type) {
  19. if (!cacheMap[type]) {
  20. cacheMap[type] = {}
  21. }
  22. return cacheMap[type]
  23. }
  24. function getPromise (type, id) {
  25. const key = `${type}__${id}`
  26. const cache = getCache()
  27. if (!cache[key]) {
  28. const promise = (type === EventTarget.PROGRAM ? getProgramInst : getScheduleInst)(id)
  29. promise.catch(() => {
  30. cache[key] = null
  31. })
  32. cache[key] = promise
  33. }
  34. return cache[key]
  35. }
  36. export function getImage (type, id) {
  37. switch (type) {
  38. case EventTarget.PROGRAM:
  39. return getPromise(type, id).then(getProgramImage)
  40. case EventTarget.RECUR:
  41. return getPromise(type, id).then(getScheduleImage)
  42. default:
  43. return Promise.resolve(null)
  44. }
  45. }