From 36e110d27cd3531f912342cdcea3d819d69acf9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=87=AA=E8=BE=BE?= <980324341@qq.com> Date: Sat, 14 Nov 2026 16:50:06 +0800 Subject: [PATCH] =?UTF-8?q?feat(jd):=20=E9=A6=96=E9=A1=B5=E4=B8=80?= =?UTF-8?q?=E7=BA=A7=E5=85=A5=E5=8F=A3=E3=80=8C=E4=BA=AC=E4=B8=9C=E3=80=8D?= =?UTF-8?q?=EF=BC=8B=E5=B7=A5=E4=BD=9C=E5=8F=B0=E4=B8=A4=E8=8F=9C=E5=8D=95?= =?UTF-8?q?=EF=BC=8C=E8=8E=B7=E5=8F=96=E5=BA=97=E9=93=BA=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E4=B8=80=E9=94=AE=E9=87=87=E9=9B=86=E5=B8=A6=E8=BF=9B=E5=BA=A6?= =?UTF-8?q?=E6=9D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 首页在亚马逊/视频右侧新增「京东」一级入口(恒显示),落地页 /jd-console 按亚马逊控制台样式做两张工具卡:获取店铺数据(可用)、商品上架(占位,点开只提示「暂未开通」)。 获取店铺数据改成一键:建单 → 推 jd-shop-collect-run 到本机队列 → 轮询服务端进度。进度只读两处真源——任务行状态 + scope 行合并写游标里的 itemsReceived/cursor.total(客户端抓列表第一页时把 total 写进游标),没有分母就退回不定态动画,不编造百分比;终态即停轮询。后端新增 GET /api/jd-collect/tasks/{taskId}/progress(按 taskId+userId 校归属)。 判据:JdCollectProgressQueryTest(5)、jd-console-tool-catalog.test.ts(5)、jd-console-pending-menu.mount(3)、jd-collect-progress.mount(6);catch 棘轮 blocks 314→315 按名登记(轮询 catch 非静默)。 --- .../jd/controller/JdCollectController.java | 17 + .../jd/model/vo/JdCollectProgressVo.java | 46 ++ .../jd/service/JdCollectTaskService.java | 103 +++ .../config/CatchSilenceRatchetTest.java | 18 +- .../service/JdCollectProgressQueryTest.java | 129 ++++ .../src/pages/home/DesktopHomePage.vue | 11 + frontend-vue/src/pages/jd/JdCollectTab.vue | 650 ++++++++++++++---- frontend-vue/src/pages/jd/JdConsolePage.vue | 462 +++++++++++++ frontend-vue/src/pages/jd/tool-catalog.ts | 42 ++ frontend-vue/src/router/index.ts | 6 + frontend-vue/src/shared/api/endpoints.ts | 2 + .../shared/api/types/modules/jd-collect.ts | 32 +- .../tests/catch-silence-ratchet.test.ts | 5 +- frontend-vue/tests/endpoints.test.ts | 2 + .../tests/jd-console-tool-catalog.test.ts | 54 ++ .../mount/jd-collect-progress.mount.test.ts | 179 +++++ .../jd-console-pending-menu.mount.test.ts | 67 ++ 17 files changed, 1682 insertions(+), 143 deletions(-) create mode 100644 backend-java/src/main/java/com/nanri/aiimage/modules/jd/model/vo/JdCollectProgressVo.java create mode 100644 backend-java/src/test/java/com/nanri/aiimage/modules/jd/service/JdCollectProgressQueryTest.java create mode 100644 frontend-vue/src/pages/jd/JdConsolePage.vue create mode 100644 frontend-vue/src/pages/jd/tool-catalog.ts create mode 100644 frontend-vue/tests/jd-console-tool-catalog.test.ts create mode 100644 frontend-vue/tests/mount/jd-collect-progress.mount.test.ts create mode 100644 frontend-vue/tests/mount/jd-console-pending-menu.mount.test.ts diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/jd/controller/JdCollectController.java b/backend-java/src/main/java/com/nanri/aiimage/modules/jd/controller/JdCollectController.java index e74766e78..5503ca010 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/jd/controller/JdCollectController.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/jd/controller/JdCollectController.java @@ -4,6 +4,7 @@ import com.nanri.aiimage.common.api.ApiResponse; import com.nanri.aiimage.common.security.AdminAuthSupport; import com.nanri.aiimage.modules.jd.model.dto.JdCollectTaskCreateRequest; import com.nanri.aiimage.modules.jd.model.dto.JdIngestChunkRequest; +import com.nanri.aiimage.modules.jd.model.vo.JdCollectProgressVo; import com.nanri.aiimage.modules.jd.model.vo.JdCollectTaskVo; import com.nanri.aiimage.modules.jd.model.vo.JdIngestChunkVo; import com.nanri.aiimage.modules.jd.service.JdCollectTaskService; @@ -19,10 +20,12 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** @@ -58,6 +61,20 @@ public class JdCollectController { adminAuthSupport.currentDeviceId(httpRequest))); } + @GetMapping("/tasks/{taskId}/progress") + @Operation(summary = "查询京东采集进度", description = "用户端「获取店铺数据」进度条数据源:" + + "任务状态 + 已回传商品数 + 列表 total(客户端抓首页时写入游标) + 当前页/Tab + 已收批次数。" + + "进度查询与建单同口径鉴权:uid 按会话身份回填,任务 owner 与 uid 不一致即 403。") + public ApiResponse progress( + @Parameter(description = "任务 ID", required = true, example = "33890") + @PathVariable Long taskId, + @Parameter(description = "任务所属用户 ID;省略时取当前登录身份", required = false, example = "1") + @RequestParam(value = "user_id", required = false) Long userId, + HttpServletRequest httpRequest) { + return ApiResponse.success(collectTaskService.queryProgress(taskId, + adminAuthSupport.requireOwnershipUserId(httpRequest, userId))); + } + @PostMapping("/tasks/{taskId}/chunk") @Operation(summary = "提交采集分片回传", description = "采集器按批次回传列表/详情/图片清单;" + "游标合并写(缺件沿用前值)、行 upsert-only;isEnd=true 任务落 SUCCESS;" diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/jd/model/vo/JdCollectProgressVo.java b/backend-java/src/main/java/com/nanri/aiimage/modules/jd/model/vo/JdCollectProgressVo.java new file mode 100644 index 000000000..326c38d7c --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/jd/model/vo/JdCollectProgressVo.java @@ -0,0 +1,46 @@ +package com.nanri.aiimage.modules.jd.model.vo; + +import lombok.Data; + +import java.time.LocalDateTime; + +/** + * 京东店铺采集进度(v1.5 用户端"获取店铺数据"进度条数据源)。 + * + *

数据只取两处真源:{@code biz_file_task}(任务状态/终态原因)与 + * {@code biz_task_scope_state}(合并写游标:{@code itemsReceived}、{@code cursor.total}、 + * {@code receivedChunkCount})。不在服务端另算一套百分比——客户端抓完一页就把本页 + * total 写进游标(合并写,缺键沿用前值,见铁律 30),页面据此算进度。 + */ +@Data +public class JdCollectProgressVo { + + private Long taskId; + + /** PENDING / RUNNING / SUCCESS / FAILED(biz_file_task.status 原值)。 */ + private String status; + + /** 已回传落库的商品数(scope.state_json.itemsReceived 累计值)。 */ + private Integer itemsReceived; + + /** + * 列表总数:客户端抓第一页时从京麦列表接口拿到的 total,没拿到过就为 {@code null}。 + * 页面据此决定是"百分比进度条"还是"不定态进度条"。 + */ + private Integer total; + + /** 当前采集页(游标,断点续跑会从这里接着跑)。 */ + private Integer page; + + /** 当前采集 Tab(ONSALE / OFFSALE)。 */ + private String listTab; + + /** 已收到的分片批次数。 */ + private Integer batches; + + /** 终态原因(FAILED 时为客户端上报的真实原因)。 */ + private String message; + + /** 最近一次回传/合并写的时间。 */ + private LocalDateTime updatedAt; +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/jd/service/JdCollectTaskService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/jd/service/JdCollectTaskService.java index 5e1f9c0dd..813962f7b 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/jd/service/JdCollectTaskService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/jd/service/JdCollectTaskService.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.nanri.aiimage.common.exception.BusinessException; import com.nanri.aiimage.config.CollectRoutingProperties; import com.nanri.aiimage.modules.jd.model.dto.JdCollectTaskCreateRequest; +import com.nanri.aiimage.modules.jd.model.vo.JdCollectProgressVo; import com.nanri.aiimage.modules.jd.model.vo.JdCollectTaskVo; import com.nanri.aiimage.modules.task.mapper.FileTaskMapper; import com.nanri.aiimage.modules.task.mapper.TaskScopeStateMapper; @@ -20,7 +21,10 @@ import org.springframework.dao.DuplicateKeyException; import org.springframework.stereotype.Service; import java.time.LocalDateTime; +import java.util.LinkedHashMap; import java.util.Locale; +import java.util.Map; +import java.util.Objects; import java.util.Set; /** @@ -159,4 +163,103 @@ public class JdCollectTaskService { .eq(TaskScopeStateEntity::getModuleType, MODULE_TYPE) .last("limit 1")); } + + /** + * 查询采集进度(v1.5 用户端「获取店铺数据」进度条)。 + * + *

只读两处真源:任务行(状态/终态原因)与 scope 行(合并写游标里的 + * {@code itemsReceived}/{@code cursor.total}/{@code cursor.page})。这里不重算百分比—— + * 分母来自客户端抓列表第一页时拿到的 total,没拿到过就返回 {@code null}, + * 由页面降级成不定态进度条(不编造一个假百分比)。 + * + *

归属校验按 {@code (taskId, userId)}(铁律 11/22):管理员代客时 userId 来自 + * 请求参数,若与任务 owner 不一致一律拒,不让 taskId 裸查越权。 + */ + public JdCollectProgressVo queryProgress(Long taskId, Long userId) { + if (taskId == null || taskId <= 0) { + throw new BusinessException("任务 ID 不合法"); + } + FileTaskEntity task = fileTaskMapper.selectById(taskId); + if (task == null || !MODULE_TYPE.equals(task.getModuleType())) { + throw new BusinessException("任务不存在: " + taskId); + } + if (userId != null && task.getUserId() != null && !Objects.equals(userId, task.getUserId())) { + throw new BusinessException(403, "无权查看该任务"); + } + + JdCollectProgressVo vo = new JdCollectProgressVo(); + vo.setTaskId(task.getId()); + vo.setStatus(task.getStatus()); + vo.setMessage(task.getErrorMessage()); + vo.setUpdatedAt(task.getUpdatedAt()); + + TaskScopeStateEntity scope = findScopeRow(taskId); + if (scope == null) { + // scope 行缺失(历史/异常建单)时只回任务级状态,进度按 0 计,不抛错—— + // 进度查询是展示面,不该因为一行辅助表缺失把页面打成红条。 + vo.setItemsReceived(nz(task.getSuccessFileCount())); + vo.setBatches(0); + return vo; + } + Map state = parseScopeState(scope.getStateJson()); + Map cursor = asMap(state.get("cursor")); + vo.setItemsReceived(Math.max(nz(task.getSuccessFileCount()), asInt(state.get("itemsReceived")))); + vo.setTotal(asNullableInt(cursor.get("total"))); + vo.setPage(asNullableInt(cursor.get("page"))); + Object tab = cursor.get("tab"); + vo.setListTab(tab == null ? null : String.valueOf(tab)); + vo.setBatches(scope.getReceivedChunkCount() == null ? 0 : scope.getReceivedChunkCount()); + if (scope.getUpdatedAt() != null) { + vo.setUpdatedAt(scope.getUpdatedAt()); + } + return vo; + } + + /** 读 scope 状态 JSON;损坏时返回空对象并留痕(查询面不因一行脏 JSON 抛错)。 */ + private Map parseScopeState(String stateJson) { + if (stateJson == null || stateJson.isBlank()) { + return new LinkedHashMap<>(); + } + try { + Map parsed = objectMapper.readValue(stateJson, + objectMapper.getTypeFactory().constructMapType(LinkedHashMap.class, String.class, Object.class)); + return parsed == null ? new LinkedHashMap<>() : parsed; + } catch (Exception ex) { + log.warn("[jd-collect] scope 状态 JSON 解析失败,进度按 0 展示 taskId 相关行: {}", ex.getMessage()); + return new LinkedHashMap<>(); + } + } + + @SuppressWarnings("unchecked") + private static Map asMap(Object raw) { + return raw instanceof Map ? new LinkedHashMap<>((Map) raw) : new LinkedHashMap<>(); + } + + private static int asInt(Object raw) { + Integer value = asNullableInt(raw); + return value == null ? 0 : value; + } + + /** + * 宽松读整数:数字类型直接用,字符串只认 1~9 位十进制(int 范围内必然不溢出)。 + * 不拿 {@code NumberFormatException} 当控制流——脏值在进度展示面是正常的空结果, + * 不该为它添一个 catch(catch 棘轮同形判据)。 + */ + private static Integer asNullableInt(Object raw) { + if (raw instanceof Number number) { + return number.intValue(); + } + if (raw == null) { + return null; + } + String text = String.valueOf(raw).trim(); + if (!text.matches("-?\\d{1,9}")) { + return null; + } + return Integer.valueOf(text); + } + + private static int nz(Integer value) { + return value == null ? 0 : Math.max(0, value); + } } diff --git a/backend-java/src/test/java/com/nanri/aiimage/config/CatchSilenceRatchetTest.java b/backend-java/src/test/java/com/nanri/aiimage/config/CatchSilenceRatchetTest.java index a859f595f..ee4200a20 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/config/CatchSilenceRatchetTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/config/CatchSilenceRatchetTest.java @@ -73,7 +73,9 @@ class CatchSilenceRatchetTest { // + parseAttrGroups 的 attr_json 兜底;Phase 2/4 的 7 枚全是 RETHROW、Phase 2 的 scope 行 // DuplicateKeyException 是 LOG_ONLY,都不占本格),逐处记名见 STRICT_TOTAL 上方注释; // LOG_ONLY 宽口径格 251 没动(宽口径实测 194,离上限很远)。 - static final int GIVEN_NOT_RETHROW = 615; + // 11-14 v1.5「获取店铺数据」进度条抬 615 -> 616:一枚不重抛(进度查询读 scope.state_json + // 失败 log.warn + 空对象),逐处记名与误差方向见 STRICT_TOTAL 上方注释;LOG_ONLY 一格没动。 + static final int GIVEN_NOT_RETHROW = 616; static final int GIVEN_LOG_ONLY = 251; static final int GIVEN_EMPTY = 56; @@ -357,7 +359,14 @@ class CatchSilenceRatchetTest { // 「京东时间格式不识别(应为 yyyy-MM-dd HH:mm:ss): <原文>」(RETHROW)。误差方向: // 客户端回传脏时间戳之前直接 DateTimeParseException 裸抛 = 回传口 500 裸堆栈(P1-3); // 改成吞掉置 null = 登记时间被悄悄抹掉。 - static final int STRICT_TOTAL = 1091; + // 11-14 v1.5「获取店铺数据」进度条抬 1091 -> 1092,一枚逐处记名: + // `JdCollectTaskService.parseScopeState`:进度查询读 scope.state_json 时 + // `catch Exception` ⇒ log.warn 带原因 + 返回空对象(NOT_RETHROW)。误差方向: + // 改成往外抛 = 一行脏 JSON 把进度查询打成 500、进度条直接红条;改成静默返回空对象 + // 不记日志 = 游标损坏永远没人知道。演示面只降级不抛错,但必须有那一行 WARN。 + // (相邻的数值解析刻意不用 NumberFormatException 当控制流:改为 1~9 位十进制正则前置, + // 省掉两枚 catch,EMPTY 一格没涨。) + static final int STRICT_TOTAL = 1092; /** * 重抛型 catch 的**下限**(不是上限):本轮把两处吞异常改成重抛之后实测 388。 * 这一格只能涨不能跌——它是"异常会往外走"的总量,把它钉成"只许减"就等于拦着别人修吞异常。 @@ -491,7 +500,10 @@ class CatchSilenceRatchetTest { // 本格含 LOG_ONLY(incl 口径):同批 Phase 2 建单 scope 行那枚 DuplicateKeyException 复用 // 先到行把 LOG_ONLY 抬 193 -> 194 时连带占本格一枚额度 ⇒ 614 -> 615(出处同 STRICT_TOTAL // 上方注释;这是本格**含 LOG_ONLY** 口径的连带,不是又一枚新的不重抛)。 - static final int STRICT_NOT_RETHROW = 615; + // 11-14 v1.5「获取店铺数据」进度条:严格口径实测 615 -> 616(一枚不重抛: + // JdCollectTaskService.parseScopeState 读 scope.state_json 失败 log.warn + 空对象, + // 出处与误差方向见 STRICT_TOTAL 上方注释;logOnly 一格没动)。 + static final int STRICT_NOT_RETHROW = 616; /** * 10-17 抬 172 -> 173,出处就是上面点名过的 {@code TaskHeartbeatLivenessService.stamp()}: * 块体只有「取到 key 名 + log.warn」一句,写心跳证据失败时既不重试也不抛出—— diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/jd/service/JdCollectProgressQueryTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/jd/service/JdCollectProgressQueryTest.java new file mode 100644 index 000000000..ee437badd --- /dev/null +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/jd/service/JdCollectProgressQueryTest.java @@ -0,0 +1,129 @@ +package com.nanri.aiimage.modules.jd.service; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.nanri.aiimage.common.exception.BusinessException; +import com.nanri.aiimage.modules.jd.model.vo.JdCollectProgressVo; +import com.nanri.aiimage.modules.task.mapper.FileTaskMapper; +import com.nanri.aiimage.modules.task.mapper.TaskScopeStateMapper; +import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity; +import com.nanri.aiimage.modules.task.model.entity.TaskScopeStateEntity; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.time.LocalDateTime; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +/** + * 京东采集进度查询(v1.5「获取店铺数据」进度条数据源)。 + * + *

判据三件:① 进度只读真源(任务行 + scope 合并写游标),不编造百分比; + * ② 归属按 {@code (taskId, userId)} 校验,taskId 裸查不能越权;③ scope 行缺失或 JSON + * 损坏时降级展示而不是把页面打成红条。 + */ +@ExtendWith(MockitoExtension.class) +class JdCollectProgressQueryTest { + + @Mock + private FileTaskMapper fileTaskMapper; + @Mock + private TaskScopeStateMapper taskScopeStateMapper; + @Spy + private ObjectMapper objectMapper = new ObjectMapper(); + + @InjectMocks + private JdCollectTaskService service; + + private FileTaskEntity runningTask(long userId) { + FileTaskEntity task = new FileTaskEntity(); + task.setId(33890L); + task.setModuleType(JdCollectTaskService.MODULE_TYPE); + task.setUserId(userId); + task.setStatus("RUNNING"); + task.setSuccessFileCount(0); + task.setUpdatedAt(LocalDateTime.of(2026, 11, 14, 10, 0)); + return task; + } + + private TaskScopeStateEntity scope(String stateJson, Integer batches) { + TaskScopeStateEntity scope = new TaskScopeStateEntity(); + scope.setTaskId(33890L); + scope.setModuleType(JdCollectTaskService.MODULE_TYPE); + scope.setStateJson(stateJson); + scope.setReceivedChunkCount(batches); + scope.setUpdatedAt(LocalDateTime.of(2026, 11, 14, 10, 1)); + return scope; + } + + @Test + void 进度按合并写游标读出商品数与总数() { + when(fileTaskMapper.selectById(33890L)).thenReturn(runningTask(7L)); + when(taskScopeStateMapper.selectOne(any())).thenReturn(scope( + "{\"cursor\":{\"tab\":\"ONSALE\",\"page\":3,\"total\":300},\"itemsReceived\":120}", + 4)); + + JdCollectProgressVo vo = service.queryProgress(33890L, 7L); + + assertThat(vo.getStatus()).isEqualTo("RUNNING"); + assertThat(vo.getItemsReceived()).isEqualTo(120); + assertThat(vo.getTotal()).as("分母只来自客户端写入游标的 total,服务端不另算") + .isEqualTo(300); + assertThat(vo.getPage()).isEqualTo(3); + assertThat(vo.getListTab()).isEqualTo("ONSALE"); + assertThat(vo.getBatches()).isEqualTo(4); + } + + @Test + void 归属不符拒绝查询() { + when(fileTaskMapper.selectById(33890L)).thenReturn(runningTask(7L)); + + assertThatThrownBy(() -> service.queryProgress(33890L, 99L)) + .isInstanceOf(BusinessException.class) + .hasMessageContaining("无权查看该任务"); + } + + @Test + void scope缺失时回退任务成功数且总数留空() { + FileTaskEntity task = runningTask(7L); + task.setStatus("SUCCESS"); + task.setSuccessFileCount(58); + when(fileTaskMapper.selectById(33890L)).thenReturn(task); + when(taskScopeStateMapper.selectOne(any())).thenReturn(null); + + JdCollectProgressVo vo = service.queryProgress(33890L, 7L); + + assertThat(vo.getItemsReceived()).isEqualTo(58); + assertThat(vo.getTotal()).isNull(); + assertThat(vo.getBatches()).isZero(); + } + + @Test + void 游标脏JSON降级为零进度不抛错() { + when(fileTaskMapper.selectById(33890L)).thenReturn(runningTask(7L)); + when(taskScopeStateMapper.selectOne(any())).thenReturn(scope("{\"cursor\":{半个", 1)); + + JdCollectProgressVo vo = service.queryProgress(33890L, 7L); + + assertThat(vo.getItemsReceived()).isZero(); + assertThat(vo.getTotal()).isNull(); + assertThat(vo.getBatches()).isEqualTo(1); + } + + @Test + void 非京东模块任务按不存在拒绝() { + FileTaskEntity task = runningTask(7L); + task.setModuleType("SIMILAR_ASIN"); + when(fileTaskMapper.selectById(33890L)).thenReturn(task); + + assertThatThrownBy(() -> service.queryProgress(33890L, 7L)) + .isInstanceOf(BusinessException.class) + .hasMessageContaining("任务不存在"); + } +} diff --git a/frontend-vue/src/pages/home/DesktopHomePage.vue b/frontend-vue/src/pages/home/DesktopHomePage.vue index c94104542..1bea04a41 100644 --- a/frontend-vue/src/pages/home/DesktopHomePage.vue +++ b/frontend-vue/src/pages/home/DesktopHomePage.vue @@ -63,6 +63,15 @@ :class="{ disabled: !videoHref }" data-column-key="wb" >视频 + + + 京东

{{ toastText }}
@@ -115,6 +124,8 @@ const permissionUnavailable = ref(false) const amazonHref = computed(() => resolvePageHref('/new_web_source/amazon-console.html')) const videoHref = computed(() => resolvePageHref('/new_web_source/image-video.html')) +/** 京东工作台(两个菜单:获取店铺数据 / 商品上架,见 pages/jd/tool-catalog.ts) */ +const jdHref = computed(() => resolvePageHref('/new_web_source/jd-console.html')) const canShow = computed(() => permissionUnavailable.value ? { amazon: true, video: true } : permissionState.value, diff --git a/frontend-vue/src/pages/jd/JdCollectTab.vue b/frontend-vue/src/pages/jd/JdCollectTab.vue index cdea1c892..ce2cb13d0 100644 --- a/frontend-vue/src/pages/jd/JdCollectTab.vue +++ b/frontend-vue/src/pages/jd/JdCollectTab.vue @@ -1,40 +1,157 @@ diff --git a/frontend-vue/src/pages/jd/JdConsolePage.vue b/frontend-vue/src/pages/jd/JdConsolePage.vue new file mode 100644 index 000000000..57a83d620 --- /dev/null +++ b/frontend-vue/src/pages/jd/JdConsolePage.vue @@ -0,0 +1,462 @@ + + + + + diff --git a/frontend-vue/src/pages/jd/tool-catalog.ts b/frontend-vue/src/pages/jd/tool-catalog.ts new file mode 100644 index 000000000..e65e97552 --- /dev/null +++ b/frontend-vue/src/pages/jd/tool-catalog.ts @@ -0,0 +1,42 @@ +/** + * 京东运营工具台 · 工具目录 + * + * 一级入口在桌面首页(与「亚马逊 / 视频」并列,见 DesktopHomePage),进来后是这两个菜单: + * 1. 获取店铺数据:一键抓已登录京麦后台的店铺商品(列表 + 详情 + 图片),落后台「京东数据库」; + * 2. 商品上架:功能未落地,先按"假菜单"占位(点击提示暂未开通),不发 href。 + * + * 卡片形状照亚马逊工具台(tool-catalog.ts)的 ToolInfo,样式复用同一套深色卡片墙。 + */ + +export type JdToolInfo = { + /** 工具 id */ + id: string + /** 菜单名称 */ + name: string + /** 卡片一句话描述 */ + desc: string + /** 角标文字(无则不显示) */ + tag?: string + /** 功能页地址;未提供表示暂未开通(占位菜单) */ + href?: string +} + +export const JD_TOOLS: ReadonlyArray = [ + { + id: 'jd-collect', + name: '获取店铺数据', + desc: '一键抓取已登录京麦后台的店铺商品(列表 + 详情全部属性 + 图片),导入后台「京东数据库」', + tag: '核心', + href: '/jd-collect', + }, + { + id: 'jd-publish', + name: '商品上架', + desc: '把京东数据库里的商品批量上架到京麦店铺(建设中,敬请期待)', + tag: '规划中', + }, +] + +export const JD_TOOL_MAP: ReadonlyMap = new Map( + JD_TOOLS.map((tool) => [tool.id, tool] as const), +) diff --git a/frontend-vue/src/router/index.ts b/frontend-vue/src/router/index.ts index a94b5fb46..9bcd72ac3 100644 --- a/frontend-vue/src/router/index.ts +++ b/frontend-vue/src/router/index.ts @@ -67,6 +67,12 @@ const routes = [ name: 'appearance-patent', component: () => import('@/pages/brand/components/BrandAppearancePatentTab.vue'), }, + // ---- 京东运营工具台(首页一级入口「京东」的落地页)---- + { + path: '/jd-console', + name: 'jd-console', + component: () => import('@/pages/jd/JdConsolePage.vue'), + }, { path: '/jd-collect', name: 'jd-collect', diff --git a/frontend-vue/src/shared/api/endpoints.ts b/frontend-vue/src/shared/api/endpoints.ts index 1b71816ac..51898d6a9 100644 --- a/frontend-vue/src/shared/api/endpoints.ts +++ b/frontend-vue/src/shared/api/endpoints.ts @@ -207,6 +207,8 @@ export const API_ENDPOINTS = { /** 京东店铺商品采集(v1.4):建单走 LOCAL 通道,由本机客户端在已登录京麦的窗口内执行。 */ jdCollect: { create: '/api/jd-collect/tasks', + /** 采集进度(用户端「获取店铺数据」进度条数据源) */ + progress: '/api/jd-collect/tasks/{taskId}/progress', heartbeat: '/api/jd-collect/tasks/{taskId}/heartbeat', chunk: '/api/jd-collect/tasks/{taskId}/chunk', }, diff --git a/frontend-vue/src/shared/api/types/modules/jd-collect.ts b/frontend-vue/src/shared/api/types/modules/jd-collect.ts index 5238cc721..628e441c7 100644 --- a/frontend-vue/src/shared/api/types/modules/jd-collect.ts +++ b/frontend-vue/src/shared/api/types/modules/jd-collect.ts @@ -1,7 +1,8 @@ -import { post, type JavaApiResponse, unwrapJavaResponse } from '../../http.ts' +import { get, post, type JavaApiResponse, unwrapJavaResponse } from '../../http.ts' import { buildJavaUrl } from '../../url.ts' import { getCurrentUserId } from '../../user.ts' import { API_ENDPOINTS } from '../../endpoints.ts' +import { getTaskProgressTimeoutMs } from '../../../task-progress-config.ts' /** 京东店铺采集建单回执(蓝图 §3.2):任务落 PENDING、执行通道固定 LOCAL,由本机客户端领取执行。 */ export interface JdCollectTaskVo { @@ -17,6 +18,35 @@ export type JdListTab = 'ONSALE' | 'OFFSALE' * 建单(v1.4 AC-1):选择店铺 + 商品 Tab + 采集范围。未选店铺 / 未登记京麦登录态时 * 服务端按蓝图 §3.2 拒绝并返回点名文案(业务码信封,unwrapJavaResponse 抛出)。 */ +/** + * 采集进度回执(v1.5「获取店铺数据」进度条)。 + * + * `total` 只在客户端抓过列表首页、把 total 写进合并写游标之后才有值;为 null 时页面 + * 用不定态进度条,不编造百分比。`itemsReceived` 是服务端已落库的商品数(慢一拍于客户端 + * 本地计数,这是它诚实的代价——界面不该拿本地内存数冒充后台进度)。 + */ +export interface JdCollectProgressVo { + taskId: number; + status: string; + itemsReceived: number | null; + total: number | null; + page: number | null; + listTab: string | null; + batches: number | null; + message: string | null; + updatedAt: string | null; +} + +/** 轮询采集进度:uid 与建单同口径(会话身份),任务 owner 不一致服务端直接拒。 */ +export function fetchJdCollectProgress(taskId: number) { + const path = buildJavaUrl(API_ENDPOINTS.jdCollect.progress.replace('{taskId}', String(taskId))) + const query = `?user_id=${encodeURIComponent(String(getCurrentUserId()))}` + return unwrapJavaResponse(get>(path + query, { + // 进度请求按短超时:轮询里一次挂住会把进度条一起冻住(同 progress/light 口径)。 + timeout: getTaskProgressTimeoutMs(), + })) +} + export function createJdCollectTask(shopName: string, listTabs: JdListTab[], maxProducts = 0) { return unwrapJavaResponse(post, Record>( buildJavaUrl(API_ENDPOINTS.jdCollect.create), diff --git a/frontend-vue/tests/catch-silence-ratchet.test.ts b/frontend-vue/tests/catch-silence-ratchet.test.ts index f8550abce..79b72d194 100644 --- a/frontend-vue/tests/catch-silence-ratchet.test.ts +++ b/frontend-vue/tests/catch-silence-ratchet.test.ts @@ -70,7 +70,10 @@ const BASELINE = { // 页面推送包一支 try/catch(P0-1 派发链)——推送失败/桥不可用时 pushNote 附一句提示、 // 建单照常保留(判据:推送是尽力而为的快路径,失败回滚建单反而丢单)。错误进了界面 // 文案,非静默;silent 一格没涨。 - blocks: 314, + // 315 = 314 + 1(2026-11-14 v1.5「获取店铺数据」进度条):JdCollectTab.vue 的 + // pollProgress 轮询 catch——单次进度查询失败不打断轮询(网络抖动/切页), + // 连续 5 次才把一句中文提示写进 errorMessage,异常原文只进 console;非静默。 + blocks: 315, // 2026-10-30(ops Phase 6/可读棘轮):ApiSecretSettingsPanel.vue 1113→775 行,面板里那处 // 静默 catch(余额查询失败只 console.warn、界面留空)随 loadBalance 整块搬进 // useProxyPlan.ts。不是新增静默:面板 1→0、新文件记 1,silent 总数与 blocks 都一格没动, diff --git a/frontend-vue/tests/endpoints.test.ts b/frontend-vue/tests/endpoints.test.ts index 7d7bccd88..73c2caeb2 100644 --- a/frontend-vue/tests/endpoints.test.ts +++ b/frontend-vue/tests/endpoints.test.ts @@ -314,8 +314,10 @@ test('test_endpoints_frozen_snapshot', () => { }, // 2026-11-14 v1.4 京东数据库:三支端点(建单/心跳/分片回传)随 Phase 3 落码, // 冻结快照只许显式追加——端点漂移会让"新开一条匿名可达口"没人看见。 + // 2026-11-14 v1.5:progress(进度条数据源,鉴权与建单同口径:uid 会话身份 + owner 校验)。 "jdCollect": { "create": "/api/jd-collect/tasks", + "progress": "/api/jd-collect/tasks/{taskId}/progress", "heartbeat": "/api/jd-collect/tasks/{taskId}/heartbeat", "chunk": "/api/jd-collect/tasks/{taskId}/chunk" }, diff --git a/frontend-vue/tests/jd-console-tool-catalog.test.ts b/frontend-vue/tests/jd-console-tool-catalog.test.ts new file mode 100644 index 000000000..be57edc72 --- /dev/null +++ b/frontend-vue/tests/jd-console-tool-catalog.test.ts @@ -0,0 +1,54 @@ +import assert from 'node:assert/strict' +import test from 'node:test' +import { existsSync, readFileSync } from 'node:fs' +import { join } from 'node:path' + +import { JD_TOOLS, JD_TOOL_MAP } from '../src/pages/jd/tool-catalog.ts' + +/** 京东一级入口(v1.5):首页「京东」→ 京东工作台两个菜单(获取店铺数据 / 商品上架占位)。 */ + +const ROOT = process.cwd() + +function read(relativePath: string): string { + const path = join(ROOT, relativePath) + assert.ok(existsSync(path), `文件缺失:${path}`) + return readFileSync(path, 'utf8') +} + +test('京东工作台只有两个菜单:获取店铺数据 + 商品上架', () => { + assert.equal(JD_TOOLS.length, 2, '京东工作台菜单数应为 2') + const names = JD_TOOLS.map((tool) => tool.name) + assert.deepEqual(names, ['获取店铺数据', '商品上架']) + assert.equal(new Set(JD_TOOLS.map((tool) => tool.id)).size, 2, '工具 id 不应重复') + assert.ok(JD_TOOL_MAP.get('jd-collect'), 'JD_TOOL_MAP 应按 id 命中获取店铺数据') +}) + +test('获取店铺数据指向采集页,商品上架先做占位菜单', () => { + const collect = JD_TOOLS.find((tool) => tool.id === 'jd-collect') + const publish = JD_TOOLS.find((tool) => tool.id === 'jd-publish') + assert.equal(collect?.href, '/jd-collect', '获取店铺数据应指向 /jd-collect') + assert.equal(publish?.href, undefined, '商品上架先做假菜单:不得挂路由') +}) + +test('路由器注册京东工作台与采集页', () => { + const router = read('src/router/index.ts') + assert.ok(router.includes("path: '/jd-console'"), 'router 缺 /jd-console') + assert.ok(router.includes('JdConsolePage.vue'), 'router 未指向 JdConsolePage') + assert.ok(router.includes("path: '/jd-collect'"), 'router 缺 /jd-collect') +}) + +test('首页京东入口与亚马逊/视频并列', () => { + const home = read('src/pages/home/DesktopHomePage.vue') + assert.ok(home.includes('jd-console.html'), '首页京东入口未指向京东工作台') + assert.ok(/>京东 { + const page = read('src/pages/jd/JdCollectTab.vue') + assert.ok(page.includes('fetchJdCollectProgress'), '采集页必须轮询服务端进度(不拿本地内存数充进度)') + assert.ok(page.includes('progress-track'), '采集页缺进度条轨道') + assert.ok(page.includes('jd-progress-indeterminate'), + '拿不到 total 时必须走不定态进度条,不得编造百分比') + assert.ok(page.includes('一键获取店铺数据'), '主按钮应是一键获取') + assert.ok(page.includes('jd-shop-collect-run'), '建单后必须推送本机客户端队列') +}) diff --git a/frontend-vue/tests/mount/jd-collect-progress.mount.test.ts b/frontend-vue/tests/mount/jd-collect-progress.mount.test.ts new file mode 100644 index 000000000..41a981d06 --- /dev/null +++ b/frontend-vue/tests/mount/jd-collect-progress.mount.test.ts @@ -0,0 +1,179 @@ +/** + * 「获取店铺数据」一键采集 + 进度条挂载用例(v1.5)。 + * + * 用户口径原话:「获取店铺就是点击获取后就自动抓已打开的京麦后台的店铺数据导入到后台 需要有进度条」。 + * 四组靶子,缺一条就是假绿: + * ① 点一次按钮就走完整链路:建单 → 把 `jd-shop-collect-run` 推进本机客户端队列; + * ② 有 total 时进度条按已落库商品数算出真实百分比(分母只来自服务端游标,不编造); + * ③ total 缺失时走不定态动画(没有分母就不许编一个假百分比); + * ④ 终态即收手:SUCCESS 落 100% + 导入完成文案,FAILED 用服务端原因,两种情况都不再轮询。 + * + * 跑法:`npx vitest run tests/mount/jd-collect-progress.mount.test.ts` + */ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { flushPromises, mount, type VueWrapper } from '@vue/test-utils' + +import JdCollectTab from '@/pages/jd/JdCollectTab.vue' + +const api = vi.hoisted(() => ({ + createJdCollectTask: vi.fn(), + fetchJdCollectProgress: vi.fn(), + enqueue: vi.fn(), +})) + +vi.mock('@/shared/api/types/modules/jd-collect', () => ({ + createJdCollectTask: api.createJdCollectTask, + fetchJdCollectProgress: api.fetchJdCollectProgress, +})) +vi.mock('@/shared/bridges/pywebview', () => ({ + getPywebviewApi: () => ({ enqueue_json: api.enqueue }), +})) + +const POLL_MS = 2000 + +let wrapper: VueWrapper | null = null + +function baseProgress(over: Record = {}) { + return { + taskId: 88, + status: 'RUNNING', + itemsReceived: 30, + total: 100, + page: 2, + listTab: 'ONSALE', + batches: 1, + message: null, + updatedAt: null, + ...over, + } +} + +async function mountPage() { + wrapper = mount(JdCollectTab, { + global: { + stubs: { + NotificationBell: true, + RouterLink: { template: '' }, + 'router-link': { template: '' }, + }, + }, + }) + await flushPromises() + return wrapper +} + +async function startCollect(w: VueWrapper) { + await w.find('.collect-btn').trigger('click') + await flushPromises() +} + +beforeEach(() => { + vi.useFakeTimers() + localStorage.setItem('username', 'admin') + api.createJdCollectTask.mockReset() + api.fetchJdCollectProgress.mockReset() + api.enqueue.mockReset() + api.createJdCollectTask.mockResolvedValue({ taskId: 88, taskNo: 'JD-1', collectChannel: 'LOCAL' }) + api.enqueue.mockResolvedValue({ success: true }) + vi.spyOn(console, 'error').mockImplementation(() => {}) +}) + +afterEach(() => { + wrapper?.unmount() + wrapper = null + vi.useRealTimers() + vi.restoreAllMocks() + localStorage.clear() +}) + +describe('获取店铺数据:一键采集 + 进度条', () => { + it('点一次按钮就建单并把采集载荷推进本机客户端队列', async () => { + api.fetchJdCollectProgress.mockResolvedValue(baseProgress()) + const w = await mountPage() + await startCollect(w) + + expect(api.createJdCollectTask).toHaveBeenCalledTimes(1) + // 店铺名留空 → 按「京麦后台」记录(与后端 shop_name 语义一致) + expect(api.createJdCollectTask).toHaveBeenCalledWith('京麦后台', ['ONSALE', 'OFFSALE'], 0) + expect(api.enqueue).toHaveBeenCalledTimes(1) + const payload = api.enqueue.mock.calls[0][0] + expect(payload.type).toBe('jd-shop-collect-run') + expect(payload.data).toEqual({ + taskId: 88, + shopName: '京麦后台', + listTabs: ['ONSALE', 'OFFSALE'], + maxProducts: 0, + }) + }) + + it('有 total 时按已落库商品数显示真实百分比与进度元信息', async () => { + api.fetchJdCollectProgress.mockResolvedValue(baseProgress()) + const w = await mountPage() + await startCollect(w) + + expect(w.find('.progress-track').exists()).toBe(true) + expect(w.find('.progress-percent').text()).toBe('30%') + const fill = w.find('.progress-fill') + expect(fill.attributes('style')).toContain('width: 30%') + expect(fill.classes()).not.toContain('progress-fill--indeterminate') + expect(w.find('.progress-meta').text()).toBe('已采集 30 个商品 · 共 100 个 · 第 2 页 · 已回传 1 批') + expect(w.find('.progress-status').text()).toBe('正在获取店铺数据…') + }) + + it('没有分母(total 缺失)时走不定态动画,不编造百分比', async () => { + api.fetchJdCollectProgress.mockResolvedValue(baseProgress({ total: null, page: null, batches: 0 })) + const w = await mountPage() + await startCollect(w) + + expect(w.find('.progress-percent').exists()).toBe(false) + expect(w.find('.progress-fill').classes()).toContain('progress-fill--indeterminate') + expect(w.find('.progress-meta').text()).toBe('已采集 30 个商品') + }) + + it('SUCCESS 终态:进度满格并提示已导入后台,且停止轮询', async () => { + api.fetchJdCollectProgress.mockResolvedValue(baseProgress({ itemsReceived: 100 })) + const w = await mountPage() + await startCollect(w) + + api.fetchJdCollectProgress.mockResolvedValue( + baseProgress({ status: 'SUCCESS', itemsReceived: 100 }), + ) + await vi.advanceTimersByTimeAsync(POLL_MS) + await flushPromises() + + expect(w.find('.progress-percent').text()).toBe('100%') + expect(w.find('.progress-status').text()).toBe('获取完成') + expect(w.find('.feedback.success').text()).toContain('已导入后台「京东数据库」') + const callsAfterTerminal = api.fetchJdCollectProgress.mock.calls.length + await vi.advanceTimersByTimeAsync(POLL_MS * 3) + await flushPromises() + expect(api.fetchJdCollectProgress.mock.calls.length).toBe(callsAfterTerminal) + }) + + it('FAILED 终态:用服务端原因说明失败,按钮恢复可点', async () => { + api.fetchJdCollectProgress.mockResolvedValue(baseProgress()) + const w = await mountPage() + await startCollect(w) + + api.fetchJdCollectProgress.mockResolvedValue( + baseProgress({ status: 'FAILED', message: '京麦后台未登录,请先在客户端登录京麦' }), + ) + await vi.advanceTimersByTimeAsync(POLL_MS) + await flushPromises() + + expect(w.find('.progress-status').text()).toBe('获取失败') + expect(w.find('.feedback.error').text()).toContain('京麦后台未登录') + expect(w.find('.collect-btn').attributes('disabled')).toBeUndefined() + }) + + it('建单失败:只报错不留假进度条,一次进度请求也不发', async () => { + api.createJdCollectTask.mockRejectedValue(new Error('缺少登录凭据')) + const w = await mountPage() + await startCollect(w) + + expect(w.find('.feedback.error').text()).toContain('缺少登录凭据') + expect(w.find('.progress-track').exists()).toBe(false) + expect(api.fetchJdCollectProgress).not.toHaveBeenCalled() + expect(api.enqueue).not.toHaveBeenCalled() + }) +}) diff --git a/frontend-vue/tests/mount/jd-console-pending-menu.mount.test.ts b/frontend-vue/tests/mount/jd-console-pending-menu.mount.test.ts new file mode 100644 index 000000000..90bce5a6e --- /dev/null +++ b/frontend-vue/tests/mount/jd-console-pending-menu.mount.test.ts @@ -0,0 +1,67 @@ +/** + * 京东工作台挂载用例(v1.5):首页「京东」进来后是两个菜单。 + * + * 三件事一起判,缺一条就是假绿: + * ① 两个菜单名都在 DOM 里(获取店铺数据 / 商品上架); + * ② 获取店铺数据是「可用」卡片(有 href → router-link 分支,文案是「打开工具」); + * ③ 商品上架是「假菜单」:没有 href,点开只弹一句「暂未开通」,不发任何请求。 + * + * 跑法:cd frontend-vue && npx vitest run(tests/mount/** 由 vitest.config.ts 收)。 + */ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { mount, type VueWrapper } from '@vue/test-utils' + +import JdConsolePage from '@/pages/jd/JdConsolePage.vue' + +let wrapper: VueWrapper | null = null + +function mountConsole() { + wrapper = mount(JdConsolePage, { + global: { + stubs: { + NotificationBell: true, + RouterLink: { template: '' }, + 'router-link': { template: '' }, + }, + }, + }) + return wrapper +} + +describe('京东工作台两个菜单', () => { + beforeEach(() => { + window.localStorage.setItem('username', 'admin') + }) + + afterEach(() => { + wrapper?.unmount() + wrapper = null + window.localStorage.clear() + }) + + it('两个菜单名都渲染出来', () => { + const w = mountConsole() + const text = w.text() + expect(text).toContain('获取店铺数据') + expect(text).toContain('商品上架') + expect(w.findAll('.tool-card').length).toBe(2) + }) + + it('获取店铺数据是可用卡片,商品上架是占位(暂未开通)', () => { + const w = mountConsole() + const cards = w.findAll('.tool-card') + expect(cards[0].classes()).not.toContain('tool-card--pending') + expect(cards[0].text()).toContain('打开工具') + expect(cards[1].classes()).toContain('tool-card--pending') + expect(cards[1].text()).toContain('暂未开通') + }) + + it('点假菜单只弹提示,不跳转', async () => { + const w = mountConsole() + const pending = w.findAll('.tool-card')[1] + await pending.trigger('click') + const toast = w.find('.toast') + expect(toast.text()).toContain('商品上架暂未开通') + expect(toast.classes()).toContain('show') + }) +})