oms/frontend/src/api/shopDatabase.ts
liufumingren 23a7715dc9 feat(shop): 数据库店铺新增回传单号设置,发货确认后把运单号写回客户库
- shop_database_configs 增加回传设置:方式(不回传/写回字段/写回行)、目标表、运单号字段、定位字段、间隔符、写回行字段映射
- 配置弹窗新增「回传单号」区块:写回字段选表+字段+定位字段+间隔符;写回行按回传值(订单号/运单号/快递公司编码/快递公司名称/回传时间)逐项选列
- 保存时校验回传表与列在客户库中存在,回传行必填项(订单号、运单号)
- 发货确认(WMS 回推运单号)后自动回传:写回字段按间隔符合并写单值,写回行按订单号先删后插保证幂等;失败只记日志不影响发货确认

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-05 16:33:15 +08:00

167 lines
4.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { get, post, put } from '@/utils/request'
/** 数据库店铺可选类型 */
export interface ShopDatabaseTypeOption {
value: string
label: string
}
/** 数据库店铺可映射的 OMS 字段 */
export interface ShopDatabaseDatasetField {
key: string
label: string
required: boolean
}
/** 一组数据集定义(商品表 / 订单表 / 订单明细表) */
export interface ShopDatabaseDataset {
/** 数据集标识:product / order / order_item */
key: string
label: string
/** 保存表名的配置字段名 */
table_column: string
/** 保存字段映射的配置字段名 */
field_column: string
fields: ShopDatabaseDatasetField[]
}
/** OMS 字段 → 源表列名 */
export type ShopDatabaseFieldMapping = Record<string, string>
/** 回传单号方式选项 */
export interface ShopDatabaseCallbackMode {
value: number
label: string
}
/** 数据库店铺配置(密码不回显) */
export interface ShopDatabaseConfig {
shop_id: number
db_type: string
host: string
port: number | null
database_name: string
username: string
/** 是否已保存密码(密码不回显) */
has_password: boolean
product_table: string
order_table: string
order_item_table: string
product_fields: ShopDatabaseFieldMapping
order_fields: ShopDatabaseFieldMapping
order_item_fields: ShopDatabaseFieldMapping
/** 回传单号方式:0不回传 1写回字段 2写回行 */
callback_mode: number
callback_table: string
callback_field: string
callback_key_field: string
callback_separator: string
callback_fields: ShopDatabaseFieldMapping
last_test_at?: string | null
last_test_success?: number | null
last_test_message?: string
}
/** 连接参数(保存 / 测试 / 连接共用),密码留空表示沿用已保存密码 */
export interface ShopDatabaseConnectionPayload {
db_type?: string
host?: string
port?: number | null
database_name?: string
username?: string
password?: string
}
/** 保存配置入参(连接参数 + 三张数据集选表与字段映射) */
export interface ShopDatabaseSavePayload extends ShopDatabaseConnectionPayload {
product_table?: string
order_table?: string
order_item_table?: string
product_fields?: ShopDatabaseFieldMapping
order_fields?: ShopDatabaseFieldMapping
order_item_fields?: ShopDatabaseFieldMapping
callback_mode?: number
callback_table?: string
callback_field?: string
callback_key_field?: string
callback_separator?: string
callback_fields?: ShopDatabaseFieldMapping
}
/** 库内数据表 */
export interface ShopDatabaseTable {
name: string
comment?: string
}
/** 表字段 */
export interface ShopDatabaseColumn {
name: string
type: string
comment?: string
}
/** 配置详情(含可映射字段定义) */
export function getShopDatabaseConfig(shopId: number) {
return get<{
config: ShopDatabaseConfig
db_types: ShopDatabaseTypeOption[]
datasets: ShopDatabaseDataset[]
callback_modes: ShopDatabaseCallbackMode[]
callback_fields: ShopDatabaseDatasetField[]
default_callback_separator: string
}>(`/shops/${shopId}/database-config`)
}
/** 保存配置:连接信息 + 选表 + 字段映射 */
export function saveShopDatabaseConfig(shopId: number, data: ShopDatabaseSavePayload) {
return put<{ id: number; verified: boolean }>(`/shops/${shopId}/database-config`, data)
}
/** 测试连通(服务端连接,浏览器不直连) */
export function testShopDatabaseConnection(shopId: number, data: ShopDatabaseConnectionPayload) {
return post<{ success: boolean; message: string; server_version?: string }>(
`/shops/${shopId}/database-config/test`,
data,
)
}
/** 连接并拉取库内数据表 */
export function connectShopDatabase(shopId: number, data: ShopDatabaseConnectionPayload) {
return post<{ success: boolean; tables: ShopDatabaseTable[] }>(
`/shops/${shopId}/database-config/connect`,
data,
)
}
/** 用已保存配置拉取数据表 */
export function getShopDatabaseTables(shopId: number) {
return get<{ tables: ShopDatabaseTable[] }>(`/shops/${shopId}/database-config/tables`)
}
/** 拉取指定表的字段(字段映射下拉用) */
export function getShopDatabaseColumns(shopId: number, table: string) {
return get<{ table: string; columns: ShopDatabaseColumn[] }>(`/shops/${shopId}/database-config/columns`, {
table,
})
}
/** 拉取店铺商品(按配置的商品表与字段映射读取客户库) */
export function pullShopDatabaseProducts(shopId: number) {
return post<{ total: number; created: number; updated: number }>(
`/shops/${shopId}/database-config/pull-products`,
{},
)
}
/** 拉取店铺订单(按配置的订单/明细表与字段映射读取客户库明文订单) */
export function pullShopDatabaseOrders(shopId: number, data: { days?: number }) {
return post<{
orders: number
created: number
updated: number
items: number
skipped: number
}>(`/shops/${shopId}/database-config/pull-orders`, data)
}