event.js 1.2 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 { ScheduleType } 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 getInstName ({ name }) {
  13. return name
  14. }
  15. function getProgramImage ({ img }) {
  16. return getAssetUrl(img)
  17. }
  18. function getScheduleImage ({ events }) {
  19. return getImage(ScheduleType.CALENDAR, events[0].programId)
  20. }
  21. function getCache (type) {
  22. if (!cacheMap[type]) {
  23. cacheMap[type] = {}
  24. }
  25. return cacheMap[type]
  26. }
  27. function getPromise (type, id) {
  28. const cache = getCache()
  29. if (!cache[id]) {
  30. const promise = (type === ScheduleType.CALENDAR ? getProgramInst : getScheduleInst)(id)
  31. promise.catch(() => {
  32. cache[id] = null
  33. })
  34. cache[id] = promise
  35. }
  36. return cache[id]
  37. }
  38. export function getName (type, id) {
  39. return getPromise(type, id).then(getInstName)
  40. }
  41. export function getImage (type, id) {
  42. return getPromise(type, id).then(type === ScheduleType.CALENDAR ? getProgramImage : getScheduleImage)
  43. }