diff --git a/app/service/systemMethods/stock/warehouse.js b/app/service/systemMethods/stock/warehouse.js index 153c171..b06a05b 100644 --- a/app/service/systemMethods/stock/warehouse.js +++ b/app/service/systemMethods/stock/warehouse.js @@ -51,11 +51,19 @@ class WarehouseService extends MainService { * @param {Object} data 请求体 * @param {Object} [data.pagination] 分页参数 * @param {Object} [data.where] 筛选条件 + * @param {Object} [data.sort] 排序参数 + * @param {string} [data.sort.sortField] 排序字段(兼容:sort_field) + * @param {string} [data.sort.sortOrder] 排序顺序(兼容:sort_order) * @return {Promise} 统一成功包装响应 */ async list(data = {}) { const { service } = this; const { pagination } = data; + const sort = data.sort || {}; + const sortParams = { + sortField: sort.sortField || sort.sort_field || 'id', + sortOrder: sort.sortOrder || sort.sort_order || 'DESC', + }; if (this.isPlainObject(data)) { const rawWhere = this.isPlainObject(data.where) ? data.where : {}; data.where = this.normalizeWarehouseRecycleWhere(rawWhere); @@ -75,7 +83,7 @@ class WarehouseService extends MainService { }, }, }); - const result = await service.systems.stockStore.getWarehouseStock(where, pagination); + const result = await service.systems.stockStore.getWarehouseStock(where, pagination, sortParams); return this.success('ok', result); } diff --git a/app/service/systems/storage/inventory.js b/app/service/systems/storage/inventory.js index 66977ff..7ee56a8 100644 --- a/app/service/systems/storage/inventory.js +++ b/app/service/systems/storage/inventory.js @@ -3,6 +3,33 @@ const Service = require('egg').Service; const DAY_MS = 24 * 60 * 60 * 1000; +/** + * 库存明细(逐条)模式允许的排序字段白名单(wms_storage 真实列), + * 防止非法字段进入 ORDER BY 导致 SQL 报错或注入。 + */ +const STORAGE_BATCH_SORT_FIELD_WHITELIST = new Set([ + 'id', 'stock_id', 'space_id', 'batch_no', 'inventory_type', + 'quantity', 'locked_quantity', 'available_quantity', + 'production_date', 'expiry_date', 'inbound_time', + 'created_at', 'updated_at', +]); + +/** + * 库存汇总(按 stock_id 合并)模式允许的排序字段 → 聚合别名映射。 + * 汇总查询中 quantity/locked_quantity/available_quantity 等为 SUM/MIN/MAX 别名, + * MySQL 支持 ORDER BY 别名,因此直接按别名排序。 + */ +const STORAGE_AGGREGATE_ORDER_FIELD_MAP = { + quantity: 'quantity', + locked_quantity: 'locked_quantity', + available_quantity: 'available_quantity', + batch_count: 'batch_count', + first_inbound_time: 'first_inbound_time', + last_inbound_time: 'last_inbound_time', + production_date: 'production_date', + expiry_date: 'expiry_date', +}; + /** * 计算库存批次的保质期预警状态。 * @@ -1135,7 +1162,7 @@ class InventoryService extends Service { * @param {number} [pageParams.page=1] 页码 * @param {number} [pageParams.pageSize=20] 每页数量 * @param {Object} [sortParams={}] 排序参数 - * @param {string} [sortParams.sortField='created_at'] 排序字段 + * @param {string} [sortParams.sortField='created_at'] 排序字段(明细模式支持 wms_storage 真实列;汇总模式仅支持 quantity/locked_quantity/available_quantity/batch_count/first_inbound_time/last_inbound_time/production_date/expiry_date,其余回退 last_inbound_time) * @param {string} [sortParams.sortOrder='DESC'] 排序方向 * @param {boolean} [groupByBatch=false] 是否按批次明细返回 * @return {Promise<{list: Object[], pagination: {page: number, pageSize: number, total: number, totalPages: number}}>} 库存列表结果 @@ -1146,8 +1173,14 @@ class InventoryService extends Service { const nowMs = Date.now(); const warehouseId = ctx.user.store_id; - const { page = 1, pageSize = 20 } = pageParams; - const { sortField = 'created_at', sortOrder = 'DESC' } = sortParams; + const page = Math.max(Number(pageParams.page || pageParams.currentPage || 1) || 1, 1); + const pageSize = Math.max(Number(pageParams.pageSize || 20) || 20, 1); + const sortField = sortParams.sortField || sortParams.sort_field || 'created_at'; + const sortOrder = sortParams.sortOrder || sortParams.sort_order || 'DESC'; + const safeSortOrder = String(sortOrder).toUpperCase() === 'ASC' ? 'ASC' : 'DESC'; + // 明细模式按真实列排序;汇总模式按聚合别名排序(默认 last_inbound_time,与历史行为一致) + const batchSortField = STORAGE_BATCH_SORT_FIELD_WHITELIST.has(sortField) ? sortField : 'created_at'; + const aggregateOrderField = STORAGE_AGGREGATE_ORDER_FIELD_MAP[sortField] || 'last_inbound_time'; // 基础条件 const baseWhere = { @@ -1188,7 +1221,7 @@ class InventoryService extends Service { // 按批次明细:逐条返回 const { count: total, rows } = await ctx.model.Storage.findAndCountAll({ where: baseWhere, - order: [[ sortField, sortOrder ]], + order: [[ batchSortField, safeSortOrder ]], offset, limit, include: [ @@ -1291,7 +1324,7 @@ class InventoryService extends Service { [ fn('COUNT', col('wms_storage.id')), 'batch_count' ], ], group: groupFields, - order: [[ literal('last_inbound_time'), sortOrder ]], + order: [[ literal(aggregateOrderField), safeSortOrder ]], offset, limit, include: [ @@ -1660,6 +1693,9 @@ class InventoryService extends Service { const { Op, fn, col, literal } = app.Sequelize; const warehouseId = ctx.user.store_id; const { sortField = 'id', sortOrder = 'DESC' } = sortParams; + const safeSortOrder = String(sortOrder).toUpperCase() === 'ASC' ? 'ASC' : 'DESC'; + const batchSortField = STORAGE_BATCH_SORT_FIELD_WHITELIST.has(sortField) ? sortField : 'id'; + const aggregateOrderField = STORAGE_AGGREGATE_ORDER_FIELD_MAP[sortField] || 'last_inbound_time'; const pageSize = 500; let page = 1; let hasNext = true; @@ -1692,7 +1728,7 @@ class InventoryService extends Service { 'supplier_batch_no', 'store_id', 'warehouse_id', 'created_at', 'updated_at', ], - order: [[ sortField, sortOrder ]], + order: [[ batchSortField, safeSortOrder ]], offset: (page - 1) * pageSize, limit: pageSize, include: [ @@ -1783,7 +1819,7 @@ class InventoryService extends Service { [ fn('MAX', col('wms_storage.updated_at')), 'last_updated_at' ], ], group: groupFields, - order: [[ literal('last_inbound_time'), sortOrder ]], + order: [[ literal(aggregateOrderField), safeSortOrder ]], offset: (page - 1) * pageSize, limit: pageSize, include: [