guide.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { ref } from 'vue'
  2. import { createTemploraryID, isTemploraryID } from './sys'
  3. import { autoSetModeCallback } from './sys'
  4. import {
  5. fetchGuides,
  6. postAddGuide,
  7. postDeleteGuide,
  8. postUpdateGuide,
  9. uploadFile,
  10. } from '@/api'
  11. import {
  12. deleteStoreItem,
  13. addStoreItem,
  14. updateStoreItem,
  15. fetchStoreItems,
  16. saveStoreItems,
  17. recoverStoreItems
  18. } from '@/utils'
  19. import type { GuidePath as SGuidePath, Guide as SGuide } from '@/api'
  20. export type GuidePath = LocalMode<SGuidePath, 'cover'>
  21. export type GuidePaths = GuidePath[]
  22. export type Guide = Omit<LocalMode<SGuide, 'cover'>, 'paths'> & { paths: GuidePaths }
  23. export type Guides = Guide[]
  24. export const guides = ref<Guides>([])
  25. export const createGuide = (guide: Partial<Guide> = {}): Guide => ({
  26. id: createTemploraryID(),
  27. title: `路径${guides.value.length + 1}`,
  28. cover: '',
  29. paths: [],
  30. ...guide
  31. })
  32. export const createGuidePath = (path: Partial<GuidePath> = {}): GuidePath => ({
  33. id: createTemploraryID(),
  34. cover: '',
  35. time: 1,
  36. speed: 1,
  37. position: {x: 0, y: 0, z: 0},
  38. target: {x: 0, y: 0, z: 0},
  39. ...path
  40. })
  41. let bcGuides: Guides = []
  42. export const getBackupGuides = () => bcGuides
  43. export const backupGuides = () => {
  44. bcGuides = guides.value.map(guide => ({
  45. ...guide,
  46. paths: guide.paths.map(path => ({...path}))
  47. }))
  48. }
  49. export const transformGuide = async (guide: Guide): Promise<SGuide> => {
  50. let guideCover: string = ''
  51. const pathsCover: string[] = []
  52. const uploadGuideCover = uploadFile(guide.cover)
  53. .then(cover => guideCover = cover)
  54. const uploadPathsCver = guide.paths.map((path, index) =>
  55. uploadFile(path.cover)
  56. .then(cover => pathsCover[index] = cover)
  57. )
  58. await Promise.all([uploadGuideCover, ...uploadPathsCver])
  59. return {
  60. ...guide,
  61. paths: guide.paths.map((path, i) => ({...path, cover: pathsCover[i]})),
  62. cover: guideCover
  63. }
  64. }
  65. export const recoverGuides = recoverStoreItems(guides, getBackupGuides)
  66. export const addGuide = addStoreItem(guides, postAddGuide, transformGuide)
  67. export const updateGuide = updateStoreItem(guides, (guide) => {
  68. return postUpdateGuide({
  69. ...guide,
  70. paths: guide.paths.map(path => ({...path, id: isTemploraryID(path.id) ? undefined : path.id})) as any
  71. })
  72. }, transformGuide)
  73. export const deleteGuide = deleteStoreItem(guides, guide => postDeleteGuide(guide.id))
  74. export const initialGuides = fetchStoreItems(guides, fetchGuides, backupGuides)
  75. export const saveGuides = saveStoreItems(
  76. guides,
  77. getBackupGuides,
  78. {
  79. add: addGuide,
  80. update: updateGuide,
  81. delete: deleteGuide,
  82. }
  83. )
  84. export const autoSaveGuides = autoSetModeCallback(guides, {
  85. backup: backupGuides,
  86. recovery: recoverGuides,
  87. save: saveGuides,
  88. })