diff --git a/lesingle-creation-frontend/src/views/public/create/views/CreatingView.vue b/lesingle-creation-frontend/src/views/public/create/views/CreatingView.vue index 72bdd51..12e7b32 100644 --- a/lesingle-creation-frontend/src/views/public/create/views/CreatingView.vue +++ b/lesingle-creation-frontend/src/views/public/create/views/CreatingView.vue @@ -207,7 +207,9 @@ const startWebSocket = (workId: string) => { closeWebSocket() saveWorkId('') const route = getRouteByStatus(STATUS.COMPLETED, workId) - if (route) setTimeout(() => router.replace(route), 800) + if (route) { + setTimeout(() => router.replace(route), 800) + } } else if (data.progress < 0) { closeWebSocket() saveWorkId('') @@ -246,52 +248,59 @@ const closeWebSocket = () => { stompClient = null } } +async function getWorkDetailApi(workId: string) { + try { + const work = await getWorkDetail(workId) + if (!work) return + if (consecutiveErrors > 0 || networkWarn.value) { + consecutiveErrors = 0 + networkWarn.value = false + } + + if (work.progress != null && work.progress > progress.value) progress.value = work.progress + if (work.progressMessage) stage.value = friendlyStage(progress.value, work.progressMessage) + + if (work.status >= STATUS.COMPLETED) { + progress.value = 100 + stage.value = '绘本创作完成' + clearInterval(pollTimer!) + pollTimer = null + saveWorkId('') + const route = getRouteByStatus(work.status, workId) + if (route) replaceWhenCreationAdvances(route) + } else if (work.status === STATUS.FAILED) { + clearInterval(pollTimer!) + pollTimer = null + saveWorkId('') + error.value = sanitizeError(work.failReason) + } + } catch { + consecutiveErrors++ + if (consecutiveErrors > MAX_POLL_ERRORS) { + clearInterval(pollTimer!) + pollTimer = null + networkWarn.value = false + error.value = '网络连接异常,创作仍在后台进行中' + } else if (consecutiveErrors > MAX_SILENT_ERRORS) { + networkWarn.value = true + } + } +} // ─── B2 轮询 ─── -const startPolling = (workId: string) => { +const startPolling = async (workId: string) => { if (pollTimer) clearInterval(pollTimer) consecutiveErrors = 0 networkWarn.value = false pollTimer = setInterval(async () => { - try { - const work = await getWorkDetail(workId) - if (!work) return - - if (consecutiveErrors > 0 || networkWarn.value) { - consecutiveErrors = 0 - networkWarn.value = false - } - - if (work.progress != null && work.progress > progress.value) progress.value = work.progress - if (work.progressMessage) stage.value = friendlyStage(progress.value, work.progressMessage) - - if (work.status >= STATUS.COMPLETED) { - progress.value = 100 - stage.value = '绘本创作完成' - clearInterval(pollTimer!) - pollTimer = null - saveWorkId('') - const route = getRouteByStatus(work.status, workId) - if (route) replaceWhenCreationAdvances(route) - } else if (work.status === STATUS.FAILED) { - clearInterval(pollTimer!) - pollTimer = null - saveWorkId('') - error.value = sanitizeError(work.failReason) - } - } catch { - consecutiveErrors++ - if (consecutiveErrors > MAX_POLL_ERRORS) { - clearInterval(pollTimer!) - pollTimer = null - networkWarn.value = false - error.value = '网络连接异常,创作仍在后台进行中' - } else if (consecutiveErrors > MAX_SILENT_ERRORS) { - networkWarn.value = true - } - } - }, 8000) + await getWorkDetailApi(workId); + }, 8000); + try { + await getWorkDetailApi(workId); + } catch (error) { + console.log('error', error); + } } const startCreation = async () => { @@ -323,9 +332,10 @@ const startCreation = async () => { saveWorkId(workId) progress.value = 10 stage.value = '故事构思中…' - startWebSocket(workId) - + // startWebSocket(workId) + startPolling(store.workId) } catch (e: any) { + console.error('e', e); if (store.workId) { progress.value = 10 stage.value = '创作已提交到后台…' @@ -366,15 +376,19 @@ onMounted(() => { tipTimer = setInterval(() => { currentTipIdx.value = (currentTipIdx.value + 1) % creatingTips.length }, 3500) - // 恢复 workId const urlWorkId = new URLSearchParams(window.location.search).get('workId') + console.log('store.workId', urlWorkId, window.location.search) if (urlWorkId) { saveWorkId(urlWorkId) } else { restoreWorkId() } - + try { + getWorkDetailApi(store.workId) + } catch (error) { + console.log('error', error); + } if (store.workId) { submitted = true progress.value = 10 diff --git a/lesingle-creation-frontend/src/views/public/create/views/PreviewView.vue b/lesingle-creation-frontend/src/views/public/create/views/PreviewView.vue index b479685..f5a498d 100644 --- a/lesingle-creation-frontend/src/views/public/create/views/PreviewView.vue +++ b/lesingle-creation-frontend/src/views/public/create/views/PreviewView.vue @@ -53,13 +53,7 @@ export default { name: 'PreviewView' }