perf(代码审查落地): 后端 A1-A8 与前端 C1-C8 加固,retention 清理补索引(V141)
A 组(backend-java): - A1 载荷引用计数不再无条件回落全表 COUNT - A2 结果行读取由 LIMIT 1 改整页 - A3 ziniao 索引缓存过期判断下推 SQL - A4 循环子任务认领改 CAS,失败一律抛 - A5 建单事务里不碰外部 IO - A6 连接池补网络层超时(connectTimeout/socketTimeout,env 可覆盖) - A7 日志留现场 + 异步落盘 - A8 其余:货源图片预取、结果工作簿装配等 - V141:三条 retention DELETE 的可用索引,其中 biz_price_track_loop_run 用 STORED 生成列承载 COALESCE 时间线(该表实测 881 行/17MB,COPY 秒级) C 组(frontend-vue):URL 取值走 safe-url、行号取值走 row-id、 交付视频任务状态抽成 delivery-video-task-state.ts,各补用例。
This commit is contained in:
@@ -135,7 +135,7 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onBeforeUnmount, onMounted, reactive, ref } from 'vue'
|
||||
import { computed, onBeforeUnmount, onMounted, reactive, ref, shallowRef, triggerRef } from 'vue'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import AmazonToolPageShell from '@/pages/amazon/components/AmazonToolPageShell.vue';
|
||||
import FileUploadZone from '@/shared/components/FileUploadZone.vue'
|
||||
@@ -239,8 +239,11 @@ const dashboard = ref<CollectDataDashboardVo>({
|
||||
})
|
||||
const currentItems = ref<CollectDataHistoryItem[]>([])
|
||||
const historyItems = ref<CollectDataHistoryItem[]>([])
|
||||
/** 轮询过程中收到的任务最新快照,按 taskId 缓存,用于历史返回前的占位展示 */
|
||||
const taskSnapshots = ref<Record<number, CollectDataTaskDetailVo>>({})
|
||||
/**
|
||||
* 轮询过程中收到的任务最新快照,按 taskId 缓存,用于历史返回前的占位展示
|
||||
* (审查 C3:整份快照含行明细、体积大,改 shallowRef,写入一律整份替换并 triggerRef)
|
||||
*/
|
||||
const taskSnapshots = shallowRef<Record<number, CollectDataTaskDetailVo>>({})
|
||||
|
||||
const POLLING_STORAGE_KEY = 'brand:collect-data:polling-task-ids'
|
||||
|
||||
@@ -306,6 +309,7 @@ const progressLoop = useTaskProgressLoop<CollectDataTaskDetailVo>({
|
||||
// 只有轮询字段(或行明细)真的变了才展开快照对象:整份快照含行明细,无变化时重建纯属浪费
|
||||
if (!prev || merged.items !== prev.items || taskProgressSignatureOf(prev) !== taskProgressSignatureOf(merged)) {
|
||||
taskSnapshots.value = { ...taskSnapshots.value, [taskId]: merged }
|
||||
triggerRef(taskSnapshots)
|
||||
}
|
||||
patchCurrentItem(taskId, detail.items?.[0], detail.task?.status)
|
||||
},
|
||||
@@ -314,6 +318,7 @@ const progressLoop = useTaskProgressLoop<CollectDataTaskDetailVo>({
|
||||
const next = { ...taskSnapshots.value }
|
||||
delete next[taskId]
|
||||
taskSnapshots.value = next
|
||||
triggerRef(taskSnapshots)
|
||||
}
|
||||
await Promise.all([loadDashboard(), loadHistory()])
|
||||
},
|
||||
|
||||
@@ -69,7 +69,7 @@
|
||||
<div class="section-title">备选店铺</div>
|
||||
<div v-if="!candidates.length" class="empty-candidates">暂无备选店铺,请先输入并确认</div>
|
||||
<div v-else class="candidate-table-scroll">
|
||||
<el-table :data="candidates" row-key="id" height="160" class="candidate-table"
|
||||
<el-table :data="pagedCandidates" row-key="id" height="160" class="candidate-table"
|
||||
@selection-change="onSelectionChange">
|
||||
<el-table-column type="selection" width="42" />
|
||||
<el-table-column prop="shopName" label="店铺名" min-width="120" show-overflow-tooltip />
|
||||
@@ -80,6 +80,9 @@
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
</div>
|
||||
<el-pagination v-if="candidateTotal > candidatePageSize" class="candidate-pagination"
|
||||
layout="total, prev, pager, next" :total="candidateTotal" :page-size="candidatePageSize"
|
||||
v-model:current-page="candidatePage" />
|
||||
</div>
|
||||
|
||||
<template v-if="!asinModeEnabled">
|
||||
@@ -211,7 +214,7 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, onUnmounted, ref } from 'vue'
|
||||
import { computed, onMounted, onUnmounted, ref, shallowRef, triggerRef } from 'vue'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import AmazonToolPageShell from '@/pages/amazon/components/AmazonToolPageShell.vue';
|
||||
import ModuleTemplateDownload from '@/shared/components/ModuleTemplateDownload.vue'
|
||||
@@ -294,6 +297,13 @@ const {
|
||||
total: matchedTotal,
|
||||
paged: pagedMatchedItems,
|
||||
} = useTablePaging(matchedItems)
|
||||
// F9:备选店铺表同样只渲染当前页(listPriceTrackCandidates 是全量返回,与其余工具页一致)
|
||||
const {
|
||||
page: candidatePage,
|
||||
pageSize: candidatePageSize,
|
||||
total: candidateTotal,
|
||||
paged: pagedCandidates,
|
||||
} = useTablePaging(candidates)
|
||||
const adding = ref(false)
|
||||
const matching = ref(false)
|
||||
const pushing = ref(false)
|
||||
@@ -376,7 +386,13 @@ const dashboard = ref<PriceTrackDashboardVo>({
|
||||
const historyItems = ref<PriceTrackHistoryItem[]>([])
|
||||
const pollingTaskIds = ref<number[]>([])
|
||||
const taskDetails = ref<Record<number, string>>({})
|
||||
const taskSnapshots = ref<Record<number, PriceTrackTaskDetailVo>>({})
|
||||
/**
|
||||
* 任务快照容器(审查 C3):整份快照数百 KB,深响应式会把每层 items 都代理化,
|
||||
* 并在整份替换时级联重算 currentSectionItems → currentTaskViews → el-table。
|
||||
* 这里改 shallowRef 只跟踪「整份被替换」——本文件所有写入都是 taskSnapshots.value = 新对象,
|
||||
* 写入后统一 triggerRef 显式通知依赖,视图更新链保持原样。
|
||||
*/
|
||||
const taskSnapshots = shallowRef<Record<number, PriceTrackTaskDetailVo>>({})
|
||||
|
||||
/** 任务快照的 task 时间(开始=创建,结束=完成;运行中结束显示进行中) */
|
||||
function snapTask(item: PriceTrackHistoryItem) {
|
||||
@@ -487,6 +503,7 @@ function loadTaskSnapshotsFromStorage() {
|
||||
if (id > 0 && v && typeof v === 'object') out[id] = v as PriceTrackTaskDetailVo
|
||||
}
|
||||
taskSnapshots.value = out
|
||||
triggerRef(taskSnapshots)
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
@@ -1033,6 +1050,7 @@ async function waitForTaskTerminal(taskId: number) {
|
||||
if (detail) {
|
||||
const prev = taskSnapshots.value[taskId]
|
||||
taskSnapshots.value = { ...taskSnapshots.value, [taskId]: prev ? { ...prev, task: { ...(prev.task || {}), ...(detail.task || {}) } } : detail }
|
||||
triggerRef(taskSnapshots)
|
||||
saveTaskSnapshotsToStorage()
|
||||
}
|
||||
if (status) {
|
||||
@@ -1136,6 +1154,7 @@ async function processMatchedQueue() {
|
||||
items: taskVo.items,
|
||||
},
|
||||
}
|
||||
triggerRef(taskSnapshots)
|
||||
taskDetails.value = {
|
||||
...taskDetails.value,
|
||||
[taskVo.taskId]: 'RUNNING',
|
||||
@@ -1222,6 +1241,7 @@ function recordCreatedTask(taskVo: PriceTrackCreateTaskVo) {
|
||||
items: taskVo.items,
|
||||
},
|
||||
}
|
||||
triggerRef(taskSnapshots)
|
||||
taskDetails.value = {
|
||||
...taskDetails.value,
|
||||
[taskVo.taskId]: 'RUNNING',
|
||||
@@ -1464,6 +1484,7 @@ async function refreshTaskBatch() {
|
||||
}
|
||||
if (snapshotsChanged) {
|
||||
taskSnapshots.value = nextSnapshots
|
||||
triggerRef(taskSnapshots)
|
||||
saveTaskSnapshotsToStorage()
|
||||
}
|
||||
saveTaskDetailsToStorage()
|
||||
@@ -1532,6 +1553,7 @@ function removePollingTask(taskId: number) {
|
||||
const next = { ...taskSnapshots.value }
|
||||
delete next[taskId]
|
||||
taskSnapshots.value = next
|
||||
triggerRef(taskSnapshots)
|
||||
// 终态清理属于关键变更,跳过节流立即落盘
|
||||
saveTaskSnapshotsToStorage(true)
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
<div class="section-title">备选区域</div>
|
||||
<div v-if="!candidates.length" class="empty-candidates">暂无备选店铺,请先输入并确认</div>
|
||||
<div v-else class="candidate-table-scroll">
|
||||
<el-table :data="candidates" row-key="id" height="240" class="candidate-table"
|
||||
<el-table :data="pagedCandidates" row-key="id" height="240" class="candidate-table"
|
||||
@selection-change="onSelectionChange">
|
||||
<el-table-column type="selection" width="42" />
|
||||
<el-table-column prop="shop_name" label="店铺名" min-width="120" show-overflow-tooltip />
|
||||
@@ -29,6 +29,9 @@
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
</div>
|
||||
<el-pagination v-if="candidateTotal > candidatePageSize" class="candidate-pagination"
|
||||
layout="total, prev, pager, next" :total="candidateTotal" :page-size="candidatePageSize"
|
||||
v-model:current-page="candidatePage" />
|
||||
|
||||
<div class="section-title">处理国家与顺序</div>
|
||||
<p class="hint country-pref-hint">
|
||||
@@ -147,7 +150,7 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, onUnmounted, ref } from 'vue'
|
||||
import { computed, onMounted, onUnmounted, ref, shallowRef, triggerRef } from 'vue'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import AmazonToolPageShell from '@/pages/amazon/components/AmazonToolPageShell.vue';
|
||||
import TaskCenterPanel from '@/shared/components/tasks/TaskCenterPanel.vue'
|
||||
@@ -202,6 +205,13 @@ const {
|
||||
total: matchedTotal,
|
||||
paged: pagedMatchedItems,
|
||||
} = useTablePaging(matchedItems)
|
||||
// F9:备选店铺表同样只渲染当前页(loadCandidates 是全量返回,与其余工具页一致)
|
||||
const {
|
||||
page: candidatePage,
|
||||
pageSize: candidatePageSize,
|
||||
total: candidateTotal,
|
||||
paged: pagedCandidates,
|
||||
} = useTablePaging(candidates)
|
||||
const adding = ref(false)
|
||||
const matching = ref(false)
|
||||
const pushing = ref(false)
|
||||
@@ -353,8 +363,11 @@ const dashboard = ref<ProductRiskDashboardVo>({
|
||||
const historyItems = ref<ProductRiskHistoryItem[]>([])
|
||||
const pollingTaskIds = ref<number[]>([])
|
||||
const taskDetails = ref<Record<number, string>>({})
|
||||
/** 本地缓存 POST /tasks/batch 各 task 最新快照 + 创建任务响应,便于刷新后查看 */
|
||||
const taskSnapshots = ref<Record<number, ProductRiskTaskDetailVo>>({})
|
||||
/**
|
||||
* 本地缓存 POST /tasks/batch 各 task 最新快照 + 创建任务响应,便于刷新后查看
|
||||
* (审查 C3:整份快照数百 KB,改 shallowRef 只跟踪整份替换,写入后统一 triggerRef)
|
||||
*/
|
||||
const taskSnapshots = shallowRef<Record<number, ProductRiskTaskDetailVo>>({})
|
||||
|
||||
/** 任务快照的 task 时间(开始=创建,结束=完成;运行中结束显示进行中) */
|
||||
function snapTask(item: ProductRiskHistoryItem) {
|
||||
@@ -434,6 +447,7 @@ function loadTaskSnapshotsFromStorage() {
|
||||
if (id > 0 && v && typeof v === 'object') out[id] = v as ProductRiskTaskDetailVo
|
||||
}
|
||||
taskSnapshots.value = out
|
||||
triggerRef(taskSnapshots)
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
@@ -590,6 +604,7 @@ function removePollingTask(taskId: number) {
|
||||
const next = { ...taskSnapshots.value }
|
||||
delete next[taskId]
|
||||
taskSnapshots.value = next
|
||||
triggerRef(taskSnapshots)
|
||||
saveTaskSnapshotsToStorage()
|
||||
}
|
||||
}
|
||||
@@ -779,6 +794,7 @@ async function refreshTaskBatch() {
|
||||
}
|
||||
}
|
||||
taskSnapshots.value = nextSnapshots
|
||||
triggerRef(taskSnapshots)
|
||||
saveTaskSnapshotsToStorage()
|
||||
saveTaskDetailsToStorage()
|
||||
// 幽灵任务清理:light 明确判 missing 且重型端点复核也查无 ⇒ 服务端任务行已被删
|
||||
@@ -880,6 +896,7 @@ async function waitForTaskTerminal(taskId: number) {
|
||||
...taskSnapshots.value,
|
||||
[taskId]: prev ? { ...prev, task: { ...(prev.task || {}), ...(detail.task || {}) } } : detail,
|
||||
}
|
||||
triggerRef(taskSnapshots)
|
||||
saveTaskSnapshotsToStorage()
|
||||
}
|
||||
if (status) {
|
||||
@@ -1124,6 +1141,7 @@ async function processMatchedBatchQueue(toPush: ProductRiskShopQueueItem[]) {
|
||||
items: created.items,
|
||||
},
|
||||
}
|
||||
triggerRef(taskSnapshots)
|
||||
saveTaskSnapshotsToStorage()
|
||||
addPollingTask(taskId)
|
||||
ensurePolling(true)
|
||||
@@ -1220,6 +1238,7 @@ async function processMatchedQueue() {
|
||||
items: created.items,
|
||||
},
|
||||
}
|
||||
triggerRef(taskSnapshots)
|
||||
saveTaskSnapshotsToStorage()
|
||||
addPollingTask(taskId)
|
||||
ensurePolling(true)
|
||||
|
||||
@@ -173,7 +173,7 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
|
||||
import { computed, onBeforeUnmount, onMounted, ref, shallowRef, triggerRef, watch } from 'vue'
|
||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||
|
||||
import AmazonToolPageShell from '@/pages/amazon/components/AmazonToolPageShell.vue';
|
||||
@@ -269,7 +269,8 @@ const pendingFileIds = ref<number[]>([])
|
||||
const activeFileId = ref<number | null>(null)
|
||||
const dispatchOptions = ref<PublishDispatchOptions | null>(null)
|
||||
const queuedBatches = ref<PublishQueueBatch[]>([])
|
||||
const taskSnapshots = ref<Record<number, PublishTaskDetailVo>>({})
|
||||
/** 任务快照(审查 C3):整份可达数百 KB,shallowRef 只跟踪整份替换,写入后统一 triggerRef */
|
||||
const taskSnapshots = shallowRef<Record<number, PublishTaskDetailVo>>({})
|
||||
const historyItems = ref<PublishTaskDetailVo[]>([])
|
||||
const missingTaskIds = ref<number[]>([])
|
||||
const deletingTaskIds = ref<number[]>([])
|
||||
@@ -333,6 +334,7 @@ function mergeDetail(previous: PublishTaskDetailVo | undefined, incoming: Publis
|
||||
function applyTaskSnapshot(taskId: number, detail: PublishTaskDetailVo) {
|
||||
const merged = mergeDetail(taskSnapshots.value[taskId], detail)
|
||||
taskSnapshots.value = { ...taskSnapshots.value, [taskId]: merged }
|
||||
triggerRef(taskSnapshots)
|
||||
missingTaskIds.value = missingTaskIds.value.filter((id) => id !== taskId)
|
||||
if (currentTaskId.value === taskId) {
|
||||
currentFiles.value = mergeFiles(currentFiles.value, merged.files || [])
|
||||
@@ -707,6 +709,7 @@ function updateCurrentFile(fileId: number, patch: Partial<PublishFileItem>) {
|
||||
files: update(taskSnapshots.value[taskId].files || []),
|
||||
},
|
||||
}
|
||||
triggerRef(taskSnapshots)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1074,6 +1077,7 @@ function removeTaskLocally(taskId: number) {
|
||||
const snapshots = { ...taskSnapshots.value }
|
||||
delete snapshots[taskId]
|
||||
taskSnapshots.value = snapshots
|
||||
triggerRef(taskSnapshots)
|
||||
missingTaskIds.value = Array.from(new Set([...missingTaskIds.value, taskId]))
|
||||
queuedBatches.value = queuedBatches.value.filter((batch) => batch.taskId !== taskId)
|
||||
if (currentTaskId.value === taskId) {
|
||||
|
||||
@@ -129,7 +129,7 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, onUnmounted, ref } from 'vue'
|
||||
import { computed, onMounted, onUnmounted, ref, shallowRef, triggerRef } from 'vue'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import AmazonToolPageShell from '@/pages/amazon/components/AmazonToolPageShell.vue';
|
||||
import TaskCenterPanel from '@/shared/components/tasks/TaskCenterPanel.vue'
|
||||
@@ -216,7 +216,8 @@ const activeDispatched = ref(false)
|
||||
const activeCountryCodes = ref<string[]>([])
|
||||
const activeZiniaoVersion = ref<'new' | 'old'>('new')
|
||||
const pollingTaskIds = ref<number[]>([])
|
||||
const taskSnapshots = ref<Record<number, ShopDataCrawlTaskDetailVo>>({})
|
||||
/** 任务快照(审查 C3):整份含行明细、体积大,shallowRef 只跟踪整份替换,写入后统一 triggerRef */
|
||||
const taskSnapshots = shallowRef<Record<number, ShopDataCrawlTaskDetailVo>>({})
|
||||
const autoQueueEnabled = ref(false)
|
||||
let countryPrefSaveTimer: number | null = null
|
||||
let disposed = false
|
||||
@@ -302,7 +303,7 @@ function loadLocalState() {
|
||||
autoQueueEnabled.value = state.autoQueueEnabled || false
|
||||
} catch { pendingQueue.value = []; activeTaskId.value = null; activeQueueItem.value = null }
|
||||
try { pollingTaskIds.value = JSON.parse(window.localStorage.getItem(storageKey('polling-task-ids')) || '[]') } catch { pollingTaskIds.value = [] }
|
||||
try { taskSnapshots.value = JSON.parse(window.localStorage.getItem(storageKey('task-snapshots')) || '{}') } catch { taskSnapshots.value = {} }
|
||||
try { taskSnapshots.value = JSON.parse(window.localStorage.getItem(storageKey('task-snapshots')) || '{}'); triggerRef(taskSnapshots) } catch { taskSnapshots.value = {}; triggerRef(taskSnapshots) }
|
||||
if (activeDispatched.value && activeQueueItem.value) removeMatchedRow(activeQueueItem.value)
|
||||
}
|
||||
|
||||
@@ -403,6 +404,7 @@ function mergeProgress(detail: ShopDataCrawlTaskDetailVo) {
|
||||
const taskId = detail.task?.id || detail.items?.[0]?.taskId
|
||||
if (!taskId) return
|
||||
taskSnapshots.value = { ...taskSnapshots.value, [taskId]: detail }
|
||||
triggerRef(taskSnapshots)
|
||||
const incoming = (detail.items || []).map((item) => ({ ...item, taskId: item.taskId || taskId, taskStatus: item.taskStatus || detail.task?.status, error: item.error || detail.task?.errorMessage }))
|
||||
historyItems.value = mergeHistoryItems(historyItems.value, incoming, {
|
||||
keyOf: historyKey,
|
||||
@@ -555,6 +557,7 @@ async function processQueue() {
|
||||
pollingTaskIds.value = [...new Set([...pollingTaskIds.value, created.taskId])]
|
||||
const createdItems = (created.items || []).map((item) => ({ ...item, taskId: item.taskId || created.taskId }))
|
||||
taskSnapshots.value = { ...taskSnapshots.value, [created.taskId]: { task: { id: created.taskId, status: createdItems[0]?.taskStatus }, items: createdItems } }
|
||||
triggerRef(taskSnapshots)
|
||||
historyItems.value = [...createdItems, ...historyItems.value.filter((item) => item.taskId !== created.taskId)]
|
||||
saveQueueState()
|
||||
continue
|
||||
|
||||
@@ -14,12 +14,15 @@
|
||||
<div class="section-title">备选店铺</div>
|
||||
<div v-if="!candidates.length" class="empty-candidates">暂无备选店铺,请先输入并添加</div>
|
||||
<div v-else class="candidate-table-scroll">
|
||||
<el-table :data="candidates" row-key="id" height="240" class="candidate-table" @selection-change="onSelectionChange">
|
||||
<el-table :data="pagedCandidates" row-key="id" height="240" class="candidate-table" @selection-change="onSelectionChange">
|
||||
<el-table-column type="selection" width="42" />
|
||||
<el-table-column prop="shop_name" label="店铺名" min-width="120" show-overflow-tooltip />
|
||||
<el-table-column label="操作" width="72" align="center"><template #default="{ row }"><button type="button" class="link-danger" @click="removeCandidate(row)">删除</button></template></el-table-column>
|
||||
</el-table>
|
||||
</div>
|
||||
<el-pagination v-if="candidateTotal > candidatePageSize" class="candidate-pagination"
|
||||
layout="total, prev, pager, next" :total="candidateTotal" :page-size="candidatePageSize"
|
||||
v-model:current-page="candidatePage" />
|
||||
<div class="section-title">国家配置</div>
|
||||
<p class="hint country-pref-hint">勾选的国家会在每一个执行时间点都执行一遍。勾选区会按照已选顺序排在前面,下方支持拖拽排序,和商品风险页保持一致。</p>
|
||||
<div class="country-pref-checks">
|
||||
@@ -48,8 +51,8 @@
|
||||
<div class="section-title">定时执行</div>
|
||||
<label class="schedule-switch"><input v-model="scheduleEnabled" type="checkbox" :disabled="pushing" @change="onScheduleToggle" /><span>启用多时间点调度</span></label>
|
||||
<div v-if="scheduleEnabled" class="schedule-config">
|
||||
<div v-for="(value, index) in schedulePickerValues" :key="`schedule-${index}`" class="schedule-row">
|
||||
<el-date-picker :model-value="value" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" format="MM-DD HH:mm" placeholder="选择执行时间" :disabled="pushing" class="schedule-picker" @update:model-value="updateScheduleValue(index, $event)" />
|
||||
<div v-for="(row, index) in schedulePickerValues" :key="row.id" class="schedule-row">
|
||||
<el-date-picker :model-value="row.value" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" format="MM-DD HH:mm" placeholder="选择执行时间" :disabled="pushing" class="schedule-picker" @update:model-value="updateScheduleValue(index, $event)" />
|
||||
<button type="button" class="schedule-remove" :disabled="pushing || schedulePickerValues.length <= 1" @click="removeScheduleValue(index)">删除</button>
|
||||
</div>
|
||||
<button type="button" class="schedule-add" :disabled="pushing" @click="addScheduleValue">添加时间点</button>
|
||||
@@ -116,7 +119,7 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, onUnmounted, ref } from 'vue'
|
||||
import { computed, onMounted, onUnmounted, ref, shallowRef, triggerRef } from 'vue'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import AmazonToolPageShell from '@/pages/amazon/components/AmazonToolPageShell.vue';
|
||||
import TaskCenterPanel from '@/shared/components/tasks/TaskCenterPanel.vue'
|
||||
@@ -128,6 +131,7 @@ import { getPollingProgressBatch } from '@/shared/api/task-progress-polling.ts'
|
||||
import { getPywebviewApi } from '@/shared/bridges/pywebview'
|
||||
import { getTaskPollIntervalMs } from '@/shared/task-progress-config'
|
||||
import { createCategorizedTimers } from '@/shared/utils/categorized-timers'
|
||||
import { createRowId } from '@/shared/utils/row-id'
|
||||
import { saveUrlWithProgress } from '@/shared/utils/download-progress'
|
||||
import { taskStatusClass, taskStatusText } from '@/shared/utils/task-status-text'
|
||||
import { useZiniaoVersion } from '@/shared/utils/ziniao-version'
|
||||
@@ -148,6 +152,13 @@ const {
|
||||
total: matchedTotal,
|
||||
paged: pagedMatchedItems,
|
||||
} = useTablePaging(matchedItems)
|
||||
// F9:备选店铺表同样只渲染当前页(loadCandidates 是全量返回,与其余工具页一致)
|
||||
const {
|
||||
page: candidatePage,
|
||||
pageSize: candidatePageSize,
|
||||
total: candidateTotal,
|
||||
paged: pagedCandidates,
|
||||
} = useTablePaging(candidates)
|
||||
const shopMatchListingFilter = ref<ListingFilterValue>('Active')
|
||||
const adding = ref(false)
|
||||
const matching = ref(false)
|
||||
@@ -163,12 +174,18 @@ const countryPrefSaving = ref(false)
|
||||
const countryPrefUserTouched = ref(false)
|
||||
let countryPrefSaveTimer: ReturnType<typeof setTimeout> | null = null
|
||||
const scheduleEnabled = ref(true)
|
||||
const schedulePickerValues = ref<string[]>([getDefaultScheduleValue()])
|
||||
/**
|
||||
* 定时行状态:id 是给 v-for 用的稳定 key(此前用 index,删中间行时后面的 el-date-picker
|
||||
* 实例会被错位复用),value 仍是页面上看到的时间串。业务侧只读 value。
|
||||
*/
|
||||
type ScheduleRow = { id: string; value: string }
|
||||
const schedulePickerValues = ref<ScheduleRow[]>([{ id: createRowId('schedule'), value: getDefaultScheduleValue() }])
|
||||
const dashboard = ref<ShopMatchDashboardVo>({ candidateCount: 0, processedTaskCount: 0, successTaskCount: 0, failedTaskCount: 0 })
|
||||
const historyItems = ref<ShopMatchHistoryItem[]>([])
|
||||
const pollingTaskIds = ref<number[]>([])
|
||||
const taskDetails = ref<Record<number, string>>({})
|
||||
const taskSnapshots = ref<Record<number, ShopMatchTaskDetailVo>>({})
|
||||
/** 任务快照(审查 C3):整份数百 KB,shallowRef 只跟踪整份替换;本文件所有写入都是 .value = 新对象且随后 triggerRef */
|
||||
const taskSnapshots = shallowRef<Record<number, ShopMatchTaskDetailVo>>({})
|
||||
|
||||
/** 任务快照的 task 时间(开始=创建,结束=完成;运行中结束显示进行中) */
|
||||
function snapTask(item: ShopMatchHistoryItem) {
|
||||
@@ -260,7 +277,7 @@ function loadPollingIdsFromStorage() { try { const raw = window.localStorage.get
|
||||
function savePollingIds() { setStorageJson(sessionKey(), pollingTaskIds.value, pollingTaskIds.value.length === 0) }
|
||||
function loadTaskDetailsFromStorage() { try { const raw = window.localStorage.getItem(taskStatusStorageKey()); taskDetails.value = raw ? JSON.parse(raw) : {} } catch { taskDetails.value = {} } }
|
||||
function saveTaskDetailsToStorage() { setStorageJson(taskStatusStorageKey(), taskDetails.value, Object.keys(taskDetails.value).length === 0) }
|
||||
function loadTaskSnapshotsFromStorage() { try { const raw = window.localStorage.getItem(taskSnapshotsStorageKey()); taskSnapshots.value = raw ? JSON.parse(raw) : {} } catch { taskSnapshots.value = {} } }
|
||||
function loadTaskSnapshotsFromStorage() { try { const raw = window.localStorage.getItem(taskSnapshotsStorageKey()); taskSnapshots.value = raw ? JSON.parse(raw) : {}; triggerRef(taskSnapshots) } catch { taskSnapshots.value = {}; triggerRef(taskSnapshots) } }
|
||||
function saveTaskSnapshotsToStorage() { setStorageJson(taskSnapshotsStorageKey(), taskSnapshots.value, Object.keys(taskSnapshots.value).length === 0) }
|
||||
function loadMatchedItemsFromStorage() { try { const raw = window.localStorage.getItem(matchedItemsStorageKey()); matchedItems.value = raw ? JSON.parse(raw) : [] } catch { matchedItems.value = [] } }
|
||||
function saveMatchedItemsToStorage() { setStorageJson(matchedItemsStorageKey(), matchedItems.value, matchedItems.value.length === 0) }
|
||||
@@ -275,7 +292,7 @@ function removePollingTask(taskId: number) {
|
||||
if (taskSnapshots.value[taskId]) {
|
||||
const nextSnapshots = { ...taskSnapshots.value }
|
||||
delete nextSnapshots[taskId]
|
||||
taskSnapshots.value = nextSnapshots
|
||||
taskSnapshots.value = nextSnapshots; triggerRef(taskSnapshots)
|
||||
saveTaskSnapshotsToStorage()
|
||||
}
|
||||
savePollingIds()
|
||||
@@ -323,9 +340,9 @@ function onCountryDragStart(index: number) { dragCountryIndex.value = index }
|
||||
function onCountryDragEnd() { dragCountryIndex.value = null }
|
||||
function onCountryDrop(toIndex: number) { const from = dragCountryIndex.value; dragCountryIndex.value = null; if (from == null || from === toIndex) return; countryPrefUserTouched.value = true; const list = [...orderedCountryCodes.value]; const [item] = list.splice(from, 1); list.splice(toIndex, 0, item); orderedCountryCodes.value = list; scheduleSaveCountryPreference() }
|
||||
function onScheduleToggle() { if (scheduleEnabled.value) schedulePickerValues.value = sanitizeSchedulePickerValues(schedulePickerValues.value, true); if (!scheduleEnabled.value) schedulePickerValues.value = [] }
|
||||
function addScheduleValue() { const last = schedulePickerValues.value[schedulePickerValues.value.length - 1]; const nextValue = last ? nextScheduleValueAfter(last, 60) : getDefaultScheduleValue(); schedulePickerValues.value = [...schedulePickerValues.value, nextValue] }
|
||||
function addScheduleValue() { const last = schedulePickerValues.value[schedulePickerValues.value.length - 1]; const nextValue = last ? nextScheduleValueAfter(last.value, 60) : getDefaultScheduleValue(); schedulePickerValues.value = [...schedulePickerValues.value, { id: createRowId('schedule'), value: nextValue }] }
|
||||
function removeScheduleValue(index: number) { if (schedulePickerValues.value.length <= 1) return; schedulePickerValues.value = schedulePickerValues.value.filter((_, idx) => idx !== index) }
|
||||
function updateScheduleValue(index: number, value: string | null) { const list = [...schedulePickerValues.value]; list[index] = value || ''; schedulePickerValues.value = sanitizeSchedulePickerValues(list, false) }
|
||||
function updateScheduleValue(index: number, value: string | null) { const list = [...schedulePickerValues.value]; list[index] = { ...list[index], value: value || '' }; schedulePickerValues.value = sanitizeSchedulePickerValues(list, false) }
|
||||
function mergeMatchedItems(base: ShopMatchShopQueueItem[], incoming: ShopMatchShopQueueItem[]) { const map = new Map<string, ShopMatchShopQueueItem>(); for (const item of base) map.set(rowKeyForMatch(item), item); for (const item of incoming) map.set(rowKeyForMatch(item), item); return Array.from(map.values()) }
|
||||
async function runMatch() { const names = selectedCandidates.value.map((item) => item.shop_name).filter(Boolean); if (!names.length) { ElMessage.warning('请先选择候选店铺'); return } matching.value = true; try { const data = await matchShopMatchShops(names); matchedItems.value = mergeMatchedItems(matchedItems.value, data.items || []); saveMatchedItemsToStorage(); if (autoQueueEnabled.value) void processMatchedQueue(); ElMessage.success(`本次返回 ${data.items?.length || 0} 条匹配结果`) } catch (error) { ElMessage.error(error instanceof Error ? error.message : '匹配失败') } finally { matching.value = false } }
|
||||
function removeMatchedRow(row: ShopMatchShopQueueItem) { matchedItems.value = matchedItems.value.filter((item) => rowKeyForMatch(item) !== rowKeyForMatch(row)); saveMatchedItemsToStorage() }
|
||||
@@ -333,43 +350,46 @@ function removeMatchedRowsLocally(rows: ShopMatchHistoryItem[] | ShopMatchShopQu
|
||||
function parseScheduleValues() {
|
||||
if (!scheduleEnabled.value) return undefined
|
||||
const now = Date.now()
|
||||
const values = schedulePickerValues.value.map((item) => item.trim()).filter(Boolean)
|
||||
if (!values.length) throw new Error('请至少选择一个执行时间')
|
||||
const withTime = values.map((value) => {
|
||||
const date = parseScheduleDateTime(value)
|
||||
if (!date) throw new Error(`时间格式无效: ${value}`)
|
||||
return { raw: formatScheduleDateTime(date), picker: formatPickerDateTime(date), time: date.getTime() }
|
||||
const rows = schedulePickerValues.value
|
||||
.map((row) => ({ id: row.id, value: row.value.trim() }))
|
||||
.filter((row) => row.value)
|
||||
if (!rows.length) throw new Error('请至少选择一个执行时间')
|
||||
const withTime = rows.map((row) => {
|
||||
const date = parseScheduleDateTime(row.value)
|
||||
if (!date) throw new Error(`时间格式无效: ${row.value}`)
|
||||
return { id: row.id, raw: formatScheduleDateTime(date), picker: formatPickerDateTime(date), time: date.getTime() }
|
||||
}).sort((a, b) => a.time - b.time)
|
||||
for (let i = 1; i < withTime.length; i += 1) if (withTime[i].time === withTime[i - 1].time) throw new Error('执行时间不能重复')
|
||||
const futureOnly = withTime.filter((item) => item.time > now)
|
||||
if (!futureOnly.length) throw new Error('执行时间必须晚于当前时间,请重新选择')
|
||||
if (futureOnly.length !== withTime.length) {
|
||||
schedulePickerValues.value = futureOnly.map((item) => item.picker)
|
||||
// 回填时沿用各行原有 id:否则被移除的行会让后面行的 key 变化,日期控件实例跟着错位
|
||||
schedulePickerValues.value = futureOnly.map((item) => ({ id: item.id, value: item.picker }))
|
||||
ElMessage.warning('已自动移除早于当前时间的执行时间')
|
||||
}
|
||||
return futureOnly.map((item) => item.raw)
|
||||
}
|
||||
function sanitizeSchedulePickerValues(values: string[], ensureOne: boolean) {
|
||||
function sanitizeSchedulePickerValues(values: ScheduleRow[], ensureOne: boolean): ScheduleRow[] {
|
||||
const now = Date.now()
|
||||
const normalized = values
|
||||
.map((item) => item.trim())
|
||||
.filter(Boolean)
|
||||
.map((value) => {
|
||||
const date = parseScheduleDateTime(value)
|
||||
return date ? { value: formatPickerDateTime(date), time: date.getTime() } : null
|
||||
.map((row) => ({ id: row.id, value: row.value.trim() }))
|
||||
.filter((row) => row.value)
|
||||
.map((row) => {
|
||||
const date = parseScheduleDateTime(row.value)
|
||||
return date ? { id: row.id, value: formatPickerDateTime(date), time: date.getTime() } : null
|
||||
})
|
||||
.filter((item): item is { value: string; time: number } => !!item)
|
||||
.filter((item): item is { id: string; value: string; time: number } => !!item)
|
||||
.filter((item) => item.time > now)
|
||||
.sort((a, b) => a.time - b.time)
|
||||
|
||||
const deduped: string[] = []
|
||||
const deduped: ScheduleRow[] = []
|
||||
const seen = new Set<string>()
|
||||
for (const item of normalized) {
|
||||
if (seen.has(item.value)) continue
|
||||
seen.add(item.value)
|
||||
deduped.push(item.value)
|
||||
deduped.push({ id: item.id, value: item.value })
|
||||
}
|
||||
if (!deduped.length && ensureOne) return [getDefaultScheduleValue()]
|
||||
if (!deduped.length && ensureOne) return [{ id: createRowId('schedule'), value: getDefaultScheduleValue() }]
|
||||
return deduped
|
||||
}
|
||||
function getJavaApiBaseUrl() { return `${window.location.origin}/newApi/api` }
|
||||
@@ -415,7 +435,7 @@ function buildQueuePayload(taskId: number, item: Pick<ShopMatchHistoryItem, 'sho
|
||||
}
|
||||
}
|
||||
function nextScheduleStage(taskId: number) { const task = taskSnapshots.value[taskId]?.task; const stages = task?.scheduleStages || []; const currentStageIndex = task?.currentStageIndex; if (typeof currentStageIndex !== 'number') return null; const stage = stages.find((item) => item.stageIndex === currentStageIndex); if (!stage?.scheduledAt) return null; return { stageIndex: currentStageIndex, scheduledAt: stage.scheduledAt, finalStage: currentStageIndex === stages.length - 1 } }
|
||||
async function dispatchScheduledTask(taskId: number, item: ShopMatchHistoryItem) { const api = getPywebviewApi(); if (!api?.enqueue_json) throw new Error('当前客户端未提供 enqueue_json'); const stage = nextScheduleStage(taskId); const snapshot = taskSnapshots.value[taskId]; const countryCodes = snapshot?.task?.countryCodes || orderedCountryCodes.value; if (!stage) throw new Error(`任务 ${taskId} 缺少待执行阶段`); clearDispatchTimer(taskId); await withTransientRetry(() => activateShopMatchTask(taskId, stage.stageIndex), (attempt, maxAttempts) => { queuePushResult.value = `任务 ${taskId} 等待服务恢复后再激活执行(${attempt}/${maxAttempts})...` }); taskDetails.value = { ...taskDetails.value, [taskId]: 'RUNNING' }; if (snapshot?.task) taskSnapshots.value = { ...taskSnapshots.value, [taskId]: { ...snapshot, task: { ...snapshot.task, status: 'RUNNING', activeStageIndex: stage.stageIndex } } }; saveTaskSnapshotsToStorage(); saveTaskDetailsToStorage(); const payload = buildQueuePayload(taskId, item, countryCodes, stage.stageIndex, stage.finalStage); queuePayloadText.value = JSON.stringify(payload, null, 2); const result = await api.enqueue_json(payload); if (!result?.success) throw new Error(result?.error || `任务 ${taskId} 启动失败`); addPollingTask(taskId); queuePushResult.value = `任务 ${taskId} 已启动第 ${stage.stageIndex + 1} 轮`; ensurePolling(true) }
|
||||
async function dispatchScheduledTask(taskId: number, item: ShopMatchHistoryItem) { const api = getPywebviewApi(); if (!api?.enqueue_json) throw new Error('当前客户端未提供 enqueue_json'); const stage = nextScheduleStage(taskId); const snapshot = taskSnapshots.value[taskId]; const countryCodes = snapshot?.task?.countryCodes || orderedCountryCodes.value; if (!stage) throw new Error(`任务 ${taskId} 缺少待执行阶段`); clearDispatchTimer(taskId); await withTransientRetry(() => activateShopMatchTask(taskId, stage.stageIndex), (attempt, maxAttempts) => { queuePushResult.value = `任务 ${taskId} 等待服务恢复后再激活执行(${attempt}/${maxAttempts})...` }); taskDetails.value = { ...taskDetails.value, [taskId]: 'RUNNING' }; if (snapshot?.task) taskSnapshots.value = { ...taskSnapshots.value, [taskId]: { ...snapshot, task: { ...snapshot.task, status: 'RUNNING', activeStageIndex: stage.stageIndex } } }; triggerRef(taskSnapshots); saveTaskSnapshotsToStorage(); saveTaskDetailsToStorage(); const payload = buildQueuePayload(taskId, item, countryCodes, stage.stageIndex, stage.finalStage); queuePayloadText.value = JSON.stringify(payload, null, 2); const result = await api.enqueue_json(payload); if (!result?.success) throw new Error(result?.error || `任务 ${taskId} 启动失败`); addPollingTask(taskId); queuePushResult.value = `任务 ${taskId} 已启动第 ${stage.stageIndex + 1} 轮`; ensurePolling(true) }
|
||||
function scheduledTimestamp(value?: string) { const date = parseScheduleDateTime(value); return date ? date.getTime() : Number.NaN }
|
||||
function findReadyScheduledTasks() { const taskIds = new Set<number>(pollingTaskIds.value); for (const key of Object.keys(taskSnapshots.value)) { const id = normalizeTaskId(key); if (id > 0) taskIds.add(id) } for (const item of historyItems.value) { const id = normalizeTaskId(item.taskId); if (id > 0) taskIds.add(id) } const ready = Array.from(taskIds).map((taskId) => { const snapshot = taskSnapshots.value[taskId]; const taskStatus = taskDetails.value[taskId] || snapshot?.task?.status; const item = snapshot?.items?.[0] || historyItems.value.find((row) => normalizeTaskId(row.taskId) === taskId); const stage = nextScheduleStage(taskId); if (taskStatus !== 'SCHEDULED' || !item || !stage?.scheduledAt) return null; const scheduledTime = scheduledTimestamp(stage.scheduledAt); if (!Number.isFinite(scheduledTime) || scheduledTime > Date.now()) return null; return { taskId, item, scheduledTime } }).filter((item): item is { taskId: number; item: ShopMatchHistoryItem; scheduledTime: number } => !!item); ready.sort((a, b) => a.scheduledTime - b.scheduledTime || a.taskId - b.taskId); return ready }
|
||||
async function pumpScheduledQueue() {
|
||||
@@ -456,7 +476,7 @@ function clearSleepTimers() { timers.clearCategory('queue-wait') }
|
||||
function isTransientBackendError(error: unknown) { const message = (error instanceof Error ? error.message : String(error || '')).toLowerCase(); return TRANSIENT_BACKEND_ERROR_PATTERNS.some((pattern) => message.includes(pattern.toLowerCase())) }
|
||||
async function withTransientRetry<T>(action: () => Promise<T>, onRetry: (attempt: number, maxAttempts: number) => void, maxAttempts = 10) { let attempt = 0; while (true) { if (disposed) throw new Error('component disposed'); try { return await action() } catch (error) { attempt += 1; if (!isTransientBackendError(error) || attempt >= maxAttempts) throw error; onRetry(attempt, maxAttempts); await sleep(getPollIntervalMs()) } } }
|
||||
async function refreshTaskBatch() { if (!pollingTaskIds.value.length) return { settledTaskIds: [] as number[] }; try { // F5:主轮询只读 status 等轻量白名单字段,走 light 端点减小响应体;轻量异常/空结果时回退重型 batch(否则会漏刷新结果)
|
||||
const batch = await getPollingProgressBatch('shopMatch', pollingTaskIds.value, { fallback: () => getShopMatchTaskProgressBatch(pollingTaskIds.value) }); const nextSnapshots = { ...taskSnapshots.value }; const settledTaskIds = new Set<number>(); for (const missingId of batch.missingTaskIds || []) { removeTaskLocally(missingId); delete nextSnapshots[missingId]; settledTaskIds.add(missingId) } for (const detail of batch.items || []) { const taskId = detail.task?.id; if (!taskId) continue; const prev = nextSnapshots[taskId]; nextSnapshots[taskId] = prev ? { ...prev, task: { ...(prev.task || {}), ...(detail.task || {}) } } : detail; const status = detail.task?.status || ''; if (!status) continue; taskDetails.value[taskId] = status; if (isTaskTerminalStatus(status)) { removePollingTask(taskId); settledTaskIds.add(taskId); continue } if (status === 'SCHEDULED') { stopPollingTask(taskId); settledTaskIds.add(taskId) } } taskSnapshots.value = nextSnapshots; saveTaskSnapshotsToStorage(); saveTaskDetailsToStorage(); restoreScheduledDispatches(); return { settledTaskIds: Array.from(settledTaskIds) } } catch { return { settledTaskIds: [] as number[] } } }
|
||||
const batch = await getPollingProgressBatch('shopMatch', pollingTaskIds.value, { fallback: () => getShopMatchTaskProgressBatch(pollingTaskIds.value) }); const nextSnapshots = { ...taskSnapshots.value }; const settledTaskIds = new Set<number>(); for (const missingId of batch.missingTaskIds || []) { removeTaskLocally(missingId); delete nextSnapshots[missingId]; settledTaskIds.add(missingId) } for (const detail of batch.items || []) { const taskId = detail.task?.id; if (!taskId) continue; const prev = nextSnapshots[taskId]; nextSnapshots[taskId] = prev ? { ...prev, task: { ...(prev.task || {}), ...(detail.task || {}) } } : detail; const status = detail.task?.status || ''; if (!status) continue; taskDetails.value[taskId] = status; if (isTaskTerminalStatus(status)) { removePollingTask(taskId); settledTaskIds.add(taskId); continue } if (status === 'SCHEDULED') { stopPollingTask(taskId); settledTaskIds.add(taskId) } } taskSnapshots.value = nextSnapshots; triggerRef(taskSnapshots); saveTaskSnapshotsToStorage(); saveTaskDetailsToStorage(); restoreScheduledDispatches(); return { settledTaskIds: Array.from(settledTaskIds) } } catch { return { settledTaskIds: [] as number[] } } }
|
||||
function getPollIntervalMs() { return getTaskPollIntervalMs() }
|
||||
function scheduleNextPoll(immediate = false) { if (disposed) return; if (pollTimer.value) { if (!immediate) return; timers.clearTimer('task-poll', pollTimer.value); pollTimer.value = null } const run = async () => { pollTimer.value = null; if (disposed) return; if (pollingInFlight.value || !pollingTaskIds.value.length) { if (pollingTaskIds.value.length) scheduleNextPoll(); return } pollingInFlight.value = true; try { const { settledTaskIds } = await refreshTaskBatch(); if (settledTaskIds.length) await Promise.allSettled([loadHistory(), loadDashboard()]) } finally { pollingInFlight.value = false } if (!disposed && pollingTaskIds.value.length) pollTimer.value = timers.setTimeout('task-poll', run, getPollIntervalMs()) }; if (immediate) void run(); else pollTimer.value = timers.setTimeout('task-poll', run, getPollIntervalMs()) }
|
||||
function ensurePolling(immediate = false) { scheduleNextPoll(immediate) }
|
||||
@@ -464,7 +484,7 @@ function stopPolling() { if (pollTimer.value) { timers.clearTimer('task-poll', p
|
||||
async function waitForTaskTerminal(taskId: number) { let transientErrorCount = 0; const maxTransientErrors = 30; while (true) { if (disposed) return 'STOPPED'; try { // F5:等待终态的紧循环只读 status,走 light 端点减小轮询响应体,异常回退重型 batch
|
||||
const batch = await getPollingProgressBatch('shopMatch', [taskId], {
|
||||
fallback: () => getShopMatchTaskProgressBatch([taskId]),
|
||||
}); if (transientErrorCount > 0) queuePushResult.value = `任务 ${taskId} 服务已恢复,继续等待执行结果...`; transientErrorCount = 0; if ((batch.missingTaskIds || []).includes(taskId)) { removeTaskLocally(taskId); await refreshTaskViewsBestEffort(); restoreScheduledDispatches(); return 'FAILED' } const detail = (batch.items || []).find((item) => item.task?.id === taskId); const status = detail?.task?.status || ''; if (detail) { const prev = taskSnapshots.value[taskId]; taskSnapshots.value = { ...taskSnapshots.value, [taskId]: prev ? { ...prev, task: { ...(prev.task || {}), ...(detail.task || {}) } } : detail }; saveTaskSnapshotsToStorage() } if (status) { taskDetails.value[taskId] = status; saveTaskDetailsToStorage() } if (isTaskTerminalStatus(status)) { removePollingTask(taskId); await refreshTaskViewsBestEffort(); restoreScheduledDispatches(); return status } } catch (error) { if (!isTransientBackendError(error)) throw error; transientErrorCount += 1; if (transientErrorCount >= maxTransientErrors) throw new Error(`任务 ${taskId} 等待服务恢复超时,请稍后手动刷新查看状态`); queuePushResult.value = `任务 ${taskId} 运行中,正在等待服务恢复(${transientErrorCount}/${maxTransientErrors})...`; if (transientErrorCount === 1 || transientErrorCount % 5 === 0) ElMessage.warning(`任务 ${taskId} 运行中,服务暂时不可用,正在自动重试`); await sleep(getPollIntervalMs()); continue } await sleep(getPollIntervalMs()) } }
|
||||
}); if (transientErrorCount > 0) queuePushResult.value = `任务 ${taskId} 服务已恢复,继续等待执行结果...`; transientErrorCount = 0; if ((batch.missingTaskIds || []).includes(taskId)) { removeTaskLocally(taskId); await refreshTaskViewsBestEffort(); restoreScheduledDispatches(); return 'FAILED' } const detail = (batch.items || []).find((item) => item.task?.id === taskId); const status = detail?.task?.status || ''; if (detail) { const prev = taskSnapshots.value[taskId]; taskSnapshots.value = { ...taskSnapshots.value, [taskId]: prev ? { ...prev, task: { ...(prev.task || {}), ...(detail.task || {}) } } : detail }; triggerRef(taskSnapshots); saveTaskSnapshotsToStorage() } if (status) { taskDetails.value[taskId] = status; saveTaskDetailsToStorage() } if (isTaskTerminalStatus(status)) { removePollingTask(taskId); await refreshTaskViewsBestEffort(); restoreScheduledDispatches(); return status } } catch (error) { if (!isTransientBackendError(error)) throw error; transientErrorCount += 1; if (transientErrorCount >= maxTransientErrors) throw new Error(`任务 ${taskId} 等待服务恢复超时,请稍后手动刷新查看状态`); queuePushResult.value = `任务 ${taskId} 运行中,正在等待服务恢复(${transientErrorCount}/${maxTransientErrors})...`; if (transientErrorCount === 1 || transientErrorCount % 5 === 0) ElMessage.warning(`任务 ${taskId} 运行中,服务暂时不可用,正在自动重试`); await sleep(getPollIntervalMs()); continue } await sleep(getPollIntervalMs()) } }
|
||||
async function waitForScheduledTaskStageExit(taskId: number) {
|
||||
let transientErrorCount = 0
|
||||
const maxTransientErrors = 30
|
||||
@@ -484,7 +504,7 @@ async function waitForScheduledTaskStageExit(taskId: number) {
|
||||
const status = detail?.task?.status || ''
|
||||
if (detail) {
|
||||
const prev = taskSnapshots.value[taskId]
|
||||
taskSnapshots.value = { ...taskSnapshots.value, [taskId]: prev ? { ...prev, task: { ...(prev.task || {}), ...(detail.task || {}) } } : detail }
|
||||
taskSnapshots.value = { ...taskSnapshots.value, [taskId]: prev ? { ...prev, task: { ...(prev.task || {}), ...(detail.task || {}) } } : detail }; triggerRef(taskSnapshots)
|
||||
saveTaskSnapshotsToStorage()
|
||||
}
|
||||
if (status) {
|
||||
@@ -573,7 +593,7 @@ function formatMatchStatus(status?: string) { const value = (status || '').trim(
|
||||
function formatMatchRemark(row: ShopMatchShopQueueItem) { const message = (row.matchMessage || '').trim(); if (message) return message; if (row.matched && row.matchStatus === 'MATCHED') return '已匹配成功,可启动任务'; if (row.matched) return '已关联店铺,请查看状态确认'; if (row.matchStatus === 'CONFLICT') return '存在多个同名店铺,请人工确认'; if (row.matchStatus === 'PENDING') return '店铺尚未匹配完成,请稍后查看'; return '未匹配成功,请检查店铺名' }
|
||||
async function pushToPythonQueue() { autoQueueEnabled.value = true; await processMatchedQueue() }
|
||||
function nextMatchedQueueItem() { return matchedItems.value.find((item) => item.matched) }
|
||||
async function processMatchedQueue() { if (disposed || queueWorkerRunning.value) return; const api = getPywebviewApi(); if (!api?.enqueue_json) { ElMessage.error('当前客户端未提供 enqueue_json'); return } const matched = matchedItems.value.filter((item) => item.matched); if (!matched.length) { ElMessage.warning('请先匹配可用店铺'); return } if (!orderedCountryCodes.value.length) { ElMessage.warning('请至少勾选一个国家'); return } let scheduleValues: string[] | undefined; try { scheduleValues = parseScheduleValues() } catch (error) { ElMessage.error(error instanceof Error ? error.message : '定时配置无效'); return } queueWorkerRunning.value = true; pushing.value = true; queuePayloadText.value = ''; let successCount = 0; let failedCount = 0; let index = 0; try { while (!disposed && autoQueueEnabled.value) { const item = nextMatchedQueueItem(); if (!item) break; index += 1; try { const created = await withTransientRetry(() => createShopMatchTask([buildTaskCreateItem(item)], orderedCountryCodes.value, scheduleValues), (attempt, maxAttempts) => { queuePushResult.value = `服务暂时不可用,正在重试创建任务(${attempt}/${maxAttempts})...` }); const taskId = created.taskId; const initialStatus = scheduleValues?.length ? 'SCHEDULED' : 'RUNNING'; const snapshot: ShopMatchTaskDetailVo = { task: { id: taskId, status: initialStatus, scheduledAt: scheduleValues?.[0], countryCodes: [...orderedCountryCodes.value], currentStageIndex: scheduleValues?.length ? 0 : undefined, activeStageIndex: undefined, scheduleStages: (scheduleValues || []).map((value, stageIndex) => ({ stageIndex, scheduledAt: value, status: stageIndex === 0 ? 'SCHEDULED' : 'PENDING' })) }, items: created.items }; taskSnapshots.value = { ...taskSnapshots.value, [taskId]: snapshot }; taskDetails.value = { ...taskDetails.value, [taskId]: initialStatus }; saveTaskSnapshotsToStorage(); saveTaskDetailsToStorage(); const createdItem = created.items?.[0]; if (!createdItem) { removeMatchedRowsLocally([item]); continue } if (scheduleValues?.length) { successCount += 1; restoreScheduledDispatches(); queuePushResult.value = `任务 ${taskId} 已创建,共 ${scheduleValues.length} 个执行时间点`; removeMatchedRowsLocally([item]); continue } const payload = buildQueuePayload(taskId, item, orderedCountryCodes.value); queuePayloadText.value = JSON.stringify(payload, null, 2); const result = await api.enqueue_json(payload); if (!result?.success) { failedCount += 1; queuePushResult.value = `任务 ${taskId} 启动失败,已自动继续下一条:${result?.error || '未知错误'}`; ElMessage.error(queuePushResult.value); removeMatchedRowsLocally([item]); continue } addPollingTask(taskId); ensurePolling(true); removeMatchedRowsLocally([item]); queuePushResult.value = `任务 ${taskId} 已提交,等待执行完成`; const finalStatus = await waitForTaskTerminal(taskId); if (finalStatus !== 'SUCCESS') { failedCount += 1; queuePushResult.value = `任务 ${taskId} 执行失败,已自动继续下一条`; ElMessage.error(queuePushResult.value); continue } successCount += 1; queuePushResult.value = `任务 ${taskId} 已完成` } catch (error) { if (disposed) break; failedCount += 1; queuePushResult.value = `第 ${index} 条任务处理失败,已自动继续下一条:${error instanceof Error ? error.message : '未知错误'}`; ElMessage.error(queuePushResult.value); removeMatchedRowsLocally([item]); continue } } restoreScheduledDispatches(); if (!scheduleValues?.length) ensurePolling(true); if (!disposed && (successCount > 0 || failedCount > 0)) ElMessage.success(scheduleValues?.length ? `定时任务已创建:成功 ${successCount} 条,失败 ${failedCount} 条` : `店铺启动已完成:成功 ${successCount} 条,失败 ${failedCount} 条`) } catch (error) { if (disposed) return; const message = error instanceof Error ? error.message : '启动失败'; queuePushResult.value = message; ElMessage.error(message) } finally { queueWorkerRunning.value = false; pushing.value = false } }
|
||||
async function processMatchedQueue() { if (disposed || queueWorkerRunning.value) return; const api = getPywebviewApi(); if (!api?.enqueue_json) { ElMessage.error('当前客户端未提供 enqueue_json'); return } const matched = matchedItems.value.filter((item) => item.matched); if (!matched.length) { ElMessage.warning('请先匹配可用店铺'); return } if (!orderedCountryCodes.value.length) { ElMessage.warning('请至少勾选一个国家'); return } let scheduleValues: string[] | undefined; try { scheduleValues = parseScheduleValues() } catch (error) { ElMessage.error(error instanceof Error ? error.message : '定时配置无效'); return } queueWorkerRunning.value = true; pushing.value = true; queuePayloadText.value = ''; let successCount = 0; let failedCount = 0; let index = 0; try { while (!disposed && autoQueueEnabled.value) { const item = nextMatchedQueueItem(); if (!item) break; index += 1; try { const created = await withTransientRetry(() => createShopMatchTask([buildTaskCreateItem(item)], orderedCountryCodes.value, scheduleValues), (attempt, maxAttempts) => { queuePushResult.value = `服务暂时不可用,正在重试创建任务(${attempt}/${maxAttempts})...` }); const taskId = created.taskId; const initialStatus = scheduleValues?.length ? 'SCHEDULED' : 'RUNNING'; const snapshot: ShopMatchTaskDetailVo = { task: { id: taskId, status: initialStatus, scheduledAt: scheduleValues?.[0], countryCodes: [...orderedCountryCodes.value], currentStageIndex: scheduleValues?.length ? 0 : undefined, activeStageIndex: undefined, scheduleStages: (scheduleValues || []).map((value, stageIndex) => ({ stageIndex, scheduledAt: value, status: stageIndex === 0 ? 'SCHEDULED' : 'PENDING' })) }, items: created.items }; taskSnapshots.value = { ...taskSnapshots.value, [taskId]: snapshot }; triggerRef(taskSnapshots); taskDetails.value = { ...taskDetails.value, [taskId]: initialStatus }; saveTaskSnapshotsToStorage(); saveTaskDetailsToStorage(); const createdItem = created.items?.[0]; if (!createdItem) { removeMatchedRowsLocally([item]); continue } if (scheduleValues?.length) { successCount += 1; restoreScheduledDispatches(); queuePushResult.value = `任务 ${taskId} 已创建,共 ${scheduleValues.length} 个执行时间点`; removeMatchedRowsLocally([item]); continue } const payload = buildQueuePayload(taskId, item, orderedCountryCodes.value); queuePayloadText.value = JSON.stringify(payload, null, 2); const result = await api.enqueue_json(payload); if (!result?.success) { failedCount += 1; queuePushResult.value = `任务 ${taskId} 启动失败,已自动继续下一条:${result?.error || '未知错误'}`; ElMessage.error(queuePushResult.value); removeMatchedRowsLocally([item]); continue } addPollingTask(taskId); ensurePolling(true); removeMatchedRowsLocally([item]); queuePushResult.value = `任务 ${taskId} 已提交,等待执行完成`; const finalStatus = await waitForTaskTerminal(taskId); if (finalStatus !== 'SUCCESS') { failedCount += 1; queuePushResult.value = `任务 ${taskId} 执行失败,已自动继续下一条`; ElMessage.error(queuePushResult.value); continue } successCount += 1; queuePushResult.value = `任务 ${taskId} 已完成` } catch (error) { if (disposed) break; failedCount += 1; queuePushResult.value = `第 ${index} 条任务处理失败,已自动继续下一条:${error instanceof Error ? error.message : '未知错误'}`; ElMessage.error(queuePushResult.value); removeMatchedRowsLocally([item]); continue } } restoreScheduledDispatches(); if (!scheduleValues?.length) ensurePolling(true); if (!disposed && (successCount > 0 || failedCount > 0)) ElMessage.success(scheduleValues?.length ? `定时任务已创建:成功 ${successCount} 条,失败 ${failedCount} 条` : `店铺启动已完成:成功 ${successCount} 条,失败 ${failedCount} 条`) } catch (error) { if (disposed) return; const message = error instanceof Error ? error.message : '启动失败'; queuePushResult.value = message; ElMessage.error(message) } finally { queueWorkerRunning.value = false; pushing.value = false } }
|
||||
onMounted(async () => { loadPollingIdsFromStorage(); loadTaskDetailsFromStorage(); loadTaskSnapshotsFromStorage(); loadMatchedItemsFromStorage(); schedulePickerValues.value = sanitizeSchedulePickerValues(schedulePickerValues.value, scheduleEnabled.value); syncPollingIdsWithTaskState(); await Promise.allSettled([loadCandidates(), loadCountryPreference(), loadHistory(), loadDashboard()]); window.addEventListener('focus', handleScheduleRecovery); document.addEventListener('visibilitychange', handleScheduleRecovery); restoreScheduledDispatches(); if (pollingTaskIds.value.length) ensurePolling(true) })
|
||||
onUnmounted(() => { disposed = true; stopPolling(); stopScheduleHeartbeat(); clearSleepTimers(); window.removeEventListener('focus', handleScheduleRecovery); document.removeEventListener('visibilitychange', handleScheduleRecovery); if (countryPrefSaveTimer) timers.clearTimer('preference-save', countryPrefSaveTimer); for (const timer of dispatchTimers.values()) timers.clearTimer('scheduled-dispatch', timer); dispatchTimers.clear(); timers.clearScope() })
|
||||
|
||||
|
||||
@@ -143,14 +143,14 @@
|
||||
<div class="kv-section">结果文件</div>
|
||||
<div v-for="(url, index) in resultFiles(detailItem)" :key="url" class="kv-row">
|
||||
<span class="kv-key">文件 {{ index + 1 }}</span>
|
||||
<a class="kv-val link" :href="url" target="_blank" rel="noopener">{{ url }}</a>
|
||||
<a class="kv-val link" :href="safeHttpUrl(url)" target="_blank" rel="noopener">{{ url }}</a>
|
||||
</div>
|
||||
</template>
|
||||
<template v-if="detailItem.task_data?.file_url">
|
||||
<div class="kv-section">源文件</div>
|
||||
<div class="kv-row">
|
||||
<span class="kv-key">源文件</span>
|
||||
<a class="kv-val link" :href="detailItem.task_data.file_url" target="_blank" rel="noopener">{{ detailItem.task_data.file_url }}</a>
|
||||
<a class="kv-val link" :href="sourceFileHref(detailItem)" target="_blank" rel="noopener">{{ detailItem.task_data.file_url }}</a>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
@@ -159,7 +159,7 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onBeforeUnmount, onMounted, reactive, ref } from 'vue'
|
||||
import { computed, onBeforeUnmount, onMounted, reactive, ref, watch } from 'vue'
|
||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||
import AmazonToolPageShell from '@/pages/amazon/components/AmazonToolPageShell.vue'
|
||||
import {
|
||||
@@ -167,6 +167,9 @@ import {
|
||||
type VariantTaskListItem,
|
||||
} from '@/shared/bridges/pywebview'
|
||||
import { saveUrlWithProgress } from '@/shared/utils/download-progress'
|
||||
import { safeHttpUrl } from '@/shared/utils/safe-url'
|
||||
import { createCategorizedTimers } from '@/shared/utils/categorized-timers'
|
||||
import { useHistoryPolling } from '@/shared/composables/useHistoryPolling'
|
||||
import { taskStatusClass, taskStatusText, type TaskStatusOptions } from '@/shared/utils/task-status-text'
|
||||
|
||||
/** 本地任务记录:桥接返回的列表项 + 前端维护字段 */
|
||||
@@ -252,7 +255,6 @@ function statusClass(status?: number | string) {
|
||||
}
|
||||
|
||||
let autoTimer: number | null = null
|
||||
let refreshTimer: number | null = null
|
||||
// 导出轮询用独立计时器:此前与自动刷新共用 autoTimer,两条链互相覆盖,必有一条无法被 cleanup 清掉
|
||||
let exportTimer: number | null = null
|
||||
// 组件已卸载标记:轮询链的异步回调在卸载后仍会续链(切页后继续打接口)
|
||||
@@ -302,6 +304,11 @@ function resultFiles(item?: VariantEntry) {
|
||||
return toArray(item?.file_url)
|
||||
}
|
||||
|
||||
/** 源文件链接:task_data 是客户端原样透传的开放对象,渲染前必须过 http(s) 白名单 */
|
||||
function sourceFileHref(item?: VariantEntry) {
|
||||
return safeHttpUrl(item?.task_data?.file_url)
|
||||
}
|
||||
|
||||
function hasResultFiles(item: VariantTaskListItem) {
|
||||
const resType = Number(item.res_type)
|
||||
return (resType === 1 || String(item.res_type) === '1') && resultFiles(item).length > 0
|
||||
@@ -659,49 +666,70 @@ function showDetail(item: VariantTaskListItem) {
|
||||
detailVisible.value = true
|
||||
}
|
||||
|
||||
const timers = createCategorizedTimers('variant')
|
||||
|
||||
/** 列表里是否还有排队/执行中的任务(没有就不该再定时打接口) */
|
||||
const hasActiveTask = computed(() => currentTasks.value.some((item) => isBusy(item.status)))
|
||||
|
||||
function clearAutoRefresh() {
|
||||
if (!autoTimer) return
|
||||
window.clearTimeout(autoTimer)
|
||||
autoTimer = null
|
||||
}
|
||||
|
||||
function scheduleAutoRefresh() {
|
||||
if (autoTimer) {
|
||||
window.clearTimeout(autoTimer)
|
||||
autoTimer = null
|
||||
}
|
||||
clearAutoRefresh()
|
||||
autoTimer = window.setTimeout(async () => {
|
||||
if (disposed) {
|
||||
return
|
||||
}
|
||||
await loadTasks(currentPage.value || 1)
|
||||
// 卸载后不得再续链:异步回调返回时组件可能已销毁,续链会持续打接口
|
||||
if (!disposed && currentTasks.value.some((item) => isBusy(item.status))) {
|
||||
if (!disposed && hasActiveTask.value) {
|
||||
scheduleAutoRefresh()
|
||||
}
|
||||
}, 3000)
|
||||
}
|
||||
|
||||
function startPolling() {
|
||||
void loadTasks(1)
|
||||
refreshTimer = window.setInterval(() => {
|
||||
void loadTasks(currentPage.value || 1)
|
||||
}, 60 * 1000)
|
||||
}
|
||||
/**
|
||||
* 历史列表轮询(审查 C4):此前是无条件的 60 秒 setInterval(既无 shouldPoll 也无可见性判断),
|
||||
* 页面挂着不动也一直打 /batch/list。现在只在「有活跃任务」时按同样的 60 秒节奏刷新,空闲即停;
|
||||
* 定时器统一登记进 categorized-timers,卸载时由 clearScope 兜底清理。
|
||||
*/
|
||||
const historyPolling = useHistoryPolling({
|
||||
timers,
|
||||
isDisposed: () => disposed,
|
||||
shouldPoll: () => hasActiveTask.value,
|
||||
refresh: () => loadTasks(currentPage.value || 1),
|
||||
// 保持原 60 秒节奏,只是加上了「有活跃任务」的门槛
|
||||
intervalMs: () => 60 * 1000,
|
||||
})
|
||||
|
||||
// 活跃任务出现 → 起 3 秒快刷链 + 60 秒列表轮询;全部落定 → 两条链都停
|
||||
watch(hasActiveTask, (active) => {
|
||||
if (active) {
|
||||
scheduleAutoRefresh()
|
||||
historyPolling.start()
|
||||
} else {
|
||||
clearAutoRefresh()
|
||||
historyPolling.stop()
|
||||
}
|
||||
})
|
||||
|
||||
function cleanup() {
|
||||
disposed = true
|
||||
if (autoTimer) {
|
||||
window.clearTimeout(autoTimer)
|
||||
autoTimer = null
|
||||
}
|
||||
clearAutoRefresh()
|
||||
if (exportTimer) {
|
||||
window.clearTimeout(exportTimer)
|
||||
exportTimer = null
|
||||
}
|
||||
if (refreshTimer) {
|
||||
window.clearInterval(refreshTimer)
|
||||
refreshTimer = null
|
||||
}
|
||||
historyPolling.stop()
|
||||
timers.clearScope()
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
startPolling()
|
||||
scheduleAutoRefresh()
|
||||
// 首发只发一次:此前 startPolling() 的立即 loadTasks(1) 与 scheduleAutoRefresh() 各打一遍同一页
|
||||
void loadTasks(1)
|
||||
})
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
|
||||
@@ -153,7 +153,7 @@
|
||||
placeholder="例如:防滑耐磨、复古网面透气设计。" />
|
||||
</div>
|
||||
<div class="product-assets">
|
||||
<AiAssetDropzone v-for="(asset, index) in currentWorkspace.productAssets" :key="index"
|
||||
<AiAssetDropzone v-for="(asset, index) in currentWorkspace.productAssets" :key="asset.id"
|
||||
:title="`产品图 ${index + 1}`" :description="index === 0 ? '最多上传 5 张,提交时按接口数组格式传入。' : ''"
|
||||
placeholder="上传产品图" helper="支持 JPG / PNG" accept="image/*" :file-name="asset.fileName"
|
||||
:preview-url="asset.previewUrl" :preview-kind="asset.previewKind" :source-url="asset.sourceUrl"
|
||||
@@ -213,7 +213,7 @@
|
||||
placeholder="例如:防滑耐磨、复古网面透气设计。" />
|
||||
</div>
|
||||
<div class="product-assets">
|
||||
<AiAssetDropzone v-for="(asset, index) in currentWorkspace.productAssets" :key="index"
|
||||
<AiAssetDropzone v-for="(asset, index) in currentWorkspace.productAssets" :key="asset.id"
|
||||
:title="`产品图 ${index + 1}`" :description="index === 0 ? '最多上传 5 张,提交时按接口数组格式传入。' : ''"
|
||||
placeholder="上传产品图" helper="支持 JPG / PNG" accept="image/*" :file-name="asset.fileName"
|
||||
:preview-url="asset.previewUrl" :preview-kind="asset.previewKind" :source-url="asset.sourceUrl"
|
||||
@@ -371,11 +371,11 @@
|
||||
</div>
|
||||
<div v-if="assemblyVideoUrl" class="video-result-panel__link">
|
||||
<span>视频地址</span>
|
||||
<a :href="assemblyVideoUrl" target="_blank" rel="noreferrer">{{ assemblyVideoUrl }}</a>
|
||||
<a :href="safeHttpUrl(assemblyVideoUrl)" target="_blank" rel="noreferrer">{{ assemblyVideoUrl }}</a>
|
||||
</div>
|
||||
<div v-if="assemblyDebugUrl" class="video-result-panel__link">
|
||||
<span>调试链接</span>
|
||||
<a :href="assemblyDebugUrl" target="_blank" rel="noreferrer">{{ assemblyDebugUrl }}</a>
|
||||
<a :href="safeHttpUrl(assemblyDebugUrl)" target="_blank" rel="noreferrer">{{ assemblyDebugUrl }}</a>
|
||||
</div>
|
||||
<details v-if="assemblyResultText" class="video-result-panel__debug">
|
||||
<summary>查看原始返回</summary>
|
||||
@@ -500,6 +500,8 @@ import AiAssetDropzone from '@/shared/components/ai-workflow/AiAssetDropzone.vue
|
||||
import AiChoicePills from '@/shared/components/ai-workflow/AiChoicePills.vue'
|
||||
import AiSectionCard from '@/shared/components/ai-workflow/AiSectionCard.vue'
|
||||
import { saveUrlWithProgress } from '@/shared/utils/download-progress'
|
||||
import { createRowId } from '@/shared/utils/row-id'
|
||||
import { safeHttpUrl } from '@/shared/utils/safe-url'
|
||||
import {
|
||||
cloneImageVideoVoice,
|
||||
deleteImageVideoVoice,
|
||||
@@ -515,6 +517,7 @@ import {
|
||||
type ImageVideoAsyncTaskVo,
|
||||
type ImageVideoWorkflowParameters,
|
||||
} from '@/shared/api/java-modules'
|
||||
import { isTerminalImageVideoTask } from '@/pages/image-video/delivery-video-task-state'
|
||||
|
||||
type WorkspaceTab = 'remake' | 'imageToVideo'
|
||||
type PreviewKind = 'image' | 'video' | 'file'
|
||||
@@ -534,6 +537,8 @@ const IMAGE_VIDEO_TASK_MAX_POLLS = 720
|
||||
let disposed = false
|
||||
|
||||
type AssetState = {
|
||||
/** v-for 稳定 key(审查 C6):产品图可删中间行,用 index 当 key 会让上传槽组件实例错位复用 */
|
||||
id: string
|
||||
fileName: string
|
||||
previewUrl: string
|
||||
previewKind: PreviewKind
|
||||
@@ -675,6 +680,7 @@ const voiceCloneAsset = reactive<AssetState>(createAssetState())
|
||||
|
||||
function createAssetState(): AssetState {
|
||||
return {
|
||||
id: createRowId('asset'),
|
||||
fileName: '',
|
||||
previewUrl: '',
|
||||
previewKind: 'file',
|
||||
@@ -890,7 +896,8 @@ async function setProductAsset(tab: WorkspaceTab, index: number, file: File) {
|
||||
function clearProductAsset(tab: WorkspaceTab, index: number) {
|
||||
const productAssets = workspaces[tab].productAssets
|
||||
if (productAssets.length === 1) {
|
||||
Object.assign(productAssets[0], createAssetState())
|
||||
// 只剩一行时原地清空:保留 id,避免这一行的 :key 变化导致上传槽组件被重建
|
||||
Object.assign(productAssets[0], createAssetState(), { id: productAssets[0].id })
|
||||
} else {
|
||||
productAssets.splice(index, 1)
|
||||
}
|
||||
@@ -987,25 +994,6 @@ function sleep(milliseconds: number) {
|
||||
return new Promise<void>((resolve) => window.setTimeout(resolve, milliseconds))
|
||||
}
|
||||
|
||||
function normalizeTaskStatus(value: string | undefined) {
|
||||
return (value || '').trim().toUpperCase()
|
||||
}
|
||||
|
||||
function isTerminalCozeStatus(value: string | undefined) {
|
||||
return ['SUCCESS', 'SUCCEEDED', 'COMPLETED', 'DONE', 'FINISHED', 'FAILED', 'FAIL', 'ERROR', 'CANCELED', 'CANCELLED']
|
||||
.includes(normalizeTaskStatus(value))
|
||||
}
|
||||
|
||||
function isRecoverableFailedImageVideoTask(task: ImageVideoAsyncTaskVo) {
|
||||
return task.status === 'FAILED'
|
||||
&& Boolean(task.cozeExecuteId)
|
||||
&& !isTerminalCozeStatus(task.cozeStatus)
|
||||
}
|
||||
|
||||
function isTerminalImageVideoTask(task: ImageVideoAsyncTaskVo) {
|
||||
return task.status === 'SUCCESS' || (task.status === 'FAILED' && !isRecoverableFailedImageVideoTask(task))
|
||||
}
|
||||
|
||||
async function waitForImageVideoTask(ticket: ImageVideoAsyncTaskVo): Promise<unknown> {
|
||||
let task = ticket
|
||||
for (let pollCount = 0; pollCount < IMAGE_VIDEO_TASK_MAX_POLLS; pollCount += 1) {
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* 交付视频工作流的任务终态判定(从 DeliveryVideoWorkspace.vue 抽出)。
|
||||
*
|
||||
* 单独成模块的理由:这组谓词决定「失败任务要不要继续轮询、什么时候算真失败」,
|
||||
* 判错的后果是把没跑完的任务当完成回传(本项目在外观专利/跟价上吃过同一类亏)。
|
||||
* 留在 2700 行的单文件组件里就没法单测,抽出来才有断言可写。
|
||||
*/
|
||||
import type { ImageVideoAsyncTaskVo } from '@/shared/api/java-modules'
|
||||
|
||||
/** Coze 侧已经不会再变化的状态:命中即代表本地没必要继续等远端回执 */
|
||||
const TERMINAL_COZE_STATUSES = [
|
||||
'SUCCESS', 'SUCCEEDED', 'COMPLETED', 'DONE', 'FINISHED',
|
||||
'FAILED', 'FAIL', 'ERROR', 'CANCELED', 'CANCELLED',
|
||||
]
|
||||
|
||||
export function normalizeTaskStatus(value: string | undefined): string {
|
||||
return (value || '').trim().toUpperCase()
|
||||
}
|
||||
|
||||
export function isTerminalCozeStatus(value: string | undefined): boolean {
|
||||
return TERMINAL_COZE_STATUSES.includes(normalizeTaskStatus(value))
|
||||
}
|
||||
|
||||
/**
|
||||
* 「本地记为失败、但远端还在跑」的可恢复单:
|
||||
* 有 cozeExecuteId 且远端状态未终态 → 继续轮询,不向用户报失败。
|
||||
*/
|
||||
export function isRecoverableFailedImageVideoTask(task: ImageVideoAsyncTaskVo): boolean {
|
||||
return task.status === 'FAILED'
|
||||
&& Boolean(task.cozeExecuteId)
|
||||
&& !isTerminalCozeStatus(task.cozeStatus)
|
||||
}
|
||||
|
||||
/** 终态:成功,或「失败且不可恢复」。 */
|
||||
export function isTerminalImageVideoTask(task: ImageVideoAsyncTaskVo): boolean {
|
||||
return task.status === 'SUCCESS' || (task.status === 'FAILED' && !isRecoverableFailedImageVideoTask(task))
|
||||
}
|
||||
@@ -10,32 +10,120 @@
|
||||
import type { DesktopConfig, DesktopConfigUpdate, PywebviewApi } from './pywebview'
|
||||
import { extractErrorMessage, http } from '@/shared/api/http'
|
||||
|
||||
/** 虚拟路径 → 浏览器 File 对象(虚拟路径仅存活于当前页面会话,刷新即失效) */
|
||||
/**
|
||||
* 虚拟路径 → 浏览器 File 对象(虚拟路径仅存活于当前页面会话,刷新即失效)
|
||||
*
|
||||
* 桌面 WebView 常驻不刷新,整份 Excel File(数 MB)会一直被 Map 持有,所以这里做双重有界化:
|
||||
* 1. 上传成功即 delete(文件已进服务端,页面后续只用返回的 fileKey,不再需要本地 File);
|
||||
* 2. 只选不传(用户放弃提交)的条目按 FIFO 淘汰,容量上限见 MAX_TRACKED_WEB_FILES。
|
||||
*/
|
||||
const webFileMap = new Map<string, File>()
|
||||
let webFileSeq = 0
|
||||
const MAX_TRACKED_WEB_FILES = 8
|
||||
|
||||
/** 放入新的 File 前先按 FIFO 淘汰最旧条目,保证 Map 不会随选文件次数无界增长。 */
|
||||
function rememberWebFile(id: string, file: File) {
|
||||
while (webFileMap.size >= MAX_TRACKED_WEB_FILES) {
|
||||
const oldest = webFileMap.keys().next()
|
||||
if (oldest.done) break
|
||||
webFileMap.delete(oldest.value)
|
||||
}
|
||||
webFileMap.set(id, file)
|
||||
}
|
||||
|
||||
const CONFIG_STORAGE_KEY = 'web_desktop_config'
|
||||
|
||||
function readStoredConfig(): DesktopConfig {
|
||||
/**
|
||||
* 敏感配置字段(口令/密钥类)判定:沿用 bridges/pywebview.ts 中 DesktopConfig /
|
||||
* DesktopConfigUpdate 已声明的字段命名(aliprice_pwd 这类 *_pwd 口令后缀),
|
||||
* 再覆盖同族写法 passwd/password 与密钥/令牌(secret/token/api_key)。
|
||||
*
|
||||
* 不含用户名(aliprice_usename)与代理地址(proxy_url):页面要用它们做回填与展示,
|
||||
* 收紧这两类字段会改变既有可见行为。
|
||||
*/
|
||||
const SENSITIVE_CONFIG_KEY_RE = /(?:^|[_-])(?:pwd|passwd|password|secret|token|api[_-]?key)$/i
|
||||
|
||||
function isSensitiveConfigKey(key: string) {
|
||||
return SENSITIVE_CONFIG_KEY_RE.test(key)
|
||||
}
|
||||
|
||||
/**
|
||||
* 敏感字段在本会话内的内存副本:浏览器形态不落 localStorage(同页 XSS / 共用机器读不到明文),
|
||||
* 但 read_config 仍能取回刚保存的值,页面「保存 → 回显 → 提交」链路行为不变。
|
||||
* 桌面形态(pywebview)走原生桥的 app.json,代码根本不经过本文件。
|
||||
*/
|
||||
const webMemoryConfig: Record<string, unknown> = {}
|
||||
|
||||
/** 读磁盘原始副本(不含内存值) */
|
||||
function readDiskConfig(): DesktopConfig {
|
||||
try {
|
||||
const raw = window.localStorage.getItem(CONFIG_STORAGE_KEY) || ''
|
||||
return raw ? (JSON.parse(raw) as DesktopConfig) : {}
|
||||
if (!raw) return {}
|
||||
const parsed = JSON.parse(raw) as DesktopConfig
|
||||
// 历史版本把口令明文写进过 localStorage:读到时一并丢弃
|
||||
const out: Record<string, unknown> = {}
|
||||
for (const [key, value] of Object.entries(parsed || {})) {
|
||||
if (isSensitiveConfigKey(key)) continue
|
||||
out[key] = value
|
||||
}
|
||||
return out as DesktopConfig
|
||||
} catch {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
function writeStoredConfig(data: DesktopConfigUpdate): DesktopConfig {
|
||||
const merged: DesktopConfig = { ...readStoredConfig(), ...data }
|
||||
let storedSensitivePurged = false
|
||||
|
||||
/** 首次读写时把磁盘上已有的敏感字段抹掉一次(之后写入路径不再产生敏感字段) */
|
||||
function purgeStoredSensitiveOnce() {
|
||||
if (storedSensitivePurged || typeof window === 'undefined') return
|
||||
storedSensitivePurged = true
|
||||
try {
|
||||
window.localStorage.setItem(CONFIG_STORAGE_KEY, JSON.stringify(merged))
|
||||
const raw = window.localStorage.getItem(CONFIG_STORAGE_KEY) || ''
|
||||
if (!raw) return
|
||||
const parsed = JSON.parse(raw) as Record<string, unknown>
|
||||
if (!Object.keys(parsed || {}).some(isSensitiveConfigKey)) return
|
||||
const cleaned: Record<string, unknown> = {}
|
||||
for (const [key, value] of Object.entries(parsed)) {
|
||||
if (isSensitiveConfigKey(key)) continue
|
||||
cleaned[key] = value
|
||||
}
|
||||
window.localStorage.setItem(CONFIG_STORAGE_KEY, JSON.stringify(cleaned))
|
||||
} catch {
|
||||
/* 清理失败不阻断主流程:读取侧同样会丢弃敏感字段 */
|
||||
}
|
||||
}
|
||||
|
||||
function readStoredConfig(): DesktopConfig {
|
||||
purgeStoredSensitiveOnce()
|
||||
return { ...readDiskConfig(), ...webMemoryConfig } as DesktopConfig
|
||||
}
|
||||
|
||||
function writeStoredConfig(data: DesktopConfigUpdate): DesktopConfig {
|
||||
purgeStoredSensitiveOnce()
|
||||
const persistable: Record<string, unknown> = {}
|
||||
for (const [key, value] of Object.entries(data || {})) {
|
||||
if (isSensitiveConfigKey(key)) {
|
||||
// 口令/密钥类:只进内存,绝不落盘
|
||||
webMemoryConfig[key] = value
|
||||
continue
|
||||
}
|
||||
persistable[key] = value
|
||||
}
|
||||
const persisted: DesktopConfig = { ...readDiskConfig(), ...persistable } as DesktopConfig
|
||||
try {
|
||||
window.localStorage.setItem(CONFIG_STORAGE_KEY, JSON.stringify(persisted))
|
||||
} catch {
|
||||
/* 本地存储异常时忽略:配置读取仍是空对象 */
|
||||
}
|
||||
return merged
|
||||
// 返回结构与桌面桥一致(含本次保存的敏感字段),页面按原样使用
|
||||
return { ...persisted, ...webMemoryConfig } as DesktopConfig
|
||||
}
|
||||
|
||||
/** 打开浏览器文件选择框;用户取消时返回空数组(不阻塞后续选择) */
|
||||
/** 文件选择框挂起兜底超时:旧内核取消时不触发 cancel 事件,Promise 会永远挂起 */
|
||||
const FILE_PICK_TIMEOUT_MS = 10 * 60 * 1000
|
||||
|
||||
/** 打开浏览器文件选择框;用户取消/超时都返回空数组(不阻塞后续选择) */
|
||||
function pickBrowserFiles(accept = '', multiple = false): Promise<File[]> {
|
||||
return new Promise((resolve) => {
|
||||
const input = document.createElement('input')
|
||||
@@ -44,11 +132,21 @@ function pickBrowserFiles(accept = '', multiple = false): Promise<File[]> {
|
||||
input.multiple = multiple
|
||||
input.style.display = 'none'
|
||||
document.body.appendChild(input)
|
||||
let settled = false
|
||||
// 统一 cleanup:无论 change / cancel / 超时,都摘掉临时 DOM 节点并清掉计时器
|
||||
// (此前取消路径只 remove 一次,挂起路径上 DOM 节点永不摘除)
|
||||
const cleanup = () => {
|
||||
if (settled) return
|
||||
settled = true
|
||||
window.clearTimeout(guardTimer)
|
||||
input.remove()
|
||||
}
|
||||
const guardTimer = window.setTimeout(() => {
|
||||
cleanup()
|
||||
resolve([])
|
||||
}, FILE_PICK_TIMEOUT_MS)
|
||||
input.addEventListener('cancel', () => {
|
||||
// 现代浏览器支持 file input 的 cancel 事件;旧浏览器取消时 Promise 保持挂起(下次选择会覆盖 UI,无副作用)
|
||||
// 现代浏览器支持 file input 的 cancel 事件;旧浏览器取消时不触发,由上面的超时兜底
|
||||
cleanup()
|
||||
resolve([])
|
||||
})
|
||||
@@ -68,7 +166,7 @@ function pickBrowserFiles(accept = '', multiple = false): Promise<File[]> {
|
||||
function registerWebFiles(files: File[]): string[] {
|
||||
return files.map((file) => {
|
||||
const id = `web-file:${++webFileSeq}/${file.name}`
|
||||
webFileMap.set(id, file)
|
||||
rememberWebFile(id, file)
|
||||
return id
|
||||
})
|
||||
}
|
||||
@@ -143,6 +241,11 @@ export const webFallbackApi: PywebviewApi = {
|
||||
try {
|
||||
// http 实例对 /newApi 前缀自动附带 Bearer;FormData 的 Content-Type 交由浏览器自动生成(含 boundary)
|
||||
const response = await http.post('/newApi/api/files/upload', form, { timeout: 120000 })
|
||||
// 消费完即释放:文件已落到服务端,页面后续只用返回的 fileKey/url,不再需要这份本地 File。
|
||||
// 失败分支保留条目,让用户能在不重新选文件的情况下重试(超量时由 rememberWebFile 的 FIFO 淘汰兜底)。
|
||||
if (response.data?.success) {
|
||||
webFileMap.delete(filePath)
|
||||
}
|
||||
return response.data
|
||||
} catch (error) {
|
||||
return { success: false, error: extractErrorMessage(error) }
|
||||
@@ -161,11 +264,12 @@ export const webFallbackApi: PywebviewApi = {
|
||||
return downloadTemplateFile(`/static/${encodeURIComponent(name)}`, name)
|
||||
},
|
||||
|
||||
/** 代理等桌面配置降级到 localStorage(Web 环境与桌面端配置相互独立) */
|
||||
/** 代理等桌面配置降级:非敏感字段落 localStorage,口令/密钥类只存内存(见 writeStoredConfig) */
|
||||
async read_config() {
|
||||
return readStoredConfig()
|
||||
},
|
||||
|
||||
/** 浏览器形态的桌面配置写入:敏感字段拒绝持久化,仅保留在会话内存中 */
|
||||
async save_config(data) {
|
||||
return writeStoredConfig(data)
|
||||
},
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
* - 服务端(/api/user-secrets)是唯一权威:按登录用户绑定,客户端只缓存元数据与最近输入的值;
|
||||
* - `getStoredApiSecret` / `getStoredApiSecretSnapshot` 保持同步读语义(提交任务瞬间立即取用),
|
||||
* 读取顺序:内存 → 本地镜像 → 旧版 v1 记录(`brand:api-secret:{uid}:{moduleKey}`);
|
||||
* - 本地镜像(localStorage)在浏览器形态只存脱敏值与状态位,明文只活在本页内存;
|
||||
* 桌面形态(有 window.pywebview.api 原生存储)维持原有整条镜像,不动既有流程;
|
||||
* - 保存/清空/检测走异步接口;服务端拉取失败一律软失败(返回 unknown,不阻断使用);
|
||||
* - 旧版本地密钥在首次加载时自动迁移到服务端(只填空缺、不覆盖)。
|
||||
*/
|
||||
@@ -28,7 +30,10 @@ export type { UserApiSecretCheckResult }
|
||||
export type ApiSecretLoadState = 'complete' | 'incomplete' | 'unknown'
|
||||
|
||||
export interface ApiSecretSnapshot {
|
||||
/** 本地明文缓存(来自用户输入或旧记录迁移);服务端不回传明文,换机后可能为空。 */
|
||||
/**
|
||||
* 本地明文(来自用户输入或旧记录迁移),只存内存 Map;
|
||||
* 浏览器形态的磁盘镜像不含此字段,服务端不回传明文,换机/刷新后可能为空。
|
||||
*/
|
||||
value: string
|
||||
/** 服务端脱敏值(权威展示,如 sk-a****1234)。 */
|
||||
masked: string
|
||||
@@ -93,6 +98,19 @@ function buildLegacyKey(moduleKey: string) {
|
||||
return `${LEGACY_PREFIX}:${currentUid()}:${moduleKey}`
|
||||
}
|
||||
|
||||
/**
|
||||
* 形态判定:桌面客户端(pywebview)会注入 window.pywebview.api 并提供原生 app.json 存储,
|
||||
* 其本地明文缓存是既有任务链路依赖(重启后仍能取用);纯浏览器形态没有原生存储,
|
||||
* localStorage 是同页脚本可直接读到的明文落点,必须收紧。
|
||||
*
|
||||
* 这里刻意用内联的 window 断言而不是 import hasPywebview():静态引入 bridges/pywebview
|
||||
* 会把 web-fallback → http 拉进 node 测试环境(本模块被单测直接 import)。
|
||||
*/
|
||||
function isNativeDesktopStorage(): boolean {
|
||||
if (typeof window === 'undefined') return false
|
||||
return Boolean((window as unknown as { pywebview?: { api?: unknown } }).pywebview?.api)
|
||||
}
|
||||
|
||||
function emptyEntry(): ApiSecretCacheEntry {
|
||||
return {
|
||||
schema: 2,
|
||||
@@ -128,7 +146,7 @@ function readMirror(moduleKey: string): ApiSecretCacheEntry | null {
|
||||
if (!raw) return null
|
||||
const parsed = JSON.parse(raw) as Partial<ApiSecretCacheEntry>
|
||||
if (parsed.schema !== 2) return null
|
||||
return {
|
||||
const entry: ApiSecretCacheEntry = {
|
||||
schema: 2,
|
||||
value: typeof parsed.value === 'string' ? parsed.value : '',
|
||||
masked: typeof parsed.masked === 'string' ? parsed.masked : '',
|
||||
@@ -141,15 +159,23 @@ function readMirror(moduleKey: string): ApiSecretCacheEntry | null {
|
||||
checkedAt: typeof parsed.checkedAt === 'number' ? parsed.checkedAt : null,
|
||||
updatedAt: typeof parsed.updatedAt === 'number' ? parsed.updatedAt : null,
|
||||
}
|
||||
// 浏览器形态:历史版本可能已把明文写进磁盘,读到就顺手抹掉(明文只留内存)
|
||||
if (entry.value && !isNativeDesktopStorage()) {
|
||||
entry.value = ''
|
||||
writeMirror(moduleKey, entry)
|
||||
}
|
||||
return entry
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/** 磁盘镜像写入:浏览器形态只写脱敏值与状态位,明文不落地;桌面形态维持既有整条镜像。 */
|
||||
function writeMirror(moduleKey: string, entry: ApiSecretCacheEntry) {
|
||||
if (typeof window === 'undefined') return
|
||||
const persisted: ApiSecretCacheEntry = isNativeDesktopStorage() ? entry : { ...entry, value: '' }
|
||||
try {
|
||||
window.localStorage.setItem(buildMirrorKey(moduleKey), JSON.stringify(entry))
|
||||
window.localStorage.setItem(buildMirrorKey(moduleKey), JSON.stringify(persisted))
|
||||
} catch {
|
||||
/* 本地镜像写入失败不影响主流程 */
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* 列表行稳定 key(审查 C6)。
|
||||
*
|
||||
* `v-for` 用 index 当 :key 时,删除中间行会让后续行的组件实例被错位复用
|
||||
* (上传槽/日期选择器的内部状态跟着串行)。这里给状态对象发一个整页会话内唯一的 id。
|
||||
*
|
||||
* crypto.randomUUID 只在安全上下文可用;桌面客户端 WebView2 走 https 加载页面时有,
|
||||
* 但为兼容 http/旧内核仍保留「时间戳 + 进程内自增 + 随机数」的降级实现(同页会话内唯一即可)。
|
||||
*/
|
||||
let rowSeq = 0
|
||||
|
||||
export function createRowId(prefix = 'row'): string {
|
||||
const randomUuid = typeof crypto !== 'undefined' ? crypto.randomUUID : undefined
|
||||
if (randomUuid) {
|
||||
try {
|
||||
return `${prefix}:${randomUuid.call(crypto)}`
|
||||
} catch {
|
||||
/* 非安全上下文 Some WebView 实现会抛,走降级分支 */
|
||||
}
|
||||
}
|
||||
rowSeq += 1
|
||||
return `${prefix}:${Date.now().toString(36)}-${rowSeq.toString(36)}-${Math.random().toString(36).slice(2, 10)}`
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* 外链白名单(审查 C6):渲染成 a[href] / window.open 之前过滤协议。
|
||||
*
|
||||
* 结果文件地址、task_data 里的 file_url 等都是客户端/服务端原样透传的开放字段
|
||||
* (见 shared/bridges/pywebview.ts 对 task_data 的说明),页面直接绑到 :href 上时,
|
||||
* `javascript:` / `data:` 这类伪协议会在当前文档上下文里执行。这里只放行 http/https。
|
||||
*/
|
||||
|
||||
const ALLOWED_PROTOCOLS = new Set(['http:', 'https:'])
|
||||
|
||||
/** 非 http(s) 或无法解析的地址一律返回 '#',其余原样返回(相对路径按当前页面补全后判定)。 */
|
||||
export function safeHttpUrl(raw?: string | null): string {
|
||||
const value = typeof raw === 'string' ? raw.trim() : ''
|
||||
if (!value) return '#'
|
||||
const base = typeof window !== 'undefined' && window.location?.href ? window.location.href : 'http://localhost/'
|
||||
let parsed: URL
|
||||
try {
|
||||
parsed = new URL(value, base)
|
||||
} catch {
|
||||
return '#'
|
||||
}
|
||||
return ALLOWED_PROTOCOLS.has(parsed.protocol) ? value : '#'
|
||||
}
|
||||
Reference in New Issue
Block a user