jsonTool.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import axios from "axios"
  2. import { useSlidesStore } from "@/store"
  3. import { slides as slidesData } from "@/mocks/slides"
  4. /**
  5. * http 请求json
  6. */
  7. export function getHttpJson(url: string): Promise<{ code: number; data: Record<string, any> }> {
  8. return new Promise((res, rej) => {
  9. axios
  10. .get(url)
  11. .then(resData => {
  12. if (resData.status === 200 && typeof resData.data === "object") {
  13. res({
  14. code: 200,
  15. data: resData.data
  16. })
  17. } else {
  18. res({
  19. code: 500,
  20. data: {}
  21. })
  22. }
  23. })
  24. .catch(() => {
  25. res({
  26. code: 500,
  27. data: {}
  28. })
  29. })
  30. })
  31. }
  32. export function jsonToPpt(jsonData: Record<string, any>) {
  33. const slidesStore = useSlidesStore()
  34. const { width, theme, slides } = jsonData
  35. slidesStore.updateSlideIndex(0)
  36. slidesStore.setViewportSize(width || 1920)
  37. slidesStore.setViewportRatio(theme.viewportRatio)
  38. slidesStore.setTheme(theme)
  39. slidesStore.setSlides(slides.length ? slides : slidesData)
  40. }
  41. export function getJsonToBlob() {
  42. const slidesStore = useSlidesStore()
  43. const { slides, theme, viewportRatio, viewportSize, title } = slidesStore
  44. const json = {
  45. width: viewportSize,
  46. height: viewportSize * viewportRatio,
  47. slides: slides,
  48. theme: Object.assign(theme, { viewportRatio })
  49. }
  50. return {
  51. blob: new Blob([JSON.stringify(json)]),
  52. title
  53. }
  54. }