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:
@@ -185,6 +185,19 @@
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<configuration>
|
||||
<!-- src/main/resources/db/aiimage.sql 是一份 120MB 的生产库 Navicat 转储
|
||||
(含 biz_shop_key 等真实数据行),Flyway 不认它(无 V<n>__ 前缀),
|
||||
但默认资源打包会原样塞进 BOOT-INF/classes/ —— 于是每台服务器上的运行 jar
|
||||
里都带着一份生产数据快照。排除只影响 jar,文件本身仍在仓库原位。 -->
|
||||
<excludes>
|
||||
<exclude>db/aiimage.sql</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
|
||||
+12
-5
@@ -121,6 +121,9 @@ public class AppearancePatentTaskService {
|
||||
private static final String CONTENT_TYPE_ZIP = "application/zip";
|
||||
private static final int CHUNK_PAYLOAD_MERGE_RETRY_LIMIT = 3;
|
||||
private static final int RESULT_ROWS_READ_RETRY_LIMIT = 3;
|
||||
|
||||
/** 结果行读取时 chunk 的 keyset 页大小,与 similarasin 侧 loadChunksKeyset 的 500 同口径。 */
|
||||
private static final int RESULT_ROWS_CHUNK_PAGE_SIZE = 500;
|
||||
private static final long RESULT_ROWS_READ_RETRY_DELAY_MS = 500L;
|
||||
private static final Duration TASK_LOCK_TTL = Duration.ofMinutes(5);
|
||||
private static final long TASK_LOCK_WAIT_MILLIS = 10000L;
|
||||
@@ -2538,7 +2541,7 @@ public class AppearancePatentTaskService {
|
||||
.eq(TaskChunkEntity::getTaskId, taskId)
|
||||
.eq(TaskChunkEntity::getModuleType, MODULE_TYPE)
|
||||
.orderByAsc(TaskChunkEntity::getId)
|
||||
.last("LIMIT 1");
|
||||
.last("LIMIT " + RESULT_ROWS_CHUNK_PAGE_SIZE);
|
||||
if (lastChunkId != null) {
|
||||
query.gt(TaskChunkEntity::getId, lastChunkId);
|
||||
}
|
||||
@@ -2546,10 +2549,14 @@ public class AppearancePatentTaskService {
|
||||
if (page == null || page.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
TaskChunkEntity chunk = page.getFirst();
|
||||
appendRequestedResultRows(result, readChunkRows(chunk).values(), requestedKeys);
|
||||
lastChunkId = chunk.getId();
|
||||
if (lastChunkId == null) {
|
||||
// 整页取 chunk 后逐片解载荷:原先每轮 LIMIT 1 只取一行,一个结果文件要把该任务的
|
||||
// chunk 表逐行翻完,每行一次 SQL + 一次对象读 + 一次整片 JSON 反序列化
|
||||
for (TaskChunkEntity chunk : page) {
|
||||
appendRequestedResultRows(result, readChunkRows(chunk).values(), requestedKeys);
|
||||
}
|
||||
TaskChunkEntity tail = page.getLast();
|
||||
lastChunkId = tail == null ? null : tail.getId();
|
||||
if (lastChunkId == null || page.size() < RESULT_ROWS_CHUNK_PAGE_SIZE) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
+27
-15
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.config.ImageVideoProperties;
|
||||
import com.nanri.aiimage.common.service.DistributedJobLockService;
|
||||
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
|
||||
import com.nanri.aiimage.modules.imagevideo.mapper.ImageVideoAsyncTaskMapper;
|
||||
import com.nanri.aiimage.modules.imagevideo.model.entity.ImageVideoAsyncTaskEntity;
|
||||
@@ -46,6 +47,7 @@ public class ImageVideoArchiveService {
|
||||
private final OssStorageService ossStorageService;
|
||||
private final ImageVideoProperties properties;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final com.nanri.aiimage.common.service.DistributedJobLockService distributedJobLockService;
|
||||
|
||||
void captureSubmitResponse(ImageVideoAsyncTaskEntity task, Object response) {
|
||||
if (!isWorkflowTask(task) || response == null) {
|
||||
@@ -107,22 +109,32 @@ public class ImageVideoArchiveService {
|
||||
|
||||
@Scheduled(fixedDelayString = "${aiimage.image-video.retention-cleanup-delay-ms:600000}")
|
||||
public void cleanupExpiredWorkflowTasks() {
|
||||
LocalDateTime cutoff = LocalDateTime.now().minusDays(RETENTION_DAYS);
|
||||
List<ImageVideoAsyncTaskEntity> expiredTasks = taskMapper.selectList(
|
||||
new LambdaQueryWrapper<ImageVideoAsyncTaskEntity>()
|
||||
.eq(ImageVideoAsyncTaskEntity::getTaskType, TASK_TYPE)
|
||||
.lt(ImageVideoAsyncTaskEntity::getSubmittedAt, cutoff)
|
||||
.orderByAsc(ImageVideoAsyncTaskEntity::getId)
|
||||
.last("LIMIT " + RETENTION_CLEANUP_BATCH_SIZE));
|
||||
int deleted = 0;
|
||||
for (ImageVideoAsyncTaskEntity task : expiredTasks) {
|
||||
if (!deleteArchivedObjects(task)) {
|
||||
continue;
|
||||
}
|
||||
deleted += taskMapper.deleteById(task.getId());
|
||||
// 归档认领有 CAS 兜底,但过期清理没有:两个节点各自 selectList 拿到同一批 200 行后
|
||||
// 会重复删同一批 OSS 对象、重复 deleteById,且任一侧中途失败都会给对方留下
|
||||
// 「对象已删、行还在」的半截状态。锁 TTL 给到 20 分钟,覆盖最慢的一批远端删除。
|
||||
DistributedJobLockService.LockHandle jobLock = distributedJobLockService.tryLock(
|
||||
"image-video:retention-cleanup", java.time.Duration.ofMinutes(20));
|
||||
if (jobLock == null) {
|
||||
return;
|
||||
}
|
||||
if (deleted > 0) {
|
||||
log.info("[image-video] expired workflow task cleanup deleted={} cutoff={}", deleted, cutoff);
|
||||
try (jobLock) {
|
||||
LocalDateTime cutoff = LocalDateTime.now().minusDays(RETENTION_DAYS);
|
||||
List<ImageVideoAsyncTaskEntity> expiredTasks = taskMapper.selectList(
|
||||
new LambdaQueryWrapper<ImageVideoAsyncTaskEntity>()
|
||||
.eq(ImageVideoAsyncTaskEntity::getTaskType, TASK_TYPE)
|
||||
.lt(ImageVideoAsyncTaskEntity::getSubmittedAt, cutoff)
|
||||
.orderByAsc(ImageVideoAsyncTaskEntity::getId)
|
||||
.last("LIMIT " + RETENTION_CLEANUP_BATCH_SIZE));
|
||||
int deleted = 0;
|
||||
for (ImageVideoAsyncTaskEntity task : expiredTasks) {
|
||||
if (!deleteArchivedObjects(task)) {
|
||||
continue;
|
||||
}
|
||||
deleted += taskMapper.deleteById(task.getId());
|
||||
}
|
||||
if (deleted > 0) {
|
||||
log.info("[image-video] expired workflow task cleanup deleted={} cutoff={}", deleted, cutoff);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+17
-7
@@ -238,13 +238,23 @@ public class ImageVideoAsyncTaskService {
|
||||
|
||||
@Scheduled(fixedDelayString = "${aiimage.image-video.failed-task-cleanup-delay-ms:60000}")
|
||||
public void cleanupFailedTasks() {
|
||||
LocalDateTime cutoff = LocalDateTime.now().minusMinutes(FAILED_TASK_RETENTION_MINUTES);
|
||||
int deleted = taskMapper.delete(new LambdaQueryWrapper<ImageVideoAsyncTaskEntity>()
|
||||
.eq(ImageVideoAsyncTaskEntity::getStatus, TaskStatus.FAILED.name())
|
||||
.lt(ImageVideoAsyncTaskEntity::getUpdatedAt, cutoff)
|
||||
.last("LIMIT " + EXPIRED_DELETE_BATCH_SIZE));
|
||||
if (deleted > 0) {
|
||||
log.info("[image-video] removed expired failed tasks count={}", deleted);
|
||||
// 本方法是全类唯一直接对共享表发 DELETE ... LIMIT 的定时任务:两节点各跑一份时会
|
||||
// 命中同一批 (status, updated_at) 区间并互相等行锁。扫描/轮询/接管三条已用任务锁,
|
||||
// 这里补齐;锁超时后另一节点接管,删除本身幂等,不会漏清也不会重复扣。
|
||||
DistributedJobLockService.LockHandle jobLock =
|
||||
distributedJobLockService.tryLock("image-video:failed-task-cleanup", java.time.Duration.ofSeconds(120));
|
||||
if (jobLock == null) {
|
||||
return;
|
||||
}
|
||||
try (jobLock) {
|
||||
LocalDateTime cutoff = LocalDateTime.now().minusMinutes(FAILED_TASK_RETENTION_MINUTES);
|
||||
int deleted = taskMapper.delete(new LambdaQueryWrapper<ImageVideoAsyncTaskEntity>()
|
||||
.eq(ImageVideoAsyncTaskEntity::getStatus, TaskStatus.FAILED.name())
|
||||
.lt(ImageVideoAsyncTaskEntity::getUpdatedAt, cutoff)
|
||||
.last("LIMIT " + EXPIRED_DELETE_BATCH_SIZE));
|
||||
if (deleted > 0) {
|
||||
log.info("[image-video] removed expired failed tasks count={}", deleted);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+4
-2
@@ -15,12 +15,14 @@ public interface PriceTrackLoopRunMapper extends BaseMapper<PriceTrackLoopRunEnt
|
||||
* 分批删除已结束且超过保留期的循环批次行。
|
||||
*
|
||||
* <p>只删终态行:RUNNING 的批次删了会让后续 dispatch / childFinished 找不到记录。
|
||||
* 时间线用 COALESCE 兜底——终态行本应写 finished_at,历史行可能只有 updated_at。
|
||||
* 时间线用 V141 的 STORED 生成列 {@code cleanup_at}(= COALESCE(finished_at, updated_at,
|
||||
* created_at),口径与改造前完全一致):原来这个表达式直接写在 WHERE 里不可走索引,
|
||||
* 每条 DELETE 都要全表扫描并对扫过的行加锁,而 (status, cleanup_at) 索引已就位。
|
||||
*/
|
||||
@Delete("""
|
||||
DELETE FROM biz_price_track_loop_run
|
||||
WHERE status IN ('SUCCESS', 'FAILED', 'STOPPED')
|
||||
AND COALESCE(finished_at, updated_at, created_at) < #{cutoff}
|
||||
AND cleanup_at < #{cutoff}
|
||||
LIMIT #{batchSize}
|
||||
""")
|
||||
int deleteFinishedBefore(@Param("cutoff") LocalDateTime cutoff, @Param("batchSize") int batchSize);
|
||||
|
||||
+25
-8
@@ -169,6 +169,14 @@ public class PriceTrackLoopRunService {
|
||||
return toVo(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把刚创建的子任务认领为循环任务的当前子任务。
|
||||
*
|
||||
* <p>认领失败一律抛异常而不是静默返回:调用方 {@code PriceTrackTaskService.createTask}
|
||||
* 与本方法同处一个事务(默认传播 REQUIRED),抛出会让刚 insert 的子任务连同扣次一起回滚。
|
||||
* 原先各处判不到就 {@code return},子任务照样建成并真实执行——同一个店铺槽位跑出两个
|
||||
* RUNNING 子任务(重复采集、重复扣用户次数),且第一个失去跟踪。
|
||||
*/
|
||||
@Transactional
|
||||
public void bindChildTask(Long loopRunId, Long childTaskId, Integer roundIndex, Integer shopIndex) {
|
||||
if (loopRunId == null || childTaskId == null) {
|
||||
@@ -176,26 +184,35 @@ public class PriceTrackLoopRunService {
|
||||
}
|
||||
PriceTrackLoopRunEntity entity = loopRunMapper.selectById(loopRunId);
|
||||
if (entity == null || isTerminal(entity.getStatus())) {
|
||||
return;
|
||||
throw new BusinessException("循环任务已结束或不存在,子任务未绑定");
|
||||
}
|
||||
if (entity.getCurrentRound() == null || entity.getCurrentShopIndex() == null) {
|
||||
return;
|
||||
throw new BusinessException("循环任务轮次状态缺失,子任务未绑定");
|
||||
}
|
||||
if (!entity.getCurrentRound().equals(roundIndex) || !entity.getCurrentShopIndex().equals(shopIndex)) {
|
||||
return;
|
||||
throw new BusinessException("循环任务轮次或店铺指针已推进,本次派发已作废");
|
||||
}
|
||||
if (entity.getActiveTaskId() != null && !isChildTaskTerminal(entity.getActiveTaskId())) {
|
||||
return;
|
||||
throw new BusinessException("当前仍有执行中的子任务,本次派发已作废");
|
||||
}
|
||||
entity.setActiveTaskId(childTaskId);
|
||||
entity.setUpdatedAt(LocalDateTime.now());
|
||||
// 字段级更新 + status CAS:整行 updateById 会覆盖并发写入的 stop_requested,
|
||||
// 导致「停止循环」请求被静默丢弃
|
||||
loopRunMapper.update(null, new LambdaUpdateWrapper<PriceTrackLoopRunEntity>()
|
||||
// 认领槽位必须连带 CAS 掉读取时的 active_task_id:只比 status 时两个并发派发都能通过
|
||||
// (status 没变),后写的那个把先绑的子任务顶掉,两个 RUNNING 子任务同时真实执行。
|
||||
Long expectedActiveTaskId = entity.getActiveTaskId();
|
||||
LambdaUpdateWrapper<PriceTrackLoopRunEntity> claim = new LambdaUpdateWrapper<PriceTrackLoopRunEntity>()
|
||||
.eq(PriceTrackLoopRunEntity::getId, entity.getId())
|
||||
.eq(PriceTrackLoopRunEntity::getStatus, entity.getStatus())
|
||||
.set(PriceTrackLoopRunEntity::getActiveTaskId, childTaskId)
|
||||
.set(PriceTrackLoopRunEntity::getUpdatedAt, LocalDateTime.now()));
|
||||
.set(PriceTrackLoopRunEntity::getUpdatedAt, LocalDateTime.now());
|
||||
if (expectedActiveTaskId == null) {
|
||||
claim.isNull(PriceTrackLoopRunEntity::getActiveTaskId);
|
||||
} else {
|
||||
claim.eq(PriceTrackLoopRunEntity::getActiveTaskId, expectedActiveTaskId);
|
||||
}
|
||||
if (loopRunMapper.update(null, claim) == 0) {
|
||||
throw new BusinessException("循环任务槽位已被并发派发认领,本次派发已作废");
|
||||
}
|
||||
log.info("[price-track-loop] child bound loopRunId={} childTaskId={} roundIndex={} shopIndex={}",
|
||||
loopRunId, childTaskId, roundIndex, shopIndex);
|
||||
}
|
||||
|
||||
+99
-82
@@ -460,7 +460,6 @@ public class PriceTrackTaskService {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public PriceTrackCreateTaskVo createTask(PriceTrackCreateTaskRequest request, String deviceId) {
|
||||
if (request.isStatusMode() == request.isAsinMode()) throw new BusinessException("跟价模式必须二选一");
|
||||
if (request.isAsinMode() && (request.getAsinFiles() == null || request.getAsinFiles().isEmpty())) {
|
||||
@@ -490,91 +489,98 @@ public class PriceTrackTaskService {
|
||||
log.info("[price-track] createTask skipAsins countries={} asinMode={}",
|
||||
skipAsinsByCountry.keySet(), request.isAsinMode());
|
||||
|
||||
// 创建任务记录
|
||||
FileTaskEntity task = new FileTaskEntity();
|
||||
task.setTaskNo(MODULE_TYPE + "-" + IdUtil.getSnowflakeNextIdStr());
|
||||
task.setModuleType(MODULE_TYPE);
|
||||
task.setTaskMode("PYTHON_QUEUE");
|
||||
task.setStatus("RUNNING");
|
||||
task.setSourceFileCount(uniqueItems.size());
|
||||
task.setSuccessFileCount(0);
|
||||
task.setFailedFileCount(0);
|
||||
task.setCreatedBy("user:" + request.getUserId());
|
||||
task.setUserId(request.getUserId());
|
||||
task.setDeviceId(AdminAuthSupport.normalizeDeviceId(deviceId));
|
||||
task.setCreatedAt(LocalDateTime.now());
|
||||
task.setUpdatedAt(LocalDateTime.now());
|
||||
fileTaskMapper.insert(task);
|
||||
priceTrackTaskCacheService.touchTaskHeartbeat(task.getId());
|
||||
if (request.isAsinMode()) {
|
||||
saveTaskAsinRowsPayload(task.getId(), asinRowsForPayload);
|
||||
}
|
||||
if (request.getLoopRunId() != null) {
|
||||
priceTrackLoopRunService.bindChildTask(request.getLoopRunId(), task.getId(), request.getRoundIndex(), request.getShopIndex());
|
||||
}
|
||||
|
||||
List<PriceTrackResultItemVo> snapshot = new ArrayList<>();
|
||||
// 逐店铺 fileResultMapper.insert 看似该改批量(500 店铺 = 500 次往返),但**不能改**:
|
||||
// 下面的 toSnapshotVo 依赖 MyBatis-Plus insert 回填的自增主键(fr.getId() → vo.resultId),
|
||||
// 而本仓的手写 INSERT ... VALUES (...),(...) 批量写法不回填主键,改了会让快照 resultId 为 null。
|
||||
// 要批量化必须同时解决「批量插入 + 按序回填 id」(MySQL 需 useGeneratedKeys + 顺序保证)。
|
||||
for (PriceTrackMatchShopsVo.PriceTrackShopQueueItem item : uniqueItems) {
|
||||
String norm = ziniaoShopSwitchService.normalizeShopName(item.getShopName());
|
||||
if (norm.isBlank()) throw new BusinessException("店铺名称无效");
|
||||
FileResultEntity fr = new FileResultEntity();
|
||||
fr.setTaskId(task.getId());
|
||||
fr.setModuleType(MODULE_TYPE);
|
||||
fr.setSourceFilename(norm);
|
||||
fr.setSourceFileUrl(Objects.toString(item.getShopId(), null));
|
||||
fr.setUserId(request.getUserId());
|
||||
fr.setSuccess(0);
|
||||
fr.setCreatedAt(LocalDateTime.now());
|
||||
fileResultMapper.insert(fr);
|
||||
PriceTrackResultItemVo snapshotItem = toSnapshotVo(fr, item, task.getStatus());
|
||||
snapshotItem.setLoopRunId(request.getLoopRunId());
|
||||
snapshotItem.setRoundIndex(request.getRoundIndex());
|
||||
snapshot.add(snapshotItem);
|
||||
}
|
||||
|
||||
// 保存请求上下文快照,供 Python 消费和前端轮询展示使用
|
||||
try {
|
||||
Map<String, Object> ctx = new LinkedHashMap<>();
|
||||
ctx.put("userId", request.getUserId());
|
||||
ctx.put("statusMode", request.isStatusMode());
|
||||
ctx.put("asinMode", request.isAsinMode());
|
||||
ctx.put("countryCodes", request.getCountryCodes());
|
||||
// 不再保存全量 ASIN 数据到 requestJson
|
||||
// ctx.put("skipAsinsByCountry", skipAsinsByCountry);
|
||||
// ctx.put("skipAsinDetailsByCountry", skipAsinDetailsByCountry);
|
||||
// ctx.put("asinRowsByCountry", asinRowsByCountry);
|
||||
// ctx.put("minimumPriceByCountryAndAsin", minimumPriceByCountryAndAsin);
|
||||
// 文件模式:保存文件路径,供后续分页读取
|
||||
// Excel/CSV 解析(POI 全表读,可达秒级)刻意留在事务之外:事务里只做 DB 写与载荷落库,
|
||||
// 避免创建跟价任务时长时间占住连接池连接(本仓历史上 biz_task_file_job 一类锁等
|
||||
// 就是被「事务内含外部 IO」放大的)。载荷 PUT 仍留在事务内:它失败必须整体回滚,
|
||||
// 不能让「任务已建、ASIN 载荷为空」的半成品进入 Python 队列。
|
||||
final Map<String, List<Map<String, String>>> parsedAsinRows = asinRowsForPayload;
|
||||
return inRequiredTransaction(() -> {
|
||||
// 创建任务记录
|
||||
FileTaskEntity task = new FileTaskEntity();
|
||||
task.setTaskNo(MODULE_TYPE + "-" + IdUtil.getSnowflakeNextIdStr());
|
||||
task.setModuleType(MODULE_TYPE);
|
||||
task.setTaskMode("PYTHON_QUEUE");
|
||||
task.setStatus("RUNNING");
|
||||
task.setSourceFileCount(uniqueItems.size());
|
||||
task.setSuccessFileCount(0);
|
||||
task.setFailedFileCount(0);
|
||||
task.setCreatedBy("user:" + request.getUserId());
|
||||
task.setUserId(request.getUserId());
|
||||
task.setDeviceId(AdminAuthSupport.normalizeDeviceId(deviceId));
|
||||
task.setCreatedAt(LocalDateTime.now());
|
||||
task.setUpdatedAt(LocalDateTime.now());
|
||||
fileTaskMapper.insert(task);
|
||||
priceTrackTaskCacheService.touchTaskHeartbeat(task.getId());
|
||||
if (request.isAsinMode()) {
|
||||
ctx.put("asinFiles", request.getAsinFiles());
|
||||
saveTaskAsinRowsPayload(task.getId(), parsedAsinRows);
|
||||
}
|
||||
if (request.getLoopRunId() != null) {
|
||||
priceTrackLoopRunService.bindChildTask(request.getLoopRunId(), task.getId(), request.getRoundIndex(), request.getShopIndex());
|
||||
}
|
||||
ctx.put("items", uniqueItems);
|
||||
ctx.put("loopRunId", request.getLoopRunId());
|
||||
ctx.put("roundIndex", request.getRoundIndex());
|
||||
ctx.put("shopIndex", request.getShopIndex());
|
||||
task.setRequestJson(objectMapper.writeValueAsString(ctx));
|
||||
task.setResultJson(objectMapper.writeValueAsString(snapshot));
|
||||
} catch (Exception ex) {
|
||||
throw new BusinessException("序列化任务数据失败");
|
||||
}
|
||||
fileTaskMapper.updateById(task);
|
||||
priceTrackTaskCacheService.saveTaskCache(task);
|
||||
|
||||
// TODO: Python 侧轮询 RUNNING 任务,pickup 后开始处理
|
||||
// Java 侧通过 submitResult 接口接收 Python 回传结果
|
||||
List<PriceTrackResultItemVo> snapshot = new ArrayList<>();
|
||||
// 逐店铺 fileResultMapper.insert 看似该改批量(500 店铺 = 500 次往返),但**不能改**:
|
||||
// 下面的 toSnapshotVo 依赖 MyBatis-Plus insert 回填的自增主键(fr.getId() → vo.resultId),
|
||||
// 而本仓的手写 INSERT ... VALUES (...),(...) 批量写法不回填主键,改了会让快照 resultId 为 null。
|
||||
// 要批量化必须同时解决「批量插入 + 按序回填 id」(MySQL 需 useGeneratedKeys + 顺序保证)。
|
||||
for (PriceTrackMatchShopsVo.PriceTrackShopQueueItem item : uniqueItems) {
|
||||
String norm = ziniaoShopSwitchService.normalizeShopName(item.getShopName());
|
||||
if (norm.isBlank()) throw new BusinessException("店铺名称无效");
|
||||
FileResultEntity fr = new FileResultEntity();
|
||||
fr.setTaskId(task.getId());
|
||||
fr.setModuleType(MODULE_TYPE);
|
||||
fr.setSourceFilename(norm);
|
||||
fr.setSourceFileUrl(Objects.toString(item.getShopId(), null));
|
||||
fr.setUserId(request.getUserId());
|
||||
fr.setSuccess(0);
|
||||
fr.setCreatedAt(LocalDateTime.now());
|
||||
fileResultMapper.insert(fr);
|
||||
PriceTrackResultItemVo snapshotItem = toSnapshotVo(fr, item, task.getStatus());
|
||||
snapshotItem.setLoopRunId(request.getLoopRunId());
|
||||
snapshotItem.setRoundIndex(request.getRoundIndex());
|
||||
snapshot.add(snapshotItem);
|
||||
}
|
||||
|
||||
PriceTrackCreateTaskVo vo = new PriceTrackCreateTaskVo();
|
||||
vo.setTaskId(task.getId());
|
||||
vo.setItems(snapshot);
|
||||
vo.setSkipAsinsByCountry(skipAsinsByCountry);
|
||||
vo.setSkipAsinDetailsByCountry(skipAsinDetailsByCountry);
|
||||
vo.setAsinRowsByCountry(asinRowsByCountry);
|
||||
vo.setMinimumPriceByCountryAndAsin(minimumPriceByCountryAndAsin);
|
||||
return vo;
|
||||
// 保存请求上下文快照,供 Python 消费和前端轮询展示使用
|
||||
try {
|
||||
Map<String, Object> ctx = new LinkedHashMap<>();
|
||||
ctx.put("userId", request.getUserId());
|
||||
ctx.put("statusMode", request.isStatusMode());
|
||||
ctx.put("asinMode", request.isAsinMode());
|
||||
ctx.put("countryCodes", request.getCountryCodes());
|
||||
// 不再保存全量 ASIN 数据到 requestJson
|
||||
// ctx.put("skipAsinsByCountry", skipAsinsByCountry);
|
||||
// ctx.put("skipAsinDetailsByCountry", skipAsinDetailsByCountry);
|
||||
// ctx.put("asinRowsByCountry", asinRowsByCountry);
|
||||
// ctx.put("minimumPriceByCountryAndAsin", minimumPriceByCountryAndAsin);
|
||||
// 文件模式:保存文件路径,供后续分页读取
|
||||
if (request.isAsinMode()) {
|
||||
ctx.put("asinFiles", request.getAsinFiles());
|
||||
}
|
||||
ctx.put("items", uniqueItems);
|
||||
ctx.put("loopRunId", request.getLoopRunId());
|
||||
ctx.put("roundIndex", request.getRoundIndex());
|
||||
ctx.put("shopIndex", request.getShopIndex());
|
||||
task.setRequestJson(objectMapper.writeValueAsString(ctx));
|
||||
task.setResultJson(objectMapper.writeValueAsString(snapshot));
|
||||
} catch (Exception ex) {
|
||||
throw new BusinessException("序列化任务数据失败");
|
||||
}
|
||||
fileTaskMapper.updateById(task);
|
||||
priceTrackTaskCacheService.saveTaskCache(task);
|
||||
|
||||
// TODO: Python 侧轮询 RUNNING 任务,pickup 后开始处理
|
||||
// Java 侧通过 submitResult 接口接收 Python 回传结果
|
||||
|
||||
PriceTrackCreateTaskVo vo = new PriceTrackCreateTaskVo();
|
||||
vo.setTaskId(task.getId());
|
||||
vo.setItems(snapshot);
|
||||
vo.setSkipAsinsByCountry(skipAsinsByCountry);
|
||||
vo.setSkipAsinDetailsByCountry(skipAsinDetailsByCountry);
|
||||
vo.setAsinRowsByCountry(asinRowsByCountry);
|
||||
vo.setMinimumPriceByCountryAndAsin(minimumPriceByCountryAndAsin);
|
||||
return vo;
|
||||
});
|
||||
}
|
||||
|
||||
public void submitResult(Long taskId, PriceTrackSubmitResultRequest request) {
|
||||
@@ -2263,6 +2269,17 @@ public class PriceTrackTaskService {
|
||||
return template.execute(status -> action.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认传播(REQUIRED)事务段:供「外部 IO 已移出、只剩 DB 写」的方法体使用。
|
||||
* 与 {@link #inNewTransaction} 的差别是不挂起外层事务;单测直接 new 本类时退化为直调。
|
||||
*/
|
||||
private <T> T inRequiredTransaction(java.util.function.Supplier<T> action) {
|
||||
if (transactionManager == null) {
|
||||
return action.get();
|
||||
}
|
||||
return new TransactionTemplate(transactionManager).execute(status -> action.get());
|
||||
}
|
||||
|
||||
private TaskDistributedLockService.LockHandle acquireTaskLock(Long taskId) {
|
||||
return TaskFinalizeSupport.acquireTaskLock(taskDistributedLockService, MODULE_TYPE, taskId);
|
||||
}
|
||||
|
||||
+27
-1
@@ -17,6 +17,8 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.support.TransactionSynchronization;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
@@ -117,8 +119,12 @@ public class ShopKeyService {
|
||||
/**
|
||||
* 后台手动检测白名单:绕过缓存真实打一次紫鸟接口,并重置自动重试次数。
|
||||
* 白名单放行需紫鸟公司侧授权,自动重试达阈值后会停试,此按钮供授权后重新验证。
|
||||
*
|
||||
* <p>不加 {@code @Transactional}:本方法只有一次单语句 UPDATE,没有需要原子提交的多元写,
|
||||
* 而 {@code probeApiKeyUpstream} 是真实出网的紫鸟调用(上游代理挂起时按超时计可达数十秒)。
|
||||
* 挂在事务上等于把连接池的连接按上游 RTT 扣住,多次点击即可耗尽池——这正是本仓
|
||||
* 「事务内含外部 IO」要避免的形态。
|
||||
*/
|
||||
@Transactional
|
||||
public ShopKeyItemVo checkWhitelist(Long id) {
|
||||
ShopKeyEntity entity = getById(id);
|
||||
ziniaoApiKeyProvider.invalidateProxyUrlCache();
|
||||
@@ -214,7 +220,27 @@ public class ShopKeyService {
|
||||
return vo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 密钥变更后重刷紫鸟店铺索引。
|
||||
*
|
||||
* invalidateIndex() 是「删 1 张持久表全部索引行 + 清 7 类瞬时缓存」的重操作,
|
||||
* 放在 create/update/delete 的事务里等于把一次单行写放大成几十条 DELETE 的持锁时间,
|
||||
* 而缓存本来就不参与回滚。因此:有事务时挂到提交之后执行,没有事务时照旧立即执行。
|
||||
*/
|
||||
private void triggerShopIndexRefresh() {
|
||||
if (TransactionSynchronizationManager.isSynchronizationActive()) {
|
||||
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
|
||||
@Override
|
||||
public void afterCommit() {
|
||||
invalidateIndexSafely();
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
invalidateIndexSafely();
|
||||
}
|
||||
|
||||
private void invalidateIndexSafely() {
|
||||
try {
|
||||
ziniaoShopIndexService.invalidateIndex();
|
||||
} catch (Exception ex) {
|
||||
|
||||
+5
-7
@@ -58,6 +58,9 @@ public class SimilarAsinImagePrefetchService {
|
||||
/** 批量命中检查/批量 touch 单次 IN 上限,避免超长 SQL 参数列表。 */
|
||||
private static final int CACHE_LOOKUP_BATCH_SIZE = 500;
|
||||
|
||||
/** url_hash 十六进制编码用:逐字节 String.format 在按 URL 调用的路径上会放大成几万次格式化。 */
|
||||
private static final java.util.HexFormat HEX_FORMAT = java.util.HexFormat.of();
|
||||
|
||||
/** 单个 task 最多缓存的待预热 URL 数,避免持续回传时内存无界增长。 */
|
||||
private static final int MAX_PENDING_URLS_PER_TASK = 5_000;
|
||||
/** 单机最多同时维护的预热 task 数。 */
|
||||
@@ -316,7 +319,7 @@ public class SimilarAsinImagePrefetchService {
|
||||
* 未命中 url 不产生任何 touch。输入按传入顺序返回,未命中为 null。
|
||||
* 失败/开关关闭返回空列表,由调用方走回退路径。
|
||||
*/
|
||||
List<byte[]> lookupBatch(List<String> urls) {
|
||||
public List<byte[]> lookupBatch(List<String> urls) {
|
||||
List<byte[]> result = new ArrayList<>();
|
||||
if (!properties.isImageDbCacheEnabled()) {
|
||||
return result;
|
||||
@@ -415,12 +418,7 @@ public class SimilarAsinImagePrefetchService {
|
||||
private static String sha256Hex(String value) {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
||||
byte[] digest = md.digest(value.getBytes(StandardCharsets.UTF_8));
|
||||
StringBuilder sb = new StringBuilder(digest.length * 2);
|
||||
for (byte b : digest) {
|
||||
sb.append(String.format("%02x", b & 0xFF));
|
||||
}
|
||||
return sb.toString();
|
||||
return HEX_FORMAT.formatHex(md.digest(value.getBytes(StandardCharsets.UTF_8)));
|
||||
} catch (NoSuchAlgorithmException ex) {
|
||||
// SHA-256 在 JDK 中是必备算法,正常环境下不会落到这里。
|
||||
return null;
|
||||
|
||||
+12
-1
@@ -132,6 +132,8 @@ public class SimilarAsinTaskService implements SimilarAsinPipelineHost {
|
||||
private static final String CONTENT_TYPE_XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
||||
private static final String CONTENT_TYPE_ZIP = "application/zip";
|
||||
private static final int MAX_LIGHT_TASK_IDS = 200;
|
||||
/** progressBatch 的 IN 子句上限:看板单次轮询的在册任务量级远小于它,取值留了 5 倍余量。 */
|
||||
private static final int MAX_BATCH_TASK_IDS = 1000;
|
||||
/** Task 19:Python 回传逐行日志采样频率(每 N 行记一行)。 */
|
||||
private static final long PYTHON_INBOUND_LOG_EVERY_N = 20L;
|
||||
private static final Duration TASK_LOCK_TTL = Duration.ofMinutes(5);
|
||||
@@ -657,10 +659,19 @@ public class SimilarAsinTaskService implements SimilarAsinPipelineHost {
|
||||
|
||||
public SimilarAsinTaskBatchVo progressBatch(List<Long> taskIds) {
|
||||
SimilarAsinTaskBatchVo vo = new SimilarAsinTaskBatchVo();
|
||||
List<Long> normalizedIds = taskIds == null ? List.of() : taskIds.stream()
|
||||
List<Long> requestedIds = taskIds == null ? List.of() : taskIds.stream()
|
||||
.filter(id -> id != null && id > 0)
|
||||
.distinct()
|
||||
.toList();
|
||||
// 上限只为挡住超长 IN 子句。被截的尾部不进 missingTaskIds(与 progressLight 同口径:
|
||||
// 看板会把 missing 当成「这张单不存在」,而它们只是本轮没查),改为记 WARN 留现场。
|
||||
if (requestedIds.size() > MAX_BATCH_TASK_IDS) {
|
||||
log.warn("[similar-asin] progressBatch 收到 {} 个 id,超过上限 {},本轮只刷新前 {} 个",
|
||||
requestedIds.size(), MAX_BATCH_TASK_IDS, MAX_BATCH_TASK_IDS);
|
||||
}
|
||||
List<Long> normalizedIds = requestedIds.size() > MAX_BATCH_TASK_IDS
|
||||
? requestedIds.subList(0, MAX_BATCH_TASK_IDS)
|
||||
: requestedIds;
|
||||
if (normalizedIds.isEmpty()) {
|
||||
return vo;
|
||||
}
|
||||
|
||||
+72
-30
@@ -91,6 +91,9 @@ public class SimilarAsinResultWorkbookAssembler {
|
||||
private static final int IMG_COL_PUZZLE1 = 13;
|
||||
private static final int IMG_COL_PUZZLE2 = 14;
|
||||
private static final int RESULT_ROWS_READ_RETRY_LIMIT = 3;
|
||||
|
||||
/** 结果行读取时 chunk / orphan scope_state 的 keyset 页大小,与 loadChunksKeyset 的 500 同口径。 */
|
||||
private static final int RESULT_ROWS_PAGE_SIZE = 500;
|
||||
private static final long RESULT_ROWS_READ_RETRY_DELAY_MS = 500L;
|
||||
|
||||
private final SimilarAsinProperties properties;
|
||||
@@ -562,6 +565,11 @@ public class SimilarAsinResultWorkbookAssembler {
|
||||
/**
|
||||
* Select and decode one chunk payload at a time. Only rows needed by the
|
||||
* current source workbook are retained in the returned map.
|
||||
*
|
||||
* <p>按 keyset 整页取 chunk(与 {@link SimilarAsinChunkMergeSupport#loadChunksKeyset} 同口径):
|
||||
* 原先每轮 {@code LIMIT 1} 只取一行,一个源文件要把该任务的 chunk 表逐行翻完,
|
||||
* 每行一次 SQL + 一次对象读 + 一次整片 JSON 反序列化;请求行全部命中后立即收尾,
|
||||
* 不再把剩余 chunk 也解一遍。
|
||||
*/
|
||||
private Map<String, SimilarAsinResultRowDto> loadPersistedResultRowsForRows(
|
||||
Long taskId,
|
||||
@@ -578,7 +586,7 @@ public class SimilarAsinResultWorkbookAssembler {
|
||||
.eq(TaskChunkEntity::getTaskId, taskId)
|
||||
.eq(TaskChunkEntity::getModuleType, MODULE_TYPE)
|
||||
.orderByAsc(TaskChunkEntity::getId)
|
||||
.last("LIMIT 1");
|
||||
.last("LIMIT " + RESULT_ROWS_PAGE_SIZE);
|
||||
if (lastChunkId != null) {
|
||||
query.gt(TaskChunkEntity::getId, lastChunkId);
|
||||
}
|
||||
@@ -586,10 +594,16 @@ public class SimilarAsinResultWorkbookAssembler {
|
||||
if (page == null || page.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
TaskChunkEntity chunk = page.getFirst();
|
||||
appendRequestedResultRows(result, chunkPayloadSupport.readChunkRows(chunk).values(), requestedKeys, false);
|
||||
lastChunkId = chunk.getId();
|
||||
if (lastChunkId == null) {
|
||||
for (TaskChunkEntity chunk : page) {
|
||||
appendRequestedResultRows(result, chunkPayloadSupport.readChunkRows(chunk).values(),
|
||||
requestedKeys, false);
|
||||
}
|
||||
TaskChunkEntity tail = page.getLast();
|
||||
lastChunkId = tail == null ? null : tail.getId();
|
||||
if (lastChunkId == null || page.size() < RESULT_ROWS_PAGE_SIZE) {
|
||||
break;
|
||||
}
|
||||
if (allRequestedRowsResolved(requestedRows, result)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -597,6 +611,21 @@ public class SimilarAsinResultWorkbookAssembler {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求行是否都已取到结果。判据必须与 {@link SimilarAsinChunkMergeSupport#findResultRow}
|
||||
* 的匹配口径一致(主键或 legacy 键任一命中即算已取到)——直接用 result.size() 与
|
||||
* requestedKeys.size() 比会永远不成立,因为 requestedKeys 每行都塞了主键 + legacy 两个键。
|
||||
*/
|
||||
private boolean allRequestedRowsResolved(List<SimilarAsinParsedRowVo> requestedRows,
|
||||
Map<String, SimilarAsinResultRowDto> result) {
|
||||
for (SimilarAsinParsedRowVo row : requestedRows) {
|
||||
if (SimilarAsinChunkMergeSupport.findResultRow(row, result) == null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private Set<String> resultLookupKeys(List<SimilarAsinParsedRowVo> rows) {
|
||||
Set<String> keys = new LinkedHashSet<>();
|
||||
for (SimilarAsinParsedRowVo row : rows == null ? List.<SimilarAsinParsedRowVo>of() : rows) {
|
||||
@@ -651,7 +680,7 @@ public class SimilarAsinResultWorkbookAssembler {
|
||||
.eq(TaskScopeStateEntity::getModuleType, MODULE_TYPE)
|
||||
.likeRight(TaskScopeStateEntity::getScopeKey, SimilarAsinChunkPayloadSupport.ORPHAN_SCOPE_KEY_PREFIX)
|
||||
.orderByAsc(TaskScopeStateEntity::getId)
|
||||
.last("LIMIT 1");
|
||||
.last("LIMIT " + RESULT_ROWS_PAGE_SIZE);
|
||||
if (lastStateId != null) {
|
||||
query.gt(TaskScopeStateEntity::getId, lastStateId);
|
||||
}
|
||||
@@ -659,29 +688,33 @@ public class SimilarAsinResultWorkbookAssembler {
|
||||
if (page == null || page.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
TaskScopeStateEntity state = page.getFirst();
|
||||
lastStateId = state.getId();
|
||||
if (lastStateId == null) {
|
||||
return;
|
||||
}
|
||||
if (state.getParsedPayloadJson() == null || state.getParsedPayloadJson().isBlank()) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
String payloadJson = transientPayloadStorageService.resolvePayload(
|
||||
state.getParsedPayloadJson(), "read similar ASIN orphan rows failed");
|
||||
JsonNode array = objectMapper.readTree(payloadJson);
|
||||
if (!array.isArray()) {
|
||||
TaskScopeStateEntity tail = page.getLast();
|
||||
lastStateId = tail == null ? null : tail.getId();
|
||||
boolean lastPage = page.size() < RESULT_ROWS_PAGE_SIZE || lastStateId == null;
|
||||
for (TaskScopeStateEntity state : page) {
|
||||
if (state == null || state.getParsedPayloadJson() == null
|
||||
|| state.getParsedPayloadJson().isBlank()) {
|
||||
continue;
|
||||
}
|
||||
List<SimilarAsinResultRowDto> matchingRows = new ArrayList<>();
|
||||
for (JsonNode node : array) {
|
||||
matchingRows.add(objectMapper.treeToValue(node, SimilarAsinResultRowDto.class));
|
||||
try {
|
||||
String payloadJson = transientPayloadStorageService.resolvePayload(
|
||||
state.getParsedPayloadJson(), "read similar ASIN orphan rows failed");
|
||||
JsonNode array = objectMapper.readTree(payloadJson);
|
||||
if (!array.isArray()) {
|
||||
continue;
|
||||
}
|
||||
List<SimilarAsinResultRowDto> matchingRows = new ArrayList<>();
|
||||
for (JsonNode node : array) {
|
||||
matchingRows.add(objectMapper.treeToValue(node, SimilarAsinResultRowDto.class));
|
||||
}
|
||||
appendRequestedResultRows(sink, matchingRows, requestedKeys, true);
|
||||
} catch (Exception ex) {
|
||||
log.warn("[similar-asin] read requested orphan rows failed taskId={} stateId={} err={}",
|
||||
taskId, state.getId(), ex.getMessage());
|
||||
}
|
||||
appendRequestedResultRows(sink, matchingRows, requestedKeys, true);
|
||||
} catch (Exception ex) {
|
||||
log.warn("[similar-asin] read requested orphan rows failed taskId={} stateId={} err={}",
|
||||
taskId, state.getId(), ex.getMessage());
|
||||
}
|
||||
if (lastPage) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -788,14 +821,23 @@ public class SimilarAsinResultWorkbookAssembler {
|
||||
if (!prefetchUrls.isEmpty()) {
|
||||
// DB cache 命中的缩略图也立即落到任务临时目录,避免整批字节停留在堆中。
|
||||
long dbCacheStart = System.currentTimeMillis();
|
||||
List<String> remainingUrls = new ArrayList<>(prefetchUrls.size());
|
||||
int dbHit = 0;
|
||||
// 一次 IN 批量查(lookupBatch 内部只对命中项 touch last_used_at):
|
||||
// 原先逐 URL 调 lookup(),按注释里的量级(4 源 × 4000 URL)每次装配要发几千条单行 SQL。
|
||||
Set<String> pendingUrlSet = new LinkedHashSet<>();
|
||||
for (String url : prefetchUrls) {
|
||||
ensureResultAssemblyNotInterrupted();
|
||||
if (url == null || imageSpool.get(url) != null || taskImageCache.containsKey(url)) {
|
||||
continue;
|
||||
}
|
||||
byte[] cachedBytes = imagePrefetchService.lookup(url);
|
||||
pendingUrlSet.add(url);
|
||||
}
|
||||
List<String> pendingUrls = new ArrayList<>(pendingUrlSet);
|
||||
List<byte[]> cachedBatch = imagePrefetchService.lookupBatch(pendingUrls);
|
||||
List<String> remainingUrls = new ArrayList<>();
|
||||
int dbHit = 0;
|
||||
for (int i = 0; i < pendingUrls.size(); i++) {
|
||||
ensureResultAssemblyNotInterrupted();
|
||||
String url = pendingUrls.get(i);
|
||||
byte[] cachedBytes = i < cachedBatch.size() ? cachedBatch.get(i) : null;
|
||||
if (cachedBytes == null) {
|
||||
remainingUrls.add(url);
|
||||
continue;
|
||||
|
||||
+10
-11
@@ -323,33 +323,32 @@ public class TransientPayloadStorageService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 带 task_id 收敛的 chunk 引用计数;带 task 查不到时回退全局查询,
|
||||
* 保证即使指针格式与预期不符也不会漏判引用(漏判会导致对象被误删)。
|
||||
* 带 task_id 收敛的 chunk 引用计数。
|
||||
*
|
||||
* <p>只有在指针解析不出 taskId 时才退回全局查询。指针可解析时不再回落全局:
|
||||
* 对象 key 里带 taskId、引用不跨任务(见 {@link #isStillReferenced} 的口径说明),
|
||||
* 而物理删除又约定在 DB 行删除之后执行,所以带 task 查到 0 才是正常状态——
|
||||
* 原先「查到 0 就无条件再全局查一次」让每次删除都必然退化成
|
||||
* {@code payload_json}(JSON 列,无索引)上的全表扫描,而本方法是删除路径的公共链路且被逐行调用。
|
||||
*/
|
||||
private Long referencedChunkCount(Long taskId, List<String> values) {
|
||||
if (taskId != null) {
|
||||
Long scoped = taskChunkMapper.selectCount(new LambdaQueryWrapper<TaskChunkEntity>()
|
||||
return taskChunkMapper.selectCount(new LambdaQueryWrapper<TaskChunkEntity>()
|
||||
.eq(TaskChunkEntity::getTaskId, taskId)
|
||||
.in(TaskChunkEntity::getPayloadJson, values));
|
||||
if (scoped != null && scoped > 0L) {
|
||||
return scoped;
|
||||
}
|
||||
}
|
||||
return taskChunkMapper.selectCount(new LambdaQueryWrapper<TaskChunkEntity>()
|
||||
.in(TaskChunkEntity::getPayloadJson, values));
|
||||
}
|
||||
|
||||
/** 带 task_id 收敛的 scope_state 引用计数;回退逻辑同 {@link #referencedChunkCount}。 */
|
||||
/** 带 task_id 收敛的 scope_state 引用计数;回落口径同 {@link #referencedChunkCount}。 */
|
||||
private Long referencedScopeStateCount(Long taskId, List<String> values) {
|
||||
if (taskId != null) {
|
||||
Long scoped = taskScopeStateMapper.selectCount(new LambdaQueryWrapper<TaskScopeStateEntity>()
|
||||
return taskScopeStateMapper.selectCount(new LambdaQueryWrapper<TaskScopeStateEntity>()
|
||||
.eq(TaskScopeStateEntity::getTaskId, taskId)
|
||||
.and(w -> w.in(TaskScopeStateEntity::getParsedPayloadJson, values)
|
||||
.or()
|
||||
.in(TaskScopeStateEntity::getStateJson, values)));
|
||||
if (scoped != null && scoped > 0L) {
|
||||
return scoped;
|
||||
}
|
||||
}
|
||||
return taskScopeStateMapper.selectCount(new LambdaQueryWrapper<TaskScopeStateEntity>()
|
||||
.and(w -> w.in(TaskScopeStateEntity::getParsedPayloadJson, values)
|
||||
|
||||
+14
-6
@@ -76,16 +76,21 @@ public class ZiniaoMemoryStoreService {
|
||||
public <T> List<T> listByType(String cacheType, Class<T> valueType, int limit) {
|
||||
String normalizedType = normalizeRequired(cacheType, "cacheType 不能为空");
|
||||
int safeLimit = Math.max(limit, 1);
|
||||
// 过期判断必须在 SQL 里做:原来先按 cache_key 取 LIMIT N 再在 Java 里滤掉过期行,
|
||||
// 于是过期行会白占 N 这个窗口——同一 cache_type 存量超过 N 时,排在窗口之后的
|
||||
// 有效行根本取不到,调用方(店铺名索引兜底扫描)把「其实存在」判成未命中。
|
||||
// 同时过期行不再把 payload_json 整列拖回应用侧反序列化。
|
||||
// 走 uk_type_key(cache_type, cache_key):等值 + 排序都命中索引,expires_at 作回表过滤。
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
List<ZiniaoMemoryStoreEntity> entities = ziniaoMemoryStoreMapper.selectList(new LambdaQueryWrapper<ZiniaoMemoryStoreEntity>()
|
||||
.eq(ZiniaoMemoryStoreEntity::getCacheType, normalizedType)
|
||||
.gt(ZiniaoMemoryStoreEntity::getExpiresAt, now)
|
||||
.orderByAsc(ZiniaoMemoryStoreEntity::getCacheKey)
|
||||
.last("LIMIT " + safeLimit));
|
||||
if (entities.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
return entities.stream()
|
||||
.filter(entity -> entity.getExpiresAt() != null && entity.getExpiresAt().isAfter(now))
|
||||
.map(entity -> deserialize(entity.getPayloadJson(), valueType))
|
||||
.filter(Objects::nonNull)
|
||||
.toList();
|
||||
@@ -133,8 +138,11 @@ public class ZiniaoMemoryStoreService {
|
||||
entity.setCreatedAt(now);
|
||||
entity.setUpdatedAt(now);
|
||||
ziniaoMemoryStoreMapper.insert(entity);
|
||||
// 店铺索引条目按「每店两把键(shopId + 名称别名)× 每轮刷新」成批写入,
|
||||
// 逐行 INFO 等于把刷新规模原样刷进日志文件;汇总行数已在
|
||||
// ZiniaoShopIndexService 的 `[ziniao-index] refresh persist` 里给出,这里降到 DEBUG。
|
||||
if (CACHE_TYPE_SHOP_INDEX_ENTRY.equals(normalizedType)) {
|
||||
log.info("[ziniao-shop-index-db] insert cacheKey={} companyNameSample={} expiresAt={} bytes={}",
|
||||
log.debug("[ziniao-shop-index-db] insert cacheKey={} companyNameSample={} expiresAt={} bytes={}",
|
||||
normalizedKey, sampleCompanyNameForLog(companyName), expiresAt, payloadJson.length());
|
||||
}
|
||||
return;
|
||||
@@ -146,7 +154,7 @@ public class ZiniaoMemoryStoreService {
|
||||
entity.setUpdatedAt(now);
|
||||
ziniaoMemoryStoreMapper.updateById(entity);
|
||||
if (CACHE_TYPE_SHOP_INDEX_ENTRY.equals(normalizedType)) {
|
||||
log.info("[ziniao-shop-index-db] update cacheKey={} id={} companyNameSample={} expiresAt={} bytes={}",
|
||||
log.debug("[ziniao-shop-index-db] update cacheKey={} id={} companyNameSample={} expiresAt={} bytes={}",
|
||||
normalizedKey, entity.getId(), sampleCompanyNameForLog(companyName), expiresAt, payloadJson.length());
|
||||
}
|
||||
}
|
||||
@@ -159,7 +167,7 @@ public class ZiniaoMemoryStoreService {
|
||||
if (entity != null) {
|
||||
ziniaoMemoryStoreMapper.deleteById(entity.getId());
|
||||
if (CACHE_TYPE_SHOP_INDEX_ENTRY.equals(normalizedType)) {
|
||||
log.info("[ziniao-shop-index-db] delete cacheKey={} id={} companyNameSample={}",
|
||||
log.debug("[ziniao-shop-index-db] delete cacheKey={} id={} companyNameSample={}",
|
||||
entity.getCacheKey(), entity.getId(), sampleCompanyNameForLog(entity.getCompanyName()));
|
||||
}
|
||||
}
|
||||
@@ -200,7 +208,7 @@ public class ZiniaoMemoryStoreService {
|
||||
}
|
||||
updated += ziniaoMemoryStoreMapper.updateById(row);
|
||||
if (CACHE_TYPE_SHOP_INDEX_ENTRY.equals(row.getCacheType())) {
|
||||
log.info("[ziniao-shop-index-db] mark-stale cacheKey={} id={} companyNameSample={} expiresAt={} bytes={}",
|
||||
log.debug("[ziniao-shop-index-db] mark-stale cacheKey={} id={} companyNameSample={} expiresAt={} bytes={}",
|
||||
row.getCacheKey(), row.getId(), sampleCompanyNameForLog(row.getCompanyName()),
|
||||
row.getExpiresAt(), row.getPayloadJson() == null ? 0 : row.getPayloadJson().length());
|
||||
}
|
||||
|
||||
@@ -27,6 +27,13 @@ spring:
|
||||
max-lifetime: ${AIIMAGE_DB_POOL_MAX_LIFETIME_MS:1500000}
|
||||
keepalive-time: ${AIIMAGE_DB_POOL_KEEPALIVE_TIME_MS:120000}
|
||||
leak-detection-threshold: ${AIIMAGE_DB_POOL_LEAK_DETECT_MS:15000}
|
||||
# 网络层超时兜底:此前只有 Hikari 的取连接/校验超时,socket 读没有上限——
|
||||
# 一条卡在 DB 侧的语句会把 Tomcat 工作线程挂到服务端结束为止,只能人工 kill 容器。
|
||||
# 刻意走 data-source-properties 而不是改上面 URL 的兜底默认值:生产用 AIIMAGE_DB_URL
|
||||
# 整体覆盖 URL,只改兜底值对线上无效。单位为毫秒(Connector/J 口径)。
|
||||
data-source-properties:
|
||||
connectTimeout: ${AIIMAGE_DB_CONNECT_TIMEOUT_MS:5000}
|
||||
socketTimeout: ${AIIMAGE_DB_SOCKET_TIMEOUT_MS:300000}
|
||||
flyway:
|
||||
enabled: ${AIIMAGE_FLYWAY_ENABLED:true}
|
||||
locations: classpath:db
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
-- 保留清理(retention)三条分批 DELETE 的可用索引。
|
||||
--
|
||||
-- 三条 DELETE 的 WHERE 都落不到任何现有索引上,只能全表扫描;InnoDB 的 DELETE 会对扫描过的行
|
||||
-- 加锁,03:50/04:20/04:40 三条又挤在同一窗口,于是清理任务与同期写入互相等待(生产的锁等待
|
||||
-- 与慢 SQL 有一条来源就在这里)。这里只追加索引/生成列,不改任何历史迁移文件。
|
||||
|
||||
-- 1) device_log_file:DELETE ... WHERE log_date < #{cutoff} LIMIT n
|
||||
-- uk_device_log_file(source, device_id, file_name, log_date) 里 log_date 在第 4 列,
|
||||
-- 不带前三列时左前缀用不上;idx_device_log_last_upload 是另一条时间线(V127:24-25)。
|
||||
ALTER TABLE device_log_file
|
||||
ADD KEY idx_device_log_log_date (log_date),
|
||||
ALGORITHM = INPLACE, LOCK = NONE;
|
||||
|
||||
-- 2) shop_data_duplicate_scan:DELETE ... WHERE created_at < #{cutoff} AND id < #{protectFromId} LIMIT n
|
||||
-- 现有 idx_status_created(status, created_at) 首列是 status,而本 DELETE 不带 status 条件(V108)。
|
||||
ALTER TABLE shop_data_duplicate_scan
|
||||
ADD KEY idx_sds_created_at (created_at),
|
||||
ALGORITHM = INPLACE, LOCK = NONE;
|
||||
|
||||
-- 3) biz_price_track_loop_run:DELETE ... WHERE status IN ('SUCCESS','FAILED','STOPPED')
|
||||
-- AND COALESCE(finished_at, updated_at, created_at) < #{cutoff} LIMIT n
|
||||
-- 被 COALESCE 包住的表达式不可走索引(非 sargable),所以落一个 STORED 生成列承载同一时间线,
|
||||
-- 再建 (status, cleanup_at) 复合索引;现有 idx_user_status(user_id, status) 首列是 user_id,
|
||||
-- 同样用不上(V26:20-21)。
|
||||
--
|
||||
-- 应用侧不写这一列:PriceTrackLoopRunEntity 没有 cleanup_at 字段,MyBatis-Plus 按实体列生成
|
||||
-- INSERT/UPDATE 的列清单,不会带上它。加 STORED 生成列需要重建表(ALGORITHM=COPY),
|
||||
-- 该表行数=循环批次数量级,且每行带 3 个 MEDIUMTEXT(V26:12-14),
|
||||
-- 上线前需在从库实测耗时再定执行窗口。
|
||||
ALTER TABLE biz_price_track_loop_run
|
||||
ADD COLUMN cleanup_at DATETIME GENERATED ALWAYS AS (COALESCE(finished_at, updated_at, created_at)) STORED
|
||||
COMMENT '保留清理时间线(生成列,由 finished_at/updated_at/created_at 兜底,应用勿写)',
|
||||
ALGORITHM = COPY;
|
||||
|
||||
ALTER TABLE biz_price_track_loop_run
|
||||
ADD KEY idx_loop_run_cleanup (status, cleanup_at),
|
||||
ALGORITHM = INPLACE, LOCK = NONE;
|
||||
@@ -26,16 +26,30 @@
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
|
||||
<fileNamePattern>${APP_LOG_ARCHIVE}</fileNamePattern>
|
||||
<maxFileSize>50MB</maxFileSize>
|
||||
<maxHistory>1</maxHistory>
|
||||
<!-- 原先 maxHistory=1 且 cleanHistoryOnStart=true:每次发版重启就把一天以上的
|
||||
历史日志抹掉,跨天问题(跟价/外观任务 4~24 小时)事后无现场可查。
|
||||
磁盘占用由 totalSizeCap 兜住,不靠砍天数。 -->
|
||||
<maxHistory>14</maxHistory>
|
||||
<totalSizeCap>500MB</totalSizeCap>
|
||||
<cleanHistoryOnStart>true</cleanHistoryOnStart>
|
||||
<cleanHistoryOnStart>false</cleanHistoryOnStart>
|
||||
</rollingPolicy>
|
||||
</appender>
|
||||
|
||||
<!-- 文件落盘改异步:高频调度日志(1s/2s 级)此前同步占住业务线程,
|
||||
而巡检的日志速率判据正是靠这些行。discardingThreshold=0 表示队列满时
|
||||
宁可阻塞调用线程也不丢日志——丢日志会让"最近时刻"判据假死。 -->
|
||||
<appender name="FILE_ASYNC" class="ch.qos.logback.classic.AsyncAppender">
|
||||
<queueSize>8192</queueSize>
|
||||
<discardingThreshold>0</discardingThreshold>
|
||||
<neverBlock>false</neverBlock>
|
||||
<includeCallerData>false</includeCallerData>
|
||||
<appender-ref ref="FILE"/>
|
||||
</appender>
|
||||
|
||||
<logger name="com.nanri.aiimage" level="${APP_LOG_LEVEL}"/>
|
||||
|
||||
<root level="${ROOT_LOG_LEVEL}">
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
<appender-ref ref="FILE"/>
|
||||
<appender-ref ref="FILE_ASYNC"/>
|
||||
</root>
|
||||
</configuration>
|
||||
|
||||
+6
-4
@@ -27,7 +27,7 @@ class ImageVideoArchiveServiceTest {
|
||||
ImageVideoAsyncTaskMapper taskMapper = mock(ImageVideoAsyncTaskMapper.class);
|
||||
OssStorageService ossStorageService = mock(OssStorageService.class);
|
||||
ImageVideoArchiveService archiveService = new ImageVideoArchiveService(
|
||||
taskMapper, ossStorageService, new ImageVideoProperties(), new ObjectMapper());
|
||||
taskMapper, ossStorageService, new ImageVideoProperties(), new ObjectMapper(), mock(com.nanri.aiimage.common.service.DistributedJobLockService.class, org.mockito.Answers.RETURNS_MOCKS));
|
||||
ImageVideoAsyncTaskEntity task = new ImageVideoAsyncTaskEntity();
|
||||
task.setId(10L);
|
||||
task.setTaskType(ImageVideoArchiveService.TASK_TYPE);
|
||||
@@ -46,7 +46,7 @@ class ImageVideoArchiveServiceTest {
|
||||
ImageVideoAsyncTaskMapper taskMapper = mock(ImageVideoAsyncTaskMapper.class);
|
||||
OssStorageService ossStorageService = mock(OssStorageService.class);
|
||||
ImageVideoArchiveService archiveService = new ImageVideoArchiveService(
|
||||
taskMapper, ossStorageService, new ImageVideoProperties(), new ObjectMapper());
|
||||
taskMapper, ossStorageService, new ImageVideoProperties(), new ObjectMapper(), mock(com.nanri.aiimage.common.service.DistributedJobLockService.class, org.mockito.Answers.RETURNS_MOCKS));
|
||||
ImageVideoAsyncTaskEntity task = new ImageVideoAsyncTaskEntity();
|
||||
task.setId(11L);
|
||||
task.setTaskType(ImageVideoArchiveService.TASK_TYPE);
|
||||
@@ -82,7 +82,8 @@ class ImageVideoArchiveServiceTest {
|
||||
mock(ImageVideoAsyncTaskMapper.class),
|
||||
mock(OssStorageService.class),
|
||||
new ImageVideoProperties(),
|
||||
objectMapper);
|
||||
objectMapper,
|
||||
mock(com.nanri.aiimage.common.service.DistributedJobLockService.class, org.mockito.Answers.RETURNS_MOCKS));
|
||||
ImageVideoAsyncTaskEntity task = new ImageVideoAsyncTaskEntity();
|
||||
task.setTaskType(ImageVideoArchiveService.TASK_TYPE);
|
||||
|
||||
@@ -106,7 +107,8 @@ class ImageVideoArchiveServiceTest {
|
||||
mock(ImageVideoAsyncTaskMapper.class),
|
||||
mock(OssStorageService.class),
|
||||
new ImageVideoProperties(),
|
||||
new ObjectMapper());
|
||||
new ObjectMapper(),
|
||||
mock(com.nanri.aiimage.common.service.DistributedJobLockService.class, org.mockito.Answers.RETURNS_MOCKS));
|
||||
ImageVideoAsyncTaskEntity task = new ImageVideoAsyncTaskEntity();
|
||||
task.setTaskType(ImageVideoArchiveService.TASK_TYPE);
|
||||
|
||||
|
||||
@@ -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 : '#'
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { resolve, dirname } from 'node:path'
|
||||
import {
|
||||
isRecoverableFailedImageVideoTask,
|
||||
isTerminalCozeStatus,
|
||||
isTerminalImageVideoTask,
|
||||
normalizeTaskStatus,
|
||||
} from '../src/pages/image-video/delivery-video-task-state.ts'
|
||||
import type { ImageVideoAsyncTaskVo } from '../src/shared/api/types/modules/image-video.ts'
|
||||
|
||||
const here = dirname(fileURLToPath(import.meta.url))
|
||||
|
||||
function task(patch: Partial<ImageVideoAsyncTaskVo>): ImageVideoAsyncTaskVo {
|
||||
return { taskId: 1, taskType: 'workflow', status: 'RUNNING', ...patch } as ImageVideoAsyncTaskVo
|
||||
}
|
||||
|
||||
// 判错的后果不是「少一次轮询」而是「把没跑完的任务当完成回传」,所以四条边界都要钉住。
|
||||
test('test_success_is_terminal', () => {
|
||||
const done = task({ status: 'SUCCESS', cozeExecuteId: 'exec-1', cozeStatus: 'running' })
|
||||
assert.equal(isTerminalImageVideoTask(done), true)
|
||||
assert.equal(isRecoverableFailedImageVideoTask(done), false, '成功单不属于可恢复失败')
|
||||
})
|
||||
|
||||
test('test_failed_without_execute_id_is_terminal', () => {
|
||||
assert.equal(isTerminalImageVideoTask(task({ status: 'FAILED' })), true)
|
||||
assert.equal(isTerminalImageVideoTask(task({ status: 'FAILED', cozeExecuteId: '' })), true,
|
||||
'cozeExecuteId 空串不能当「远端还在跑」,否则轮询要空转满 720 次')
|
||||
})
|
||||
|
||||
test('test_failed_with_running_remote_is_recoverable', () => {
|
||||
const recover = task({ status: 'FAILED', cozeExecuteId: 'exec-1', cozeStatus: 'running' })
|
||||
assert.equal(isRecoverableFailedImageVideoTask(recover), true)
|
||||
assert.equal(isTerminalImageVideoTask(recover), false, '远端未终态时不能判完成')
|
||||
assert.equal(isTerminalImageVideoTask(task({ status: 'FAILED', cozeExecuteId: 'exec-1' })), false,
|
||||
'cozeStatus 缺失按「未知=还在跑」处理')
|
||||
})
|
||||
|
||||
test('test_failed_with_terminal_remote_is_terminal', () => {
|
||||
for (const status of ['SUCCESS', 'succeeded', ' COMPLETED ', 'done', 'FINISHED', 'FAILED', 'fail', 'ERROR', 'CANCELED', 'cancelled']) {
|
||||
const item = task({ status: 'FAILED', cozeExecuteId: 'exec-1', cozeStatus: status })
|
||||
assert.equal(isTerminalCozeStatus(status), true, `${status} 应识别为远端终态`)
|
||||
assert.equal(isTerminalImageVideoTask(item), true, `远端 ${status} 时本地应判终态`)
|
||||
}
|
||||
})
|
||||
|
||||
test('test_status_normalization_and_unknown_remote', () => {
|
||||
assert.equal(normalizeTaskStatus(undefined), '')
|
||||
assert.equal(normalizeTaskStatus(' failed '), 'FAILED')
|
||||
for (const status of [undefined, '', 'RUNNING', 'PENDING', 'waiting', 'queued']) {
|
||||
assert.equal(isTerminalCozeStatus(status), false, `${status} 不是远端终态`)
|
||||
}
|
||||
})
|
||||
|
||||
test('test_pending_like_statuses_are_never_terminal', () => {
|
||||
for (const status of ['PENDING', 'RUNNING', 'WAITING', 'POLLING'] as const) {
|
||||
const item = task({ status })
|
||||
assert.equal(isTerminalImageVideoTask(item), false)
|
||||
assert.equal(isRecoverableFailedImageVideoTask(item), false)
|
||||
}
|
||||
})
|
||||
|
||||
// 漂移护栏:判定只允许有一份实现。组件里再写一个同名函数会被 import 遮蔽成「本地版说了算」,
|
||||
// 而本地版没人单测——正是这类逻辑历史上改坏的形状。
|
||||
const COMPONENT_RELATIVE_PATH = 'src/pages/image-video/components/DeliveryVideoWorkspace.vue'
|
||||
|
||||
function componentSource(): string {
|
||||
return readFileSync(resolve(here, '..', COMPONENT_RELATIVE_PATH), 'utf-8')
|
||||
}
|
||||
|
||||
test('test_component_imports_predicates_instead_of_redeclaring', () => {
|
||||
const source = componentSource()
|
||||
assert.match(
|
||||
source,
|
||||
/import\s*\{[^}]*\bisTerminalImageVideoTask\b[^}]*\}\s*from\s*'@\/pages\/image-video\/delivery-video-task-state'/,
|
||||
'组件必须从公共模块导入终态判定',
|
||||
)
|
||||
for (const name of ['isTerminalImageVideoTask', 'isRecoverableFailedImageVideoTask', 'isTerminalCozeStatus', 'normalizeTaskStatus']) {
|
||||
assert.doesNotMatch(
|
||||
source,
|
||||
new RegExp(`function\\s+${name}\\s*\\(`),
|
||||
`组件内不得再声明 ${name}(会与导入同名遮蔽)`,
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
test('test_redeclare_detector_actually_catches_a_shadow', () => {
|
||||
const polluted = componentSource().replace(
|
||||
/async function waitForImageVideoTask/,
|
||||
'function isTerminalImageVideoTask(task: ImageVideoAsyncTaskVo) {\n return true\n}\n\nasync function waitForImageVideoTask',
|
||||
)
|
||||
assert.notEqual(polluted, componentSource(), '变异注入未生效,用例本身失效')
|
||||
assert.match(polluted, /function\s+isTerminalImageVideoTask\s*\(/, '检测正则要能抓到重复声明')
|
||||
})
|
||||
@@ -0,0 +1,153 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { readFileSync, readdirSync, statSync } from 'node:fs'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { resolve, dirname, join, relative } from 'node:path'
|
||||
import { safeHttpUrl } from '../src/shared/utils/safe-url.ts'
|
||||
import { createRowId } from '../src/shared/utils/row-id.ts'
|
||||
|
||||
const here = dirname(fileURLToPath(import.meta.url))
|
||||
const srcRoot = resolve(here, '../src')
|
||||
|
||||
/** 静态常量出口(构建期写死,不经服务端/客户端透传) */
|
||||
const STATIC_HREF_ALLOWLIST = new Set(['ALIPRICE_URL'])
|
||||
|
||||
function listVueFiles(dir: string, acc: string[] = []): string[] {
|
||||
for (const entry of readdirSync(dir)) {
|
||||
const full = join(dir, entry)
|
||||
if (statSync(full).isDirectory()) listVueFiles(full, acc)
|
||||
else if (entry.endsWith('.vue')) acc.push(full)
|
||||
}
|
||||
return acc
|
||||
}
|
||||
|
||||
/** 找出「函数体里调过 safeHttpUrl」的本地包装函数名(如 sourceFileHref)。 */
|
||||
function vettedWrapperNames(sources: string[]): Set<string> {
|
||||
const names = new Set<string>()
|
||||
for (const source of sources) {
|
||||
// 顶层函数以行首 } 收尾(与本仓 vue 文件书写一致),按函数体取,避免把邻近函数的调用算进来
|
||||
for (const match of source.matchAll(/function\s+(\w+)\s*\([^)]*\)\s*\{([\s\S]*?)\n\}/g)) {
|
||||
if (match[2].includes('safeHttpUrl(')) names.add(match[1])
|
||||
}
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
type HrefBinding = { file: string; line: string }
|
||||
|
||||
function collectHrefBindings(sources: Record<string, string>): HrefBinding[] {
|
||||
const found: HrefBinding[] = []
|
||||
for (const [file, content] of Object.entries(sources)) {
|
||||
for (const line of content.split(/\r?\n/)) {
|
||||
if (line.includes(':href="')) found.push({ file, line })
|
||||
}
|
||||
}
|
||||
return found
|
||||
}
|
||||
|
||||
function unvettedBindings(bindings: HrefBinding[], wrappers: Set<string>): HrefBinding[] {
|
||||
return bindings.filter(({ line }) => {
|
||||
const expr = (line.match(/:href="([^"]*)"/)?.[1] ?? '').trim()
|
||||
if (expr.includes('safeHttpUrl(')) return false
|
||||
if (/^[A-Z0-9_]+$/.test(expr) && STATIC_HREF_ALLOWLIST.has(expr)) return false
|
||||
const callName = expr.match(/^(\w+)\(/)?.[1]
|
||||
if (callName && wrappers.has(callName)) return false
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
test('test_safe_http_url_allows_only_http_and_https', () => {
|
||||
assert.equal(safeHttpUrl('https://cdn.example.com/a.mp4'), 'https://cdn.example.com/a.mp4')
|
||||
assert.equal(safeHttpUrl('http://127.0.0.1:5173/x'), 'http://127.0.0.1:5173/x')
|
||||
// 相对路径按当前页面补全后判定协议,保持原样返回(交给浏览器解析)
|
||||
assert.equal(safeHttpUrl('/newApi/api/files/x'), '/newApi/api/files/x')
|
||||
})
|
||||
|
||||
test('test_safe_http_url_blocks_pseudo_protocols', () => {
|
||||
for (const bad of [
|
||||
'javascript:alert(1)',
|
||||
'JavaScript:alert(1)',
|
||||
' javascript:alert(1) ',
|
||||
'java\tscript:alert(1)',
|
||||
'data:text/html,<script>alert(1)</script>',
|
||||
'vbscript:msgbox(1)',
|
||||
'file:///C:/Windows/win.ini',
|
||||
'http://[bad',
|
||||
]) {
|
||||
assert.equal(safeHttpUrl(bad), '#', `${bad} 必须被拦成 #`)
|
||||
}
|
||||
})
|
||||
|
||||
test('test_safe_http_url_handles_empty_input', () => {
|
||||
for (const empty of [undefined, null, '', ' ']) {
|
||||
assert.equal(safeHttpUrl(empty as string | null | undefined), '#')
|
||||
}
|
||||
})
|
||||
|
||||
test('test_safe_http_url_uses_page_origin_when_window_exists', () => {
|
||||
const previous = (globalThis as Record<string, unknown>).window
|
||||
;(globalThis as Record<string, unknown>).window = { location: { href: 'https://app.example.com/#group-front' } }
|
||||
try {
|
||||
assert.equal(safeHttpUrl('/api/result.zip'), '/api/result.zip')
|
||||
// 带 origin 的伪协议不会因为「相对路径」被放行
|
||||
assert.equal(safeHttpUrl('javascript:alert(1)'), '#')
|
||||
} finally {
|
||||
if (previous === undefined) delete (globalThis as Record<string, unknown>).window
|
||||
else (globalThis as Record<string, unknown>).window = previous
|
||||
}
|
||||
})
|
||||
|
||||
test('test_create_row_id_is_unique_per_call', () => {
|
||||
const ids = new Set<string>()
|
||||
for (let i = 0; i < 2000; i += 1) ids.add(createRowId('asset'))
|
||||
assert.equal(ids.size, 2000, '同一页会话内行 id 不能重复')
|
||||
for (const id of ids) assert.match(id, /^asset:/)
|
||||
})
|
||||
|
||||
test('test_create_row_id_falls_back_without_random_uuid', () => {
|
||||
const cryptoGlobal = (globalThis as { crypto?: Crypto }).crypto
|
||||
if (!cryptoGlobal) return // 没有 crypto 全局时走的就是降级路径
|
||||
const restore = Object.getOwnPropertyDescriptor(cryptoGlobal, 'randomUUID')
|
||||
try {
|
||||
// 1) randomUUID 缺失(http 非安全上下文下 WebView 不提供)
|
||||
Object.defineProperty(cryptoGlobal, 'randomUUID', { value: undefined, configurable: true, writable: true })
|
||||
const missing = createRowId('row')
|
||||
assert.match(missing, /^row:[0-9a-z]+-[0-9a-z]+-[0-9a-z]+$/, '降级实现要带前缀且分三段')
|
||||
// 2) 存在但会抛(部分内核在非安全上下文直接抛错)
|
||||
Object.defineProperty(cryptoGlobal, 'randomUUID', {
|
||||
value: () => { throw new Error('not a secure context') },
|
||||
configurable: true, writable: true,
|
||||
})
|
||||
const thrown = createRowId('row')
|
||||
assert.match(thrown, /^row:/, '抛错也必须产出可用 id')
|
||||
assert.notEqual(thrown, missing)
|
||||
} finally {
|
||||
if (restore) Object.defineProperty(cryptoGlobal, 'randomUUID', restore)
|
||||
else delete (cryptoGlobal as unknown as Record<string, unknown>).randomUUID
|
||||
}
|
||||
})
|
||||
|
||||
test('test_every_dynamic_href_outlet_goes_through_safe_http_url', () => {
|
||||
const files = listVueFiles(srcRoot)
|
||||
const sources: Record<string, string> = {}
|
||||
for (const file of files) {
|
||||
sources[relative(srcRoot, file).replace(/\\/g, '/')] = readFileSync(file, 'utf-8')
|
||||
}
|
||||
const bindings = collectHrefBindings(sources)
|
||||
assert.ok(bindings.length >= 4, `扫描到的 :href 出口只有 ${bindings.length} 处,检测面失效`)
|
||||
const violations = unvettedBindings(bindings, vettedWrapperNames(Object.values(sources)))
|
||||
assert.deepEqual(violations, [], '存在未经协议过滤的动态 :href')
|
||||
})
|
||||
|
||||
test('test_href_outlet_detector_actually_flags_raw_binding', () => {
|
||||
const wrappers = vettedWrapperNames(['function sourceFileHref(item) {\n return safeHttpUrl(item?.url)\n}'])
|
||||
assert.deepEqual([...wrappers], ['sourceFileHref'], '包装函数识别失效')
|
||||
const synthetic: HrefBinding[] = [
|
||||
{ file: 'a.vue', line: '<a :href="safeHttpUrl(x)">' },
|
||||
{ file: 'b.vue', line: '<a :href="sourceFileHref(x)">' },
|
||||
{ file: 'c.vue', line: '<a :href="ALIPRICE_URL">' },
|
||||
{ file: 'd.vue', line: '<a :href="item.task_data.file_url">' },
|
||||
]
|
||||
const violations = unvettedBindings(synthetic, wrappers)
|
||||
assert.deepEqual(violations.map((v) => v.file), ['d.vue'], '检测必须只漏放原始绑定')
|
||||
})
|
||||
Reference in New Issue
Block a user