liufumingren 16d5f18fa1 feat(openapi): interface_type_id=2 强制合约注入 + 全链路 store_id 防护
核心改动:
- openapi.js: 新增 resolveOpenapiContract 强制合约解析(required:true JOIN),
  dispatchOpenapiMethod 统一注入上下文(setInfo)
- 5个 openapiMethods: 移除重复 resolveAppInfo, 统一使用 this.getInfo()
- deliveryorder/entryorder/stockout: findByPk(itemId) 后增加 store_id 一致性校验

安全修复:
- 修复 itemId 路径跨货主越权漏洞(FindByPk 无 store_id 过滤)
- deliveryorder/inventory/singleitem 查询补全 store_id 过滤
- 合约缺失返回 403 而非穿透业务层

测试覆盖: 9方法×3层防护(合约/注入/过滤)全部通过集成测试
2026-07-15 00:10:40 +08:00

58 lines
1.9 KiB
JavaScript
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.

'use strict';
const MainService = require('../main');
/**
* OpenAPI 库存服务。
* 提供库存查询能力,映射到 StockStore 底层模型。
*/
class OpenapiInventoryService extends MainService {
/**
* 库存查询。
* 上下文(storeId)已由 dispatchOpenapiMethod 统一注入,查询结果按货主范围校验。
* 根据商品 ID 列表查询对应的库存信息,包括总库存、锁定量和可用量。
* @param {Object} payload 调用参数。
* @param {Object} payload.body 请求体数据,需包含 itemIds(商品 ID 数组)。
* @param {Object} payload.query 请求 query 数据。
* @return {Promise<Object>} 查询结果,成功时返回 items 数组。
*/
async query(payload = {}) {
const { body = {} } = payload;
const { itemIds } = body;
if (!Array.isArray(itemIds) || itemIds.length === 0) {
return this.fail('itemIds 参数缺失或为空');
}
const { storeId } = this.getInfo();
const items = [];
for (const itemId of itemIds) {
const result = await this.ctx.service.systems.stockStore.getItemStorages(itemId);
if (result.success && result.data) {
// 按货主范围过滤:仅返回属于当前合约货主的库存
if (Number(result.data.storage.storeId) !== Number(storeId)) {
continue;
}
items.push({
itemId: result.data.stock.id,
itemCode: result.data.stock.item_code,
warehouseCode: result.data.stock.warehouse_code,
quantity: result.data.storage.quantity,
lockQuantity: result.data.storage.lockedQuantity,
inventoryType: 'ZP',
});
}
}
if (items.length === 0) {
return this.fail('未查询到有效库存信息');
}
return this.success('库存查询成功', { items });
}
}
module.exports = OpenapiInventoryService;