Files
crawler-plugin/admin-frontend-vue/src/pages/jd/JdProductDetailDrawer.vue
T

557 lines
16 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
/** 京东商品详情抽屉(v1.4 Phase 5):点行打开,展示基本信息 + attrGroups 分块表格 +
* 图片网格(MAIN/ATTR/DETAIL 三区,attr 图按 attrName 归并)。
* 基本信息=商品全部字段(B104:列表只摆部分字段,类目/信息分/SKU 数/上架时间/京东创建/
* 修改时间/店铺/分组都在这里看)。
* 图片点击放大预览用自绘 modal-mask(先例:RecordsHistoryPage 的 thumb + modal-mask +
* 原图新标签;不用 element-plus)。数据由本组件按 id 自取(公共库无公司维度),失败条内提示可重试。 */
import { computed, ref, watch } from 'vue'
import { fetchJdProductDetail } from './jd-product-api.ts'
import type {
JdProductImage,
JdProductItem,
JdProductAttrGroup,
JdProductSku,
} from './jd-product-model.ts'
import { jdPriceRange, jdSkuDimensions, jdSkuSummary } from './jd-product-model.ts'
import { formatDateTime } from '@/utils/datetime'
import { requestErrorMessage } from '../../api/envelope.ts'
const props = defineProps<{
item: JdProductItem | null
visible: boolean
}>()
const emit = defineEmits<{ (event: 'close'): void }>()
const loading = ref(false)
const loadFailure = ref('')
const attrGroups = ref<JdProductAttrGroup[]>([])
const images = ref<JdProductImage[]>([])
const skus = ref<JdProductSku[]>([])
const attrJsonRaw = ref('')
/** SKU 矩阵维度(京麦「颜色 / 尺码」两列)与表头汇总。 */
const skuDimensions = computed(() => jdSkuDimensions(skus.value))
const skuSummary = computed(() => jdSkuSummary(skus.value))
function skuAttrValue(sku: JdProductSku, dimension: string): string {
return sku.saleAttrs.find((attr) => attr.name === dimension)?.value || '-'
}
/** 图片三区:MAIN / ATTR / DETAIL;未知类型归 DETAIL 之外的「其他」,不丢。 */
const mainImages = computed(() => images.value.filter((image) => image.imageType === 'MAIN'))
const attrImageGroups = computed(() => {
const buckets = new Map<string, JdProductImage[]>()
for (const image of images.value) {
if (image.imageType !== 'ATTR') continue
const key = image.attrName || '属性图'
const bucket = buckets.get(key)
if (bucket) bucket.push(image)
else buckets.set(key, [image])
}
return [...buckets.entries()].map(([attrName, list]) => ({ attrName, list }))
})
const detailImages = computed(() => images.value.filter((image) => image.imageType === 'DETAIL'))
const otherImages = computed(() =>
images.value.filter((image) => image.imageType !== 'MAIN' && image.imageType !== 'ATTR' && image.imageType !== 'DETAIL'),
)
/** 放大预览:点缩略图弹自绘 modal-mask,原图可新标签打开。 */
const previewVisible = ref(false)
const previewUrl = ref('')
function openPreview(url: string): void {
previewUrl.value = url
previewVisible.value = true
}
function close(): void {
previewVisible.value = false
emit('close')
}
async function loadDetail(): Promise<void> {
const item = props.item
if (!item) return
loading.value = true
loadFailure.value = ''
attrGroups.value = []
images.value = []
skus.value = []
attrJsonRaw.value = ''
try {
const detail = await fetchJdProductDetail(item.id)
attrGroups.value = detail.attrGroups
images.value = detail.images
skus.value = detail.skus
attrJsonRaw.value = detail.attrJsonRaw
if (!detail.attrGroups.length && detail.attrJsonRaw) {
// 结构化解析失败但原文在:兜底展示原文,不静默吞
loadFailure.value = ''
}
} catch (error) {
loadFailure.value = requestErrorMessage(error) || '商品详情加载失败'
} finally {
loading.value = false
}
}
watch(
() => [props.visible, props.item?.id] as const,
([visible]) => {
previewVisible.value = false
previewUrl.value = ''
if (visible && props.item) void loadDetail()
},
)
function dash(value: string | number | null | undefined): string {
const text = value === null || value === undefined ? '' : String(value).trim()
return text || '-'
}
</script>
<template>
<div v-if="visible" class="drawer-mask" @click.self="close">
<aside class="drawer">
<header class="drawer-head">
<h3>商品详情{{ item?.productCode ? ` · ${item.productCode}` : '' }}</h3>
<button class="drawer-close" type="button" aria-label="关闭" @click="close">×</button>
</header>
<div class="drawer-body">
<div v-if="loadFailure" class="drawer-failure">
<span>{{ loadFailure }}</span>
<button class="btn btn-secondary" type="button" @click="loadDetail">重试</button>
</div>
<p v-else-if="loading" class="drawer-tip">加载中...</p>
<template v-else>
<section class="detail-block">
<h4>基本信息</h4>
<dl class="info-grid">
<div class="info-item info-item-wide">
<dt>标题</dt>
<dd :title="item?.title || ''">{{ dash(item?.title) }}</dd>
</div>
<div class="info-item"><dt>品牌</dt><dd>{{ dash(item?.brand) }}</dd></div>
<div class="info-item"><dt>类目</dt><dd :title="item?.categoryPath || ''">{{ dash(item?.categoryPath) }}</dd></div>
<div class="info-item"><dt>商品编码</dt><dd>{{ dash(item?.productCode) }}</dd></div>
<div class="info-item"><dt>货号</dt><dd>{{ dash(item?.itemNum) }}</dd></div>
<div class="info-item"><dt>SKU</dt><dd>{{ dash(item?.skuId) }}</dd></div>
<div class="info-item"><dt>SKU 数</dt><dd>{{ dash(item?.skuCount) }}</dd></div>
<div class="info-item"><dt>信息分</dt><dd>{{ dash(item?.healthScore) }}</dd></div>
<div class="info-item"><dt>价格区间</dt><dd>{{ dash(jdPriceRange(item ?? { priceMin: '', priceMax: '' })) }}</dd></div>
<div class="info-item"><dt>总库存</dt><dd>{{ dash(item?.stockTotal) }}</dd></div>
<div class="info-item"><dt>状态</dt><dd>{{ dash(item?.saleStatus) }}</dd></div>
<div class="info-item"><dt>店铺</dt><dd :title="item?.shopName || ''">{{ dash(item?.shopName) }}</dd></div>
<div class="info-item"><dt>分类</dt><dd>{{ dash(item?.groupName || '未分类') }}</dd></div>
<div class="info-item"><dt>上架时间</dt><dd>{{ item?.jdOnlineAt ? formatDateTime(item.jdOnlineAt) : '-' }}</dd></div>
<div class="info-item"><dt>京东创建时间</dt><dd>{{ item?.jdCreatedAt ? formatDateTime(item.jdCreatedAt) : '-' }}</dd></div>
<div class="info-item"><dt>京东修改时间</dt><dd>{{ item?.jdUpdatedAt ? formatDateTime(item.jdUpdatedAt) : '-' }}</dd></div>
<div class="info-item"><dt>同步时间</dt><dd>{{ formatDateTime(item?.syncedAt || '') }}</dd></div>
</dl>
</section>
<section v-if="attrGroups.length" class="detail-block">
<h4>属性与设置(按京麦 Tab 分组)</h4>
<div v-for="group in attrGroups" :key="group.groupName" class="attr-group">
<h5>{{ group.groupName }}</h5>
<table>
<tbody>
<tr v-for="(attr, index) in group.attrs" :key="`${attr.key}-${index}`">
<th>{{ attr.label || attr.key || '-' }}</th>
<td>{{ attr.value || '-' }}</td>
</tr>
</tbody>
</table>
</div>
</section>
<section v-else-if="attrJsonRaw" class="detail-block">
<h4>属性(原始 JSON)</h4>
<pre class="attr-raw">{{ attrJsonRaw }}</pre>
</section>
<section class="detail-block">
<h4>销售属性 / SKU 矩阵</h4>
<template v-if="skus.length">
<p class="sku-summary">{{ skuSummary }}</p>
<div class="sku-table-wrap">
<table class="sku-table">
<thead>
<tr>
<th>序号</th>
<th v-for="dimension in skuDimensions" :key="dimension">{{ dimension }}</th>
<th>京东价</th>
<th>库存</th>
<th>SKU ID</th>
<th>SKU 名称</th>
</tr>
</thead>
<tbody>
<tr v-for="(sku, index) in skus" :key="sku.skuId">
<td>{{ sku.skuSeq || index + 1 }}</td>
<td v-for="dimension in skuDimensions" :key="dimension">{{ skuAttrValue(sku, dimension) }}</td>
<td>{{ sku.price ? `¥${sku.price}` : '-' }}</td>
<td>{{ dash(sku.stock) }}</td>
<td class="sku-id">{{ sku.skuId }}</td>
<td :title="sku.skuName">{{ dash(sku.skuName) }}</td>
</tr>
</tbody>
</table>
</div>
</template>
<p v-else class="drawer-tip">暂无 SKU 矩阵(V198 之前采集的旧行需重新采集)</p>
</section>
<section class="detail-block">
<h4>主图</h4>
<div v-if="mainImages.length" class="image-grid">
<img
v-for="image in mainImages"
:key="image.url"
:src="image.url"
alt=""
title="点击放大预览"
@click="openPreview(image.url)"
/>
</div>
<p v-else class="drawer-tip">暂无主图</p>
</section>
<section v-for="bucket in attrImageGroups" :key="bucket.attrName" class="detail-block">
<h4>属性图 · {{ bucket.attrName }}</h4>
<div class="image-grid">
<img
v-for="image in bucket.list"
:key="image.url"
:src="image.url"
alt=""
title="点击放大预览"
@click="openPreview(image.url)"
/>
</div>
</section>
<section class="detail-block">
<h4>详情图</h4>
<div v-if="detailImages.length" class="image-grid">
<img
v-for="image in detailImages"
:key="image.url"
:src="image.url"
alt=""
title="点击放大预览"
@click="openPreview(image.url)"
/>
</div>
<p v-else class="drawer-tip">暂无详情图</p>
</section>
<section v-if="otherImages.length" class="detail-block">
<h4>其他图</h4>
<div class="image-grid">
<img
v-for="image in otherImages"
:key="image.url"
:src="image.url"
alt=""
title="点击放大预览"
@click="openPreview(image.url)"
/>
</div>
</section>
</template>
</div>
</aside>
<div v-if="previewVisible" class="modal-mask" @click.self="previewVisible = false">
<div class="preview-modal">
<div class="preview-head">
<h3>图片预览</h3>
<button class="drawer-close" type="button" aria-label="关闭" @click="previewVisible = false">×</button>
</div>
<img class="preview-image" :src="previewUrl" alt="" />
<div class="preview-foot">
<a :href="previewUrl" target="_blank" rel="noopener">新标签打开原图</a>
<button class="btn btn-secondary" type="button" @click="previewVisible = false">关闭</button>
</div>
</div>
</div>
</div>
</template>
<style scoped>
.drawer-mask {
position: fixed;
inset: 0;
z-index: 2600;
display: flex;
justify-content: flex-end;
background: rgba(45, 66, 86, 0.36);
backdrop-filter: blur(4px);
}
.drawer {
display: flex;
flex-direction: column;
width: min(920px, 100%);
height: 100%;
background: #ffffff;
box-shadow: -18px 0 40px rgba(39, 67, 94, 0.18);
}
.drawer-head {
display: flex;
align-items: center;
justify-content: space-between;
padding: 16px 20px;
border-bottom: 1px solid #dbe5ee;
}
.drawer-head h3 {
margin: 0;
font-size: 15px;
font-weight: 650;
color: #24384d;
}
.drawer-close {
width: 30px;
height: 30px;
border: 1px solid transparent;
border-radius: 9px;
background: none;
color: #5b6f83;
font-size: 20px;
line-height: 1;
cursor: pointer;
}
.drawer-close:hover {
border-color: #cbd9e6;
background: #eef4fa;
color: #24384d;
}
.drawer-body {
flex: 1;
min-height: 0;
overflow-y: auto;
padding: 16px 20px 28px;
display: flex;
flex-direction: column;
gap: 18px;
}
.drawer-tip {
margin: 0;
color: #8293a5;
font-size: 13.5px;
}
.drawer-failure {
display: flex;
align-items: center;
gap: 12px;
padding: 12px 14px;
border: 1px solid #f0c9c2;
border-radius: 10px;
background: #fdf1ef;
color: #a04432;
font-size: 13.5px;
}
.detail-block h4 {
margin: 0 0 10px;
font-size: 13.5px;
font-weight: 650;
color: #4e6479;
}
.info-grid {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 10px 16px;
margin: 0;
}
.info-item {
min-width: 0;
}
.info-item-wide {
grid-column: 1 / -1;
}
.info-item dt {
color: #5b6f83;
font-size: 12px;
margin-bottom: 3px;
}
.info-item dd {
margin: 0;
color: #24384d;
font-size: 13.5px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.attr-group {
margin-bottom: 12px;
}
.attr-group h5 {
margin: 0 0 6px;
color: #5b6f83;
font-size: 12.5px;
}
.attr-group table {
width: 100%;
border-collapse: collapse;
border: 1px solid #dbe5ee;
border-radius: 8px;
overflow: hidden;
}
.attr-group th,
.attr-group td {
padding: 7px 10px;
border-bottom: 1px solid #e7eef5;
font-size: 13px;
text-align: left;
vertical-align: top;
}
.attr-group th {
width: 180px;
background: #f2f7fb;
color: #4e6479;
font-weight: 600;
}
.attr-group tr:last-child th,
.attr-group tr:last-child td {
border-bottom: 0;
}
.attr-raw {
margin: 0;
padding: 10px 12px;
border: 1px solid #dbe5ee;
border-radius: 8px;
background: #f8fbfd;
color: #24384d;
font-size: 12px;
white-space: pre-wrap;
word-break: break-all;
max-height: 240px;
overflow-y: auto;
}
.sku-summary {
margin: 0 0 8px;
color: #5b6f83;
font-size: 12.5px;
}
.sku-table-wrap {
overflow-x: auto;
border: 1px solid #dbe5ee;
border-radius: 8px;
}
.sku-table {
width: 100%;
border-collapse: collapse;
font-size: 12.5px;
white-space: nowrap;
}
.sku-table th,
.sku-table td {
padding: 7px 10px;
border-bottom: 1px solid #e7eef5;
text-align: left;
color: #24384d;
}
.sku-table th {
background: #f2f7fb;
color: #4e6479;
font-weight: 600;
}
.sku-table tbody tr:last-child td {
border-bottom: 0;
}
.sku-table .sku-id {
font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
font-size: 12px;
color: #4e6479;
}
.image-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
gap: 10px;
}
.image-grid img {
display: block;
width: 100%;
aspect-ratio: 1;
object-fit: cover;
border: 1px solid #d8e3ee;
border-radius: 8px;
background: #f2f7fb;
cursor: zoom-in;
}
.modal-mask {
position: fixed;
inset: 0;
z-index: 3000;
display: flex;
align-items: center;
justify-content: center;
padding: 40px 20px;
background: rgba(45, 66, 86, 0.36);
backdrop-filter: blur(6px);
}
.preview-modal {
display: flex;
flex-direction: column;
width: min(880px, 100%);
max-height: 88vh;
padding: 16px 20px;
border: 1px solid #c7d7e5;
border-radius: 14px;
background: #ffffff;
box-shadow: 0 22px 60px rgba(39, 67, 94, 0.2);
}
.preview-head {
display: flex;
align-items: center;
justify-content: space-between;
}
.preview-head h3 {
margin: 0;
font-size: 15px;
color: #24384d;
}
.preview-image {
display: block;
flex: 1;
min-height: 0;
max-height: 68vh;
margin: 12px 0;
object-fit: contain;
background: #f2f7fb;
border-radius: 8px;
}
.preview-foot {
display: flex;
align-items: center;
justify-content: space-between;
}
.preview-foot a {
color: #2f5d8b;
font-size: 13px;
}
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
min-height: 38px;
padding: 7px 16px;
border-radius: 9px;
font-size: 13px;
cursor: pointer;
}
.btn-secondary {
background: #ffffff;
color: #5b6f83;
border: 1px solid #c7d7e5;
}
.btn-secondary:hover {
color: #2f5d8b;
border-color: #95b1cb;
background: #edf5fb;
}
</style>