Files
shanxia2.0/web-admin/src/utils/crm-product-display.ts
T
2026-06-23 23:30:57 +08:00

79 lines
2.5 KiB
TypeScript

type TagType = 'success' | 'warning' | 'danger' | 'info' | '' | 'primary'
export interface ProductDictResolver {
getLabel: (dictType: string, value: string) => string
getOptions: (dictType: string) => Array<{ label: string; value: string; tagType: string }>
}
const CRM_PRODUCT_STATUS = 'crm_product_status'
/** 产品状态:库内 on/off;兼容历史 1/0 */
export function productStatusDictKey(status: string | number | null | undefined): string {
const s = String(status ?? '')
if (s === '1') return 'on'
if (s === '0') return 'off'
return s
}
/** 表单/提交用:统一为 on/off */
export function normalizeProductStatusValue(status: string | number | null | undefined): string {
const s = String(status ?? 'on')
if (s === '1' || s === 'on') return 'on'
if (s === '0' || s === 'off') return 'off'
return 'on'
}
function findProductStatusItem(
resolver: ProductDictResolver,
status: string | number | null | undefined
) {
const raw = String(status ?? '')
if (!raw) return undefined
const mapped = productStatusDictKey(status)
const items = resolver.getOptions(CRM_PRODUCT_STATUS)
return items.find((i) => i.value === raw) ?? items.find((i) => i.value === mapped)
}
export function getProductStatusLabel(
resolver: ProductDictResolver,
status: string | number | null | undefined
): string {
const item = findProductStatusItem(resolver, status)
if (item) return item.label
const key = productStatusDictKey(status)
if (key === 'on') return '上架'
if (key === 'off') return '下架'
return '—'
}
export function getProductStatusTagType(
resolver: ProductDictResolver,
status: string | number | null | undefined
): TagType {
const item = findProductStatusItem(resolver, status)
if (item?.tagType) return item.tagType as TagType
const key = productStatusDictKey(status)
if (key === 'on') return 'success'
if (key === 'off') return 'danger'
return 'info'
}
export function getProductUnitLabel(
getLabel: (dictType: string, value: string) => string,
unit: string | null | undefined
): string {
if (!unit) return '—'
return getLabel('crm_product_unit', unit) || unit
}
/** CRM 计量单位「吨」的字典值及历史直存文案 */
export const TON_PRODUCT_UNIT_VALUES = new Set(['dun', '吨'])
/** 地磅过磅单位为吨;空单位按业务默认视为吨 */
export function isTonProductUnit(unit: string | null | undefined): boolean {
const code = (unit || '').trim()
if (!code) return true
return TON_PRODUCT_UNIT_VALUES.has(code)
}