diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md index d7ae54c..75903af 100644 --- a/.github/copilot-instructions.md +++ b/.github/copilot-instructions.md @@ -39,8 +39,9 @@ `shops.external_source` 区分 `agent`(平台商家,`LoadMyAgentList`,订单按商家 `FxUserId` 归属) 与 `my_shop`(我的店铺,`LoadMyShopList`,订单按 `ShopId` 归属)。来源清单由 `PrintPlatformService.listShopSources()` 声明,控制器/前端不写死。 -`my_shop` 目前只支持店铺导入,`listOrders` / `listProducts` 会明确报「暂未支持」, -避免按账号 `FxUserId` 拉到别人的数据。细节见 `店管家取号-旧系统提取.md` §4.2.1.1。 +`my_shop` 的订单按 `LogicOrder.ShopId`、商品按 `SelectShopId` 取数(**不能**沿用商家 `FxUserId`, +那是账号自己)。`orderSyncTask` 查店铺时必须带上 `external_source`,否则会按错误口径拉单。 +细节见 `店管家取号-旧系统提取.md` §4.2.1.1。 ## 常见排查入口 diff --git a/backend/app/lib/printPlatform/dgj.js b/backend/app/lib/printPlatform/dgj.js index b5833ce..aff6e84 100644 --- a/backend/app/lib/printPlatform/dgj.js +++ b/backend/app/lib/printPlatform/dgj.js @@ -1656,21 +1656,28 @@ class DgjService extends PrintPlatformService { */ async resolveTemplateTargets(shop) { const data = (shop && shop.external_data) || {}; - const areaRows = [ - ...(Array.isArray(data.DbAreas) ? data.DbAreas : []), - ...(Array.isArray(data.DbArea) ? data.DbArea : []), - ]; const presets = []; - for (const row of areaRows) { - const key = String((row && (row.DbName || row.Name)) || '').toLowerCase(); - const preset = TEMPLATE_PLATFORMS[key]; - if (!preset) continue; - if (!presets.some(item => item.host === preset.host && item.type === preset.type)) presets.push(preset); - } - if (!presets.length) { - for (const host of this.resolveProductHosts(data)) { - for (const type of TEMPLATE_HOST_TYPES[host] || []) { - if (!presets.some(item => item.host === host && item.type === type)) presets.push({ host, type }); + if (String((shop && shop.external_source) || 'agent') === 'my_shop') { + // 我的店铺没有分区声明:平台类型即模板接口的 platformType,域名按平台类型推导 + const host = this.resolveMyShopHost(String(data.PlatformType || '').trim()); + const type = String(data.PlatformType || '').trim(); + if (host && type) presets.push({ host, type }); + } else { + const areaRows = [ + ...(Array.isArray(data.DbAreas) ? data.DbAreas : []), + ...(Array.isArray(data.DbArea) ? data.DbArea : []), + ]; + for (const row of areaRows) { + const key = String((row && (row.DbName || row.Name)) || '').toLowerCase(); + const preset = TEMPLATE_PLATFORMS[key]; + if (!preset) continue; + if (!presets.some(item => item.host === preset.host && item.type === preset.type)) presets.push(preset); + } + if (!presets.length) { + for (const host of this.resolveProductHosts(data)) { + for (const type of TEMPLATE_HOST_TYPES[host] || []) { + if (!presets.some(item => item.host === host && item.type === type)) presets.push({ host, type }); + } } } } @@ -1877,22 +1884,28 @@ class DgjService extends PrintPlatformService { /** * 组装商品列表查询表单(字段口径与店管家工作台一致) - * @param {string} agentId 商家 FxUserId(注意不是商家 Id);为空表示不限商家 + * + * 两套归属口径互斥: + * - 平台商家(agent):`SelectAgentId = FxUserId` + * - 我的店铺(my_shop):`SelectShopId = ShopId`(平台商品页店铺下拉的 value 就是 ShopId) + * @param {string} agentId 商家 FxUserId(注意不是商家 Id);为空表示不按商家过滤 * @param {number} pageIndex 页码,从 1 开始 * @param {number} pageSize 每页条数 - * @param {Object} [filters] 单品查询条件 + * @param {Object} [filters] 额外查询条件 * @param {string} [filters.productId] 平台商品ID * @param {string} [filters.skuId] 平台规格ID + * @param {string} [filters.shopId] 平台店铺ID(我的店铺来源) + * @param {string} [filters.platformType] 平台类型(我的店铺来源,如 TouTiao) * @return {URLSearchParams} 表单 */ buildProductForm(agentId, pageIndex, pageSize, filters = {}) { return new URLSearchParams({ PageIndex: String(pageIndex), PageSize: String(pageSize), - SelectShopId: '', + SelectShopId: filters.shopId ? String(filters.shopId) : '', SelectSupplierId: '', SelectAgentId: agentId ? String(agentId) : '', - SelectPlatformType: '', + SelectPlatformType: filters.platformType ? String(filters.platformType) : '', InputProductName: '', ProductType: 'all', ProductStatus: '0', @@ -1949,7 +1962,9 @@ class DgjService extends PrintPlatformService { * 拉取一页商品 * @param {string} host 商品接口域名 * @param {Object} options 查询参数 - * @param {string} [options.agentId] 商家 FxUserId,为空表示不限商家 + * @param {string} [options.agentId] 商家 FxUserId,为空表示不按商家过滤 + * @param {string} [options.shopId] 平台店铺ID(我的店铺来源) + * @param {string} [options.platformType] 平台类型(我的店铺来源) * @param {string} options.traceId 登录上下文 traceId * @param {string} [options.dbname] 分区库名,为空表示默认区 * @param {string} [options.productId] 平台商品ID(单品查询) @@ -1958,7 +1973,7 @@ class DgjService extends PrintPlatformService { * @param {number} options.pageSize 每页条数 * @return {Promise<{rows: Array, total: number}>} 当页商品与总数 */ - async fetchProductPage(host, { agentId, traceId, dbname, productId, skuId, pageIndex, pageSize }) { + async fetchProductPage(host, { agentId, shopId, platformType, traceId, dbname, productId, skuId, pageIndex, pageSize }) { const query = new URLSearchParams({ token: this.token, traceId: traceId || '', @@ -1978,7 +1993,7 @@ class DgjService extends PrintPlatformService { const res = await this.request(`https://${host}${PRODUCT_LIST_PATH}?${query.toString()}`, { method: 'POST', headers, - body: this.buildProductForm(agentId, pageIndex, pageSize, { productId, skuId }).toString(), + body: this.buildProductForm(agentId, pageIndex, pageSize, { productId, skuId, shopId, platformType }).toString(), }); if (res.status === 401) { @@ -2045,13 +2060,15 @@ class DgjService extends PrintPlatformService { * 避免平台在分页过程中新增/删除商品导致漏拉或重复。 * @param {string} host 商品接口域名 * @param {Object} options 查询参数 - * @param {string} options.agentId 商家 FxUserId + * @param {string} [options.agentId] 商家 FxUserId(平台商家来源) + * @param {string} [options.shopId] 平台店铺ID(我的店铺来源) + * @param {string} [options.platformType] 平台类型(我的店铺来源) * @param {string} options.traceId 登录上下文 traceId * @param {string} [options.dbname] 分区库名,为空表示默认区 * @param {string} [options.name] 分区名称 * @return {Promise} 分区商品快照 */ - async fetchAreaSnapshot(host, { agentId, traceId, dbname, name }) { + async fetchAreaSnapshot(host, { agentId, shopId, platformType, traceId, dbname, name }) { const maxAttempts = 2; let snapshot = null; @@ -2064,6 +2081,8 @@ class DgjService extends PrintPlatformService { for (let pageIndex = 1; pageIndex <= 1000; pageIndex++) { const page = await this.fetchProductPage(host, { agentId, + shopId, + platformType, traceId, dbname, pageIndex, @@ -2165,16 +2184,20 @@ class DgjService extends PrintPlatformService { * 同一域名下可能有多个物理库(默认区 / 新系统区等),只查默认区会漏掉其它区的商品。 * @param {string} host 商品接口域名 * @param {Object} options 查询参数 - * @param {string} options.agentId 商家 FxUserId + * @param {string} [options.agentId] 商家 FxUserId(平台商家来源) + * @param {string} [options.shopId] 平台店铺ID(我的店铺来源) + * @param {string} [options.platformType] 平台类型(我的店铺来源) * @param {string} options.traceId 登录上下文 traceId * @return {Promise} 域名商品快照(含各分区明细) */ - async fetchProductSnapshot(host, { agentId, traceId }) { + async fetchProductSnapshot(host, { agentId, shopId, platformType, traceId }) { const areas = await this.listProductAreas(host); const snapshots = []; for (const area of areas) { snapshots.push(await this.fetchAreaSnapshot(host, { agentId, + shopId, + platformType, traceId, dbname: area.dbname, name: area.name, @@ -2218,17 +2241,27 @@ class DgjService extends PrintPlatformService { if (!this.token || !this.cookie) { throw new Error('账号未登录:缺少 token 或 cookie,请先登录'); } - // 「我的店铺」是自营店:商品要按 SelectShopId 过滤,不能沿用商家的 FxUserId(那是账号自己, - // 会把本账号上游的其它商品/订单拉进来)。接入前先明确拒绝,避免写脏数据 - if (String((shop && shop.external_source) || 'agent') === 'my_shop') { - throw new Error('「我的店铺」暂未支持同步商品(需按店铺ID拉取,下一步接入)'); + // 两种来源的商品归属口径不同:商家用 FxUserId,我的店铺用 ShopId(自营店的 FxUserId 是账号自己, + // 按它过滤会把本账号上游的其它商品拉进来) + const data = (shop && shop.external_data) || {}; + const source = String((shop && shop.external_source) || 'agent'); + let agentId = ''; + let shopId = ''; + let platformType = ''; + let hosts; + + if (source === 'my_shop') { + shopId = String(data.ShopId || '').trim(); + if (!shopId) throw new Error('店铺缺少平台店铺ID(ShopId),无法拉取平台商品'); + platformType = String(data.PlatformType || '').trim(); + // 没有分区声明,按平台类型定位域名;查不到再走下面的兜底域名 + hosts = [ this.resolveMyShopHost(platformType) ]; + } else { + agentId = String(data.FxUserId || '').trim(); + if (!agentId) throw new Error('店铺缺少商家 FxUserId,无法拉取平台商品'); + hosts = this.resolveProductHosts(data); } - const data = (shop && shop.external_data) || {}; - const agentId = String(data.FxUserId || '').trim(); - if (!agentId) throw new Error('店铺缺少商家 FxUserId,无法拉取平台商品'); - - const hosts = this.resolveProductHosts(data); let traceId = ''; try { const info = await this.getUserInfo(); @@ -2239,7 +2272,7 @@ class DgjService extends PrintPlatformService { const snapshots = []; for (const host of hosts) { - snapshots.push(await this.fetchProductSnapshot(host, { agentId, traceId })); + snapshots.push(await this.fetchProductSnapshot(host, { agentId, shopId, platformType, traceId })); } // 店铺「所在区」声明过期(重新授权/换绑后不会自动同步)时,声明域名会一个商品都没有: @@ -2249,7 +2282,7 @@ class DgjService extends PrintPlatformService { if (declaredEmpty) { for (const host of FALLBACK_PRODUCT_HOSTS) { if (hosts.includes(host)) continue; - snapshots.push(await this.fetchProductSnapshot(host, { agentId, traceId })); + snapshots.push(await this.fetchProductSnapshot(host, { agentId, shopId, platformType, traceId })); supplementHosts.push(host); } if (supplementHosts.length && this.logger && typeof this.logger.warn === 'function') { @@ -2370,22 +2403,31 @@ class DgjService extends PrintPlatformService { if (!this.token || !this.cookie) { throw new Error('账号未登录:缺少 token 或 cookie,请先登录'); } - // 「我的店铺」的订单归属键是 LogicOrder.ShopId,不是商家的 FxUserId(自营店的 FxUserId 是账号自己, - // 按它过滤会把本账号上游的其它订单当成该店铺的单)。接入前先明确拒绝,避免写脏数据 - if (String((shop && shop.external_source) || 'agent') === 'my_shop') { - throw new Error('「我的店铺」暂未支持拉取订单(需按店铺ID拉取,下一步接入)'); - } - + // 两种来源的订单归属键不同: + // - 平台商家(agent):按 PathFlowNode.UpFxUserId = 商家 FxUserId + // - 我的店铺(my_shop):按 LogicOrder.ShopId = 平台店铺ID(自营店的 FxUserId 是账号自己, + // 按它过滤会把本账号上游的其它订单当成该店铺的单) const data = (shop && shop.external_data) || {}; - const merchantFxUserId = String(data.FxUserId || '').trim(); - if (!merchantFxUserId) throw new Error('店铺缺少商家 FxUserId,无法拉取平台订单'); + const isMyShop = String((shop && shop.external_source) || 'agent') === 'my_shop'; + let merchantFxUserId = ''; + let shopId = ''; + let hosts; + if (isMyShop) { + shopId = String(data.ShopId || '').trim(); + if (!shopId) throw new Error('店铺缺少平台店铺ID(ShopId),无法拉取平台订单'); + // 没有分区声明,按平台类型定位域名;查不到再走下面的兜底域名 + hosts = [ this.resolveMyShopHost(String(data.PlatformType || '').trim()) ]; + } else { + merchantFxUserId = String(data.FxUserId || '').trim(); + if (!merchantFxUserId) throw new Error('店铺缺少商家 FxUserId,无法拉取平台订单'); + hosts = this.resolveProductHosts(data); + } const end = options.endTime ? new Date(options.endTime) : new Date(); const start = options.startTime ? new Date(options.startTime) // 未指定时间窗时按平台「待发货订单」页的默认范围(15 天)兜底 : new Date(end.getTime() - 15 * 24 * 3600 * 1000); - const hosts = this.resolveProductHosts(data); let traceId = ''; try { @@ -2404,7 +2446,8 @@ class DgjService extends PrintPlatformService { const fallbackHosts = []; // 店铺没有声明该平台的老分区(只有新系统分区)时,该平台的老分区属于迁移前遗留数据,跳过不拉 const skippedLegacy = []; - const areaFlags = this.resolveAreaFlags(data); + // 我的店铺没有分区声明,按「新旧分区都查」处理(实际分区由各域名的 GetAreasInfo 给出) + const areaFlags = isMyShop ? {} : this.resolveAreaFlags(data); // 「所在区」声明里没有老分区时先跳过(迁移前遗留数据),但**拉到单之前不能就此收工**: // 声明只写了「抖店-二区」而订单实际还在「抖店-186(一区)」的情况确实存在, // 声明域名/新系统区都没单时再回头补扫这些老分区 @@ -2429,6 +2472,7 @@ class DgjService extends PrintPlatformService { endTime: end, erpState: ORDER_ERP_STATE_PENDING, merchantFxUserId, + shopId, }); this.rememberFilterSupport(host, area.dbname, true); } catch (err) { @@ -2460,24 +2504,30 @@ class DgjService extends PrintPlatformService { dbname: area.dbname, startTime: fallbackStart, endTime: end, + shopId, }); } if (!fetched.length) return; hitHosts.push(areaLabel); for (const row of fetched) { const upFxUserId = String(row.UpFxUserId || ''); - // 记录平台实际出现的商家:店铺声明的 FxUserId 过期时(重新授权/换绑), - // 按老 FxUserId 过滤永远是 0 条,这里留痕便于直接看出「该换成谁」 - if (upFxUserId) { - const seen = seenMerchants.get(upFxUserId) || { - count: 0, - shopName: String(row.ShopName || row.AgentShopName || row.SelfShopName || ''), - }; - seen.count += 1; - seenMerchants.set(upFxUserId, seen); + if (isMyShop) { + // 订单列表按账号维度返回,这里只保留当前自营店铺的订单 + if (String(row.ShopId || '') !== shopId) continue; + } else { + // 记录平台实际出现的商家:店铺声明的 FxUserId 过期时(重新授权/换绑), + // 按老 FxUserId 过滤永远是 0 条,这里留痕便于直接看出「该换成谁」 + if (upFxUserId) { + const seen = seenMerchants.get(upFxUserId) || { + count: 0, + shopName: String(row.ShopName || row.AgentShopName || row.SelfShopName || ''), + }; + seen.count += 1; + seenMerchants.set(upFxUserId, seen); + } + // 订单列表按账号维度返回,这里只保留当前商家的订单 + if (upFxUserId !== merchantFxUserId) continue; } - // 订单列表按账号维度返回,这里只保留当前商家的订单 - if (upFxUserId !== merchantFxUserId) continue; const order = this.normalizeOrder(row); if (!order.external_order_id) continue; // 平台已发货/已终结:不进待审核,只登记给上层用于作废本地未处理的副本 @@ -2561,12 +2611,15 @@ class DgjService extends PrintPlatformService { skipped: finished.size, finished: Array.from(finished.values()), // 店铺声明的商家在平台结果里是否出现过;没出现过通常是 FxUserId 过期(重新授权/换绑) - merchantSeen: seenMerchants.has(merchantFxUserId), - otherMerchants: Array.from(seenMerchants.entries()) - .filter(([ fxUserId ]) => fxUserId !== merchantFxUserId) - .map(([ fxUserId, item ]) => ({ fx_user_id: fxUserId, shop_name: item.shopName, count: item.count })) - .sort((left, right) => right.count - left.count) - .slice(0, 5), + // 我的店铺按 ShopId 归属,不存在「商家换绑」问题,固定为 true 不触发商家排查提示 + merchantSeen: isMyShop ? true : seenMerchants.has(merchantFxUserId), + otherMerchants: isMyShop + ? [] + : Array.from(seenMerchants.entries()) + .filter(([ fxUserId ]) => fxUserId !== merchantFxUserId) + .map(([ fxUserId, item ]) => ({ fx_user_id: fxUserId, shop_name: item.shopName, count: item.count })) + .sort((left, right) => right.count - left.count) + .slice(0, 5), // 供上层做「按订单号精确核对」时复用本次登录上下文 traceId, }; @@ -2654,10 +2707,11 @@ class DgjService extends PrintPlatformService { * @param {Date} options.startTime 下单时间起 * @param {Date} options.endTime 下单时间止 * @param {string} [options.erpState] 订单状态过滤(如 waitsellersend;不传则不限状态) - * @param {string} [options.merchantFxUserId] 商家 FxUserId(按商家过滤,不传则整账号) + * @param {string} [options.merchantFxUserId] 商家 FxUserId(平台商家来源,按商家过滤) + * @param {string} [options.shopId] 平台店铺ID(我的店铺来源,按店铺过滤) * @return {Promise} 订单原始结构列表 */ - async fetchOrderPages(host, { traceId, dbname, startTime, endTime, erpState, merchantFxUserId }) { + async fetchOrderPages(host, { traceId, dbname, startTime, endTime, erpState, merchantFxUserId, shopId }) { const rows = []; // 平台不保证返回 PageCount(实测多为 null),因此以 Total 和「本页是否满页」判断结束, // 只依赖 PageCount 会在超过 500 条时静默截断 @@ -2672,6 +2726,7 @@ class DgjService extends PrintPlatformService { pageIndex, erpState, merchantFxUserId, + shopId, }); if (!page.rows.length) break; rows.push(...page.rows); @@ -2693,10 +2748,11 @@ class DgjService extends PrintPlatformService { * @param {Date} options.endTime 下单时间止 * @param {number} options.pageIndex 页码 * @param {string} [options.erpState] 订单状态过滤(如 waitsellersend;不传则不限状态) - * @param {string} [options.merchantFxUserId] 商家 FxUserId(按商家过滤,不传则整账号) + * @param {string} [options.merchantFxUserId] 商家 FxUserId(平台商家来源,按商家过滤) + * @param {string} [options.shopId] 平台店铺ID(我的店铺来源,按 LogicOrder.ShopId 过滤) * @return {Promise<{rows: Array, total: number, pageCount: number}>} 当页订单 */ - async fetchOrderPage(host, { traceId, dbname, startTime, endTime, pageIndex, erpState, merchantFxUserId }) { + async fetchOrderPage(host, { traceId, dbname, startTime, endTime, pageIndex, erpState, merchantFxUserId, shopId }) { const query = new URLSearchParams({ token: this.token, traceId: traceId || '', @@ -2705,6 +2761,9 @@ class DgjService extends PrintPlatformService { }); const form = new URLSearchParams(); + // 上界补到该秒最后(平台页面抓包是 `YYYY-MM-DD HH:mm:ss.999`), + // 否则落在窗口最后一秒内的订单会被丢掉 + const endText = formatOrderTime(endTime); const filters = [ { TableAlias: 'o', @@ -2712,7 +2771,7 @@ class DgjService extends PrintPlatformService { TableName: 'LogicOrder', Name: 'CreateTime', Value: formatOrderTime(startTime), - ExtValue: formatOrderTime(endTime), + ExtValue: endText ? `${endText}.999` : '', Contract: 'between', }, ]; @@ -2729,8 +2788,19 @@ class DgjService extends PrintPlatformService { Contract: '=', }); } - // 商家过滤:平台按 PathFlowNode.UpFxUserId 过滤(实测有效,一次只回该商家的单) - if (merchantFxUserId) { + // 归属过滤(两者互斥): + // - 平台商家:PathFlowNode.UpFxUserId = 商家 FxUserId(实测有效,一次只回该商家的单) + // - 我的店铺:LogicOrder.ShopId = 平台店铺ID(与平台「待打订单」页选择的店铺同口径) + if (shopId) { + filters.push({ + TableAlias: 'o', + FieldType: 'int', + TableName: 'LogicOrder', + Name: 'ShopId', + Value: String(shopId), + Contract: 'in', + }); + } else if (merchantFxUserId) { filters.push({ TableAlias: 'pfn', FieldType: 'int', diff --git a/backend/app/service/orderSyncTask.js b/backend/app/service/orderSyncTask.js index 45aafda..285943b 100644 --- a/backend/app/service/orderSyncTask.js +++ b/backend/app/service/orderSyncTask.js @@ -87,11 +87,12 @@ class OrderSyncTaskService extends Service { return await this.ctx.model.Shop.findAll({ where, - // external_data 是打单平台拉单的必要字段(如店管家的商家 FxUserId、分区 DbAreas), - // 少查会让 syncOrders 收到缺字段的店铺实例,直接报「店铺缺少商家 FxUserId」 + // external_data / external_source 是打单平台拉单的必要字段 + // (来源决定按商家 FxUserId 还是按店铺 ShopId 归属,external_data 提供对应原始结构), + // 少查会让 syncOrders 收到缺字段的店铺实例,按错误口径拉单或直接报缺字段 attributes: [ 'id', 'code', 'name', 'principal_id', 'type', 'subsidiary_id', - 'external_print_account_id', 'external_data', 'default_warehouse_id', + 'external_print_account_id', 'external_source', 'external_data', 'default_warehouse_id', ], order: [[ 'id', 'ASC' ]], limit: MAX_SHOPS, diff --git a/backend/test/app/lib/dgjMyShopList.test.js b/backend/test/app/lib/dgjMyShopList.test.js index d0318c8..9277cd4 100644 --- a/backend/test/app/lib/dgjMyShopList.test.js +++ b/backend/test/app/lib/dgjMyShopList.test.js @@ -109,16 +109,89 @@ describe('printPlatform/dgj resolveShopScope', () => { }); }); -describe('printPlatform/dgj 我的店铺暂不支持拉单/拉商品', () => { +describe('printPlatform/dgj buildProductForm 归属口径', () => { const service = createService(); - const shop = { external_source: 'my_shop', external_data: { ShopId: 12345897, FxUserId: 1227061, PlatformType: 'TouTiao' } }; - it('listOrders 明确拒绝,避免按账号 FxUserId 拉到别人的订单', async () => { - await assert.rejects(() => service.listOrders(shop, {}), /暂未支持拉取订单/); + it('我的店铺:SelectShopId=ShopId,不带商家与平台过滤以外的字段', () => { + const form = service.buildProductForm('', 1, 500, { shopId: '12345897', platformType: 'TouTiao' }); + assert.strictEqual(form.get('SelectShopId'), '12345897'); + assert.strictEqual(form.get('SelectAgentId'), ''); + assert.strictEqual(form.get('SelectPlatformType'), 'TouTiao'); }); - it('listProducts 明确拒绝,避免按账号 FxUserId 拉到别人的商品', async () => { - await assert.rejects(() => service.listProducts(shop), /暂未支持同步商品/); + it('平台商家:SelectAgentId=FxUserId,不带店铺', () => { + const form = service.buildProductForm('953516', 1, 500, {}); + assert.strictEqual(form.get('SelectAgentId'), '953516'); + assert.strictEqual(form.get('SelectShopId'), ''); + assert.strictEqual(form.get('SelectPlatformType'), ''); + }); +}); + +describe('printPlatform/dgj 我的店铺缺少 ShopId', () => { + const service = createService(); + const shop = { external_source: 'my_shop', external_data: { FxUserId: 1227061, PlatformType: 'TouTiao' } }; + + it('listOrders 明确报错,不退回按账号 FxUserId 拉取', async () => { + await assert.rejects(() => service.listOrders(shop, {}), /缺少平台店铺ID/); + }); + + it('listProducts 明确报错,不退回按账号 FxUserId 拉取', async () => { + await assert.rejects(() => service.listProducts(shop), /缺少平台店铺ID/); + }); +}); + +describe('printPlatform/dgj 我的店铺订单过滤', () => { + it('按 LogicOrder.ShopId 过滤(Contract=in),不带商家条件;时间窗上界补 .999', async () => { + const service = createService(); + let captured = null; + let requestUrl = ''; + service.request = async (url, options) => { + requestUrl = url; + captured = new URLSearchParams(options.body); + return { status: 200, json: { Success: true, Data: { Rows: [], Total: 0 } }, text: '', setCookies: [] }; + }; + + await service.fetchOrderPage('fxdd.dgjapp.com', { + traceId: 'trace', + dbname: 'y/2wUDWCZOk=', + startTime: new Date('2026-11-01T00:00:00+08:00'), + endTime: new Date('2026-11-08T23:59:59+08:00'), + pageIndex: 1, + erpState: 'waitsellersend', + shopId: '12345897', + }); + + const filters = []; + for (let index = 0; index < 5; index += 1) { + const name = captured.get(`Filters[${index}][Name]`); + if (!name) break; + filters.push(`${name}:${captured.get(`Filters[${index}][Contract]`)}:${captured.get(`Filters[${index}][Value]`)}`); + } + assert.deepStrictEqual(filters, [ 'CreateTime:between:2026-11-01 00:00:00', 'ErpState:=:waitsellersend', 'ShopId:in:12345897' ]); + assert.strictEqual(captured.get('Filters[2][TableAlias]'), 'o'); + assert.strictEqual(captured.get('Filters[2][TableName]'), 'LogicOrder'); + assert.strictEqual(captured.get('Filters[0][ExtValue]'), '2026-11-08 23:59:59.999'); + assert.ok(requestUrl.includes('/NewOrder/List')); + }); + + it('同时传 shopId 与 merchantFxUserId 时以店铺为准(不串商家口径)', async () => { + const service = createService(); + let captured = null; + service.request = async (url, options) => { + captured = new URLSearchParams(options.body); + return { status: 200, json: { Success: true, Data: { Rows: [], Total: 0 } }, text: '', setCookies: [] }; + }; + await service.fetchOrderPage('fxdd.dgjapp.com', { + traceId: 'trace', dbname: '', pageIndex: 1, shopId: '12345897', merchantFxUserId: '953516', + }); + const names = []; + for (let index = 0; index < 5; index += 1) { + const name = captured.get(`Filters[${index}][Name]`); + if (!name) break; + names.push(name); + } + assert.ok(names.includes('ShopId')); + assert.ok(!names.includes('UpFxUserId')); }); }); diff --git a/frontend/src/api/shop.ts b/frontend/src/api/shop.ts index 099e797..ce8ecb1 100644 --- a/frontend/src/api/shop.ts +++ b/frontend/src/api/shop.ts @@ -21,7 +21,9 @@ export interface ShopItem { type?: number /** 来源外部打单账号ID(type=2 时有值) */ external_print_account_id?: number | null - /** 外部打单平台返回的商家原始结构 */ + /** 外部打单平台店铺来源(平台自定义,如 agent 平台商家 / my_shop 我的店铺) */ + external_source?: string | null + /** 外部打单平台返回的店铺原始结构 */ external_data?: unknown | null /** 数据库地址(type=3 数据库模式店铺使用,预留) */ db_host?: string | null diff --git a/frontend/src/views/Shop/Index.vue b/frontend/src/views/Shop/Index.vue index bb1ae1e..529df90 100644 --- a/frontend/src/views/Shop/Index.vue +++ b/frontend/src/views/Shop/Index.vue @@ -762,7 +762,7 @@ function openFetchOrder(row: ShopItem) { fetchOrderForm.days = 15 fetchOrderVisible.value = true } -/** 手动拉取订单:后端按店铺所属商家逐平台分区拉取并写入订单/明细 */ +/** 手动拉取订单:后端按店铺来源(平台商家 / 我的店铺)逐平台分区拉取并写入订单/明细 */ async function submitFetchOrder() { const shop = fetchOrderShop.value if (!shop || !shop.external_print_account_id) return @@ -812,6 +812,11 @@ function shopAreaText(row: ShopItem) { return shopAreaNames(row).join('、') } +/** 「我的店铺」来源:没有商家分区声明,不展示/刷新「所在区」 */ +function isMyShop(row: ShopItem) { + return row.type === 2 && row.external_source === 'my_shop' +} + /** 系统自动禁用原因(商家已不在打单平台合作列表时写入 extra.auto_disabled) */ function autoDisabledReason(row: ShopItem) { const extra = (row.extra || {}) as { auto_disabled?: { at?: string; reason?: string } } @@ -1001,7 +1006,7 @@ onMounted(() => {