36e110d27c
首页在亚马逊/视频右侧新增「京东」一级入口(恒显示),落地页 /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 非静默)。
621 lines
16 KiB
Vue
621 lines
16 KiB
Vue
<script setup lang="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.5):一键建单 → 推送本机客户端 → 轮询服务端进度。
|
||
*
|
||
* 三个真源与三条边界(照 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` 缺失时进度条走不定态,不编造百分比。
|
||
*/
|
||
|
||
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 itemsReceived = computed(() => progress.value?.itemsReceived ?? 0)
|
||
const totalCount = computed(() => {
|
||
const total = progress.value?.total
|
||
return typeof total === 'number' && total > 0 ? total : null
|
||
})
|
||
|
||
/** 百分比:终态 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(
|
||
id: number,
|
||
shop: string,
|
||
tabs: JdListTab[],
|
||
): Promise<string | null> {
|
||
const api = getPywebviewApi()
|
||
if (!api?.enqueue_json) {
|
||
return '当前环境未启用任务队列能力(浏览器预览环境不会启动),请在本机客户端中打开本页'
|
||
}
|
||
const payload = {
|
||
type: 'jd-shop-collect-run',
|
||
ts: Date.now(),
|
||
data: { taskId: id, shopName: shop, listTabs: tabs, maxProducts: 0 },
|
||
}
|
||
const pushResult = await api.enqueue_json(payload)
|
||
if (pushResult?.success) {
|
||
return null
|
||
}
|
||
// 桥返回的 error 原文(系统/英文)不透传进界面:console 留链,界面固定中文动作句
|
||
if (pushResult?.error) {
|
||
console.error('[jd-collect] enqueue_json 被拒', pushResult.error)
|
||
}
|
||
return '推送本机队列失败,任务将由客户端兜底拉取认领'
|
||
}
|
||
|
||
async function startCollect() {
|
||
if (isRunning.value) return
|
||
stopPolling()
|
||
errorMessage.value = ''
|
||
successMessage.value = ''
|
||
progress.value = null
|
||
phase.value = 'running'
|
||
submitting.value = true
|
||
const shop = shopName.value.trim() || '京麦后台'
|
||
const tabs: JdListTab[] = ['ONSALE', 'OFFSALE']
|
||
try {
|
||
const vo = await createJdCollectTask(shop, tabs, 0)
|
||
taskId.value = vo.taskId
|
||
let pushNote = ''
|
||
try {
|
||
const pushError = await pushToClientQueue(vo.taskId, shop, tabs)
|
||
if (pushError) pushNote = ` ${pushError}`
|
||
} catch (pushErr) {
|
||
// 推送自身异常不回滚建单:任务可由服务端兜底拉取认领。
|
||
pushNote = ' 推送本机队列失败,任务将由服务端兜底派发给本机客户端执行'
|
||
console.error('[jd-collect] 推送本机队列失败', pushErr)
|
||
}
|
||
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="page-shell module-page jd-collect-page">
|
||
<header class="jd-top">
|
||
<router-link class="home-back" :to="homeHref" title="返回首页">← 返回首页</router-link>
|
||
|
||
<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="top-right">
|
||
<NotificationBell />
|
||
<span class="admin-name">{{ username }}</span>
|
||
</div>
|
||
</header>
|
||
|
||
<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 {
|
||
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;
|
||
}
|
||
|
||
.collect-card {
|
||
width: 100%;
|
||
max-width: 720px;
|
||
padding: 26px 28px;
|
||
border: 1px solid #3e4a62;
|
||
border-radius: 12px;
|
||
background: #232a3b;
|
||
}
|
||
|
||
.form-group {
|
||
margin-bottom: 18px;
|
||
}
|
||
|
||
.form-group label {
|
||
display: block;
|
||
margin-bottom: 8px;
|
||
color: #c8d2e2;
|
||
font-size: 13px;
|
||
font-weight: 700;
|
||
}
|
||
|
||
.form-group input[type='text'] {
|
||
width: 100%;
|
||
box-sizing: border-box;
|
||
padding: 10px 12px;
|
||
border: 1px solid #3e4a62;
|
||
border-radius: 8px;
|
||
background: #1a1f2e;
|
||
color: #f5f8fc;
|
||
font-size: 14px;
|
||
outline: none;
|
||
transition: border-color 0.2s;
|
||
}
|
||
|
||
.form-group input[type='text']:focus {
|
||
border-color: #e1251b;
|
||
}
|
||
|
||
.form-group input[type='text']::placeholder {
|
||
color: #6b778d;
|
||
}
|
||
|
||
.collect-btn {
|
||
width: 100%;
|
||
padding: 13px 0;
|
||
border: none;
|
||
border-radius: 8px;
|
||
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;
|
||
}
|
||
|
||
.collect-btn:hover:not(:disabled) {
|
||
filter: brightness(1.1);
|
||
transform: translateY(-1px);
|
||
}
|
||
|
||
.collect-btn:disabled {
|
||
opacity: 0.6;
|
||
cursor: not-allowed;
|
||
}
|
||
|
||
.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;
|
||
}
|
||
|
||
.feedback.success {
|
||
background: rgba(16, 185, 129, 0.12);
|
||
color: #6ee7b7;
|
||
}
|
||
|
||
.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>
|