view.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import axios from './instance'
  2. import { VIEW_LIST, INSERT_VIEW, DELETE_VIEW, UPDATE_VIEW } from './constant'
  3. import type { Scene } from './scene'
  4. import { params } from '@/env'
  5. export type View = {
  6. id: string
  7. cover: string
  8. title: string
  9. sort: number
  10. flyData: string,
  11. } & ({ fusionId: number, num: null, numType: null } | { fusionId: null, num: string, numType: Scene['type'] })
  12. type ServiceView = {
  13. viewId: number
  14. viewTitle: string
  15. viewPoint: string
  16. viewImg: string
  17. sort: number
  18. } & ({ fusionId: string, num: null, numType: null } | { fusionId: null, num: string, numType: Scene['type'] })
  19. const toLocal = (serviceView: ServiceView) : View => ({
  20. id: serviceView.viewId.toString(),
  21. cover: serviceView.viewImg,
  22. title: serviceView.viewTitle,
  23. sort: serviceView.sort,
  24. flyData: JSON.parse(serviceView.viewPoint),
  25. num: serviceView.num,
  26. numType: serviceView.numType,
  27. fusionId: serviceView.fusionId ? Number(serviceView.fusionId) : null
  28. } as View)
  29. const toService = (view: View, isUpdate = true): PartialProps<ServiceView, 'viewId'> => ({
  30. viewId: isUpdate ? Number(view.id) : undefined,
  31. viewTitle: view.title,
  32. viewPoint: JSON.stringify(view.flyData),
  33. viewImg: view.cover,
  34. sort: view.sort,
  35. num: view.num,
  36. numType: view.numType,
  37. fusionId: view.fusionId ? view.fusionId.toString() : null
  38. })
  39. export type Views = View[]
  40. export const fetchViews = async () => {
  41. const data = await axios.get<ServiceView[]>(VIEW_LIST, { params: { caseId: params.caseId } })
  42. return data.map(toLocal)
  43. }
  44. export const postAddView = async (view: View) => {
  45. const serviceView = await axios.post<ServiceView>(INSERT_VIEW, { ...toService(view, false), caseId: params.caseId })
  46. return toLocal(serviceView)
  47. }
  48. export const postUpdateView = async (view: View) => {
  49. await axios.post(UPDATE_VIEW, toService(view))
  50. }
  51. export const postDeleteView = async (id: View['id']) => {
  52. await axios.post(DELETE_VIEW, { viewId: Number(id) })
  53. }