518 lines
16 KiB
TypeScript
518 lines
16 KiB
TypeScript
import pcasCodeData from 'china-division/dist/pcas-code.json'
|
||
|
||
export const expressInterfaceFieldLabels: Record<string, string> = {
|
||
id: 'ID',
|
||
name: '配置名称',
|
||
express_code: '快递编码',
|
||
platform_code: '平台编码',
|
||
express_platform_id: '平台接口ID',
|
||
sender_name: '发件人',
|
||
sender_tel: '发件电话',
|
||
weight: '预设重量',
|
||
token: 'Token',
|
||
viewer: '查看者',
|
||
client_id: '客户端ID',
|
||
is_share: '是否共享',
|
||
share_id: '共享记录ID',
|
||
can_use_count: '可用单量',
|
||
province_name: '省',
|
||
city_name: '市',
|
||
district_name: '区/县',
|
||
site_code: '站点编码',
|
||
product_type: '快递产品类型',
|
||
shop_name: '店铺名称',
|
||
warehouse_id: '仓库ID',
|
||
express: '关联快递',
|
||
express_name: '关联快递',
|
||
express_platform_label: '快递平台'
|
||
}
|
||
|
||
const DETAIL_KEY_ORDER = [
|
||
'id',
|
||
'name',
|
||
'express_code',
|
||
'platform_code',
|
||
'express_platform_id',
|
||
'weight',
|
||
'sender_name',
|
||
'sender_tel',
|
||
'site_code',
|
||
'shop_name',
|
||
'product_type',
|
||
'can_use_count',
|
||
'province_name',
|
||
'city_name',
|
||
'district_name',
|
||
'viewer',
|
||
'client_id',
|
||
'is_share',
|
||
'share_id',
|
||
'token',
|
||
'warehouse_id'
|
||
]
|
||
|
||
export function sortExpressInterfaceDetailKeys(keys: string[]) {
|
||
const head = DETAIL_KEY_ORDER.filter((k) => keys.includes(k))
|
||
const tail = keys.filter((k) => !DETAIL_KEY_ORDER.includes(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)
|
||
}
|
||
|
||
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) */
|
||
export const expressInterfaceColumns: ExpressInterfaceColumn[] = [
|
||
{ prop: 'id', label: 'ID', width: 80, align: 'center', formatter: (row) => formatExpressInterfaceCell(row.id) },
|
||
{ prop: 'name', label: '配置名称', minWidth: 150, formatter: (row) => row.name || '--' },
|
||
{ prop: 'express_code', label: '快递编码', minWidth: 110, formatter: (row) => row.express_code || '--' },
|
||
{ prop: 'platform_code', label: '平台编码', minWidth: 110, formatter: (row) => row.platform_code || '--' },
|
||
{
|
||
prop: 'express_platform_id',
|
||
label: '平台接口ID',
|
||
width: 110,
|
||
align: 'right',
|
||
formatter: (row) => formatExpressInterfaceNumber(row.express_platform_id)
|
||
},
|
||
{ prop: 'sender_name', label: '发件人', minWidth: 100, formatter: (row) => row.sender_name || '--' },
|
||
{ prop: 'sender_tel', label: '发件电话', minWidth: 120, formatter: (row) => row.sender_tel || '--' },
|
||
{ prop: 'shop_name', label: '店铺', minWidth: 140, formatter: (row) => row.shop_name || '--' },
|
||
{ prop: 'site_code', label: '站点编码', minWidth: 100, formatter: (row) => row.site_code || '--' },
|
||
{
|
||
prop: 'can_use_count',
|
||
label: '可用单量',
|
||
width: 96,
|
||
align: 'right',
|
||
formatter: (row) => formatExpressInterfaceNumber(row.can_use_count)
|
||
},
|
||
{
|
||
prop: 'weight',
|
||
label: '预设重量',
|
||
width: 96,
|
||
align: 'right',
|
||
formatter: (row) => formatExpressInterfaceNumber(row.weight)
|
||
},
|
||
{ prop: 'actions', label: '操作', width: 140, align: 'center', fixed: 'right' }
|
||
]
|
||
|
||
/** 按仓库+平台全量列表列(setting.expressInterface.listByWarehousePlatformCode) */
|
||
export const expressInterfaceWarehouseColumns: ExpressInterfaceColumn[] = [
|
||
{ prop: 'id', label: 'ID', width: 80, align: 'center', formatter: (row) => formatExpressInterfaceCell(row.id) },
|
||
{
|
||
prop: 'weight',
|
||
label: '权重',
|
||
width: 80,
|
||
align: 'right',
|
||
formatter: (row) => formatExpressInterfaceNumber(row.weight)
|
||
},
|
||
|
||
{ prop: 'name', label: '配置名称', minWidth: 140, formatter: (row) => row.name || '--' },
|
||
{ prop: 'express_code', label: '快递编码', minWidth: 100, formatter: (row) => row.express_code || '--' },
|
||
{
|
||
prop: 'express_name',
|
||
label: '关联快递',
|
||
minWidth: 160,
|
||
formatter: (row) => {
|
||
if (row.express?.name) {
|
||
const code = row.express?.code || row.express_code || ''
|
||
return code ? `${row.express.name}(${code})` : row.express.name
|
||
}
|
||
return row.express_name || '--'
|
||
}
|
||
},
|
||
{
|
||
prop: 'express_platform_label',
|
||
label: '快递平台',
|
||
minWidth: 100,
|
||
formatter: (row) => row.express?.platform ?? row.express_platform_label ?? '--'
|
||
},
|
||
{ prop: 'platform_code', label: '平台编码', minWidth: 100, formatter: (row) => row.platform_code || '--' },
|
||
{ prop: 'sender_name', label: '发件人', minWidth: 100, formatter: (row) => row.sender_name || '--' },
|
||
{ prop: 'sender_tel', label: '发件电话', minWidth: 110, formatter: (row) => row.sender_tel || '--' },
|
||
{ prop: 'shop_name', label: '店铺', minWidth: 140, formatter: (row) => row.shop_name || '--' },
|
||
{
|
||
prop: 'can_use_count',
|
||
label: '可用单量',
|
||
width: 90,
|
||
align: 'right',
|
||
formatter: (row) => formatExpressInterfaceNumber(row.can_use_count)
|
||
},
|
||
{ 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: 'express_platform_id',
|
||
label: '平台接口ID',
|
||
type: 'number',
|
||
placeholder: '请输入平台接口ID',
|
||
operators: ['eq'],
|
||
defaultOperator: 'eq'
|
||
},
|
||
{
|
||
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: 'product_type',
|
||
label: '快递产品类型',
|
||
type: 'text',
|
||
placeholder: '请输入产品类型',
|
||
operators: ['eq', 'contains', 'like'],
|
||
defaultOperator: 'contains'
|
||
},
|
||
{
|
||
value: 'viewer',
|
||
label: '查看者',
|
||
type: 'text',
|
||
placeholder: '请输入查看者',
|
||
operators: ['eq', 'contains', 'like'],
|
||
defaultOperator: 'contains'
|
||
},
|
||
{
|
||
value: 'client_id',
|
||
label: '客户端ID',
|
||
type: 'text',
|
||
placeholder: '请输入客户端ID',
|
||
operators: ['eq', 'contains', 'like'],
|
||
defaultOperator: 'contains'
|
||
},
|
||
|
||
]
|
||
|
||
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,
|
||
express_name: item.express?.name || '',
|
||
express_platform_label: item.express?.platform ?? ''
|
||
}))
|
||
}
|
||
|
||
/** 中国省市区(与 ManualCreatePendingOrderDialog 一致,数据来自 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 }))
|
||
}
|
||
|
||
export function buildCityOptions(provinceCode: string): RegionSelectOption[] {
|
||
const province = chinaRegionTree.find((item) => item.code === provinceCode)
|
||
return (province?.children || []).map((item) => ({ label: item.name, value: item.code }))
|
||
}
|
||
|
||
export function buildDistrictOptions(provinceCode: string, cityCode: string): RegionSelectOption[] {
|
||
const province = chinaRegionTree.find((item) => item.code === provinceCode)
|
||
const city = (province?.children || []).find((item) => item.code === cityCode)
|
||
return (city?.children || []).map((item) => ({ label: item.name, value: item.code }))
|
||
}
|
||
|
||
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,
|
||
name: '',
|
||
express_code: '',
|
||
platform_code: '',
|
||
express_platform_id: 1,
|
||
sender_name: '',
|
||
sender_tel: '',
|
||
weight: 1,
|
||
token: '',
|
||
viewer: '',
|
||
client_id: '',
|
||
is_share: 0,
|
||
share_id: undefined as number | undefined,
|
||
can_use_count: undefined as number | undefined,
|
||
site_code: '',
|
||
shop_name: '',
|
||
province_name: '',
|
||
city_name: '',
|
||
district_name: '',
|
||
product_type: ''
|
||
}
|
||
|
||
function omitEmptyString(val: unknown): string | undefined {
|
||
const s = String(val ?? '').trim()
|
||
return s || undefined
|
||
}
|
||
|
||
function omitOptionalNumber(val: unknown): number | undefined {
|
||
if (val === null || val === undefined || val === '') return undefined
|
||
const n = Number(val)
|
||
return Number.isFinite(n) ? n : undefined
|
||
}
|
||
|
||
/** setting.expressInterface.create 请求体(禁止 warehouse_id / express_platform_id) */
|
||
export function buildExpressInterfaceCreatePayload(form: Record<string, any>) {
|
||
const payload: Record<string, any> = {
|
||
name: String(form.name || '').trim(),
|
||
express_code: String(form.express_code || '').trim(),
|
||
platform_code: String(form.platform_code || '').trim()
|
||
}
|
||
|
||
const optionalStrings = [
|
||
'sender_name',
|
||
'sender_tel',
|
||
'token',
|
||
'viewer',
|
||
'client_id',
|
||
'province_name',
|
||
'city_name',
|
||
'district_name',
|
||
'site_code',
|
||
'product_type',
|
||
'shop_name'
|
||
] as const
|
||
for (const key of optionalStrings) {
|
||
const v = omitEmptyString(form[key])
|
||
if (v !== undefined) payload[key] = v
|
||
}
|
||
|
||
const weight = omitOptionalNumber(form.weight)
|
||
if (weight !== undefined) payload.weight = Math.round(weight)
|
||
|
||
const isShare = omitOptionalNumber(form.is_share)
|
||
if (isShare !== undefined) payload.is_share = isShare
|
||
|
||
const shareId = omitOptionalNumber(form.share_id)
|
||
if (shareId !== undefined) payload.share_id = shareId
|
||
|
||
const canUseCount = omitOptionalNumber(form.can_use_count)
|
||
if (canUseCount !== undefined) payload.can_use_count = canUseCount
|
||
|
||
return payload
|
||
}
|
||
|
||
/** setting.expressInterface.update 请求体 */
|
||
export function buildExpressInterfaceUpdatePayload(form: Record<string, any>) {
|
||
const payload = buildExpressInterfaceCreatePayload(form)
|
||
payload.id = form.id
|
||
const expressPlatformId = omitOptionalNumber(form.express_platform_id)
|
||
if (expressPlatformId !== undefined) payload.express_platform_id = expressPlatformId
|
||
return payload
|
||
}
|