417 lines
10 KiB
TypeScript
417 lines
10 KiB
TypeScript
/**
|
|
* API使用示例
|
|
* 展示如何使用重构后的API架构
|
|
*/
|
|
|
|
import {
|
|
editorApi,
|
|
resourceApi,
|
|
fileUploader,
|
|
fileDownloader,
|
|
ApiManager,
|
|
type ProjectData,
|
|
type ResourceData
|
|
} from '../index'
|
|
|
|
/**
|
|
* 编辑器API使用示例
|
|
*/
|
|
export class EditorApiExamples {
|
|
/**
|
|
* 创建订单示例
|
|
*/
|
|
static async createOrderExample() {
|
|
try {
|
|
const response = await editorApi.createOrder({
|
|
projectId: 'project-123',
|
|
type: 'render',
|
|
options: {
|
|
quality: 'high',
|
|
format: 'png'
|
|
}
|
|
})
|
|
|
|
if (response.success) {
|
|
console.log('订单创建成功:', response.data)
|
|
return response.data
|
|
} else {
|
|
console.error('订单创建失败:', response.message)
|
|
}
|
|
} catch (error) {
|
|
console.error('创建订单时发生错误:', error)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 保存项目示例
|
|
*/
|
|
static async saveProjectExample() {
|
|
try {
|
|
const projectData: Partial<ProjectData> = {
|
|
name: '我的3D项目',
|
|
description: '这是一个示例项目',
|
|
sceneData: {
|
|
objects: [],
|
|
lights: [],
|
|
camera: {}
|
|
}
|
|
}
|
|
|
|
const response = await editorApi.saveProject(projectData)
|
|
|
|
if (response.success) {
|
|
console.log('项目保存成功:', response.data)
|
|
return response.data
|
|
} else {
|
|
console.error('项目保存失败:', response.message)
|
|
}
|
|
} catch (error) {
|
|
console.error('保存项目时发生错误:', error)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 上传缩略图示例
|
|
*/
|
|
static async uploadThumbnailExample(file: File, projectId: string) {
|
|
try {
|
|
const response = await editorApi.uploadThumbnail(file, projectId)
|
|
|
|
if (response.success) {
|
|
console.log('缩略图上传成功:', response.data)
|
|
return response.data
|
|
} else {
|
|
console.error('缩略图上传失败:', response.message)
|
|
}
|
|
} catch (error) {
|
|
console.error('上传缩略图时发生错误:', error)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 导出项目示例
|
|
*/
|
|
static async exportProjectExample(projectId: string) {
|
|
try {
|
|
await editorApi.exportProject(projectId, 'glb')
|
|
console.log('项目导出完成')
|
|
} catch (error) {
|
|
console.error('导出项目时发生错误:', error)
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 资源API使用示例
|
|
*/
|
|
export class ResourceApiExamples {
|
|
/**
|
|
* 上传资源示例
|
|
*/
|
|
static async uploadResourceExample(file: File) {
|
|
try {
|
|
const response = await resourceApi.uploadResource(file, {
|
|
type: 'model',
|
|
tags: ['建筑', '现代'],
|
|
description: '现代建筑模型'
|
|
})
|
|
|
|
if (response.success) {
|
|
console.log('资源上传成功:', response.data)
|
|
return response.data
|
|
} else {
|
|
console.error('资源上传失败:', response.message)
|
|
}
|
|
} catch (error) {
|
|
console.error('上传资源时发生错误:', error)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 批量上传资源示例
|
|
*/
|
|
static async uploadMultipleResourcesExample(files: File[]) {
|
|
try {
|
|
const response = await resourceApi.uploadMultipleResources(files, {
|
|
type: 'texture',
|
|
tags: ['材质', '金属']
|
|
})
|
|
|
|
if (response.success) {
|
|
console.log('批量上传成功:', response.data)
|
|
return response.data
|
|
} else {
|
|
console.error('批量上传失败:', response.message)
|
|
}
|
|
} catch (error) {
|
|
console.error('批量上传时发生错误:', error)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 搜索资源示例
|
|
*/
|
|
static async searchResourcesExample() {
|
|
try {
|
|
const response = await resourceApi.searchResources({
|
|
keyword: '建筑',
|
|
type: 'model',
|
|
tags: ['现代'],
|
|
page: 1,
|
|
pageSize: 20
|
|
})
|
|
|
|
if (response.success) {
|
|
console.log('搜索结果:', response.data)
|
|
return response.data
|
|
} else {
|
|
console.error('搜索失败:', response.message)
|
|
}
|
|
} catch (error) {
|
|
console.error('搜索资源时发生错误:', error)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 下载资源示例
|
|
*/
|
|
static async downloadResourceExample(resource: ResourceData) {
|
|
try {
|
|
await resourceApi.downloadResource(resource)
|
|
console.log('资源下载完成')
|
|
} catch (error) {
|
|
console.error('下载资源时发生错误:', error)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 批量下载资源示例
|
|
*/
|
|
static async downloadMultipleResourcesExample(resources: ResourceData[]) {
|
|
try {
|
|
await resourceApi.downloadMultipleResources(resources)
|
|
console.log('批量下载完成')
|
|
} catch (error) {
|
|
console.error('批量下载时发生错误:', error)
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 文件上传使用示例
|
|
*/
|
|
export class FileUploadExamples {
|
|
/**
|
|
* 普通文件上传示例
|
|
*/
|
|
static async uploadFileExample(file: File) {
|
|
try {
|
|
const response = await fileUploader.uploadFile({
|
|
file,
|
|
onProgress: (progress) => {
|
|
console.log(`上传进度: ${progress}%`)
|
|
}
|
|
})
|
|
|
|
if (response.success) {
|
|
console.log('文件上传成功:', response.data)
|
|
return response.data
|
|
} else {
|
|
console.error('文件上传失败:', response.message)
|
|
}
|
|
} catch (error) {
|
|
console.error('上传文件时发生错误:', error)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 图片上传示例(带压缩)
|
|
*/
|
|
static async uploadImageExample(file: File) {
|
|
try {
|
|
const response = await fileUploader.uploadImage(file, {
|
|
compress: true,
|
|
quality: 0.8,
|
|
maxWidth: 1920,
|
|
maxHeight: 1080,
|
|
onProgress: (progress) => {
|
|
console.log(`图片上传进度: ${progress}%`)
|
|
}
|
|
})
|
|
|
|
if (response.success) {
|
|
console.log('图片上传成功:', response.data)
|
|
return response.data
|
|
} else {
|
|
console.error('图片上传失败:', response.message)
|
|
}
|
|
} catch (error) {
|
|
console.error('上传图片时发生错误:', error)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 大文件分片上传示例
|
|
*/
|
|
static async uploadLargeFileExample(file: File) {
|
|
try {
|
|
const response = await fileUploader.uploadLargeFile(file, {
|
|
chunkSize: 2 * 1024 * 1024, // 2MB per chunk
|
|
onProgress: (progress) => {
|
|
console.log(`大文件上传进度: ${progress}%`)
|
|
},
|
|
onChunkProgress: (chunkIndex, chunkProgress) => {
|
|
console.log(`分片 ${chunkIndex} 上传进度: ${chunkProgress}%`)
|
|
}
|
|
})
|
|
|
|
if (response.success) {
|
|
console.log('大文件上传成功:', response.data)
|
|
return response.data
|
|
} else {
|
|
console.error('大文件上传失败:', response.message)
|
|
}
|
|
} catch (error) {
|
|
console.error('上传大文件时发生错误:', error)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 批量文件上传示例
|
|
*/
|
|
static async uploadBatchExample(files: File[]) {
|
|
try {
|
|
const result = await fileUploader.uploadBatch(files, {
|
|
concurrent: 3,
|
|
onProgress: (progress) => {
|
|
console.log(`批量上传总进度: ${progress}%`)
|
|
},
|
|
onFileProgress: (file, progress) => {
|
|
console.log(`文件 ${file.name} 上传进度: ${progress}%`)
|
|
},
|
|
onFileComplete: (file, result) => {
|
|
if (result instanceof Error) {
|
|
console.error(`文件 ${file.name} 上传失败:`, result)
|
|
} else {
|
|
console.log(`文件 ${file.name} 上传成功`)
|
|
}
|
|
}
|
|
})
|
|
|
|
console.log('批量上传结果:', result)
|
|
return result
|
|
} catch (error) {
|
|
console.error('批量上传时发生错误:', error)
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 文件下载使用示例
|
|
*/
|
|
export class FileDownloadExamples {
|
|
/**
|
|
* 普通文件下载示例
|
|
*/
|
|
static async downloadFileExample(url: string, filename?: string) {
|
|
try {
|
|
await fileDownloader.downloadAndSave({
|
|
url,
|
|
filename,
|
|
onProgress: (progress) => {
|
|
console.log(`下载进度: ${progress}%`)
|
|
}
|
|
})
|
|
|
|
console.log('文件下载完成')
|
|
} catch (error) {
|
|
console.error('下载文件时发生错误:', error)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 下载为Base64示例
|
|
*/
|
|
static async downloadAsBase64Example(url: string) {
|
|
try {
|
|
const base64 = await fileDownloader.downloadAsBase64({ url })
|
|
console.log('文件下载为Base64完成:', base64.substring(0, 100) + '...')
|
|
return base64
|
|
} catch (error) {
|
|
console.error('下载为Base64时发生错误:', error)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 批量下载示例
|
|
*/
|
|
static async downloadBatchExample(urls: string[]) {
|
|
try {
|
|
const result = await fileDownloader.downloadBatch(urls, {
|
|
concurrent: 3,
|
|
onProgress: (progress) => {
|
|
console.log(`批量下载总进度: ${progress}%`)
|
|
},
|
|
onFileProgress: (url, progress) => {
|
|
console.log(`文件 ${url} 下载进度: ${progress}%`)
|
|
},
|
|
onFileComplete: (url, result) => {
|
|
if (result instanceof Error) {
|
|
console.error(`文件 ${url} 下载失败:`, result)
|
|
} else {
|
|
console.log(`文件 ${url} 下载成功`)
|
|
}
|
|
}
|
|
})
|
|
|
|
console.log('批量下载结果:', result)
|
|
return result
|
|
} catch (error) {
|
|
console.error('批量下载时发生错误:', error)
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* API管理器使用示例
|
|
*/
|
|
export class ApiManagerExamples {
|
|
/**
|
|
* 设置认证token示例
|
|
*/
|
|
static setAuthTokenExample(token: string) {
|
|
ApiManager.setAuthToken(token)
|
|
console.log('认证token已设置')
|
|
}
|
|
|
|
/**
|
|
* 检查网络连接示例
|
|
*/
|
|
static async checkConnectionExample() {
|
|
try {
|
|
const isConnected = await ApiManager.checkConnection()
|
|
console.log('网络连接状态:', isConnected ? '已连接' : '未连接')
|
|
return isConnected
|
|
} catch (error) {
|
|
console.error('检查网络连接时发生错误:', error)
|
|
return false
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取API配置示例
|
|
*/
|
|
static getConfigExample() {
|
|
const config = ApiManager.getConfig()
|
|
console.log('API配置:', config)
|
|
return config
|
|
}
|
|
|
|
/**
|
|
* 取消所有请求示例
|
|
*/
|
|
static cancelAllRequestsExample() {
|
|
ApiManager.cancelAllRequests()
|
|
console.log('所有请求已取消')
|
|
}
|
|
}
|