animation.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import axios from "./instance";
  2. import { params } from "@/env";
  3. import {
  4. AM_MODEL_LIST,
  5. INSERT_AM_MODEL,
  6. UPDATE_AM_MODEL,
  7. DELETE_AM_MODEL,
  8. } from "./constant";
  9. type ServiceAnimationModel = {
  10. key?: string
  11. id: string;
  12. title: string;
  13. url: string;
  14. showTitle: boolean;
  15. fontSize: number;
  16. globalVisibility: boolean;
  17. visibilityRange: number;
  18. frames: string
  19. actions: string
  20. subtitles: string
  21. paths: string
  22. mat?: string
  23. }
  24. export type AnimationModelAction = {
  25. amplitude: number;
  26. speed: number;
  27. time: number;
  28. duration: number;
  29. id: string;
  30. key: string;
  31. name: string;
  32. };
  33. export type AnimationModelSubtitle = {
  34. content: string;
  35. duration: number;
  36. time: number;
  37. id: string;
  38. background: string;
  39. name: string;
  40. };
  41. export type AnimationModelFrame = {
  42. time: number;
  43. id: string;
  44. name: string;
  45. mat?: {
  46. position?: SceneLocalPos;
  47. scale?: number;
  48. rotation?: SceneLocalPos;
  49. originPosition?: SceneLocalPos;
  50. };
  51. duration?: number;
  52. };
  53. export type AnimationModelPath = {
  54. reverse: boolean;
  55. pathId?: string;
  56. time: number;
  57. duration: number;
  58. id: string;
  59. name: string;
  60. };
  61. export interface AnimationModel {
  62. key?: string
  63. id: string;
  64. title: string;
  65. url: string;
  66. showTitle: boolean;
  67. fontSize: number;
  68. globalVisibility: boolean;
  69. visibilityRange: number;
  70. frames: AnimationModelFrame[];
  71. actions: AnimationModelAction[];
  72. subtitles: AnimationModelSubtitle[];
  73. paths: AnimationModelPath[];
  74. mat?: {
  75. position?: SceneLocalPos;
  76. quaAtPath?: any
  77. scale?: number;
  78. rotation?: SceneLocalPos & {w: number};
  79. quaternion?: SceneLocalPos & {w: number};
  80. originPosition?: SceneLocalPos;
  81. };
  82. }
  83. export type AnimationModels = AnimationModel[];
  84. const serviceToLocal = (serviceAM: ServiceAnimationModel): AnimationModel => ({
  85. ...serviceAM,
  86. frames: JSON.parse(serviceAM.frames),
  87. actions: JSON.parse(serviceAM.actions),
  88. subtitles: JSON.parse(serviceAM.subtitles),
  89. paths: JSON.parse(serviceAM.paths),
  90. mat: serviceAM.mat && JSON.parse(serviceAM.mat)
  91. });
  92. const localToService = (am: AnimationModel): ServiceAnimationModel => ({
  93. ...am,
  94. frames: JSON.stringify(am.frames),
  95. actions: JSON.stringify(am.actions),
  96. subtitles: JSON.stringify(am.subtitles),
  97. paths: JSON.stringify(am.paths),
  98. mat: am.mat ? JSON.stringify(am.mat) : undefined
  99. });
  100. export const fetchAnimationModels = async () => {
  101. const ams = await axios.get<ServiceAnimationModel[]>(AM_MODEL_LIST, {
  102. params: { caseId: params.caseId },
  103. });
  104. return ams.map(serviceToLocal);
  105. };
  106. export const fetchAnimationActions = async () => {
  107. return [
  108. { id: "1", action: "Walk", title: "走", url: "" },
  109. { id: "2", action: "Run", title: "跑", url: "" },
  110. { id: "3", action: "Climb", title: "爬", url: "" },
  111. { id: "2", action: "JumpUp", title: "向上跳", url: "" },
  112. { id: "3", action: "JumpDown", title: "向下跳", url: "" },
  113. { id: "2", action: "TurnLeft", title: "左转", url: "" },
  114. { id: "3", action: "TurnRight", title: "右转", url: "" },
  115. { id: "3", action: "FallForward", title: "向前倒地", url: "" },
  116. { id: "3", action: "FallBackward", title: "向后倒地", url: "" },
  117. ];
  118. };
  119. export const postInsertAnimationModel = async (am: AnimationModel) => {
  120. const addData = {
  121. ...localToService(am),
  122. caseId: params.caseId,
  123. id: undefined,
  124. };
  125. console.log('add', addData)
  126. const serviceData = await axios.post<ServiceAnimationModel>(
  127. INSERT_AM_MODEL,
  128. addData
  129. );
  130. return serviceToLocal(serviceData);
  131. };
  132. export const postUpdateAnimationModel = async (guide: AnimationModel) => {
  133. console.log('set', guide)
  134. const data = await axios.post<ServiceAnimationModel>(UPDATE_AM_MODEL, { ...localToService(guide) });
  135. return {...guide, id: data.id}
  136. };
  137. export const postDeleteAnimationModel = (id: AnimationModel["id"]) => {
  138. return axios.post<undefined>(DELETE_AM_MODEL, { id: Number(id) });
  139. };