在接口参数区块新增可选的商品描述输入,并补充表单默认值、编辑回填与 create/update 请求体字段。 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1085 lines
32 KiB
TypeScript
1085 lines
32 KiB
TypeScript
import pcasCodeData from 'china-division/dist/pca-code.json'
|
||
import type {
|
||
ExpressInterfaceCreateParams,
|
||
ExpressInterfaceUpdateParams
|
||
} from '@/api/setting.expressinterface/expressInterface'
|
||
|
||
/** setting.expressInterface.list 单条记录字段顺序(与接口返回一致) */
|
||
export const expressInterfaceListFieldOrder = [
|
||
'id',
|
||
'name',
|
||
'express_code',
|
||
'sys_express_code',
|
||
'warehouse_id',
|
||
'shop_id',
|
||
'platform_shop_id',
|
||
'template_id',
|
||
'paymode',
|
||
'brandno',
|
||
'providertype',
|
||
'providerid',
|
||
'platform_code',
|
||
'sender_name',
|
||
'sender_tel',
|
||
'weight',
|
||
'weight_type',
|
||
'viewer',
|
||
'goods_description',
|
||
'client_id',
|
||
'is_enabled',
|
||
'is_share',
|
||
'ispickup',
|
||
'shipperno',
|
||
'template_url',
|
||
'ext_data',
|
||
'province_name',
|
||
'city_name',
|
||
'district_name',
|
||
'address',
|
||
'town_name',
|
||
'site_code',
|
||
'product_type',
|
||
'shop_name',
|
||
'express',
|
||
'express_name',
|
||
'express_polyapi_extra_info',
|
||
'express_pdd_code',
|
||
'is_system_express_code',
|
||
'quantity',
|
||
'recycled_quantity',
|
||
'cancel_quantity',
|
||
'allocated_quantity'
|
||
] as const
|
||
|
||
export const expressInterfaceFieldLabels: Record<string, string> = {
|
||
id: 'ID',
|
||
name: '配置名称',
|
||
express_code: '快递编码',
|
||
sys_express_code: '系统快递编码',
|
||
platform_code: '平台编码',
|
||
platform_shop_id: '平台店铺ID',
|
||
shop_id: '店铺ID',
|
||
paymode: '付款方式',
|
||
providertype: '服务商类型',
|
||
providerid: '服务商ID',
|
||
sender_name: '发件人',
|
||
sender_tel: '发件电话',
|
||
weight: '默认重量',
|
||
weight_type: '重量类型',
|
||
viewer: '验视人',
|
||
goods_description: '商品描述',
|
||
client_id: '客户端 ID',
|
||
is_share: '是否共享',
|
||
ispickup: '上门取件',
|
||
shipperno: '电子面单客户号',
|
||
province_name: '省',
|
||
city_name: '市',
|
||
district_name: '区/县',
|
||
address: '详细地址',
|
||
town_name: '乡镇/街道',
|
||
site_code: '站点编码',
|
||
template_id: '模板ID',
|
||
template_url: '打印模板',
|
||
product_type: '产品编码',
|
||
brandno: '品牌编码',
|
||
is_enabled: '是否启用',
|
||
is_system_express_code: '是否系统快递编码',
|
||
shop_name: '店铺名称',
|
||
warehouse_id: '仓库ID',
|
||
ext_data: '扩展数据',
|
||
express: '关联快递',
|
||
express_name: '关联快递',
|
||
express_polyapi_code: 'PolyAPI 编码',
|
||
express_polyapi_extra_info: 'PolyAPI 扩展',
|
||
express_pdd_code: '拼多多编码',
|
||
express_ks_code: '快手编码',
|
||
express_dyxd_code: '抖音编码',
|
||
polyapi_code: 'PolyAPI 编码',
|
||
pdd_code: '拼多多编码',
|
||
ks_code: '快手编码',
|
||
dyxd_code: '抖音编码',
|
||
polyapi_extra_info: 'PolyAPI 扩展',
|
||
quantity: '可用面单数量',
|
||
recycled_quantity: '已回收面单数量',
|
||
cancel_quantity: '已取消面单数量',
|
||
allocated_quantity: '已分配面单数量'
|
||
}
|
||
|
||
const DETAIL_KEY_ORDER = [...expressInterfaceListFieldOrder]
|
||
|
||
export function sortExpressInterfaceDetailKeys(keys: string[]) {
|
||
const orderSet = new Set<string>(DETAIL_KEY_ORDER)
|
||
const head = DETAIL_KEY_ORDER.filter((k) => keys.includes(k))
|
||
const tail = keys.filter((k) => !orderSet.has(k)).sort()
|
||
return [...head, ...tail]
|
||
}
|
||
|
||
export function formatExpressInterfaceCell(val: unknown): string {
|
||
if (val === null || val === undefined || val === '') return '--'
|
||
if (typeof val === 'object') return JSON.stringify(val)
|
||
return String(val)
|
||
}
|
||
|
||
export function formatExpressInterfaceNumber(val: unknown): string {
|
||
if (val === null || val === undefined || val === '') return '--'
|
||
const n = Number(val)
|
||
if (Number.isNaN(n)) return '--'
|
||
return String(n)
|
||
}
|
||
|
||
/** 是否类字段展示(1/0、'1'/'0') */
|
||
export function formatExpressInterfaceYesNo(val: unknown): string {
|
||
if (val === null || val === undefined || val === '') return '--'
|
||
const n = Number(val)
|
||
if (!Number.isNaN(n)) return n === 1 ? '是' : '否'
|
||
return String(val)
|
||
}
|
||
|
||
/** 关联快递子编码(express.pdd_code / express.ks_code / express.dyxd_code / express.polyapi_code) */
|
||
export function formatExpressInterfaceRelatedCode(row: Record<string, any>, key: string): string {
|
||
const nested = row?.express?.[key]
|
||
if (nested !== null && nested !== undefined && nested !== '') return String(nested)
|
||
const flat = row?.[`express_${key}`]
|
||
if (flat !== null && flat !== undefined && flat !== '') return String(flat)
|
||
return '--'
|
||
}
|
||
|
||
export type ExpressInterfaceColumn = {
|
||
prop: string
|
||
label: string
|
||
width?: string | number
|
||
minWidth?: string | number
|
||
align?: 'left' | 'center' | 'right'
|
||
fixed?: 'left' | 'right' | boolean
|
||
formatter?: (row: Record<string, any>) => string
|
||
}
|
||
|
||
/** 单字段列定义:与 setting.expressInterface.list 返回字段一一对应 */
|
||
const expressInterfaceFieldColumnMap: Record<string, ExpressInterfaceColumn> = {
|
||
id: {
|
||
prop: 'id',
|
||
label: 'ID',
|
||
width: 80,
|
||
align: 'center',
|
||
formatter: (row) => formatExpressInterfaceCell(row.id)
|
||
},
|
||
name: { prop: 'name', label: '配置名称', minWidth: 150, formatter: (row) => row.name || '--' },
|
||
// express_code: {
|
||
// prop: 'express_code',
|
||
// label: '快递编码',
|
||
// minWidth: 110,
|
||
// formatter: (row) => row.express_code || '--'
|
||
// },
|
||
// sys_express_code: {
|
||
// prop: 'sys_express_code',
|
||
// label: '系统快递编码',
|
||
// minWidth: 120,
|
||
// formatter: (row) => row.sys_express_code || '--'
|
||
// },
|
||
is_system_express_code: {
|
||
prop: 'is_system_express_code',
|
||
label: '是否系统快递编码',
|
||
width: 130,
|
||
align: 'center',
|
||
formatter: (row) => formatExpressInterfaceYesNo(row.is_system_express_code)
|
||
},
|
||
express_name: {
|
||
prop: 'express_name',
|
||
label: '关联快递',
|
||
minWidth: 150,
|
||
formatter: (row) => {
|
||
const name = row.express?.name || row.express_name
|
||
if (!name) return '--'
|
||
const code = row.express?.code || row.express_code || ''
|
||
return code ? `${name}(${code})` : name
|
||
}
|
||
},
|
||
// platform_code: {
|
||
// prop: 'platform_code',
|
||
// label: '平台编码',
|
||
// minWidth: 110,
|
||
// formatter: (row) => row.platform_code || '--'
|
||
// },
|
||
platform_shop_id: {
|
||
prop: 'platform_shop_id',
|
||
label: '平台店铺ID',
|
||
minWidth: 120,
|
||
formatter: (row) => formatExpressInterfaceCell(row.platform_shop_id)
|
||
},
|
||
// shop_id: {
|
||
// prop: 'shop_id',
|
||
// label: '店铺ID',
|
||
// width: 90,
|
||
// align: 'right',
|
||
// formatter: (row) => formatExpressInterfaceNumber(row.shop_id)
|
||
// },
|
||
shop_name: {
|
||
prop: 'shop_name',
|
||
label: '店铺名称',
|
||
minWidth: 150,
|
||
formatter: (row) => row.shop_name || '--'
|
||
},
|
||
// warehouse_id: {
|
||
// prop: 'warehouse_id',
|
||
// label: '仓库ID',
|
||
// width: 96,
|
||
// align: 'right',
|
||
// formatter: (row) => formatExpressInterfaceNumber(row.warehouse_id)
|
||
// },
|
||
providerid: {
|
||
prop: 'providerid',
|
||
label: '服务商ID',
|
||
minWidth: 110,
|
||
formatter: (row) => formatExpressInterfaceCell(row.providerid)
|
||
},
|
||
providertype: {
|
||
prop: 'providertype',
|
||
label: '服务商类型',
|
||
minWidth: 110,
|
||
formatter: (row) => row.providertype || '--'
|
||
},
|
||
paymode: {
|
||
prop: 'paymode',
|
||
label: '付款方式',
|
||
width: 96,
|
||
align: 'right',
|
||
formatter: (row) => formatExpressInterfaceNumber(row.paymode)
|
||
},
|
||
brandno: {
|
||
prop: 'brandno',
|
||
label: '品牌编码',
|
||
minWidth: 110,
|
||
formatter: (row) => row.brandno || '--'
|
||
},
|
||
sender_name: {
|
||
prop: 'sender_name',
|
||
label: '发件人',
|
||
minWidth: 100,
|
||
formatter: (row) => row.sender_name || '--'
|
||
},
|
||
sender_tel: {
|
||
prop: 'sender_tel',
|
||
label: '发件电话',
|
||
minWidth: 120,
|
||
formatter: (row) => row.sender_tel || '--'
|
||
},
|
||
province_name: {
|
||
prop: 'province_name',
|
||
label: '省',
|
||
minWidth: 90,
|
||
formatter: (row) => row.province_name || '--'
|
||
},
|
||
city_name: {
|
||
prop: 'city_name',
|
||
label: '市',
|
||
minWidth: 90,
|
||
formatter: (row) => row.city_name || '--'
|
||
},
|
||
district_name: {
|
||
prop: 'district_name',
|
||
label: '区/县',
|
||
minWidth: 90,
|
||
formatter: (row) => row.district_name || '--'
|
||
},
|
||
town_name: {
|
||
prop: 'town_name',
|
||
label: '乡镇/街道',
|
||
minWidth: 110,
|
||
formatter: (row) => row.town_name || '--'
|
||
},
|
||
address: {
|
||
prop: 'address',
|
||
label: '详细地址',
|
||
minWidth: 180,
|
||
formatter: (row) => row.address || '--'
|
||
},
|
||
site_code: {
|
||
prop: 'site_code',
|
||
label: '站点编码',
|
||
minWidth: 100,
|
||
formatter: (row) => row.site_code || '--'
|
||
},
|
||
// template_id: {
|
||
// prop: 'template_id',
|
||
// label: '模板ID',
|
||
// minWidth: 110,
|
||
// formatter: (row) => formatExpressInterfaceCell(row.template_id)
|
||
// },
|
||
// template_url: {
|
||
// prop: 'template_url',
|
||
// label: '打印模板',
|
||
// minWidth: 200,
|
||
// formatter: (row) => row.template_url || '--'
|
||
// },
|
||
product_type: {
|
||
prop: 'product_type',
|
||
label: '产品编码',
|
||
minWidth: 100,
|
||
formatter: (row) => row.product_type || '--'
|
||
},
|
||
weight: {
|
||
prop: 'weight',
|
||
label: '默认重量',
|
||
width: 96,
|
||
align: 'right',
|
||
formatter: (row) => formatExpressInterfaceNumber(row.weight)
|
||
},
|
||
weight_type: {
|
||
prop: 'weight_type',
|
||
label: '重量类型',
|
||
width: 96,
|
||
align: 'right',
|
||
formatter: (row) => formatExpressInterfaceNumber(row.weight_type)
|
||
},
|
||
viewer: {
|
||
prop: 'viewer',
|
||
label: '验视人',
|
||
minWidth: 100,
|
||
formatter: (row) => row.viewer || '--'
|
||
},
|
||
client_id: {
|
||
prop: 'client_id',
|
||
label: '客户端ID',
|
||
minWidth: 110,
|
||
formatter: (row) => row.client_id || '--'
|
||
},
|
||
is_share: {
|
||
prop: 'is_share',
|
||
label: '是否共享',
|
||
width: 90,
|
||
align: 'center',
|
||
formatter: (row) => formatExpressInterfaceYesNo(row.is_share)
|
||
},
|
||
ispickup: {
|
||
prop: 'ispickup',
|
||
label: '上门取件',
|
||
width: 90,
|
||
align: 'center',
|
||
formatter: (row) => formatExpressInterfaceYesNo(row.ispickup)
|
||
},
|
||
shipperno: {
|
||
prop: 'shipperno',
|
||
label: '电子面单客户号',
|
||
minWidth: 140,
|
||
formatter: (row) => row.shipperno || '--'
|
||
},
|
||
express_polyapi_code: {
|
||
prop: 'express_polyapi_code',
|
||
label: 'PolyAPI 编码',
|
||
minWidth: 120,
|
||
formatter: (row) => formatExpressInterfaceRelatedCode(row, 'polyapi_code')
|
||
},
|
||
express_pdd_code: {
|
||
prop: 'express_pdd_code',
|
||
label: '拼多多编码',
|
||
minWidth: 110,
|
||
formatter: (row) => formatExpressInterfaceRelatedCode(row, 'pdd_code')
|
||
},
|
||
express_polyapi_extra_info: {
|
||
prop: 'express_polyapi_extra_info',
|
||
label: 'PolyAPI 扩展',
|
||
minWidth: 140,
|
||
formatter: (row) => formatExpressInterfaceCell(row.express_polyapi_extra_info)
|
||
},
|
||
// ext_data: {
|
||
// prop: 'ext_data',
|
||
// label: '扩展数据',
|
||
// minWidth: 160,
|
||
// formatter: (row) => formatExpressInterfaceCell(row.ext_data)
|
||
// },
|
||
quantity: {
|
||
prop: 'quantity',
|
||
label: '可用面单数量',
|
||
width: 100,
|
||
align: 'right',
|
||
formatter: (row) => formatExpressInterfaceNumber(row.quantity)
|
||
},
|
||
recycled_quantity: {
|
||
prop: 'recycled_quantity',
|
||
label: '已回收面单',
|
||
width: 96,
|
||
align: 'right',
|
||
formatter: (row) => formatExpressInterfaceNumber(row.recycled_quantity)
|
||
},
|
||
cancel_quantity: {
|
||
prop: 'cancel_quantity',
|
||
label: '已取消面单',
|
||
width: 96,
|
||
align: 'right',
|
||
formatter: (row) => formatExpressInterfaceNumber(row.cancel_quantity)
|
||
},
|
||
allocated_quantity: {
|
||
prop: 'allocated_quantity',
|
||
label: '已分配面单',
|
||
width: 96,
|
||
align: 'right',
|
||
formatter: (row) => formatExpressInterfaceNumber(row.allocated_quantity)
|
||
},
|
||
is_enabled: {
|
||
prop: 'is_enabled',
|
||
label: '状态',
|
||
width: 80,
|
||
align: 'center',
|
||
formatter: (row) => {
|
||
const enabled = row.is_enabled === 1 || row.is_enabled === '1'
|
||
return enabled
|
||
? '<span style="color:#67c23a">启用</span>'
|
||
: '<span style="color:#f56c6c">停用</span>'
|
||
}
|
||
}
|
||
}
|
||
|
||
/** 表格列顺序:与 setting.expressInterface.list 返回字段顺序保持一致 */
|
||
const expressInterfaceFieldColumnProps = [
|
||
'name',
|
||
'express_code',
|
||
'sys_express_code',
|
||
'is_system_express_code',
|
||
'express_name',
|
||
'platform_code',
|
||
'platform_shop_id',
|
||
'shop_id',
|
||
'shop_name',
|
||
'warehouse_id',
|
||
'providerid',
|
||
'providertype',
|
||
'paymode',
|
||
'brandno',
|
||
'sender_name',
|
||
'sender_tel',
|
||
'province_name',
|
||
'city_name',
|
||
'district_name',
|
||
'town_name',
|
||
'address',
|
||
'site_code',
|
||
'template_id',
|
||
'template_url',
|
||
'product_type',
|
||
'weight',
|
||
'weight_type',
|
||
'viewer',
|
||
'client_id',
|
||
'is_share',
|
||
'ispickup',
|
||
'shipperno',
|
||
'express_polyapi_code',
|
||
'express_pdd_code',
|
||
'express_polyapi_extra_info',
|
||
'ext_data',
|
||
'quantity',
|
||
'recycled_quantity',
|
||
'cancel_quantity',
|
||
'allocated_quantity',
|
||
'is_enabled'
|
||
]
|
||
|
||
function buildExpressInterfaceColumns(props: string[]): ExpressInterfaceColumn[] {
|
||
return props
|
||
.map((prop) => expressInterfaceFieldColumnMap[prop])
|
||
.filter((column): column is ExpressInterfaceColumn => Boolean(column))
|
||
}
|
||
|
||
/** 分页列表列(setting.expressInterface.list) */
|
||
export const expressInterfaceColumns: ExpressInterfaceColumn[] = [
|
||
...buildExpressInterfaceColumns(expressInterfaceFieldColumnProps),
|
||
{ prop: 'actions', label: '操作', width: 140, align: 'center', fixed: 'right' }
|
||
]
|
||
|
||
/** 按仓库+平台全量列表列(setting.expressInterface.listByWarehousePlatformCode) */
|
||
export const expressInterfaceWarehouseColumns: ExpressInterfaceColumn[] = [
|
||
...buildExpressInterfaceColumns(['id', ...expressInterfaceFieldColumnProps]),
|
||
{ prop: 'actions', label: '操作', width: 140, align: 'center', fixed: 'right' }
|
||
]
|
||
|
||
/** ScanBox 可用字段(与 list 接口 where 一致) */
|
||
export const expressInterfaceScanFields = [
|
||
{
|
||
value: 'quickWhere',
|
||
label: '关键词',
|
||
type: 'text',
|
||
placeholder: '配置名称/编码/店铺等快速搜索',
|
||
operators: ['contains'],
|
||
defaultOperator: 'contains'
|
||
},
|
||
{
|
||
value: 'platform_code',
|
||
label: '平台编码',
|
||
type: 'select',
|
||
filterable: true,
|
||
clearable: true,
|
||
options: [] as { label: string; value: string }[]
|
||
},
|
||
{
|
||
value: 'warehouse_id',
|
||
label: '仓库ID',
|
||
type: 'number',
|
||
placeholder: '按仓库+平台查询时可选',
|
||
operators: ['eq'],
|
||
defaultOperator: 'eq'
|
||
},
|
||
{
|
||
value: 'id',
|
||
label: 'ID',
|
||
type: 'number',
|
||
placeholder: '请输入ID',
|
||
operators: ['eq', 'ne'],
|
||
defaultOperator: 'eq'
|
||
},
|
||
{
|
||
value: 'name',
|
||
label: '配置名称',
|
||
type: 'text',
|
||
placeholder: '请输入配置名称',
|
||
operators: ['eq', 'contains', 'like'],
|
||
defaultOperator: 'contains'
|
||
},
|
||
{
|
||
value: 'express_code',
|
||
label: '快递编码',
|
||
type: 'select',
|
||
filterable: true,
|
||
clearable: true,
|
||
options: [] as { label: string; value: string }[]
|
||
},
|
||
|
||
{
|
||
value: 'sender_name',
|
||
label: '发件人',
|
||
type: 'text',
|
||
placeholder: '请输入发件人',
|
||
operators: ['eq', 'contains', 'like'],
|
||
defaultOperator: 'contains'
|
||
},
|
||
{
|
||
value: 'sender_tel',
|
||
label: '发件电话',
|
||
type: 'text',
|
||
placeholder: '请输入发件电话',
|
||
operators: ['eq', 'contains', 'like'],
|
||
defaultOperator: 'contains'
|
||
},
|
||
{
|
||
value: 'shop_name',
|
||
label: '店铺名称',
|
||
type: 'text',
|
||
placeholder: '请输入店铺名称',
|
||
operators: ['eq', 'contains', 'like'],
|
||
defaultOperator: 'contains'
|
||
},
|
||
{
|
||
value: 'site_code',
|
||
label: '站点编码',
|
||
type: 'text',
|
||
placeholder: '请输入站点编码',
|
||
operators: ['eq', 'contains', 'like'],
|
||
defaultOperator: 'contains'
|
||
},
|
||
{
|
||
value: 'viewer',
|
||
label: '验视人',
|
||
type: 'text',
|
||
placeholder: '请输入验视人',
|
||
operators: ['eq', 'contains', 'like'],
|
||
defaultOperator: 'contains'
|
||
},
|
||
{
|
||
value: 'is_enabled',
|
||
label: '是否启用',
|
||
type: 'select',
|
||
clearable: true,
|
||
options: [
|
||
{ label: '启用', value: 1 },
|
||
{ label: '停用', value: 0 }
|
||
]
|
||
},
|
||
|
||
]
|
||
|
||
function getFieldConfig(key: string, fields: typeof expressInterfaceScanFields) {
|
||
return fields.find((f) => f.value === key)
|
||
}
|
||
|
||
function mapFieldType(type: string, value: unknown) {
|
||
if (type === 'daterange' || Array.isArray(value)) return 'daterange'
|
||
if (type === 'number' || typeof value === 'number') return 'number'
|
||
return 'text'
|
||
}
|
||
|
||
function processConditionValue(key: string, raw: any, fields: typeof expressInterfaceScanFields) {
|
||
if (key === 'quickWhere') {
|
||
const val = raw?.value !== undefined ? raw.value : raw
|
||
const text = val == null ? '' : String(val).trim()
|
||
return text ? text : null
|
||
}
|
||
|
||
const config = getFieldConfig(key, fields)
|
||
const fieldType = config?.type || 'text'
|
||
|
||
if (raw && typeof raw === 'object' && !Array.isArray(raw)) {
|
||
const op = raw.operator || (fieldType === 'select' ? 'eq' : fieldType === 'number' ? 'eq' : 'contains')
|
||
const type = raw.type || mapFieldType(fieldType, raw.value)
|
||
|
||
if (['isnull', 'isnotnull'].includes(op)) {
|
||
return { operator: op, value: null, type }
|
||
}
|
||
|
||
const val = raw.value !== undefined ? raw.value : raw
|
||
if (val === null || val === undefined || val === '' || (Array.isArray(val) && val.length === 0)) {
|
||
return null
|
||
}
|
||
|
||
if (fieldType === 'select') {
|
||
return { operator: op || 'eq', value: val, type: 'text' }
|
||
}
|
||
|
||
return { operator: op, value: val, type }
|
||
}
|
||
|
||
if (raw === null || raw === undefined || raw === '' || (Array.isArray(raw) && raw.length === 0)) {
|
||
return null
|
||
}
|
||
|
||
const simpleOp = fieldType === 'select' || fieldType === 'number' ? 'eq' : 'contains'
|
||
return { operator: simpleOp, value: raw, type: mapFieldType(fieldType, raw) }
|
||
}
|
||
|
||
/** 将 ScanBox 条件转为 list 接口 where;quickWhere 为顶层字符串字段 */
|
||
export function normalizeScanConditionToWhere(
|
||
condition: any,
|
||
fields: typeof expressInterfaceScanFields = expressInterfaceScanFields
|
||
): Record<string, unknown> {
|
||
if (!condition) return {}
|
||
|
||
const conds =
|
||
condition?.conditions && typeof condition.conditions === 'object'
|
||
? condition.conditions
|
||
: condition && typeof condition === 'object'
|
||
? condition
|
||
: null
|
||
|
||
if (!conds) return {}
|
||
|
||
const where: Record<string, unknown> = {}
|
||
for (const key of Object.keys(conds)) {
|
||
const result = processConditionValue(key, conds[key], fields)
|
||
if (result == null) continue
|
||
if (key === 'quickWhere' && typeof result === 'string') {
|
||
where.quickWhere = result
|
||
} else {
|
||
where[key] = result
|
||
}
|
||
}
|
||
return where
|
||
}
|
||
|
||
export function extractPlainWhereValue(where: Record<string, unknown>, key: string): string | number | undefined {
|
||
const raw = where[key]
|
||
if (raw == null || raw === '') return undefined
|
||
if (typeof raw === 'object' && raw !== null && 'value' in (raw as object)) {
|
||
return (raw as { value?: string | number }).value as string | number | undefined
|
||
}
|
||
return raw as string | number
|
||
}
|
||
|
||
export function formatExpressInterfaceData(list: Record<string, any>[] = []) {
|
||
return (list || []).map((item) => ({
|
||
...item,
|
||
quantity: item.quantity ?? 0,
|
||
recycled_quantity: item.recycled_quantity ?? 0,
|
||
cancel_quantity: item.cancel_quantity ?? 0,
|
||
allocated_quantity: item.allocated_quantity ?? 0,
|
||
/** 接口返回的应是 express.name 或平铺的 express_name */
|
||
express_name: item.express?.name || item.express_name || '',
|
||
shop_name: item.shop_name || ''
|
||
}))
|
||
}
|
||
|
||
/** 解析 setting.expressInterface.listByWarehousePlatformCode 响应列表 */
|
||
export function parseExpressInterfaceWarehouseListResponse(res: any): Record<string, any>[] {
|
||
const level1 = res?.data
|
||
if (!level1) return []
|
||
|
||
if (Array.isArray(level1)) return level1
|
||
if (Array.isArray(level1.list)) return level1.list
|
||
if (Array.isArray(level1.data)) return level1.data
|
||
|
||
const level2 = level1.data
|
||
if (level2 && typeof level2 === 'object') {
|
||
if (Array.isArray(level2.list)) return level2.list
|
||
if (Array.isArray(level2.data)) return level2.data
|
||
}
|
||
|
||
return []
|
||
}
|
||
|
||
/** 中国省市区(与 CreatePendingOrderDialog 一致,数据来自 china-division) */
|
||
export type RegionNode = { code: string; name: string; children?: RegionNode[] }
|
||
|
||
export const chinaRegionTree = (Array.isArray(pcasCodeData) ? pcasCodeData : []) as RegionNode[]
|
||
|
||
export type RegionSelectOption = { label: string; value: string }
|
||
|
||
export function buildProvinceOptions(): RegionSelectOption[] {
|
||
return chinaRegionTree.map((item) => ({ label: item.name, value: item.code }))
|
||
}
|
||
|
||
/**
|
||
* 市 / 区县下拉选项缓存:
|
||
* 数据来自静态行政区划,结果不会变化;建单预览表里每行每个单元格渲染都会调一次,
|
||
* 缓存后避免每次渲染都遍历 + 新建数组(返回的是共享数组,调用方只读不修改)。
|
||
*/
|
||
const cityOptionsCache = new Map<string, RegionSelectOption[]>()
|
||
const districtOptionsCache = new Map<string, RegionSelectOption[]>()
|
||
const EMPTY_REGION_OPTIONS: RegionSelectOption[] = []
|
||
|
||
export function buildCityOptions(provinceCode: string): RegionSelectOption[] {
|
||
const key = String(provinceCode || '')
|
||
if (!key) return EMPTY_REGION_OPTIONS
|
||
const cached = cityOptionsCache.get(key)
|
||
if (cached) return cached
|
||
const province = chinaRegionTree.find((item) => item.code === key)
|
||
const options = (province?.children || []).map((item) => ({ label: item.name, value: item.code }))
|
||
cityOptionsCache.set(key, options)
|
||
return options
|
||
}
|
||
|
||
export function buildDistrictOptions(provinceCode: string, cityCode: string): RegionSelectOption[] {
|
||
const provinceKey = String(provinceCode || '')
|
||
const cityKey = String(cityCode || '')
|
||
if (!provinceKey || !cityKey) return EMPTY_REGION_OPTIONS
|
||
const cacheKey = `${provinceKey}|${cityKey}`
|
||
const cached = districtOptionsCache.get(cacheKey)
|
||
if (cached) return cached
|
||
const province = chinaRegionTree.find((item) => item.code === provinceKey)
|
||
const city = (province?.children || []).find((item) => item.code === cityKey)
|
||
const options = (city?.children || []).map((item) => ({ label: item.name, value: item.code }))
|
||
districtOptionsCache.set(cacheKey, options)
|
||
return options
|
||
}
|
||
|
||
function findRegionCodeByName(nodes: RegionNode[], name: string): string {
|
||
const trimmed = String(name || '').trim()
|
||
if (!trimmed) return ''
|
||
return nodes.find((item) => item.name === trimmed)?.code || ''
|
||
}
|
||
|
||
export function findRegionNameByCode(nodes: RegionNode[], code: string): string {
|
||
if (!code) return ''
|
||
return nodes.find((item) => item.code === code)?.name || ''
|
||
}
|
||
|
||
/** 根据已保存的省/市/区名称反查行政区划 code,用于编辑回填 */
|
||
export function resolveRegionCodesFromNames(names: {
|
||
province_name?: string
|
||
city_name?: string
|
||
district_name?: string
|
||
}) {
|
||
const provinceCode = findRegionCodeByName(chinaRegionTree, names.province_name || '')
|
||
const provinceNode = chinaRegionTree.find((item) => item.code === provinceCode)
|
||
const cityCode = provinceNode
|
||
? findRegionCodeByName(provinceNode.children || [], names.city_name || '')
|
||
: ''
|
||
const cityNode = (provinceNode?.children || []).find((item) => item.code === cityCode)
|
||
const districtCode = cityNode
|
||
? findRegionCodeByName(cityNode.children || [], names.district_name || '')
|
||
: ''
|
||
return { provinceCode, cityCode, districtCode }
|
||
}
|
||
|
||
export function getCityNodes(provinceCode: string): RegionNode[] {
|
||
const province = chinaRegionTree.find((item) => item.code === provinceCode)
|
||
return province?.children || []
|
||
}
|
||
|
||
export function getDistrictNodes(provinceCode: string, cityCode: string): RegionNode[] {
|
||
const city = getCityNodes(provinceCode).find((item) => item.code === cityCode)
|
||
return city?.children || []
|
||
}
|
||
|
||
export const defaultExpressInterfaceForm = {
|
||
id: undefined as number | undefined,
|
||
warehouse_id: undefined as number | undefined,
|
||
shop_id: undefined as number | undefined,
|
||
name: '',
|
||
express_code: '',
|
||
sys_express_code: '',
|
||
is_system_express_code: 1,
|
||
platform_code: '',
|
||
platform_shop_id: null as string | null,
|
||
providerid: '',
|
||
express_name: '',
|
||
sender_name: '',
|
||
sender_tel: '',
|
||
weight: 1,
|
||
weight_type: undefined as number | undefined,
|
||
token: '',
|
||
viewer: '',
|
||
goods_description: '',
|
||
client_id: '',
|
||
share_id: undefined as number | undefined,
|
||
ispickup: 0,
|
||
shipperno: '',
|
||
can_use_count: undefined as number | undefined,
|
||
site_code: '',
|
||
template_id: null as string | null,
|
||
template_url: '',
|
||
shop_name: '',
|
||
product_type: '',
|
||
brandno: '',
|
||
is_hide_goods: 0,
|
||
is_enabled: 1,
|
||
address: '',
|
||
town_name: '',
|
||
ext_data: undefined as Record<string, any> | undefined,
|
||
province_name: '',
|
||
city_name: '',
|
||
district_name: '',
|
||
/** 列表展示字段,不作为 create/update 入参 */
|
||
is_share: 0
|
||
}
|
||
|
||
export function parseExpressInterfaceExtData(
|
||
ext: unknown
|
||
): Record<string, any> | undefined {
|
||
if (ext == null || ext === '') return undefined
|
||
if (typeof ext === 'object') return ext as Record<string, any>
|
||
if (typeof ext === 'string') {
|
||
try {
|
||
const parsed = JSON.parse(ext)
|
||
return parsed && typeof parsed === 'object' ? parsed : undefined
|
||
} catch {
|
||
return undefined
|
||
}
|
||
}
|
||
return undefined
|
||
}
|
||
|
||
function toFormString(val: unknown): string {
|
||
if (val === null || val === undefined) return ''
|
||
return String(val).trim()
|
||
}
|
||
|
||
function toFormOptionalNumber(val: unknown): number | undefined {
|
||
if (val === null || val === undefined || val === '') return undefined
|
||
const n = Number(val)
|
||
return Number.isFinite(n) ? n : undefined
|
||
}
|
||
|
||
/** 将 list 接口单条记录写入编辑表单 */
|
||
export function applyExpressInterfaceRowToForm(
|
||
form: Record<string, any>,
|
||
row: Record<string, any>
|
||
) {
|
||
const ext = parseExpressInterfaceExtData(row.ext_data)
|
||
const rawExpressCode = toFormString(row.express_code)
|
||
const rawSysExpressCode = toFormString(row.sys_express_code)
|
||
const isCustomExpressCode = (toFormOptionalNumber(row.is_system_express_code) ?? 1) === 0
|
||
const signedExpressCode = rawSysExpressCode || rawExpressCode
|
||
const customExpressCode = isCustomExpressCode ? rawExpressCode : ''
|
||
|
||
Object.assign(form, {
|
||
id: row.id,
|
||
warehouse_id: toFormOptionalNumber(row.warehouse_id),
|
||
shop_id: toFormOptionalNumber(row.shop_id ?? row.shopId),
|
||
name: toFormString(row.name),
|
||
express_code: customExpressCode,
|
||
sys_express_code: signedExpressCode,
|
||
is_system_express_code: toFormOptionalNumber(row.is_system_express_code) ?? 1,
|
||
platform_code: toFormString(row.platform_code),
|
||
platform_shop_id: (() => {
|
||
const v = toFormString(row.platform_shop_id ?? row.platformShopId)
|
||
return v || null
|
||
})(),
|
||
providerid: toFormString(row.providerid ?? row.provider_id ?? row.providerId),
|
||
express_name: toFormString(row.express_name || row.express?.name),
|
||
sender_name: toFormString(row.sender_name),
|
||
sender_tel: toFormString(row.sender_tel),
|
||
weight: toFormOptionalNumber(row.weight) ?? 1,
|
||
weight_type: toFormOptionalNumber(row.weight_type),
|
||
token: toFormString(row.token),
|
||
viewer: toFormString(row.viewer),
|
||
goods_description: toFormString(row.goods_description),
|
||
client_id: toFormString(row.client_id),
|
||
share_id: toFormOptionalNumber(row.share_id),
|
||
ispickup: row.ispickup === 1 || row.ispickup === true ? 1 : 0,
|
||
shipperno: toFormString(row.shipperno),
|
||
can_use_count: toFormOptionalNumber(row.can_use_count),
|
||
site_code: toFormString(row.site_code),
|
||
template_id: (() => {
|
||
const v = toFormString(row.template_id ?? row.templateId)
|
||
return v || null
|
||
})(),
|
||
template_url: toFormString(row.template_url ?? row.templateUrl),
|
||
shop_name: toFormString(row.shop_name),
|
||
product_type: toFormString(row.product_type),
|
||
brandno: toFormString(row.brandno),
|
||
is_hide_goods: toFormOptionalNumber(row.is_hide_goods) ?? 0,
|
||
is_enabled: toFormOptionalNumber(row.is_enabled) ?? 1,
|
||
address: toFormString(row.address),
|
||
town_name: toFormString(row.town_name),
|
||
province_name: toFormString(row.province_name),
|
||
city_name: toFormString(row.city_name),
|
||
district_name: toFormString(row.district_name),
|
||
ext_data: ext,
|
||
is_share: row.is_share === 1 || row.is_share === true ? 1 : 0
|
||
})
|
||
}
|
||
|
||
export function formatExpressInterfaceFormDisplayValue(
|
||
key: string,
|
||
val: unknown
|
||
): string {
|
||
if (key === 'is_share' || key === 'ispickup' || key === 'is_hide_goods' || key === 'is_enabled') {
|
||
if (val === 1 || val === true) return '是'
|
||
if (val === 0 || val === false) return '否'
|
||
return '--'
|
||
}
|
||
if (key === 'ext_data') {
|
||
if (val == null || val === '') return '--'
|
||
if (typeof val === 'object') {
|
||
try {
|
||
return JSON.stringify(val, null, 2)
|
||
} catch {
|
||
return '--'
|
||
}
|
||
}
|
||
return String(val)
|
||
}
|
||
if (['can_use_count', 'weight', 'weight_type', 'id', 'warehouse_id', 'share_id', 'quantity', 'recycled_quantity', 'cancel_quantity', 'allocated_quantity'].includes(key)) {
|
||
return formatExpressInterfaceNumber(val)
|
||
}
|
||
return formatExpressInterfaceCell(val)
|
||
}
|
||
|
||
function omitEmptyString(val: unknown): string | undefined {
|
||
const s = String(val ?? '').trim()
|
||
return s || undefined
|
||
}
|
||
|
||
function toNullableString(val: unknown): string | null {
|
||
const s = String(val ?? '').trim()
|
||
return s || null
|
||
}
|
||
|
||
function omitOptionalNumber(val: unknown): number | undefined {
|
||
if (val === null || val === undefined || val === '') return undefined
|
||
const n = Number(val)
|
||
return Number.isFinite(n) ? n : undefined
|
||
}
|
||
|
||
/** UI 展示/地址匹配(含 full_address) */
|
||
export const expressInterfaceAddressDisplayKeys = [
|
||
'full_address',
|
||
'detail',
|
||
'address',
|
||
'send_address'
|
||
] as const
|
||
|
||
/** API 入参 address 取值优先级(文档要求 detail,非 full_address) */
|
||
export const expressInterfaceAddressApiKeys = ['detail', 'address', 'send_address'] as const
|
||
|
||
export function resolveExpressInterfaceAddress(
|
||
raw?: Record<string, any>,
|
||
fallback = ''
|
||
): string {
|
||
if (!raw || typeof raw !== 'object') {
|
||
return fallback
|
||
}
|
||
for (const key of expressInterfaceAddressApiKeys) {
|
||
const val = String(raw[key] ?? '').trim()
|
||
if (val) return val
|
||
}
|
||
return fallback
|
||
}
|
||
|
||
/** setting.expressInterface.create 请求体(禁止 warehouse_id) */
|
||
export function buildExpressInterfaceCreatePayload(
|
||
form: Record<string, any>
|
||
): ExpressInterfaceCreateParams {
|
||
const isCustomExpressCode = (omitOptionalNumber(form.is_system_express_code) ?? 1) === 0
|
||
const expressCode = isCustomExpressCode
|
||
? String(form.express_code || '').trim()
|
||
: String(form.sys_express_code || form.express_code || '').trim()
|
||
|
||
const payload: ExpressInterfaceCreateParams = {
|
||
name: String(form.name || '').trim(),
|
||
express_code: expressCode,
|
||
platform_code: String(form.platform_code || '').trim(),
|
||
platform_shop_id: toNullableString(form.platform_shop_id)
|
||
}
|
||
|
||
const optionalStrings = [
|
||
'sys_express_code',
|
||
'sender_name',
|
||
'sender_tel',
|
||
'providerid',
|
||
'token',
|
||
'viewer',
|
||
'goods_description',
|
||
'client_id',
|
||
'shipperno',
|
||
'province_name',
|
||
'city_name',
|
||
'district_name',
|
||
'address',
|
||
'town_name',
|
||
'site_code',
|
||
'template_id',
|
||
'template_url',
|
||
'product_type',
|
||
'brandno',
|
||
'shop_name'
|
||
] as const
|
||
for (const key of optionalStrings) {
|
||
const v =
|
||
key === 'address'
|
||
? omitEmptyString(resolveExpressInterfaceAddress(form, String(form.address ?? '').trim()))
|
||
: omitEmptyString(form[key])
|
||
if (v !== undefined) payload[key] = v
|
||
}
|
||
|
||
const optionalNumbers = [
|
||
'shop_id',
|
||
'is_system_express_code',
|
||
'is_enabled',
|
||
'is_hide_goods',
|
||
'weight',
|
||
'weight_type',
|
||
'share_id',
|
||
'ispickup'
|
||
] as const
|
||
for (const key of optionalNumbers) {
|
||
const n = omitOptionalNumber(form[key])
|
||
if (n === undefined) continue
|
||
const rounded =
|
||
key === 'shop_id' ||
|
||
key === 'is_system_express_code' ||
|
||
key === 'is_hide_goods' ||
|
||
key === 'weight' ||
|
||
key === 'weight_type' ||
|
||
key === 'ispickup'
|
||
? Math.round(n)
|
||
: n
|
||
payload[key] = rounded
|
||
}
|
||
|
||
if (form.ext_data && typeof form.ext_data === 'object') {
|
||
payload.ext_data = form.ext_data
|
||
}
|
||
|
||
return payload
|
||
}
|
||
|
||
/** setting.expressInterface.update 请求体(文档核心字段:id、shop_id、address) */
|
||
export function buildExpressInterfaceUpdatePayload(
|
||
form: Record<string, any>
|
||
): ExpressInterfaceUpdateParams & ExpressInterfaceCreateParams {
|
||
const basePayload = buildExpressInterfaceCreatePayload(form)
|
||
|
||
return {
|
||
...basePayload,
|
||
id: Number(form.id)
|
||
}
|
||
}
|