335 lines
12 KiB
TypeScript
335 lines
12 KiB
TypeScript
/** 京东数据库列表查询模型(v1.4 Phase 5):解析 JdProductPageVo(camel),纯逻辑零依赖。
|
||
* camel 为正、snake 容错(与外观专利/去重页模型同形),缺 id 弃行。
|
||
* 纯函数、无框架依赖(envelope 只借纯 unwrap),node --test 可直测。 */
|
||
import { unwrap } from '../../api/envelope.ts'
|
||
|
||
/** 京东商品列表行(后端 JdProductItemVo,camel 为正)。 */
|
||
export interface JdProductItem {
|
||
id: number
|
||
shopId: number | null
|
||
shopName: string
|
||
productCode: string
|
||
skuId: string
|
||
/** 货号(京麦后台「货号」列,与 SKU 不是同一个值)。 */
|
||
itemNum: string
|
||
/** SKU 数(京麦后台「12个SKU」列)。 */
|
||
skuCount: number | null
|
||
/** 信息分(京麦后台「信息分」列)。 */
|
||
healthScore: number | null
|
||
title: string
|
||
brand: string
|
||
categoryPath: string
|
||
priceMin: string
|
||
priceMax: string
|
||
stockTotal: number | null
|
||
/** 上架中/已下架/审核中/草稿。 */
|
||
saleStatus: string
|
||
listTab: string
|
||
jdCreatedAt: string
|
||
/** 上架时间(京麦后台「上架时间」列,与创建时间不是同一个值)。 */
|
||
jdOnlineAt: string
|
||
jdUpdatedAt: string
|
||
mainImageUrl: string
|
||
attrCount: number | null
|
||
imageCount: number | null
|
||
groupId: number | null
|
||
groupName: string
|
||
syncedAt?: string
|
||
}
|
||
|
||
export interface JdProductPageResult {
|
||
items: JdProductItem[]
|
||
total: number
|
||
page: number
|
||
pageSize: number
|
||
}
|
||
|
||
/** 前端首屏默认容量(与 filter 侧 JD_PRODUCT_PAGE_SIZE 同值 20)。 */
|
||
export const JD_PRODUCT_DEFAULT_SIZE = 20
|
||
|
||
export function emptyJdProductPage(): JdProductPageResult {
|
||
return { items: [], total: 0, page: 1, pageSize: JD_PRODUCT_DEFAULT_SIZE }
|
||
}
|
||
|
||
function text(value: unknown): string {
|
||
return typeof value === 'string' ? value.trim() : ''
|
||
}
|
||
|
||
function numberOrNull(value: unknown): number | null {
|
||
return typeof value === 'number' && Number.isFinite(value) ? Math.floor(value) : null
|
||
}
|
||
|
||
/** 价格区间展示:min/max 都空为 -,只空一侧给单值。 */
|
||
export function jdPriceRange(item: Pick<JdProductItem, 'priceMin' | 'priceMax'>): string {
|
||
const min = (item.priceMin || '').trim()
|
||
const max = (item.priceMax || '').trim()
|
||
if (min && max) return min === max ? min : `${min} ~ ${max}`
|
||
return min || max || ''
|
||
}
|
||
|
||
/** 解析单行;缺 id 视为无效。camel 为正、snake 容错。 */
|
||
export function toJdProductItem(raw: unknown): JdProductItem | null {
|
||
if (!raw || typeof raw !== 'object') return null
|
||
const record = raw as Record<string, unknown>
|
||
const id = numberOrNull(record.id)
|
||
if (id === null) return null
|
||
return {
|
||
id,
|
||
shopId: numberOrNull(record.shopId ?? record.shop_id),
|
||
shopName: text(record.shopName ?? record.shop_name),
|
||
productCode: text(record.productCode ?? record.product_code),
|
||
skuId: text(record.skuId ?? record.sku_id),
|
||
itemNum: text(record.itemNum ?? record.item_num),
|
||
skuCount: numberOrNull(record.skuCount ?? record.sku_count),
|
||
healthScore: numberOrNull(record.healthScore ?? record.health_score),
|
||
title: text(record.title),
|
||
brand: text(record.brand),
|
||
categoryPath: text(record.categoryPath ?? record.category_path),
|
||
priceMin: text(record.priceMin ?? record.price_min),
|
||
priceMax: text(record.priceMax ?? record.price_max),
|
||
stockTotal: numberOrNull(record.stockTotal ?? record.stock_total),
|
||
saleStatus: text(record.saleStatus ?? record.sale_status),
|
||
listTab: text(record.listTab ?? record.list_tab),
|
||
jdCreatedAt: text(record.jdCreatedAt ?? record.jd_created_at),
|
||
jdOnlineAt: text(record.jdOnlineAt ?? record.jd_online_at),
|
||
jdUpdatedAt: text(record.jdUpdatedAt ?? record.jd_updated_at),
|
||
mainImageUrl: text(record.mainImageUrl ?? record.main_image_url),
|
||
attrCount: numberOrNull(record.attrCount ?? record.attr_count),
|
||
imageCount: numberOrNull(record.imageCount ?? record.image_count),
|
||
groupId: numberOrNull(record.groupId ?? record.group_id),
|
||
groupName: text(record.groupName ?? record.group_name),
|
||
syncedAt: text(record.syncedAt ?? record.synced_at) || undefined,
|
||
}
|
||
}
|
||
|
||
/** 归一化分页负载(信封或已解包 VO)为前端结果;缺省字段回默认。 */
|
||
export function parseJdProductPage(payload: unknown): JdProductPageResult {
|
||
const out = emptyJdProductPage()
|
||
const core = unwrap<unknown>(payload)
|
||
if (!core || typeof core !== 'object') return out
|
||
const record = core as Record<string, unknown>
|
||
if (Array.isArray(record.items)) {
|
||
out.items = record.items
|
||
.map((raw) => toJdProductItem(raw))
|
||
.filter((item): item is JdProductItem => item !== null)
|
||
}
|
||
if (typeof record.total === 'number') out.total = Math.floor(record.total)
|
||
if (typeof record.page === 'number' && record.page >= 1) out.page = Math.floor(record.page)
|
||
const rawSize = record.pageSize ?? record.page_size
|
||
if (typeof rawSize === 'number' && rawSize >= 1) out.pageSize = Math.floor(rawSize)
|
||
return out
|
||
}
|
||
|
||
/** 详情属性块:attrGroups[{groupName, attrs:[{key,label,value}]}]。 */
|
||
export interface JdProductAttrEntry {
|
||
key: string
|
||
label: string
|
||
value: string
|
||
}
|
||
|
||
export interface JdProductAttrGroup {
|
||
groupName: string
|
||
attrs: JdProductAttrEntry[]
|
||
}
|
||
|
||
/** 详情图片:images[{imageType, attrName, sortNo, url, byteSize, width, height}]。 */
|
||
export interface JdProductImage {
|
||
imageType: string
|
||
attrName: string
|
||
sortNo: number
|
||
url: string
|
||
byteSize: number | null
|
||
width: number | null
|
||
height: number | null
|
||
}
|
||
|
||
/** 销售属性组合的一项(颜色=上衣 / 尺码=S,京麦详情页「销售属性」)。 */
|
||
export interface JdProductSkuAttr {
|
||
name: string
|
||
value: string
|
||
}
|
||
|
||
/** 详情 SKU 行(后端 JdProductDetailVo.SkuVo,V198;京麦 12 行矩阵的一行)。 */
|
||
export interface JdProductSku {
|
||
skuId: string
|
||
skuName: string
|
||
/** 商家自定义序号(京麦 skuSeq)。 */
|
||
skuSeq: string
|
||
/** 京东价(后端 BigDecimal,前端按文本展示,不做金额运算)。 */
|
||
price: string
|
||
stock: number | null
|
||
saleAttrs: JdProductSkuAttr[]
|
||
sortNo: number
|
||
}
|
||
|
||
export interface JdProductDetail {
|
||
item: JdProductItem
|
||
attrGroups: JdProductAttrGroup[]
|
||
images: JdProductImage[]
|
||
/** SKU 矩阵(京麦详情页「销售属性」区,V198;旧数据为空数组)。 */
|
||
skus: JdProductSku[]
|
||
/** attrGroups 解析失败时的原始 JSON 兜底(界面原样展示,不静默吞)。 */
|
||
attrJsonRaw: string
|
||
}
|
||
|
||
/** 归一化 attrGroups(camel/snake 同级容错),非法项跳过、块名缺省归「属性」。 */
|
||
function toAttrGroups(raw: unknown): JdProductAttrGroup[] {
|
||
if (!Array.isArray(raw)) return []
|
||
const out: JdProductAttrGroup[] = []
|
||
for (const groupRaw of raw) {
|
||
if (!groupRaw || typeof groupRaw !== 'object') continue
|
||
const group = groupRaw as Record<string, unknown>
|
||
const groupName = text(group.groupName ?? group.group_name) || '属性'
|
||
const attrsRaw = group.attrs
|
||
if (!Array.isArray(attrsRaw)) continue
|
||
const attrs: JdProductAttrEntry[] = []
|
||
for (const attrRaw of attrsRaw) {
|
||
if (!attrRaw || typeof attrRaw !== 'object') continue
|
||
const attr = attrRaw as Record<string, unknown>
|
||
attrs.push({
|
||
key: text(attr.key),
|
||
label: text(attr.label),
|
||
value: text(attr.value),
|
||
})
|
||
}
|
||
out.push({ groupName, attrs })
|
||
}
|
||
return out
|
||
}
|
||
|
||
/** 归一化 images;缺 url 弃行(界面没有可展示/可打开的目标),sortNo 缺省 0。 */
|
||
function toImages(raw: unknown): JdProductImage[] {
|
||
if (!Array.isArray(raw)) return []
|
||
const out: JdProductImage[] = []
|
||
for (const imageRaw of raw) {
|
||
if (!imageRaw || typeof imageRaw !== 'object') continue
|
||
const image = imageRaw as Record<string, unknown>
|
||
const url = text(image.url)
|
||
if (!url) continue
|
||
out.push({
|
||
imageType: text(image.imageType ?? image.image_type),
|
||
attrName: text(image.attrName ?? image.attr_name),
|
||
sortNo: numberOrNull(image.sortNo ?? image.sort_no) ?? 0,
|
||
url,
|
||
byteSize: numberOrNull(image.byteSize ?? image.byte_size),
|
||
width: numberOrNull(image.width),
|
||
height: numberOrNull(image.height),
|
||
})
|
||
}
|
||
return out
|
||
}
|
||
|
||
/** 数值/字符串都收(后端 BigDecimal 序列化成 JSON number),去尾零后展示:
|
||
* 京麦详情页写 `162`,不给 `162.00`;数字形状之外的原文照原样回(不吞异常值)。 */
|
||
function decimalText(value: unknown): string {
|
||
if (typeof value === 'number' && Number.isFinite(value)) {
|
||
return String(Math.round(value * 100) / 100)
|
||
}
|
||
const raw = text(value)
|
||
if (!/^\d+(?:\.\d+)?$/.test(raw)) return raw
|
||
return String(Math.round(Number(raw) * 100) / 100)
|
||
}
|
||
|
||
/** 归一化 SKU 矩阵(camel/snake 容错);缺 skuId 弃行(京麦每一行都以 skuId 为身份)。 */
|
||
function toSkus(raw: unknown): JdProductSku[] {
|
||
if (!Array.isArray(raw)) return []
|
||
const out: JdProductSku[] = []
|
||
for (const skuRaw of raw) {
|
||
if (!skuRaw || typeof skuRaw !== 'object') continue
|
||
const sku = skuRaw as Record<string, unknown>
|
||
const skuId = text(sku.skuId ?? sku.sku_id)
|
||
if (!skuId) continue
|
||
const saleAttrs: JdProductSkuAttr[] = []
|
||
const attrsRaw = sku.saleAttrs ?? sku.sale_attrs
|
||
if (Array.isArray(attrsRaw)) {
|
||
for (const attrRaw of attrsRaw) {
|
||
if (!attrRaw || typeof attrRaw !== 'object') continue
|
||
const attr = attrRaw as Record<string, unknown>
|
||
const value = text(attr.value)
|
||
if (!value) continue
|
||
saleAttrs.push({ name: text(attr.name) || '属性', value })
|
||
}
|
||
}
|
||
out.push({
|
||
skuId,
|
||
skuName: text(sku.skuName ?? sku.sku_name),
|
||
skuSeq: text(sku.skuSeq ?? sku.sku_seq),
|
||
price: decimalText(sku.price),
|
||
stock: numberOrNull(sku.stock),
|
||
saleAttrs,
|
||
sortNo: numberOrNull(sku.sortNo ?? sku.sort_no) ?? 0,
|
||
})
|
||
}
|
||
return out
|
||
}
|
||
|
||
/** 销售属性维度名(按首次出现顺序):京麦详情页「颜色 / 尺码」两列。 */
|
||
export function jdSkuDimensions(skus: JdProductSku[]): string[] {
|
||
const order: string[] = []
|
||
for (const sku of skus) {
|
||
for (const attr of sku.saleAttrs) {
|
||
if (!order.includes(attr.name)) order.push(attr.name)
|
||
}
|
||
}
|
||
return order
|
||
}
|
||
|
||
/** SKU 矩阵汇总:`12 个 SKU · ¥162 ~ ¥168 · 库存 2400`(缺项不硬凑)。 */
|
||
export function jdSkuSummary(skus: JdProductSku[]): string {
|
||
if (!skus.length) return ''
|
||
const prices = skus
|
||
.map((sku) => Number(sku.price))
|
||
.filter((value) => Number.isFinite(value) && value > 0)
|
||
const priceText = prices.length
|
||
? Math.min(...prices) === Math.max(...prices)
|
||
? `¥${Math.min(...prices)}`
|
||
: `¥${Math.min(...prices)} ~ ¥${Math.max(...prices)}`
|
||
: ''
|
||
const stocks = skus.map((sku) => sku.stock).filter((value): value is number => typeof value === 'number')
|
||
const stockText = stocks.length ? `库存 ${stocks.reduce((sum, value) => sum + value, 0)}` : ''
|
||
return [`${skus.length} 个 SKU`, priceText, stockText].filter(Boolean).join(' · ')
|
||
}
|
||
|
||
/** 解析详情负载(信封或已解包 VO);attrGroups 解析不出时回 attrJsonRaw 原文兜底。 */
|
||
export function parseJdProductDetail(payload: unknown): JdProductDetail {
|
||
const core = unwrap<unknown>(payload)
|
||
const record = core && typeof core === 'object' ? (core as Record<string, unknown>) : {}
|
||
const item = toJdProductItem(record) ?? {
|
||
id: 0,
|
||
shopId: null,
|
||
shopName: '',
|
||
productCode: '',
|
||
skuId: '',
|
||
itemNum: '',
|
||
skuCount: null,
|
||
healthScore: null,
|
||
title: '',
|
||
brand: '',
|
||
categoryPath: '',
|
||
priceMin: '',
|
||
priceMax: '',
|
||
stockTotal: null,
|
||
saleStatus: '',
|
||
listTab: '',
|
||
jdCreatedAt: '',
|
||
jdOnlineAt: '',
|
||
jdUpdatedAt: '',
|
||
mainImageUrl: '',
|
||
attrCount: null,
|
||
imageCount: null,
|
||
groupId: null,
|
||
groupName: '',
|
||
}
|
||
const attrGroups = toAttrGroups(record.attrGroups ?? record.attr_groups)
|
||
const attrJsonRaw = typeof record.attrJsonRaw === 'string'
|
||
? record.attrJsonRaw
|
||
: typeof record.attr_json_raw === 'string' ? record.attr_json_raw : ''
|
||
return {
|
||
item,
|
||
attrGroups,
|
||
images: toImages(record.images),
|
||
skus: toSkus(record.skus),
|
||
attrJsonRaw,
|
||
}
|
||
}
|