143 lines
3.7 KiB
TypeScript
143 lines
3.7 KiB
TypeScript
import { httpClient } from '../core/request'
|
||
import { fileUploader } from '../core/upload'
|
||
import { fileDownloader } from '../core/download'
|
||
import { ApiResponse, PaginationParams, PaginationResponse } from '../types'
|
||
import { API_ENDPOINTS } from '../config'
|
||
|
||
/**
|
||
* 项目数据类型
|
||
*/
|
||
export interface ProjectData {
|
||
id: string
|
||
name: string
|
||
description?: string
|
||
thumbnail?: string
|
||
sceneData: any
|
||
createdAt: string
|
||
updatedAt: string
|
||
userId: string
|
||
}
|
||
|
||
/**
|
||
* 订单数据类型
|
||
*/
|
||
export interface OrderData {
|
||
id: string
|
||
projectId: string
|
||
status: 'pending' | 'processing' | 'completed' | 'failed'
|
||
result?: any
|
||
createdAt: string
|
||
updatedAt: string
|
||
}
|
||
|
||
/**
|
||
* 编辑器API类
|
||
*/
|
||
export class EditorApi {
|
||
/**
|
||
* 创建订单
|
||
*/
|
||
public async createOrder(params: any): Promise<ApiResponse<OrderData>> {
|
||
return httpClient.post<OrderData>(API_ENDPOINTS.EDITOR.CREATE_ORDER, params)
|
||
}
|
||
|
||
/**
|
||
* 保存项目
|
||
*/
|
||
public async saveProject(projectData: Partial<ProjectData>): Promise<ApiResponse<ProjectData>> {
|
||
return httpClient.post<ProjectData>(API_ENDPOINTS.EDITOR.SAVE_PROJECT, projectData)
|
||
}
|
||
|
||
/**
|
||
* 加载项目
|
||
*/
|
||
public async loadProject(projectId: string): Promise<ApiResponse<ProjectData>> {
|
||
return httpClient.get<ProjectData>(`${API_ENDPOINTS.EDITOR.LOAD_PROJECT}/${projectId}`)
|
||
}
|
||
|
||
/**
|
||
* 删除项目
|
||
*/
|
||
public async deleteProject(projectId: string): Promise<ApiResponse<void>> {
|
||
return httpClient.delete<void>(`${API_ENDPOINTS.EDITOR.DELETE_PROJECT}/${projectId}`)
|
||
}
|
||
|
||
/**
|
||
* 获取项目列表
|
||
*/
|
||
public async getProjectList(params: PaginationParams): Promise<ApiResponse<PaginationResponse<ProjectData>>> {
|
||
return httpClient.get<PaginationResponse<ProjectData>>(API_ENDPOINTS.EDITOR.LOAD_PROJECT, params)
|
||
}
|
||
|
||
/**
|
||
* 上传项目缩略图
|
||
*/
|
||
public async uploadThumbnail(file: File, projectId: string): Promise<ApiResponse<{ url: string }>> {
|
||
return fileUploader.uploadImage(file, {
|
||
compress: true,
|
||
quality: 0.8,
|
||
maxWidth: 400,
|
||
maxHeight: 300,
|
||
onProgress: (progress) => {
|
||
console.log(`缩略图上传进度: ${progress}%`)
|
||
}
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 导出项目
|
||
*/
|
||
public async exportProject(projectId: string, format: 'json' | 'glb' | 'fbx' = 'json'): Promise<void> {
|
||
await fileDownloader.downloadAndSave({
|
||
url: `/editor/project/export/${projectId}`,
|
||
filename: `project_${projectId}.${format}`,
|
||
params: { format }
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 导入项目
|
||
*/
|
||
public async importProject(file: File): Promise<ApiResponse<ProjectData>> {
|
||
const uploadResponse = await fileUploader.uploadFile({
|
||
file,
|
||
fieldName: 'projectFile',
|
||
data: {
|
||
action: 'import'
|
||
}
|
||
})
|
||
|
||
if (!uploadResponse.success) {
|
||
throw new Error(uploadResponse.message)
|
||
}
|
||
|
||
// 上传成功后,调用导入API
|
||
return httpClient.post<ProjectData>('/editor/project/import', {
|
||
fileUrl: uploadResponse.data.url
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 批量导出项目
|
||
*/
|
||
public async exportMultipleProjects(projectIds: string[], format: 'json' | 'glb' | 'fbx' = 'json'): Promise<void> {
|
||
const downloadUrls = projectIds.map(id => `/editor/project/export/${id}?format=${format}`)
|
||
|
||
await fileDownloader.downloadBatch(downloadUrls, {
|
||
concurrent: 2,
|
||
onProgress: (progress) => {
|
||
console.log(`批量导出进度: ${progress}%`)
|
||
},
|
||
onFileComplete: (url, result) => {
|
||
if (result instanceof Error) {
|
||
console.error(`导出失败: ${url}`, result)
|
||
} else {
|
||
console.log(`导出完成: ${url}`)
|
||
}
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
// 创建默认实例
|
||
export const editorApi = new EditorApi()
|