feat(phase-3): 京东商品卡片组件——主图/店铺徽标/标题/价格/编码复制/采集日期/勾选(additive,防冒泡钉死)
This commit is contained in:
@@ -0,0 +1,204 @@
|
||||
<script setup lang="ts">
|
||||
/** 京东商品卡片(v1.6 蓝图 Phase 3,additive——页面接线随 Phase 5):
|
||||
* 主图(无图占位)/店铺徽标/标题/价格区间/商品编码+复制/采集日期/勾选框,
|
||||
* 字段映射与现表格「商品信息」列逐字段同源(价格走 jdPriceRange、日期走 formatDateTime)。
|
||||
*
|
||||
* 纯展示组件:入参=item+选中态+可勾选档;出事件=切换勾选、打开详情、复制编码。
|
||||
* 卡片整体点击=开详情;勾选与复制 @click.stop 防冒泡(与现表格行 @click.stop 同形)。
|
||||
* healthScore 徽标随图渲染(现表格同款「信息分」),null 不渲染。 */
|
||||
import { formatDateTime } from '@/utils/datetime'
|
||||
import { jdPriceRange, type JdProductItem } from './jd-product-model.ts'
|
||||
|
||||
const props = defineProps<{
|
||||
/** 列表行(解析层归一后的 JdProductItem)。 */
|
||||
item: JdProductItem
|
||||
/** 勾选态(页面持有,跨页累计记忆)。 */
|
||||
selected?: boolean
|
||||
/** 可勾选档:管理员级/本组可写分类账号才渲染勾选框(页面按 canWriteProducts∨canManageCategories 投影)。 */
|
||||
selectable?: boolean
|
||||
}>()
|
||||
|
||||
const emit = defineEmits<{
|
||||
/** 勾选切换(checked 来自 checkbox 原生 change)。 */
|
||||
(event: 'toggle-select', checked: boolean): void
|
||||
/** 打开详情(卡片整体点击;详情弹窗/抽屉由页面渲染)。 */
|
||||
(event: 'open'): void
|
||||
/** 复制商品编码(剪贴板与回执文案在页面,与现表格 copyText 同一条链)。 */
|
||||
(event: 'copy'): void
|
||||
}>()
|
||||
|
||||
const priceText = (): string => jdPriceRange(props.item) || '-'
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<article class="jd-card" :class="{ selected: props.selected }" @click="emit('open')">
|
||||
<label v-if="props.selectable" class="jd-card-check" @click.stop>
|
||||
<input
|
||||
type="checkbox"
|
||||
:checked="props.selected"
|
||||
aria-label="勾选商品"
|
||||
@change="emit('toggle-select', ($event.target as HTMLInputElement).checked)"
|
||||
/>
|
||||
</label>
|
||||
<div class="jd-card-figure">
|
||||
<img v-if="props.item.mainImageUrl" class="jd-card-thumb" :src="props.item.mainImageUrl" alt="" loading="lazy" />
|
||||
<span v-else class="jd-card-thumb jd-card-thumb-empty">无图</span>
|
||||
<span v-if="props.item.healthScore !== null" class="jd-card-health">信息分 {{ props.item.healthScore }}</span>
|
||||
<span v-if="props.item.shopName" class="jd-card-shop">{{ props.item.shopName }}</span>
|
||||
</div>
|
||||
<div class="jd-card-body">
|
||||
<div class="jd-card-title" :title="props.item.title">{{ props.item.title || '-' }}</div>
|
||||
<div class="jd-card-price">{{ priceText() }}</div>
|
||||
<div class="jd-card-meta">
|
||||
<span class="jd-card-code" :title="props.item.productCode">商品编码:{{ props.item.productCode || '-' }}</span>
|
||||
<button
|
||||
v-if="props.item.productCode"
|
||||
class="jd-card-copy"
|
||||
type="button"
|
||||
title="复制商品编码"
|
||||
@click.stop="emit('copy')"
|
||||
>⧉</button>
|
||||
</div>
|
||||
<div class="jd-card-date">采集:{{ formatDateTime(props.item.syncedAt || '') }}</div>
|
||||
</div>
|
||||
</article>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.jd-card {
|
||||
position: relative;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
background: #fff;
|
||||
border: 1px solid #e4e7ed;
|
||||
border-radius: 8px;
|
||||
overflow: hidden;
|
||||
cursor: pointer;
|
||||
transition: box-shadow 0.15s, border-color 0.15s;
|
||||
}
|
||||
|
||||
.jd-card:hover {
|
||||
border-color: #c6e2ff;
|
||||
box-shadow: 0 2px 12px rgba(64, 158, 255, 0.12);
|
||||
}
|
||||
|
||||
.jd-card.selected {
|
||||
border-color: #409eff;
|
||||
box-shadow: 0 0 0 1px #409eff inset;
|
||||
}
|
||||
|
||||
.jd-card-check {
|
||||
position: absolute;
|
||||
top: 8px;
|
||||
left: 8px;
|
||||
z-index: 2;
|
||||
line-height: 1;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.jd-card-check input {
|
||||
width: 15px;
|
||||
height: 15px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.jd-card-figure {
|
||||
position: relative;
|
||||
aspect-ratio: 1 / 1;
|
||||
background: #f5f7fa;
|
||||
}
|
||||
|
||||
.jd-card-thumb {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.jd-card-thumb-empty {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
color: #c0c4cc;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.jd-card-health {
|
||||
position: absolute;
|
||||
top: 8px;
|
||||
right: 8px;
|
||||
background: rgba(255, 255, 255, 0.92);
|
||||
color: #e6a23c;
|
||||
font-size: 12px;
|
||||
border-radius: 10px;
|
||||
padding: 1px 8px;
|
||||
}
|
||||
|
||||
.jd-card-shop {
|
||||
position: absolute;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
max-width: 100%;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
background: rgba(48, 49, 51, 0.62);
|
||||
color: #fff;
|
||||
font-size: 12px;
|
||||
padding: 3px 8px;
|
||||
}
|
||||
|
||||
.jd-card-body {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 6px;
|
||||
padding: 10px 12px 12px;
|
||||
}
|
||||
|
||||
.jd-card-title {
|
||||
font-size: 13px;
|
||||
color: #303133;
|
||||
line-height: 1.4;
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 2;
|
||||
-webkit-box-orient: vertical;
|
||||
overflow: hidden;
|
||||
min-height: 2.8em;
|
||||
}
|
||||
|
||||
.jd-card-price {
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
color: #f56c6c;
|
||||
}
|
||||
|
||||
.jd-card-meta {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
font-size: 12px;
|
||||
color: #606266;
|
||||
}
|
||||
|
||||
.jd-card-code {
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.jd-card-copy {
|
||||
flex-shrink: 0;
|
||||
border: none;
|
||||
background: transparent;
|
||||
cursor: pointer;
|
||||
color: #409eff;
|
||||
font-size: 13px;
|
||||
padding: 0 2px;
|
||||
}
|
||||
|
||||
.jd-card-date {
|
||||
font-size: 12px;
|
||||
color: #909399;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* 京东商品卡片(JdProductCard,v1.6 蓝图 Phase 3)的挂载契约。
|
||||
*
|
||||
* 纯展示组件(零 api 依赖):字段渲染与现表格「商品信息」列同源(价格走 jdPriceRange、
|
||||
* 日期走 formatDateTime);卡片点击=开详情,勾选/复制 @click.stop 防冒泡——这两处
|
||||
* 防冒泡是行为红线(点了复制却弹详情=误开弹窗),逐一钉死。
|
||||
*
|
||||
* 跑法:`npx vitest run tests/mount/jd-product-card.mount.test.ts`
|
||||
*/
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { mount } from '@vue/test-utils'
|
||||
|
||||
import JdProductCard from '@/pages/jd/JdProductCard.vue'
|
||||
import type { JdProductItem } from '@/pages/jd/jd-product-model.ts'
|
||||
|
||||
/** 行夹具:价格用 number 形状(2026-11-16 实录:BigDecimal 序列化成 JSON number,
|
||||
* 字符串夹具会藏住「解析层丢数字形状」的真红);字段与页面 mount 测试同源。 */
|
||||
function item(over: Partial<JdProductItem> = {}): JdProductItem {
|
||||
return {
|
||||
id: 1,
|
||||
shopId: 3,
|
||||
shopName: '旗舰自营店',
|
||||
productCode: 'JD-10086',
|
||||
skuId: 'SKU-1',
|
||||
itemNum: 'ITEM-1',
|
||||
skuCount: 12,
|
||||
healthScore: 81,
|
||||
title: '304不锈钢保温杯大容量便携',
|
||||
brand: '锅宝',
|
||||
categoryPath: '家居 > 厨具 > 保温杯',
|
||||
priceMin: '12.90',
|
||||
priceMax: '15.90',
|
||||
stockTotal: 500,
|
||||
saleStatus: '上架中',
|
||||
listTab: 'search',
|
||||
jdCreatedAt: '2026-11-07 18:49:47',
|
||||
jdOnlineAt: '2026-11-12 15:03:37',
|
||||
jdUpdatedAt: '2026-11-13 14:06:07',
|
||||
mainImageUrl: 'https://img.example/main-1.jpg',
|
||||
attrCount: 6,
|
||||
imageCount: 9,
|
||||
groupId: 7,
|
||||
groupName: '甲组',
|
||||
syncedAt: '2026-11-12 09:00:00',
|
||||
...over,
|
||||
} as JdProductItem
|
||||
}
|
||||
|
||||
describe('卡片字段渲染(与现表格「商品信息」列同源)', () => {
|
||||
it('主图/店铺徽标/标题/价格区间/商品编码/采集日期逐字段上卡', () => {
|
||||
const wrapper = mount(JdProductCard, { props: { item: item(), selected: false, selectable: true } })
|
||||
const img = wrapper.get('img.jd-card-thumb')
|
||||
expect(img.attributes('src')).toBe('https://img.example/main-1.jpg')
|
||||
expect(wrapper.get('.jd-card-shop').text()).toBe('旗舰自营店')
|
||||
expect(wrapper.get('.jd-card-title').text()).toBe('304不锈钢保温杯大容量便携')
|
||||
expect(wrapper.get('.jd-card-price').text()).toBe('12.9 ~ 15.9', '价格走 jdPriceRange(number/字符串都收、去尾零)')
|
||||
expect(wrapper.get('.jd-card-code').text()).toContain('JD-10086')
|
||||
expect(wrapper.get('.jd-card-date').text()).toContain('2026-11-12 09:00:00', '采集日期走 formatDateTime')
|
||||
expect(wrapper.get('.jd-card-health').text()).toBe('信息分 81', 'healthScore 徽标随图渲染')
|
||||
wrapper.unmount()
|
||||
})
|
||||
|
||||
it('无图占位:缺 mainImageUrl 出「无图」占位不出 img 元素;缺价与缺码按占位符兜底', () => {
|
||||
const wrapper = mount(JdProductCard, {
|
||||
props: { item: item({ mainImageUrl: '', priceMin: '', priceMax: '', productCode: '', syncedAt: '', healthScore: null }) },
|
||||
})
|
||||
expect(wrapper.find('img.jd-card-thumb').exists()).toBe(false)
|
||||
expect(wrapper.get('.jd-card-thumb-empty').text()).toBe('无图')
|
||||
expect(wrapper.get('.jd-card-price').text()).toBe('-')
|
||||
expect(wrapper.get('.jd-card-code').text()).toContain('-')
|
||||
expect(wrapper.get('.jd-card-date').text()).toContain('—', 'formatDateTime 空值回 em dash 兜底')
|
||||
expect(wrapper.find('.jd-card-copy').exists()).toBe(false, '缺编码不渲染复制按钮(现表格同形)')
|
||||
expect(wrapper.find('.jd-card-health').exists()).toBe(false, 'healthScore null 不渲染徽标')
|
||||
wrapper.unmount()
|
||||
})
|
||||
})
|
||||
|
||||
describe('交互事件(防冒泡是行为红线)', () => {
|
||||
it('点卡片整体 emit open(详情入口);点复制按钮只 emit copy 不冒泡开详情', async () => {
|
||||
const wrapper = mount(JdProductCard, { props: { item: item() } })
|
||||
await wrapper.get('.jd-card').trigger('click')
|
||||
expect(wrapper.emitted('open')).toHaveLength(1)
|
||||
|
||||
await wrapper.get('.jd-card-copy').trigger('click')
|
||||
expect(wrapper.emitted('copy')).toHaveLength(1, '复制按钮 emit copy(页面走剪贴板与回执)')
|
||||
expect(wrapper.emitted('open')?.length).toBe(1, '@click.stop 防冒泡:复制不得误开详情')
|
||||
wrapper.unmount()
|
||||
})
|
||||
|
||||
it('可勾选档渲染勾选框:change emit toggle-select;点击勾选区不冒泡开详情', async () => {
|
||||
const wrapper = mount(JdProductCard, { props: { item: item(), selected: false, selectable: true } })
|
||||
const box = wrapper.get('input[type="checkbox"]')
|
||||
await box.setValue(true)
|
||||
expect(wrapper.emitted('toggle-select')).toEqual([[true]])
|
||||
await box.setValue(false)
|
||||
expect(wrapper.emitted('toggle-select')?.at(-1)).toEqual([false])
|
||||
// 勾选区(label @click.stop)点击不触发卡片 open。
|
||||
await wrapper.get('.jd-card-check').trigger('click')
|
||||
expect(wrapper.emitted('open')).toBeFalsy()
|
||||
wrapper.unmount()
|
||||
})
|
||||
|
||||
it('不可勾选档不渲染勾选框(普通账号/纯只读行集);selected 高亮类由外部选中态控制', () => {
|
||||
const plain = mount(JdProductCard, { props: { item: item() } })
|
||||
expect(plain.find('input[type="checkbox"]').exists()).toBe(false)
|
||||
expect(plain.get('.jd-card').classes()).not.toContain('selected')
|
||||
plain.unmount()
|
||||
|
||||
const picked = mount(JdProductCard, { props: { item: item(), selected: true, selectable: true } })
|
||||
expect(picked.get('.jd-card').classes()).toContain('selected')
|
||||
expect(picked.get('input[type="checkbox"]').element.checked).toBe(true)
|
||||
picked.unmount()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user