import type { ConsignmentIntakeField, ConsignmentIntakeFieldType, ConsignmentPricingConfig, ConsignmentSpecField, ConsignmentSpecFieldType, ConsignmentSpecValues, } from '@/types/consignment' /** * 旧版客服手工入库表单的选项常量。 * 新链路以「每个寄卖配置的 specs」为准,这里只给没有配置快照的旧数据兜底。 */ export const CONSIGNMENT_SYSTEM_OPTIONS = [ { label: '苹果', value: '苹果' }, { label: '安卓', value: '安卓' }, { label: '通用', value: '通用' }, ] as const export const CONSIGNMENT_ZONE_OPTIONS = [ { label: '微信区', value: '微信区' }, { label: 'QQ区', value: 'QQ区' }, { label: '互通(QQ/微信)', value: '互通' }, ] as const export const SPEC_FIELD_TYPE_OPTIONS: Array<{ label: string; value: ConsignmentSpecFieldType }> = [ { label: '文本', value: 'text' }, { label: '长文本', value: 'textarea' }, { label: '下拉', value: 'select' }, { label: '单选', value: 'radio' }, { label: '数字', value: 'number' }, { label: '日期', value: 'date' }, ] export function specFieldTypeLabel(type: ConsignmentSpecFieldType): string { return SPEC_FIELD_TYPE_OPTIONS.find((item) => item.value === type)?.label || '文本' } export function isOptionSpecField(field: Pick): boolean { return field.type === 'select' || field.type === 'radio' } export const INTAKE_FIELD_TYPE_OPTIONS: Array<{ label: string value: ConsignmentIntakeFieldType }> = [ { label: '文本', value: 'text' }, { label: '多行文本', value: 'textarea' }, { label: '手机号', value: 'phone' }, { label: '数字', value: 'number' }, { label: '图片', value: 'image' }, { label: '对接客服', value: 'contact_supporter' }, ] export function intakeFieldTypeLabel(type: ConsignmentIntakeFieldType): string { return INTAKE_FIELD_TYPE_OPTIONS.find((item) => item.value === type)?.label || '文本' } /** 与后端 DEFAULT_CONSIGNMENT_INTAKE_FIELDS 保持一致。 */ export const DEFAULT_CONSIGNMENT_INTAKE_FIELDS: ConsignmentIntakeField[] = [ { key: 'contactSupporterId', label: '对接客服', type: 'contact_supporter', required: true, placeholder: '请选择对接客服', helpText: '用于后续提成归属与联系', maxCount: 0, sortOrder: 10, }, { key: 'sellerContact', label: '微信号 / QQ号', type: 'text', required: true, placeholder: '方便派单与紧急联系', helpText: '', maxCount: 0, sortOrder: 20, }, { key: 'sellerPhone', label: '联系电话', type: 'phone', required: true, placeholder: '手机号', helpText: '默认使用当前接单账号绑定手机号', maxCount: 0, sortOrder: 30, }, { key: 'characterName', label: '游戏内角色名', type: 'text', required: true, placeholder: '局内加好友丢枪用', helpText: '', maxCount: 0, sortOrder: 40, }, { key: 'characterId', label: '角色数字ID', type: 'text', required: true, placeholder: '如 1089238472', helpText: '', maxCount: 0, sortOrder: 50, }, { key: 'screenshots', label: '游戏主页截图', type: 'image', required: true, placeholder: '', helpText: '须包含角色名、ID 及仓库/手枪详情,最多 4 张', maxCount: 4, sortOrder: 60, }, { key: 'sellerNote', label: '其他留言与补充', type: 'textarea', required: false, placeholder: '配件、子弹、涂装或在线时段等(选填)', helpText: '', maxCount: 0, sortOrder: 70, }, ] export function formatSpecValue( field: ConsignmentSpecField, value: unknown, options: { forItemName?: boolean } = {}, ): string { const raw = String(value ?? '').trim() if (!raw) return '' if (options.forItemName && (field.key === 'diamondLevel' || field.key === 'brillianceCount')) { const numeric = Number(raw) if (!Number.isFinite(numeric)) return raw if (field.key === 'brillianceCount' && numeric <= 0) return '' return String(Math.round(numeric)) } if (isOptionSpecField(field)) { const option = field.options.find((item) => item.value === raw) if (options.forItemName && field.key === 'serverZone' && field.wildcardValues.includes(raw)) { return '' } return option?.label || raw } return raw } export function buildSpecText( specs: ConsignmentSpecField[], values: ConsignmentSpecValues | undefined, ): string { if (!values) return '' return specs .slice() .sort((a, b) => a.sortOrder - b.sortOrder || a.key.localeCompare(b.key)) .map((field) => { const display = formatSpecValue(field, values[field.key]) return display ? `${field.label} ${display}` : '' }) .filter(Boolean) .join(' / ') } /** * 物品名模板里跟在占位符后面的固定单位。 * 数值字段为 0 时 formatSpecValue 返回空串,单位必须一并省略, * 否则会留下「2钻璀璨」这种没有数字的残句。 */ const ITEM_NAME_UNIT_SUFFIX: Record = { brillianceCount: '璀璨', diamondLevel: '钻', } function fillItemNameTemplate(template: string, tokens: Map): string { return template.replace( /\{([a-zA-Z][a-zA-Z0-9_]*)\}(璀璨|钻)?/g, (_, key: string, unit?: string) => { const value = tokens.get(key) || '' if (value) return unit ? `${value}${unit}` : value return ITEM_NAME_UNIT_SUFFIX[key] === unit ? '' : unit || '' }, ) } export function buildSpecItemName( template: string, specs: ConsignmentSpecField[], values: ConsignmentSpecValues | undefined, categoryName = '', ): string { if (!values) return categoryName const tokens = new Map() for (const field of specs) { tokens.set(field.key, formatSpecValue(field, values[field.key], { forItemName: true })) } tokens.set('category', categoryName) let result = String(template || '').trim() if (result) { result = fillItemNameTemplate(result, tokens) } else { result = specs .filter((field) => field.showInItemName) .map((field) => formatSpecValue(field, values[field.key], { forItemName: true })) .filter(Boolean) .join('') } const normalized = result.replace(/\s+/g, ' ').trim() return normalized || categoryName || '寄卖物品' } export type SpecPriceResult = { priceFen: number capFen: number templatePriceFen: number breakdown: Array<{ label: string; amountFen: number }> } /** 与后端 computeConsignmentPrice 保持同口径,仅用于配置面板预览。 */ export function computeSpecPrice( pricing: ConsignmentPricingConfig, templatePriceFen = 0, ): SpecPriceResult { const template = Math.max(0, Math.round(Number(templatePriceFen || 0))) const breakdown: Array<{ label: string; amountFen: number }> = [] let priceFen = 0 if (template > 0) { breakdown.push({ label: '接单模板价', amountFen: template }) if (pricing.priceType === 'template_minus') { const minusFen = Math.max(0, Math.round(Number(pricing.minusFen || 0))) priceFen = Math.max(0, template - minusFen) if (minusFen > 0) breakdown.push({ label: '固定下调', amountFen: -minusFen }) } else { const discount = Math.min(10, Math.max(0.1, Number(pricing.discount || 10))) priceFen = Math.round((template * discount) / 10) if (discount !== 10) { breakdown.push({ label: `${discount} 折`, amountFen: priceFen - template }) } } } priceFen = Math.max(0, Math.round(priceFen)) return { priceFen, capFen: priceFen, templatePriceFen: template, breakdown, } }