fix: model service
This commit is contained in:
parent
17662aa137
commit
77e39ddb68
|
|
@ -22,12 +22,12 @@ const Sidebar: React.FC = () => {
|
|||
{ path: '/', icon: Home, label: '首页' },
|
||||
{ path: '/editor', icon: Video, label: '编辑器' },
|
||||
{ path: '/ai-video', icon: Sparkles, label: 'AI 视频' },
|
||||
{ path: '/templates', icon: Layout, label: '模板管理' },
|
||||
{ path: '/templates', icon: Layout, label: '模板库' },
|
||||
{ path: '/resource-categories', icon: Tags, label: '分类管理' },
|
||||
{ path: '/projects', icon: FolderOpen, label: '项目' },
|
||||
{ path: '/models', icon: User, label: '模特管理' },
|
||||
{ path: '/projects', icon: FolderOpen, label: '项目库' },
|
||||
{ path: '/models', icon: User, label: '模特库' },
|
||||
{ path: '/media', icon: Image, label: '媒体库' },
|
||||
{ path: '/audio', icon: Music, label: '音频' },
|
||||
{ path: '/audio', icon: Music, label: '音频库' },
|
||||
{ path: '/settings', icon: Settings, label: '设置' },
|
||||
]
|
||||
|
||||
|
|
|
|||
|
|
@ -26,30 +26,28 @@ export interface ApiResponse<T> {
|
|||
}
|
||||
|
||||
export class ModelService {
|
||||
/**
|
||||
* 尝试解析JSON响应
|
||||
*/
|
||||
private static tryJsonParse(result: any): any {
|
||||
if (typeof result === 'string') {
|
||||
try {
|
||||
const parsed = JSON.parse(result)
|
||||
return parsed.result || parsed
|
||||
} catch (error) {
|
||||
console.error('Failed to parse JSON:', error)
|
||||
return result
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
/**
|
||||
* 获取所有模特
|
||||
*/
|
||||
static async getAllModels(): Promise<ApiResponse<Model[]>> {
|
||||
try {
|
||||
console.log('Calling get_all_models...')
|
||||
const result = await invoke('get_all_models')
|
||||
console.log('Raw result from Tauri:', result)
|
||||
|
||||
if (typeof result === 'string') {
|
||||
try {
|
||||
const parsed = JSON.parse(result)
|
||||
console.log('Parsed result:', parsed)
|
||||
return parsed as ApiResponse<Model[]>
|
||||
} catch (parseError) {
|
||||
console.error('Failed to parse JSON response:', parseError)
|
||||
return {
|
||||
status: false,
|
||||
msg: `Invalid JSON response: ${result}`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result as ApiResponse<Model[]>
|
||||
return { status: true, msg: 'ok', data: this.tryJsonParse(result) } as ApiResponse<Model[]>
|
||||
} catch (error) {
|
||||
console.error('Failed to get all models:', error)
|
||||
return {
|
||||
|
|
@ -65,20 +63,7 @@ export class ModelService {
|
|||
static async getModelById(modelId: string): Promise<ApiResponse<Model>> {
|
||||
try {
|
||||
const result = await invoke('get_model_by_id', { modelId })
|
||||
|
||||
if (typeof result === 'string') {
|
||||
try {
|
||||
const parsed = JSON.parse(result)
|
||||
return parsed as ApiResponse<Model>
|
||||
} catch (parseError) {
|
||||
return {
|
||||
status: false,
|
||||
msg: `Invalid JSON response: ${result}`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result as ApiResponse<Model>
|
||||
return { status: true, msg: 'ok', data: this.tryJsonParse(result) } as ApiResponse<Model>
|
||||
} catch (error) {
|
||||
console.error('Failed to get model by id:', error)
|
||||
return {
|
||||
|
|
@ -93,25 +78,8 @@ export class ModelService {
|
|||
*/
|
||||
static async createModel(request: CreateModelRequest): Promise<ApiResponse<Model>> {
|
||||
try {
|
||||
console.log('Calling create_model with:', request)
|
||||
const result = await invoke('create_model', { request })
|
||||
console.log('Raw result from Tauri:', result)
|
||||
|
||||
if (typeof result === 'string') {
|
||||
try {
|
||||
const parsed = JSON.parse(result)
|
||||
console.log('Parsed result:', parsed)
|
||||
return parsed as ApiResponse<Model>
|
||||
} catch (parseError) {
|
||||
console.error('Failed to parse JSON response:', parseError)
|
||||
return {
|
||||
status: false,
|
||||
msg: `Invalid JSON response: ${result}`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result as ApiResponse<Model>
|
||||
return { status: true, msg: 'ok', data: this.tryJsonParse(result) } as ApiResponse<Model>
|
||||
} catch (error) {
|
||||
console.error('Failed to create model:', error)
|
||||
return {
|
||||
|
|
@ -125,25 +93,12 @@ export class ModelService {
|
|||
* 更新模特
|
||||
*/
|
||||
static async updateModel(
|
||||
modelId: string,
|
||||
modelId: string,
|
||||
request: UpdateModelRequest
|
||||
): Promise<ApiResponse<Model>> {
|
||||
try {
|
||||
const result = await invoke('update_model', { modelId, request })
|
||||
|
||||
if (typeof result === 'string') {
|
||||
try {
|
||||
const parsed = JSON.parse(result)
|
||||
return parsed as ApiResponse<Model>
|
||||
} catch (parseError) {
|
||||
return {
|
||||
status: false,
|
||||
msg: `Invalid JSON response: ${result}`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result as ApiResponse<Model>
|
||||
return { status: true, msg: 'ok', data: this.tryJsonParse(result) } as ApiResponse<Model>
|
||||
} catch (error) {
|
||||
console.error('Failed to update model:', error)
|
||||
return {
|
||||
|
|
@ -159,20 +114,7 @@ export class ModelService {
|
|||
static async deleteModel(modelId: string): Promise<ApiResponse<boolean>> {
|
||||
try {
|
||||
const result = await invoke('delete_model', { modelId })
|
||||
|
||||
if (typeof result === 'string') {
|
||||
try {
|
||||
const parsed = JSON.parse(result)
|
||||
return parsed as ApiResponse<boolean>
|
||||
} catch (parseError) {
|
||||
return {
|
||||
status: false,
|
||||
msg: `Invalid JSON response: ${result}`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result as ApiResponse<boolean>
|
||||
return { status: true, msg: 'ok', data: this.tryJsonParse(result) } as ApiResponse<boolean>
|
||||
} catch (error) {
|
||||
console.error('Failed to delete model:', error)
|
||||
return {
|
||||
|
|
@ -188,20 +130,7 @@ export class ModelService {
|
|||
static async searchModels(keyword: string): Promise<ApiResponse<Model[]>> {
|
||||
try {
|
||||
const result = await invoke('search_models', { keyword })
|
||||
|
||||
if (typeof result === 'string') {
|
||||
try {
|
||||
const parsed = JSON.parse(result)
|
||||
return parsed as ApiResponse<Model[]>
|
||||
} catch (parseError) {
|
||||
return {
|
||||
status: false,
|
||||
msg: `Invalid JSON response: ${result}`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result as ApiResponse<Model[]>
|
||||
return { status: true, msg: 'ok', data: this.tryJsonParse(result) } as ApiResponse<Model[]>
|
||||
} catch (error) {
|
||||
console.error('Failed to search models:', error)
|
||||
return {
|
||||
|
|
|
|||
Loading…
Reference in New Issue