jsonTool.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 || 0.5625)
  38. // 兼容妙极客 这里过滤一下这个数据
  39. const newSlides = formatSlides(slides)
  40. slidesStore.setTheme(theme)
  41. slidesStore.setSlides(newSlides.length ? newSlides : slidesData)
  42. }
  43. export function getJsonToBlob() {
  44. const slidesStore = useSlidesStore()
  45. const { slides, theme, viewportRatio, viewportSize, title } = slidesStore
  46. const json = {
  47. width: viewportSize,
  48. height: viewportSize * viewportRatio,
  49. slides: slides,
  50. theme: Object.assign(theme, { viewportRatio })
  51. }
  52. return {
  53. blob: new Blob([JSON.stringify(json)]),
  54. title
  55. }
  56. }
  57. function formatSlides(slides: any[]): any[] {
  58. return (slides || []).map(item => {
  59. // 背景兼容 当为渐变并且没有渐变数据的时候,判定为妙极客数据兼容
  60. if (item.background?.type === "gradient") {
  61. if (item.background.gradientType === "linear" && !item.background?.gradient) {
  62. item.background.gradient = {
  63. colors: [
  64. { pos: 0, color: (item.background.gradientColor && item.background.gradientColor[0]) || "rgba(255,255,255,1)" },
  65. { pos: 100, color: (item.background.gradientColor && item.background.gradientColor[1]) || "rgba(255,255,255,1)" }
  66. ],
  67. rotate: item.background.gradientRotate || 0,
  68. type: item.background.gradientType
  69. }
  70. }
  71. }
  72. ;(item.elements || []).map((el: any) => {
  73. // 兼容块
  74. if (el.type === "shape") {
  75. if (el.gradient?.type == "linear" && !el.gradient?.colors) {
  76. el.gradient.colors = [
  77. { pos: 0, color: (el.gradient.color && el.gradient.color[0]) || "rgba(255,255,255,1)" },
  78. { pos: 100, color: (el.gradient.color && el.gradient.color[1]) || "rgba(255,255,255,1)" }
  79. ]
  80. }
  81. }
  82. })
  83. // 兼容动画 妙极课动画没有effect属性 先把妙极客的动画去掉 之后做兼容
  84. if (item.animations) {
  85. item.animations = (item.animations || []).filter((item: Record<string, any>) => item.effect)
  86. }
  87. return item
  88. })
  89. }