Author SHA1 Message Date
wangliang a0d386b752 fix(bridge): 商品搜索结果按库存优先排序 2026-10-23 15:19:24 +08:00
+9 -3
View File
@@ -421,7 +421,8 @@ function createTools(cfg, deps = {}) {
const cmd = { const cmd = {
keyword: rpcQuery, keyword: rpcQuery,
page: 1, page: 1,
pageSize: limit, // 多取候选,下面按库存排序后再截断到 limit,避免缺货商品占满前几条
pageSize: limit * 4,
languageId: profile.languageId, languageId: profile.languageId,
tenantId: profile.tenantId, tenantId: profile.tenantId,
currencyCode: profile.currencyCode || '', currencyCode: profile.currencyCode || '',
@@ -439,7 +440,6 @@ function createTools(cfg, deps = {}) {
const list = Array.isArray(paging) ? paging : (paging.data || paging.list || []); const list = Array.isArray(paging) ? paging : (paging.data || paging.list || []);
total = result.total != null ? result.total : (paging.total != null ? paging.total : null); total = result.total != null ? result.total : (paging.total != null ? paging.total : null);
items = (Array.isArray(list) ? list : []) items = (Array.isArray(list) ? list : [])
.slice(0, limit)
.map((p) => { .map((p) => {
const name = p.spuName || p.name || p.title || null; const name = p.spuName || p.name || p.title || null;
const sku = p.sku || p.model || (Array.isArray(p.skus) && p.skus[0] ? p.skus[0].productId : null); const sku = p.sku || p.model || (Array.isArray(p.skus) && p.skus[0] ? p.skus[0].productId : null);
@@ -458,7 +458,13 @@ function createTools(cfg, deps = {}) {
mark_price: p.markPriceWithCurrency || null, mark_price: p.markPriceWithCurrency || null,
}; };
}) })
.filter((p) => p.name); .filter((p) => p.name)
// 有库存优先:stock>0 → 0,未知 → 1,缺货 → 2;sort 稳定,保留 RPC 原顺序
.sort((a, b) => {
const rank = (p) => (p.stock > 0 ? 0 : p.stock === null ? 1 : 2);
return rank(a) - rank(b);
})
.slice(0, limit);
} catch (err) { } catch (err) {
catalogError = err.message; catalogError = err.message;
} }