feat(jd): 首页一级入口「京东」+工作台两菜单,获取店铺数据一键采集带进度条

首页在亚马逊/视频右侧新增「京东」一级入口(恒显示),落地页 /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 非静默)。
This commit is contained in:
2026-11-14 16:50:06 +08:00
parent 05e5c35539
commit 36e110d27c
17 changed files with 1682 additions and 143 deletions
+512 -138
View File
@@ -1,40 +1,157 @@
<script setup lang="ts">
import { reactive, ref } from 'vue'
import { createJdCollectTask, type JdListTab } from '../../shared/api/types/modules/jd-collect.ts'
import { getPywebviewApi } from '../../shared/bridges/pywebview.ts'
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
import {
createJdCollectTask,
fetchJdCollectProgress,
type JdCollectProgressVo,
type JdListTab,
} from '@/shared/api/types/modules/jd-collect'
import { getPywebviewApi } from '@/shared/bridges/pywebview'
import { resolvePageHref } from '@/shared/page-prefix'
import { isTerminalTaskStatus } from '@/shared/utils/task-terminal'
import NotificationBell from '@/shared/components/NotificationBell.vue'
/** 京东店铺商品采集建单页(v1.4 AC-1,蓝图 §3.2)。
/** 京东「获取店铺数据」(v1.5):一键建单 → 推送本机客户端 → 轮询服务端进度。
*
* 建单后把 `jd-shop-collect-run` 载荷推进本机客户端队列(v1.4 Phase 6.1 修 P0-1:
* 只建单不推送会让任务永久停在 PENDING——兜底拉取是补救不是主链)。data 键集与
* 客户端 jd_base.process_task 读取契约同键;推送失败不回滚建单,任务可由服务端
* 兜底拉取(TaskClientPullService 白名单含 JD_SHOP_COLLECT)认领。
* 三个真源与三条边界(照 v1.4 已落地的采集契约,不另造流程):
* 1. 建单走后端 `/api/jd-collect/tasks`(通道恒 LOCAL,服务端落 PENDING);
* 2. 建单成功把 `jd-shop-collect-run` 载荷推进本机客户端队列——只建单不推送会让任务
* 卡在 PENDING 等兜底拉取(v1.4 Phase 6.1 P0-1 的教训);推送失败不回滚建单;
* 3. 进度只读服务端(`/tasks/{taskId}/progress`):任务状态 + 合并写游标里的
* itemsReceived/total。`total` 缺失时进度条走不定态,不编造百分比。
*/
const form = reactive({
shopName: '',
onsale: true,
offsale: false,
maxProducts: 0,
type ProgressStatus = 'PENDING' | 'RUNNING' | 'SUCCESS' | 'FAILED'
type ProgressPhase = 'idle' | 'running' | 'done' | 'failed'
const POLL_INTERVAL_MS = 2000
/** 一小时还没终态就停轮询(任务本身有 stale 兜底,页面不该无限烧请求)。 */
const POLL_TIMEOUT_MS = 60 * 60 * 1000
const homeHref = resolvePageHref('/new_web_source/home.html')
const backHref = resolvePageHref('/new_web_source/jd-console.html')
const username = ref('admin')
const shopName = ref('')
const submitting = ref(false)
const taskId = ref<number | null>(null)
const progress = ref<JdCollectProgressVo | null>(null)
const phase = ref<ProgressPhase>('idle')
const errorMessage = ref('')
const successMessage = ref('')
let pollTimer: number | undefined
let pollStartedAt = 0
let pollFailures = 0
const statusText = computed(() => {
const status = progress.value?.status
if (phase.value === 'done') return '获取完成'
if (phase.value === 'failed') return '获取失败'
switch (status) {
case 'PENDING':
return '等待本机客户端领取任务…'
case 'RUNNING':
return '正在获取店铺数据…'
case 'SUCCESS':
return '获取完成'
case 'FAILED':
return '获取失败'
default:
return '已提交,正在确认任务状态…'
}
})
const submitting = ref(false)
const successMessage = ref('')
const errorMessage = ref('')
const itemsReceived = computed(() => progress.value?.itemsReceived ?? 0)
const totalCount = computed(() => {
const total = progress.value?.total
return typeof total === 'number' && total > 0 ? total : null
})
function buildListTabs(): JdListTab[] {
const tabs: JdListTab[] = []
if (form.onsale) tabs.push('ONSALE')
if (form.offsale) tabs.push('OFFSALE')
return tabs
/** 百分比:终态 100;有 total 按已落库商品数算(封顶 99 直到服务端落 SUCCESS)。 */
const percent = computed<number | null>(() => {
if (phase.value === 'done') return 100
const total = totalCount.value
if (!total) return null
const done = Math.min(itemsReceived.value, total)
return Math.max(0, Math.min(99, Math.round((done * 100) / total)))
})
const percentText = computed(() => (percent.value === null ? '' : `${percent.value}%`))
const progressMeta = computed(() => {
const vo = progress.value
if (!vo) return ''
const parts: string[] = [`已采集 ${itemsReceived.value} 个商品`]
if (totalCount.value) {
parts.push(`共 ${totalCount.value} 个`)
}
if (typeof vo.page === 'number' && vo.page > 0) {
parts.push(`第 ${vo.page} 页`)
}
if (typeof vo.batches === 'number' && vo.batches > 0) {
parts.push(`已回传 ${vo.batches} 批`)
}
return parts.join(' · ')
})
const isRunning = computed(() => phase.value === 'running' || submitting.value)
function stopPolling() {
if (pollTimer !== undefined) {
window.clearTimeout(pollTimer)
pollTimer = undefined
}
}
function applyTerminal(vo: JdCollectProgressVo) {
stopPolling()
// 终态集合走共享词表(shared/utils/task-terminal,含 COMPLETED≡SUCCESS);
// 只有明确 FAILED 才当失败,其余终态都按完成展示——窄集合是六页踩过的坑。
if (vo.status !== 'FAILED') {
phase.value = 'done'
successMessage.value = `获取完成:共采集 ${vo.itemsReceived ?? 0} 个商品,` +
'已导入后台「京东数据库」。'
return
}
phase.value = 'failed'
errorMessage.value = vo.message?.trim()
|| '获取失败:没有拿到失败原因,请到客户端任务列表查看详情'
}
async function pollProgress(id: number) {
try {
const vo = await fetchJdCollectProgress(id)
progress.value = vo
pollFailures = 0
if (isTerminalTaskStatus(vo.status)) {
applyTerminal(vo)
return
}
} catch (err) {
pollFailures += 1
// 单次失败不打断轮询(网络抖动/切页);连续失败 5 次才把进度条挂上提示,
// 异常原文只留 console(英文原文不进界面)。
console.error('[jd-collect] 进度查询失败', err)
if (pollFailures >= 5) {
errorMessage.value = '进度查询连续失败,请检查网络;任务本身仍在客户端执行'
}
}
if (Date.now() - pollStartedAt > POLL_TIMEOUT_MS) {
stopPolling()
errorMessage.value = '进度查询超时,请到客户端任务列表查看采集结果'
return
}
pollTimer = window.setTimeout(() => {
void pollProgress(id)
}, POLL_INTERVAL_MS)
}
/** 建单成功后推送本机队列;返回 null 表示环境无队列能力(浏览器预览),返回 string 表示失败原因。 */
async function pushToClientQueue(
taskId: number,
shopName: string,
id: number,
shop: string,
tabs: JdListTab[],
maxProducts: number,
): Promise<string | null> {
const api = getPywebviewApi()
if (!api?.enqueue_json) {
@@ -43,7 +160,7 @@ async function pushToClientQueue(
const payload = {
type: 'jd-shop-collect-run',
ts: Date.now(),
data: { taskId, shopName, listTabs: tabs, maxProducts },
data: { taskId: id, shopName: shop, listTabs: tabs, maxProducts: 0 },
}
const pushResult = await api.enqueue_json(payload)
if (pushResult?.success) {
@@ -56,191 +173,448 @@ async function pushToClientQueue(
return '推送本机队列失败,任务将由客户端兜底拉取认领'
}
async function submit() {
async function startCollect() {
if (isRunning.value) return
stopPolling()
errorMessage.value = ''
successMessage.value = ''
const tabs = buildListTabs()
if (!form.shopName.trim()) {
errorMessage.value = '请填写要采集的京东店铺名称'
return
}
if (!tabs.length) {
errorMessage.value = '请至少勾选一个商品页签(在售中 / 已下架)'
return
}
progress.value = null
phase.value = 'running'
submitting.value = true
const shop = shopName.value.trim() || '京麦后台'
const tabs: JdListTab[] = ['ONSALE', 'OFFSALE']
try {
const vo = await createJdCollectTask(form.shopName.trim(), tabs, form.maxProducts)
const vo = await createJdCollectTask(shop, tabs, 0)
taskId.value = vo.taskId
let pushNote = ''
try {
const pushError = await pushToClientQueue(
vo.taskId, form.shopName.trim(), tabs, form.maxProducts)
if (pushError) {
pushNote = ` ${pushError}`
}
const pushError = await pushToClientQueue(vo.taskId, shop, tabs)
if (pushError) pushNote = ` ${pushError}`
} catch (pushErr) {
// 推送自身异常不回滚建单:任务可由服务端兜底拉取认领。
// 异常原文(多为英文)不进界面——首条语句写界面文案(状态写入,扫描器口径非静默),
// console 只作留链。
pushNote = ' 推送本机队列失败,任务将由服务端兜底派发给本机客户端执行'
console.error('[jd-collect] 推送本机队列失败', pushErr)
}
successMessage.value = `建单成功:任务号 ${vo.taskId}${vo.taskNo ? `(${vo.taskNo})` : ''}。` +
'任务由本机客户端执行,请保持客户端在线并已登录京麦;采集进度在客户端任务列表查看,' +
'落库数据到后台「京东数据库」菜单查询。' + pushNote
if (pushNote) {
errorMessage.value = pushNote.trim()
}
pollStartedAt = Date.now()
pollFailures = 0
void pollProgress(vo.taskId)
} catch (err) {
phase.value = 'failed'
errorMessage.value = err instanceof Error ? err.message : '建单失败,请稍后重试'
} finally {
submitting.value = false
}
}
onMounted(() => {
const localName = (window.localStorage.getItem('username') || '').trim()
if (localName) username.value = localName
})
onBeforeUnmount(() => {
stopPolling()
})
</script>
<template>
<div class="jd-collect-page">
<div class="jd-collect-panel">
<h2>京东采集</h2>
<p class="jd-collect-desc">
采集京东店铺商品(列表 + 详情全部属性 + 图片),同步到后台「京东数据库」。
任务由本机客户端执行:提交后请保持客户端在线,并确保已在客户端登录京麦。
</p>
<div class="page-shell module-page jd-collect-page">
<header class="jd-top">
<router-link class="home-back" :to="homeHref" title="返回首页">← 返回首页</router-link>
<div class="form-group">
<label for="jd-shop-name">京东店铺名称</label>
<input
id="jd-shop-name"
v-model="form.shopName"
type="text"
placeholder="填写京麦后台显示的店铺名称"
:disabled="submitting"
/>
<div class="brand">
<span class="brand-logo">富</span>
<span class="brand-text">
<span class="brand-name">数富AI</span>
<span class="brand-sub">京东运营工具台</span>
</span>
</div>
<div class="form-group">
<label>采集商品页签</label>
<div class="tab-checks">
<label class="tab-check">
<input v-model="form.onsale" type="checkbox" :disabled="submitting" />
在售中
</label>
<label class="tab-check">
<input v-model="form.offsale" type="checkbox" :disabled="submitting" />
已下架
</label>
</div>
<div class="top-right">
<NotificationBell />
<span class="admin-name">{{ username }}</span>
</div>
</header>
<div class="form-group">
<label for="jd-max-products">采集商品数上限(0 = 不限制)</label>
<input
id="jd-max-products"
v-model.number="form.maxProducts"
type="number"
min="0"
:disabled="submitting"
/>
</div>
<button class="jd-submit" type="button" :disabled="submitting" @click="submit">
{{ submitting ? '提交中…' : '开始采集' }}
</button>
<p v-if="successMessage" class="jd-feedback success">{{ successMessage }}</p>
<p v-if="errorMessage" class="jd-feedback error">{{ errorMessage }}</p>
<div class="tool-head">
<router-link class="tool-head__back" :to="backHref">← 返回京东工具台</router-link>
<span class="tool-head__title">获取店铺数据</span>
<span class="tool-head__desc">
一键抓取已登录京麦后台的店铺商品(列表 + 详情全部属性 + 图片),导入后台「京东数据库」。
</span>
</div>
<main class="jd-body">
<div class="collect-card">
<div class="form-group">
<label for="jd-shop-name">店铺名称(可选)</label>
<input
id="jd-shop-name"
v-model="shopName"
type="text"
placeholder="留空按「京麦后台」记录;填了便于在后台「京东数据库」区分店铺"
:disabled="isRunning"
/>
</div>
<button class="collect-btn" type="button" :disabled="isRunning" @click="startCollect">
{{ isRunning ? '获取中…' : '一键获取店铺数据' }}
</button>
<p class="collect-hint">
点击后本机客户端会自动打开已登录的京麦后台采集商品,请保持客户端在线且京麦已登录;
采集到的数据会实时回传并导入后台。
</p>
<section v-if="taskId" class="progress-block">
<div class="progress-head">
<span class="progress-status">{{ statusText }}</span>
<span v-if="percentText" class="progress-percent">{{ percentText }}</span>
</div>
<div class="progress-track">
<div
class="progress-fill"
:class="{ 'progress-fill--indeterminate': percent === null && phase === 'running' }"
:style="percent === null ? undefined : { width: `${percent}%` }"
></div>
</div>
<div v-if="progressMeta" class="progress-meta">{{ progressMeta }}</div>
</section>
<p v-if="successMessage" class="feedback success">{{ successMessage }}</p>
<p v-if="errorMessage" class="feedback error">{{ errorMessage }}</p>
</div>
</main>
</div>
</template>
<style scoped>
.jd-collect-page {
padding: 24px;
display: flex;
flex-direction: column;
min-height: 100vh;
background-color: #1a1f2e;
background-image: radial-gradient(rgba(45, 212, 191, 0.08) 1px, transparent 1px);
background-size: 48px 48px;
font-family: "PingFang SC", "Microsoft YaHei UI", "Microsoft YaHei", "微软雅黑", sans-serif;
}
.jd-top {
flex: 0 0 auto;
min-height: 64px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 0 28px;
background: #1a1f2e;
border-bottom: 1px solid #3e4a62;
}
.home-back {
flex-shrink: 0;
display: inline-flex;
align-items: center;
gap: 6px;
padding: 6px 14px;
border: 1px solid #3e4a62;
border-radius: 6px;
color: #c8d2e2;
font-size: 13px;
text-decoration: none;
white-space: nowrap;
transition: all 0.2s;
}
.home-back:hover {
background: #2b3447;
color: #f5f8fc;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
flex-shrink: 0;
}
.brand-logo {
display: flex;
align-items: center;
justify-content: center;
width: 38px;
height: 38px;
border-radius: 8px;
background: #e1251b;
color: #fff;
font-size: 20px;
font-weight: 700;
box-shadow: 0 2px 8px rgba(225, 37, 27, 0.32);
}
.brand-text {
display: flex;
flex-direction: column;
line-height: 1.35;
}
.brand-name {
color: #f5f8fc;
font-size: 17px;
font-weight: 700;
white-space: nowrap;
}
.brand-sub {
color: #a0acbe;
font-size: 10px;
white-space: nowrap;
}
.top-right {
display: flex;
align-items: center;
gap: 14px;
flex-shrink: 0;
}
.admin-name {
color: #c8d2e2;
font-size: 13px;
font-weight: 700;
white-space: nowrap;
}
.tool-head {
flex: 0 0 auto;
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 20px;
min-height: 62px;
padding: 10px 34px;
background: #1a1f2e;
}
.tool-head__back {
flex-shrink: 0;
display: inline-flex;
align-items: center;
gap: 6px;
padding: 6px 14px;
border: 1px solid #3e4a62;
border-radius: 6px;
color: #c8d2e2;
font-size: 13px;
text-decoration: none;
white-space: nowrap;
transition: all 0.2s;
}
.tool-head__back:hover {
background: #2b3447;
color: #f5f8fc;
}
.tool-head__title {
flex-shrink: 0;
color: #f5f8fc;
font-size: 22px;
font-weight: 700;
white-space: nowrap;
}
.tool-head__desc {
min-width: 0;
max-width: 760px;
color: #a0acbe;
font-size: 14px;
line-height: 1.5;
}
.jd-body {
flex: 1 1 auto;
min-height: 0;
overflow-y: auto;
padding: 4px 34px 40px;
display: flex;
justify-content: center;
/* 卡片按内容高度收口:flex 默认 stretch 会把它拉到整屏,空白比内容还长 */
align-items: flex-start;
}
.jd-collect-panel {
.collect-card {
width: 100%;
max-width: 560px;
background: #fff;
border: 1px solid #e5e7eb;
max-width: 720px;
padding: 26px 28px;
border: 1px solid #3e4a62;
border-radius: 12px;
padding: 24px;
}
.jd-collect-desc {
color: #6b7280;
font-size: 13px;
line-height: 1.6;
margin: 8px 0 20px;
background: #232a3b;
}
.form-group {
margin-bottom: 16px;
margin-bottom: 18px;
}
.form-group label {
display: block;
margin-bottom: 8px;
color: #c8d2e2;
font-size: 13px;
color: #374151;
margin-bottom: 6px;
font-weight: 700;
}
.form-group input[type='text'],
.form-group input[type='number'] {
.form-group input[type='text'] {
width: 100%;
box-sizing: border-box;
border: 1px solid #d1d5db;
padding: 10px 12px;
border: 1px solid #3e4a62;
border-radius: 8px;
padding: 8px 10px;
background: #1a1f2e;
color: #f5f8fc;
font-size: 14px;
outline: none;
transition: border-color 0.2s;
}
.tab-checks {
display: flex;
gap: 20px;
.form-group input[type='text']:focus {
border-color: #e1251b;
}
.tab-check {
display: flex;
align-items: center;
gap: 6px;
font-size: 14px;
color: #374151;
margin-bottom: 0;
.form-group input[type='text']::placeholder {
color: #6b778d;
}
.jd-submit {
.collect-btn {
width: 100%;
background: #2dd4bf;
color: #062a26;
padding: 13px 0;
border: none;
border-radius: 8px;
padding: 10px 0;
font-size: 15px;
font-weight: 600;
background: linear-gradient(135deg, #ff5a4f, #e1251b);
color: #ffffff;
font-size: 16px;
font-weight: 700;
font-family: inherit;
cursor: pointer;
box-shadow: 0 4px 16px rgba(225, 37, 27, 0.32);
transition: all 0.2s;
}
.jd-submit:disabled {
.collect-btn:hover:not(:disabled) {
filter: brightness(1.1);
transform: translateY(-1px);
}
.collect-btn:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.jd-feedback {
margin-top: 16px;
.collect-hint {
margin: 12px 0 0;
color: #a0acbe;
font-size: 12px;
line-height: 1.7;
}
/* ===== 进度条 ===== */
.progress-block {
margin-top: 22px;
padding: 16px 18px;
border: 1px solid #3e4a62;
border-radius: 10px;
background: #1f2637;
}
.progress-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 10px;
}
.progress-status {
color: #f5f8fc;
font-size: 14px;
font-weight: 700;
}
.progress-percent {
color: #ff8a80;
font-size: 14px;
font-weight: 700;
}
.progress-track {
position: relative;
height: 12px;
overflow: hidden;
border-radius: 999px;
background: #11161f;
border: 1px solid #2a3344;
}
.progress-fill {
height: 100%;
width: 0;
border-radius: 999px;
background: linear-gradient(90deg, #ff7a70, #e1251b);
transition: width 0.4s ease;
}
/* 没有 total(客户端还没抓到列表首页)时走不定态:不编造百分比 */
.progress-fill--indeterminate {
position: absolute;
top: 0;
left: 0;
width: 40%;
animation: jd-progress-indeterminate 1.2s ease-in-out infinite;
}
@keyframes jd-progress-indeterminate {
0% {
transform: translateX(-100%);
}
100% {
transform: translateX(250%);
}
}
.progress-meta {
margin-top: 10px;
color: #a0acbe;
font-size: 12px;
}
.feedback {
margin: 16px 0 0;
padding: 10px 12px;
border-radius: 8px;
font-size: 13px;
line-height: 1.6;
border-radius: 8px;
padding: 10px 12px;
}
.jd-feedback.success {
background: #ecfdf5;
color: #047857;
.feedback.success {
background: rgba(16, 185, 129, 0.12);
color: #6ee7b7;
}
.jd-feedback.error {
background: #fef2f2;
color: #b91c1c;
.feedback.error {
background: rgba(248, 113, 113, 0.12);
color: #fca5a5;
}
@media (max-width: 1100px) {
.tool-head {
padding: 10px 16px;
gap: 10px;
}
.jd-body {
padding: 4px 16px 24px;
}
}
</style>