fix: 首页任务改为异步调用

This commit is contained in:
imeepos 2025-09-04 20:49:05 +08:00
parent 482318e7c3
commit 72a8fb6e14
3 changed files with 30 additions and 62 deletions

View File

@ -58,9 +58,6 @@ export default function Home() {
const handleTemplateClick = async (template: Template) => { const handleTemplateClick = async (template: Template) => {
if (loading) return // 防止重复点击 if (loading) return // 防止重复点击
let recordId: string | undefined
try { try {
setLoading(true) setLoading(true)
@ -80,64 +77,25 @@ export default function Home() {
} }
}) })
// 创建历史记录(生成中状态)
try {
recordId = await dispatch(addRecord({
type: 'image-to-image',
templateId: template.code,
templateName: template.name,
inputImage: url,
status: 'generating'
}))
} catch (historyError) {
console.error('创建历史记录失败:', historyError)
}
// 执行模板处理 // 执行模板处理
Taro.showLoading({ Taro.showLoading({
title: '处理图片中...', title: '处理图片中...',
mask: true mask: true
}) })
try { try {
const result = await serverSdk.executeTemplate({ const taskId = await serverSdk.executeTemplate({
imageUrl: url, imageUrl: url,
templateCode: template.code templateCode: template.code
}) })
// 处理完成后更新历史记录状态
if (recordId) {
try {
await dispatch(updateRecord(recordId, {
outputResult: result || undefined,
status: 'completed'
}))
} catch (historyError) {
console.error('更新历史记录失败:', historyError)
// 历史记录更新失败不影响主流程,只记录错误
}
}
// 隐藏loading // 隐藏loading
Taro.hideLoading() Taro.hideLoading()
// 检查处理结果 // 检查处理结果
// 跳转到结果页面 // 跳转到结果页面
navigateTo({
url: `/pages/result/index?images=${encodeURIComponent(JSON.stringify([result!]))}`
})
} catch (e) {
// 处理失败时更新历史记录状态
if (recordId) {
try {
await dispatch(updateRecord(recordId, {
status: 'failed',
errorMessage: (e as Error).message || '处理失败'
}))
} catch (historyError) {
console.error('更新失败记录到历史失败:', historyError)
}
}
// 跳转到generate页面等待loading...
} catch (e) {
// 处理失败 // 处理失败
Taro.showToast({ Taro.showToast({
title: (e as Error).message || '处理失败', title: (e as Error).message || '处理失败',
@ -146,23 +104,8 @@ export default function Home() {
}) })
} }
} catch (error) { } catch (error) {
console.error('模板处理失败:', error)
// 如果有记录ID更新为失败状态
if (recordId) {
try {
await dispatch(updateRecord(recordId, {
status: 'failed',
errorMessage: error instanceof Error ? error.message : '处理失败'
}))
} catch (historyError) {
console.error('更新历史记录失败:', historyError)
}
}
// 隐藏loading // 隐藏loading
Taro.hideLoading() Taro.hideLoading()
// 显示错误提示 // 显示错误提示
Taro.showToast({ Taro.showToast({
title: error instanceof Error ? error.message : '处理失败', title: error instanceof Error ? error.message : '处理失败',

View File

@ -67,6 +67,8 @@ export default function Index() {
} }
} }
const resetToUpload = () => { const resetToUpload = () => {
setState({ step: 'upload', error: null }) setState({ step: 'upload', error: null })
} }

View File

@ -198,7 +198,11 @@ export class SdkServer {
} }
} }
/**
*
* @param params
* @returns
*/
async login(params: any): Promise<{ tokens: IToken, user: IUser }> { async login(params: any): Promise<{ tokens: IToken, user: IUser }> {
try { try {
const response = await this.request<{ tokens: IToken, user: IUser }>(`/api/v1/users/login`, 'POST', { const response = await this.request<{ tokens: IToken, user: IUser }>(`/api/v1/users/login`, 'POST', {
@ -214,6 +218,10 @@ export class SdkServer {
} }
} }
/**
* token是否过期
* @returns
*/
async profile() { async profile() {
try { try {
const response = await this.request<{ tokens: IToken }>(`/api/v1/users/profile`, 'GET', {}); const response = await this.request<{ tokens: IToken }>(`/api/v1/users/profile`, 'GET', {});
@ -243,7 +251,22 @@ export class SdkServer {
return templates; return templates;
} }
/**
*
* @param taskId
* @returns
*/
async getTaskProgress(taskId: string) {
try {
const response = await this.request<{ tokens: IToken }>(`/api/v1/templates/execution/${taskId}/progress`, 'GET', {});
return response.data
} catch (error) {
throw error;
}
}
/**
*
*/
async getMineLogs() { async getMineLogs() {
try { try {
const userId = Taro.getStorageSync(TOKEN_UID_KEY); const userId = Taro.getStorageSync(TOKEN_UID_KEY);