import axios from "./instance"; import { params } from "@/env"; import { AM_MODEL_LIST, INSERT_AM_MODEL, UPDATE_AM_MODEL, DELETE_AM_MODEL, } from "./constant"; type ServiceAnimationModel = { key?: string id: string; title: string; url: string; showTitle: boolean; fontSize: number; globalVisibility: boolean; visibilityRange: number; frames: string actions: string subtitles: string paths: string mat?: string } export type AnimationModelAction = { amplitude: number; speed: number; time: number; duration: number; id: string; key: string; name: string; }; export type AnimationModelSubtitle = { content: string; duration: number; time: number; id: string; background: string; name: string; }; export type AnimationModelFrame = { time: number; id: string; name: string; mat?: { position?: SceneLocalPos; scale?: number; rotation?: SceneLocalPos; originPosition?: SceneLocalPos; }; duration?: number; }; export type AnimationModelPath = { reverse: boolean; pathId?: string; time: number; duration: number; id: string; name: string; }; export interface AnimationModel { key?: string id: string; title: string; url: string; showTitle: boolean; fontSize: number; globalVisibility: boolean; visibilityRange: number; frames: AnimationModelFrame[]; actions: AnimationModelAction[]; subtitles: AnimationModelSubtitle[]; paths: AnimationModelPath[]; mat?: { position?: SceneLocalPos; quaAtPath?: any scale?: number; rotation?: SceneLocalPos & {w: number}; quaternion?: SceneLocalPos & {w: number}; originPosition?: SceneLocalPos; }; } export type AnimationModels = AnimationModel[]; const serviceToLocal = (serviceAM: ServiceAnimationModel): AnimationModel => ({ ...serviceAM, frames: JSON.parse(serviceAM.frames), actions: JSON.parse(serviceAM.actions), subtitles: JSON.parse(serviceAM.subtitles), paths: JSON.parse(serviceAM.paths), mat: serviceAM.mat && JSON.parse(serviceAM.mat) }); const localToService = (am: AnimationModel): ServiceAnimationModel => ({ ...am, frames: JSON.stringify(am.frames), actions: JSON.stringify(am.actions), subtitles: JSON.stringify(am.subtitles), paths: JSON.stringify(am.paths), mat: am.mat ? JSON.stringify(am.mat) : undefined }); export const fetchAnimationModels = async () => { const ams = await axios.get(AM_MODEL_LIST, { params: { caseId: params.caseId }, }); return ams.map(serviceToLocal); }; export const fetchAnimationActions = async () => { return [ { id: "1", action: "Walk", title: "走", url: "" }, { id: "2", action: "Run", title: "跑", url: "" }, { id: "3", action: "Climb", title: "爬", url: "" }, { id: "2", action: "JumpUp", title: "向上跳", url: "" }, { id: "3", action: "JumpDown", title: "向下跳", url: "" }, { id: "2", action: "TurnLeft", title: "左转", url: "" }, { id: "3", action: "TurnRight", title: "右转", url: "" }, { id: "3", action: "FallForward", title: "向前倒地", url: "" }, { id: "3", action: "FallBackward", title: "向后倒地", url: "" }, ]; }; export const postInsertAnimationModel = async (am: AnimationModel) => { const addData = { ...localToService(am), caseId: params.caseId, id: undefined, }; console.log('add', addData) const serviceData = await axios.post( INSERT_AM_MODEL, addData ); return serviceToLocal(serviceData); }; export const postUpdateAnimationModel = async (guide: AnimationModel) => { console.log('set', guide) const data = await axios.post(UPDATE_AM_MODEL, { ...localToService(guide) }); return {...guide, id: data.id} }; export const postDeleteAnimationModel = (id: AnimationModel["id"]) => { return axios.post(DELETE_AM_MODEL, { id: Number(id) }); };