店小秘接入:拉取「有货」订单
- 新增 listOrders:POST /api/package/list.json,state=allocated_has,按 totalPage 逐页拉 - 一个包裹一张订单;收件人取 dxmPackageAddr,明细跳过取消行 - 店小秘明细只给平台规格ID(无商品ID):店铺商品匹配支持按规格ID在本店铺唯一命中, 落库时回填 outer_item_id - 补单测(订单归一化、分页、规格ID兜底匹配)与接入文档 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
a586e3f18b
commit
d7e622a0b0
@ -48,6 +48,13 @@ const PRODUCT_LIST_PAGE_PATH = '/web/shopifyProduct/productList';
|
||||
const PRODUCT_PAGE_SIZE = 100;
|
||||
const PRODUCT_MAX_PAGES = 100;
|
||||
|
||||
/** 订单列表(「有货」待发货队列) */
|
||||
const ORDER_LIST_PATH = '/api/package/list.json';
|
||||
const ORDER_LIST_PAGE_PATH = '/web/order/allocated/has';
|
||||
const ORDER_STATE_HAS_STOCK = 'allocated_has';
|
||||
const ORDER_PAGE_SIZE = 100;
|
||||
const ORDER_MAX_PAGES = 100;
|
||||
|
||||
const REQUEST_TIMEOUT_MS = 30000;
|
||||
|
||||
const USER_AGENT =
|
||||
@ -123,6 +130,17 @@ function toNumber(value) {
|
||||
return Number.isFinite(num) ? num : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 毫秒时间戳 → Date(空值 / 非法值返回 null)
|
||||
* @param {*} value 毫秒时间戳
|
||||
* @return {Date|null} 时间
|
||||
*/
|
||||
function parseTime(value) {
|
||||
const ts = Number(value);
|
||||
if (!Number.isFinite(ts) || ts <= 0) return null;
|
||||
return new Date(ts);
|
||||
}
|
||||
|
||||
/**
|
||||
* 时间戳(毫秒)→ `YYYY-MM-DD HH:mm:ss`(北京时间),空值返回空串
|
||||
* @param {*} value 毫秒时间戳
|
||||
@ -215,6 +233,38 @@ class DxmService extends PrintPlatformService {
|
||||
return json.data || {};
|
||||
}
|
||||
|
||||
/**
|
||||
* POST 表单到平台 JSON 接口(带登录会话)
|
||||
*
|
||||
* 订单列表接口 `code=0` 时数据在 `data` 里,但有些接口直接给 `data.page`;
|
||||
* 这里统一返回 `data`,与 `requestJson` 保持一致。
|
||||
* @param {string} path 接口路径
|
||||
* @param {URLSearchParams} form 表单
|
||||
* @param {Object} [options] 参数
|
||||
* @param {string} [options.referer] Referer 页面路径
|
||||
* @return {Promise<Object>} 平台 data 字段
|
||||
*/
|
||||
async requestForm(path, form, { referer = ORDER_LIST_PAGE_PATH } = {}) {
|
||||
if (!this.cookie) throw new Error(`店小秘账号未登录:请先登录(${path})`);
|
||||
const res = await this.request(`${this.siteUrl}${path}`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
...AJAX_HEADERS,
|
||||
referer: `${this.siteUrl}${referer}`,
|
||||
cookie: this.cookie,
|
||||
},
|
||||
body: form.toString(),
|
||||
});
|
||||
const json = res.json;
|
||||
if (!json) {
|
||||
throw new Error(`店小秘接口返回异常(HTTP ${res.status}):${String(res.text || '').slice(0, 100)}`);
|
||||
}
|
||||
if (Number(json.code) !== 0) {
|
||||
throw new Error(`店小秘接口失败:${json.msg || json.message || `code=${json.code}`}(${path})`);
|
||||
}
|
||||
return json.data || {};
|
||||
}
|
||||
|
||||
/**
|
||||
* 已授权店铺的平台标识列表
|
||||
*
|
||||
@ -424,6 +474,193 @@ class DxmService extends PrintPlatformService {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 拉取店铺「有货」订单(待发货队列)
|
||||
*
|
||||
* 店小秘订单按状态分列表,OMS 只取「有货」(`state=allocated_has`):
|
||||
* `POST /api/package/list.json`,其它状态(待审核/缺货/已发货…)一律忽略。
|
||||
* 每个包裹(package)作为一张 OMS 订单,明细取 `productList`。
|
||||
* @param {Object} shop OMS 店铺记录(external_data 为平台店铺原始结构)
|
||||
* @param {Object} [options] 查询参数(当前不按时间窗过滤,有货队列本身就是待处理集合)
|
||||
* @return {Promise<{total: number, list: Array, hosts: Array, skipped: number, finished: Array}>} 订单列表
|
||||
*/
|
||||
async listOrders(shop, options = {}) { // eslint-disable-line no-unused-vars
|
||||
const data = (shop && shop.external_data) || {};
|
||||
const shopId = String(data.id || data.Id || '').trim();
|
||||
if (!shopId) throw new Error('店铺缺少平台店铺ID,无法拉取平台订单');
|
||||
|
||||
const rows = [];
|
||||
let fetched = 0;
|
||||
let totalPage = 1;
|
||||
let totalSize = 0;
|
||||
let pages = 0;
|
||||
|
||||
for (let pageNo = 1; pageNo <= Math.min(totalPage, ORDER_MAX_PAGES); pageNo += 1) {
|
||||
const page = await this.fetchOrderPage(shopId, pageNo);
|
||||
pages += 1;
|
||||
totalPage = page.totalPage || pageNo;
|
||||
totalSize = page.totalSize || totalSize;
|
||||
fetched += page.list.length;
|
||||
rows.push(...page.list.map(row => this.normalizeOrder(row, shop)));
|
||||
if (!page.list.length) break;
|
||||
}
|
||||
|
||||
return {
|
||||
total: rows.length,
|
||||
list: rows,
|
||||
// 有货队列里的都还需要处理,跳过/已完成一律为空
|
||||
skipped: 0,
|
||||
finished: [],
|
||||
hosts: [{
|
||||
host: new URL(this.siteUrl).host,
|
||||
total: totalSize,
|
||||
unique: rows.length,
|
||||
pages,
|
||||
verified: totalSize === 0 || fetched >= totalSize,
|
||||
areas: [],
|
||||
}],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 拉取一页「有货」订单
|
||||
* @param {string} shopId 平台店铺ID
|
||||
* @param {number} pageNo 页码(1 基)
|
||||
* @return {Promise<{list: Array, totalPage: number, totalSize: number}>} 分页结果
|
||||
*/
|
||||
async fetchOrderPage(shopId, pageNo = 1) {
|
||||
const form = new URLSearchParams({
|
||||
pageNo: String(pageNo),
|
||||
pageSize: String(ORDER_PAGE_SIZE),
|
||||
shopId,
|
||||
state: ORDER_STATE_HAS_STOCK,
|
||||
platform: '',
|
||||
isSearch: '0',
|
||||
searchType: 'orderId',
|
||||
authId: '-1',
|
||||
startTime: '',
|
||||
endTime: '',
|
||||
country: '',
|
||||
orderField: 'order_pay_time',
|
||||
isVoided: '0',
|
||||
isRemoved: '0',
|
||||
ruleId: '-1',
|
||||
sysRule: '',
|
||||
applyType: '',
|
||||
applyStatus: '',
|
||||
printJh: '-1',
|
||||
printMd: '-1',
|
||||
commitPlatform: '',
|
||||
productStatus: '',
|
||||
jhComment: '-1',
|
||||
storageId: '0',
|
||||
isOversea: '-1',
|
||||
isFree: '0',
|
||||
isBatch: '0',
|
||||
history: '',
|
||||
custom: '-1',
|
||||
timeOut: '0',
|
||||
refundStatus: '0',
|
||||
buyerAccount: '',
|
||||
forbiddenStatus: '-1',
|
||||
forbiddenReason: '0',
|
||||
behindTrack: '-1',
|
||||
orderId: '',
|
||||
axios_cancelToken: 'true',
|
||||
});
|
||||
const result = await this.requestForm(`${ORDER_LIST_PATH}`, form, { referer: ORDER_LIST_PAGE_PATH });
|
||||
const page = (result && result.page) || {};
|
||||
return {
|
||||
list: Array.isArray(page.list) ? page.list : [],
|
||||
totalPage: Number(page.totalPage) || 1,
|
||||
totalSize: Number(page.totalSize) || 0,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 店小秘包裹 → 统一订单结构(明细保留平台快照,系统商品匹配由上层完成)
|
||||
* @param {Object} row 平台包裹原始结构
|
||||
* @param {Object} [shop] OMS 店铺(用于补店铺名)
|
||||
* @return {Object} 统一订单结构
|
||||
*/
|
||||
normalizeOrder(row, shop) {
|
||||
const data = row || {};
|
||||
const addr = data.dxmPackageAddr || {};
|
||||
const platform = resolvePlatform(data.platform);
|
||||
const externalOrderId = String(data.id || data.orderId || '').trim();
|
||||
const goodsAmount = toNumber(data.orderAmount);
|
||||
const shipAmount = toNumber(data.shipAmount);
|
||||
const products = Array.isArray(data.productList) ? data.productList.filter(item => item && !item.cancelFlag) : [];
|
||||
const address = [ addr.detailAddress, addr.address2 ].map(value => String(value || '').trim()).filter(Boolean).join(' ');
|
||||
const fullAddress = [ addr.countryCh || addr.country, addr.province, addr.city, addr.district, address ]
|
||||
.map(value => String(value || '').trim())
|
||||
.filter(Boolean)
|
||||
.join(', ');
|
||||
return {
|
||||
external_order_id: externalOrderId,
|
||||
// 平台单号(店铺单号)仅留痕,OMS 去重键用包裹ID
|
||||
platform_order_no: String(data.orderId || '') || null,
|
||||
external_shop_id: String(data.platformShopId || data.shopId || ''),
|
||||
external_shop_name: String((shop && shop.name) || ''),
|
||||
platform_type: String(data.platform || ''),
|
||||
platform_code: platform.code,
|
||||
platform_name: platform.name,
|
||||
platform_status: String(data.orderState || data.orderStatePlatform || ''),
|
||||
order_no: externalOrderId,
|
||||
total_amount: Number((goodsAmount + shipAmount).toFixed(2)),
|
||||
pay_amount: Number((goodsAmount + shipAmount).toFixed(2)),
|
||||
receiver: {
|
||||
name: String(addr.contactPerson || data.contactName || ''),
|
||||
phone: String(addr.phoneNumber || addr.mobile || ''),
|
||||
address,
|
||||
full_address: fullAddress,
|
||||
province: String(addr.province || ''),
|
||||
city: String(addr.city || ''),
|
||||
area: String(addr.district || ''),
|
||||
town: '',
|
||||
masked: !!addr.hasAsterisk,
|
||||
},
|
||||
remark: String(data.sellerComment || data.orderRemark || '') || null,
|
||||
external_created_at: parseTime(data.orderCreateTime),
|
||||
external_updated_at: parseTime(data.updateTime || data.orderPayTime),
|
||||
raw: data,
|
||||
items: products.map(item => this.normalizeOrderItem(item)),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 店小秘订单明细 → 统一明细结构
|
||||
*
|
||||
* 订单明细只给平台规格ID(`productId`/`vid` 都是规格ID,与店铺商品的 `outer_sku_id` 对应),
|
||||
* 不给平台商品ID,因此 `outer_item_id` 留空,由店铺商品匹配按规格ID兜底并回填。
|
||||
* @param {Object} item 平台明细原始结构
|
||||
* @return {Object} 统一明细结构
|
||||
*/
|
||||
normalizeOrderItem(item) {
|
||||
const data = item || {};
|
||||
const skuId = String(data.productId || data.vid || data.stockProductId || '').trim();
|
||||
const skuCode = String(data.productSubSku || '').trim();
|
||||
const attrs = Array.isArray(data.attrList)
|
||||
? data.attrList.map(attr => `${attr.pName || ''}:${attr.pValue || ''}`).join(', ')
|
||||
: String(data.attrListStr || '');
|
||||
const quantity = toNumber(data.quantity || data.productCount) || 1;
|
||||
const price = toNumber(data.price);
|
||||
return {
|
||||
external_item_id: String(data.id || ''),
|
||||
outer_item_id: '',
|
||||
outer_sku_id: skuId,
|
||||
// 店小秘的 productSubSku 多数平台就是规格ID本身,不能当规格编码用
|
||||
sku_code: skuCode && skuCode !== skuId ? skuCode : null,
|
||||
name: String(data.productName || '').slice(0, 255),
|
||||
spec: attrs.slice(0, 255),
|
||||
image: String(data.oriProductImg || data.productImg || '').slice(0, 255),
|
||||
quantity,
|
||||
price,
|
||||
total_amount: Number((price * quantity).toFixed(2)),
|
||||
raw: data,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 平台过滤参数归一化(支持逗号分隔字符串 / 数组)
|
||||
* @param {string|string[]} value 过滤值
|
||||
|
||||
@ -37,6 +37,9 @@ function createShopProductMatcher(rows) {
|
||||
const skuIndex = new Map();
|
||||
const codeIndex = new Map();
|
||||
const itemIndex = new Map();
|
||||
// 明细只给平台规格ID、没给商品ID时(店小秘)按规格ID/规格编码兜底
|
||||
const skuOnlyIndex = new Map();
|
||||
const codeOnlyIndex = new Map();
|
||||
|
||||
for (const row of rows || []) {
|
||||
const itemId = String((row && row.outer_item_id) || '').trim();
|
||||
@ -44,16 +47,23 @@ function createShopProductMatcher(rows) {
|
||||
if (!itemId || !id) continue;
|
||||
const skuId = String((row && row.outer_sku_id) || '').trim();
|
||||
if (skuId && !skuIndex.has(`${itemId}||${skuId}`)) skuIndex.set(`${itemId}||${skuId}`, id);
|
||||
if (skuId && !skuOnlyIndex.has(skuId)) skuOnlyIndex.set(skuId, id);
|
||||
const code = String((row && row.outer_sku_code) || '').trim();
|
||||
if (code && !codeIndex.has(skuCodeKey(itemId, code))) codeIndex.set(skuCodeKey(itemId, code), id);
|
||||
if (code && !codeOnlyIndex.has(code)) codeOnlyIndex.set(code, id);
|
||||
if (!itemIndex.has(itemId)) itemIndex.set(itemId, id);
|
||||
}
|
||||
|
||||
return line => {
|
||||
const itemId = String((line && line.outer_item_id) || '').trim();
|
||||
if (!itemId) return null;
|
||||
const skuId = String((line && line.outer_sku_id) || '').trim();
|
||||
const code = String((line && line.outer_sku_code) || '').trim();
|
||||
// 平台没给商品ID(如店小秘只给规格ID):按规格ID/规格编码在整个店铺内唯一命中
|
||||
if (!itemId) {
|
||||
if (skuId) return skuOnlyIndex.get(skuId) || null;
|
||||
if (code) return codeOnlyIndex.get(code) || null;
|
||||
return null;
|
||||
}
|
||||
if (skuId) {
|
||||
const hit = skuIndex.get(`${itemId}||${skuId}`);
|
||||
if (hit) return hit;
|
||||
@ -72,19 +82,25 @@ function createShopProductMatcher(rows) {
|
||||
* 只有没有店铺信息的明细(手工录入)才退回主体范围。
|
||||
* @param {{outer_item_id?:string, outer_sku_id?:string, outer_sku_code?:string}} line 订单明细
|
||||
* @param {{shopId?:number|string, principalId?:number|string}} scope 归属范围
|
||||
* @return {Array<Object>} Sequelize where 条件列表,缺少平台商品ID时返回空数组
|
||||
* @return {Array<Object>} Sequelize where 条件列表,没有任何商品/规格标识时返回空数组
|
||||
*/
|
||||
function shopProductScopes(line, scope = {}) {
|
||||
const itemId = String((line && line.outer_item_id) || '').trim();
|
||||
if (!itemId) return [];
|
||||
const skuId = String((line && line.outer_sku_id) || '').trim();
|
||||
const code = String((line && line.outer_sku_code) || '').trim();
|
||||
|
||||
const owner = {};
|
||||
const shopId = scope.shopId;
|
||||
if (shopId != null && shopId !== '') owner.shop_id = Number(shopId);
|
||||
else if (scope.principalId != null && scope.principalId !== '') owner.principal_id = Number(scope.principalId);
|
||||
|
||||
const skuId = String((line && line.outer_sku_id) || '').trim();
|
||||
const code = String((line && line.outer_sku_code) || '').trim();
|
||||
// 平台没给商品ID(店小秘订单明细只带规格ID):按规格ID / 规格编码在本店铺内查找
|
||||
if (!itemId) {
|
||||
const where = [];
|
||||
if (skuId) where.push({ outer_sku_id: skuId, ...owner });
|
||||
if (code) where.push({ outer_sku_code: code, ...owner });
|
||||
return where;
|
||||
}
|
||||
|
||||
const where = [];
|
||||
if (skuId) where.push({ outer_item_id: itemId, outer_sku_id: skuId, ...owner });
|
||||
|
||||
@ -632,7 +632,7 @@ class ExternalPrintOrderService extends Service {
|
||||
item_type: bound.item_type,
|
||||
parse_status: bound.parse_status,
|
||||
parse_msg: bound.parse_msg,
|
||||
outer_item_id: line.outer_item_id || null,
|
||||
outer_item_id: line.outer_item_id || (matched && matched.outer_item_id) || null,
|
||||
outer_sku_id: line.outer_sku_id || null,
|
||||
sku_code: line.sku_code || null,
|
||||
// 未绑定系统商品时保留平台原样快照
|
||||
|
||||
@ -278,6 +278,140 @@ describe('printPlatform/dxm listProducts', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('printPlatform/dxm listOrders', () => {
|
||||
const shop = { name: 'c1eqfe-0a', external_data: { id: '9139394', platform: 'shopify', name: 'c1eqfe-0a' } };
|
||||
const packageRow = (id, extra = {}) => ({
|
||||
id,
|
||||
orderId: `9139394-${id.slice(-4)}`,
|
||||
shopId: '9139394',
|
||||
platform: 'shopify',
|
||||
orderState: 'allocated_has',
|
||||
orderStatePlatform: 'paid_fulfilled',
|
||||
orderAmount: 54.9,
|
||||
shipAmount: 6.9,
|
||||
orderCreateTime: Date.UTC(2026, 10, 4, 0, 0, 0),
|
||||
updateTime: Date.UTC(2026, 10, 5, 0, 0, 0),
|
||||
dxmPackageAddr: {
|
||||
contactPerson: 'Dave Fohne',
|
||||
phoneNumber: '+16183430668',
|
||||
countryCh: '美国',
|
||||
province: 'Illinois',
|
||||
city: 'Collinsville',
|
||||
district: '',
|
||||
detailAddress: '620 E Church St',
|
||||
hasAsterisk: false,
|
||||
},
|
||||
productList: [
|
||||
{
|
||||
id: `item-${id}`,
|
||||
productId: '49366194192574',
|
||||
productSubSku: '49366194192574',
|
||||
productName: 'Alaskan Red King Crab Legs',
|
||||
oriProductImg: 'https://cdn.example.com/p.png',
|
||||
attrList: [{ pName: 'Variants', pValue: 'Buy 1 Get 1 Free' }],
|
||||
quantity: 1,
|
||||
price: 54.9,
|
||||
cancelFlag: false,
|
||||
},
|
||||
{
|
||||
id: `gift-${id}`,
|
||||
productId: '999',
|
||||
productName: 'Canceled gift',
|
||||
quantity: 1,
|
||||
price: 1,
|
||||
cancelFlag: true,
|
||||
},
|
||||
],
|
||||
...extra,
|
||||
});
|
||||
|
||||
function createOrderService(pages) {
|
||||
const { service } = createService({ account: { cookie: 'JSESSIONID=ok' } });
|
||||
const calls = [];
|
||||
service.requestForm = async (path, form, options) => {
|
||||
calls.push({ path, form, options });
|
||||
const pageNo = Number(form.get('pageNo'));
|
||||
return { page: pages[pageNo - 1] || { list: [], totalPage: pages.length, totalSize: 0 } };
|
||||
};
|
||||
return { service, calls };
|
||||
}
|
||||
|
||||
it('只拉「有货」(allocated_has)队列并归一化订单/明细', async () => {
|
||||
const { service, calls } = createOrderService([
|
||||
{ list: [ packageRow('184804423947346433') ], totalPage: 1, totalSize: 1 },
|
||||
]);
|
||||
|
||||
const result = await service.listOrders(shop);
|
||||
|
||||
assert.strictEqual(calls.length, 1);
|
||||
assert.strictEqual(calls[0].path, '/api/package/list.json');
|
||||
assert.strictEqual(calls[0].form.get('state'), 'allocated_has');
|
||||
assert.strictEqual(calls[0].form.get('shopId'), '9139394');
|
||||
|
||||
assert.strictEqual(result.total, 1);
|
||||
assert.strictEqual(result.skipped, 0);
|
||||
assert.deepStrictEqual(result.finished, []);
|
||||
assert.strictEqual(result.hosts[0].verified, true);
|
||||
|
||||
const order = result.list[0];
|
||||
assert.strictEqual(order.external_order_id, '184804423947346433');
|
||||
assert.strictEqual(order.platform_order_no, '9139394-6433');
|
||||
assert.strictEqual(order.external_shop_id, '9139394');
|
||||
assert.strictEqual(order.external_shop_name, 'c1eqfe-0a');
|
||||
assert.strictEqual(order.platform_status, 'allocated_has');
|
||||
assert.strictEqual(order.platform_code, 'OTHER');
|
||||
assert.strictEqual(order.total_amount, 61.8);
|
||||
assert.strictEqual(order.pay_amount, 61.8);
|
||||
assert.deepStrictEqual(order.receiver, {
|
||||
name: 'Dave Fohne',
|
||||
phone: '+16183430668',
|
||||
address: '620 E Church St',
|
||||
full_address: '美国, Illinois, Collinsville, 620 E Church St',
|
||||
province: 'Illinois',
|
||||
city: 'Collinsville',
|
||||
area: '',
|
||||
town: '',
|
||||
masked: false,
|
||||
});
|
||||
|
||||
// 取消的明细不下发
|
||||
assert.strictEqual(order.items.length, 1);
|
||||
assert.deepStrictEqual(
|
||||
(({ external_item_id, outer_item_id, outer_sku_id, sku_code, name, spec, price, quantity, total_amount }) => ({
|
||||
external_item_id, outer_item_id, outer_sku_id, sku_code, name, spec, price, quantity, total_amount,
|
||||
}))(order.items[0]),
|
||||
{
|
||||
external_item_id: 'item-184804423947346433',
|
||||
// 店小秘明细只给规格ID,商品ID留空由店铺商品匹配回填
|
||||
outer_item_id: '',
|
||||
outer_sku_id: '49366194192574',
|
||||
sku_code: null,
|
||||
name: 'Alaskan Red King Crab Legs',
|
||||
spec: 'Variants:Buy 1 Get 1 Free',
|
||||
price: 54.9,
|
||||
quantity: 1,
|
||||
total_amount: 54.9,
|
||||
}
|
||||
);
|
||||
assert.strictEqual(order.external_created_at.getTime(), Date.UTC(2026, 10, 4, 0, 0, 0));
|
||||
});
|
||||
|
||||
it('按 totalPage 逐页拉取', async () => {
|
||||
const { service, calls } = createOrderService([
|
||||
{ list: [ packageRow('1') ], totalPage: 2, totalSize: 2 },
|
||||
{ list: [ packageRow('2') ], totalPage: 2, totalSize: 2 },
|
||||
]);
|
||||
const result = await service.listOrders(shop);
|
||||
assert.strictEqual(calls.length, 2);
|
||||
assert.strictEqual(result.total, 2);
|
||||
});
|
||||
|
||||
it('店铺缺少平台店铺ID时报错', async () => {
|
||||
const { service } = createService({ account: { cookie: 'JSESSIONID=ok' } });
|
||||
await assert.rejects(service.listOrders({ external_data: {} }), /缺少平台店铺ID/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('printPlatform/dxm listShops', () => {
|
||||
const shopifyShop = (id, name, extra = { }) => ({
|
||||
id: String(id),
|
||||
|
||||
@ -30,12 +30,19 @@ describe('app/lib/shopProductMatch 店铺商品匹配口径', () => {
|
||||
|
||||
it('空输入返回 null', () => {
|
||||
const pick = createShopProductMatcher(rows);
|
||||
assert.strictEqual(pick({ outer_item_id: '', outer_sku_id: '1' }), null);
|
||||
assert.strictEqual(pick({ outer_item_id: '', outer_sku_id: '' }), null);
|
||||
assert.strictEqual(pick({ outer_item_id: '404' }), null);
|
||||
assert.strictEqual(pick(null), null);
|
||||
assert.strictEqual(createShopProductMatcher([])({ outer_item_id: '1' }), null);
|
||||
});
|
||||
|
||||
it('平台没给商品ID、只给规格ID时按规格ID唯一命中(店小秘口径)', () => {
|
||||
const pick = createShopProductMatcher(rows);
|
||||
assert.strictEqual(pick({ outer_sku_id: '3548726733104898' }), 12);
|
||||
assert.strictEqual(pick({ outer_sku_id: '006-1' }), 22);
|
||||
assert.strictEqual(pick({ outer_sku_id: '999' }), null);
|
||||
});
|
||||
|
||||
describe('shopProductScope 查询口径', () => {
|
||||
it('有店铺信息时只认本店铺,不带主体条件', () => {
|
||||
assert.deepStrictEqual(
|
||||
@ -58,8 +65,15 @@ describe('app/lib/shopProductMatch 店铺商品匹配口径', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('没有平台商品ID时返回 null(不能按空条件乱查)', () => {
|
||||
assert.strictEqual(shopProductScope({ outer_sku_id: '1' }, { shopId: 19 }), null);
|
||||
it('没有平台商品ID但有规格ID时按规格ID匹配本店铺(店小秘口径)', () => {
|
||||
assert.deepStrictEqual(
|
||||
shopProductScope({ outer_sku_id: '3548726733104898' }, { shopId: 19 }),
|
||||
{ outer_sku_id: '3548726733104898', shop_id: 19 }
|
||||
);
|
||||
});
|
||||
|
||||
it('商品ID与规格ID都没有时返回 null(不能按空条件乱查)', () => {
|
||||
assert.strictEqual(shopProductScope({}, { shopId: 19 }), null);
|
||||
assert.strictEqual(shopProductScope(null, { shopId: 19 }), null);
|
||||
});
|
||||
});
|
||||
|
||||
66
店小秘接入.md
66
店小秘接入.md
@ -154,12 +154,70 @@ OMS 实现(`dxm.js#listProducts`):
|
||||
|
||||
---
|
||||
|
||||
## 5. 待接入
|
||||
## 5. 订单(「有货」待发货队列)
|
||||
|
||||
`dxm.js` 目前实现 `getLoginCaptcha / login / checkSession / listShopSources / listShops / listProducts`,
|
||||
OMS 只拉「有货」(`allocated_has`) 列表,其它状态忽略(实测 2026-11-24):
|
||||
|
||||
```
|
||||
POST /api/package/list.json (x-www-form-urlencoded)
|
||||
pageNo=1&pageSize=100&shopId=<平台店铺ID>&state=allocated_has
|
||||
&platform=&isSearch=0&searchType=orderId&authId=-1&startTime=&endTime=&country=
|
||||
&orderField=order_pay_time&isVoided=0&isRemoved=0&ruleId=-1&sysRule=&applyType=&applyStatus=
|
||||
&printJh=-1&printMd=-1&commitPlatform=&productStatus=&jhComment=-1&storageId=0
|
||||
&isOversea=-1&isFree=0&isBatch=0&history=&custom=-1&timeOut=0&refundStatus=0
|
||||
&buyerAccount=&forbiddenStatus=-1&forbiddenReason=0&behindTrack=-1&orderId=&axios_cancelToken=true
|
||||
```
|
||||
|
||||
```jsonc
|
||||
{
|
||||
"code": 0, "msg": "Successful",
|
||||
"data": {
|
||||
"page": {
|
||||
"pageNo": 1, "pageSize": 100, "totalPage": 1, "totalSize": 60,
|
||||
"list": [{
|
||||
"id": "184804423947346433", // 包裹ID(OMS external_order_id,唯一)
|
||||
"shopId": "9139394",
|
||||
"orderId": "9139394-1350", // 平台店铺单号(仅留痕)
|
||||
"platform": "shopify",
|
||||
"orderState": "allocated_has",
|
||||
"orderStatePlatform": "paid_fulfilled",
|
||||
"orderAmount": 54.9, "shipAmount": 6.9, "orderUnit": "USD",
|
||||
"orderCreateTime": 1793765481143, "updateTime": 1795435680000,
|
||||
"dxmPackageAddr": { // 收件人
|
||||
"contactPerson": "Dave Fohne", "phoneNumber": "+16183430668",
|
||||
"countryCh": "美国", "province": "Illinois", "city": "Collinsville", "district": "",
|
||||
"detailAddress": "620 E Church St", "hasAsterisk": false
|
||||
},
|
||||
"productList": [{
|
||||
"id": "184807702548539675", // 明细ID(OMS external_item_id)
|
||||
"productId": "49366194192574", // **平台规格ID**(= 店铺商品的 outer_sku_id)
|
||||
"productSubSku": "49366194192574",
|
||||
"productName": "…", "oriProductImg": "https://cdn.shopify.com/…",
|
||||
"attrList": [{ "pName": "Variants", "pValue": "Buy 1 Get 1 Free" }],
|
||||
"quantity": 1, "price": 54.9, "cancelFlag": false
|
||||
}]
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
OMS 实现(`dxm.js#listOrders`):
|
||||
|
||||
- 按 `totalPage` 逐页拉(每页 100),**一个包裹 = 一张 OMS 订单**,`external_order_id` = 包裹ID;
|
||||
- `total_amount` / `pay_amount` = `orderAmount + shipAmount`;收件人取 `dxmPackageAddr`;
|
||||
- 明细跳过 `cancelFlag=true` 的行;
|
||||
- **订单明细只给平台规格ID**(`productId`/`vid`/`stockProductId` 都是规格ID),不给平台商品ID,
|
||||
因此明细的 `outer_item_id` 留空,由 `lib/shopProductMatch` 按「规格ID 在店铺内唯一命中」匹配店铺商品,
|
||||
落库时再回填 `outer_item_id`(取匹配到的店铺商品)。
|
||||
|
||||
---
|
||||
|
||||
## 6. 待接入
|
||||
|
||||
`dxm.js` 目前实现 `getLoginCaptcha / login / checkSession / listShopSources / listShops / listProducts / listOrders`,
|
||||
其余方法继承基类会明确报「未实现」。后续按以下顺序补:
|
||||
|
||||
1. 订单列表(含明细)→ `listOrders`
|
||||
2. 取号 / 打单 / 发货回传 → `fetchWaybillNo` / `print`
|
||||
1. 取号 / 打单 / 发货回传 → `fetchWaybillNo` / `print`
|
||||
|
||||
每补一个接口,先抓真实报文确认字段口径,再补 `backend/test/app/lib/printPlatformDxm.test.js` 用例。
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user