feat(库存入库): 小程序品名改商品选择器 + 建品查重
- 小程序「品名」格改为商品选择器(搜索已有 / 选中带出上次设置 / 输入新名待新建), 与 Web 口径一致;生产日期与数量不带出 - 后端 createCatalogSkuForInbound 加查重:同名(+规格)已有启用商品即复用, 避免重复新建(Web / 小程序 / 批量入库 / 自助采购 四个入口一并生效) - 新增 GET /mobile/inventory/sku-options;补查重单测与设计文档
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
const {
|
||||
fetchStockInboundCategories,
|
||||
fetchStockInboundStorageZones,
|
||||
fetchStockInboundSkuOptions,
|
||||
createStockInboundCategory,
|
||||
recognizeStockInboundLines,
|
||||
submitStockInboundBatch,
|
||||
@@ -143,6 +144,8 @@ Component({
|
||||
sheetList: [],
|
||||
sheetCanCreate: false,
|
||||
sheetCreateLabel: '',
|
||||
/** 「商品选择」远程检索结果(带上次设置回填) */
|
||||
productOptions: [],
|
||||
},
|
||||
|
||||
lifetimes: {
|
||||
@@ -201,9 +204,10 @@ Component({
|
||||
this.setData({ categoryOptions: options });
|
||||
},
|
||||
|
||||
// ── 分类 / 存放区域:输入下拉 + 可新建(弹层)────────
|
||||
// ── 分类 / 存放区域 / 商品:输入下拉 + 可新建(弹层)────────
|
||||
sheetSource(type) {
|
||||
if (type === 'category') return this.data.categoryOptions || [];
|
||||
if (type === 'product') return this.data.productOptions || [];
|
||||
return (this.data.zoneOptions || []).map((z) => ({ code: z, label: z }));
|
||||
},
|
||||
|
||||
@@ -215,6 +219,13 @@ Component({
|
||||
this.openSheet('zone', Number(e.currentTarget.dataset.index));
|
||||
},
|
||||
|
||||
/** 「品名」格 = 商品选择器:打开弹层并远程搜商品档案(与 Web 口径一致) */
|
||||
openProductSheet(e) {
|
||||
const index = Number(e.currentTarget.dataset.index);
|
||||
this.openSheet('product', index);
|
||||
this.searchProducts('');
|
||||
},
|
||||
|
||||
openSheet(type, index) {
|
||||
if (!Number.isInteger(index) || index < 0) return;
|
||||
this.setData({
|
||||
@@ -234,13 +245,31 @@ Component({
|
||||
|
||||
refreshSheet(query) {
|
||||
const type = this.data.sheetType;
|
||||
const all = this.sheetSource(type);
|
||||
this.refreshSheetLocal(query, this.sheetSource(type));
|
||||
// 商品:本地过滤后仍走远程搜(商品库大,需服务端检索)
|
||||
if (type === 'product') this.searchProducts(String(query || ''));
|
||||
},
|
||||
|
||||
refreshSheetLocal(query, all) {
|
||||
const type = this.data.sheetType;
|
||||
const q = String(query || '');
|
||||
const ql = q.trim().toLowerCase();
|
||||
const list = ql ? all.filter((o) => o.label.toLowerCase().indexOf(ql) >= 0) : all;
|
||||
const list = ql
|
||||
? (all || []).filter((o) => String(o.label || '').toLowerCase().indexOf(ql) >= 0)
|
||||
: all || [];
|
||||
const trimmed = q.trim();
|
||||
const canCreate = Boolean(trimmed) && !all.some((o) => o.label.trim() === trimmed);
|
||||
const createLabel = type === 'category' ? `+ 新建分类「${trimmed}」` : `+ 使用区域「${trimmed}」`;
|
||||
const matched = (all || []).some((o) =>
|
||||
type === 'product'
|
||||
? String(o.sku_name || '').trim() === trimmed
|
||||
: String(o.label || '').trim() === trimmed
|
||||
);
|
||||
const canCreate = Boolean(trimmed) && !matched;
|
||||
const createLabel =
|
||||
type === 'category'
|
||||
? `+ 新建分类「${trimmed}」`
|
||||
: type === 'product'
|
||||
? `+ 使用「${trimmed}」作为新商品`
|
||||
: `+ 使用区域「${trimmed}」`;
|
||||
this.setData({
|
||||
sheetQuery: q,
|
||||
sheetList: list,
|
||||
@@ -249,6 +278,35 @@ Component({
|
||||
});
|
||||
},
|
||||
|
||||
/** 「品名」远程检索(防抖) */
|
||||
searchProducts(query) {
|
||||
if (this._skuTimer) clearTimeout(this._skuTimer);
|
||||
const q = String(query || '');
|
||||
this._skuTimer = setTimeout(async () => {
|
||||
try {
|
||||
const res = await fetchStockInboundSkuOptions({ search: q, limit: 20 });
|
||||
const rows = (res && (res.data || res)) || [];
|
||||
const options = (Array.isArray(rows) ? rows : []).map((o) => ({
|
||||
code: String(o.sku_code || ''),
|
||||
sku_name: String(o.sku_name || o.sku_code || ''),
|
||||
label: o.spec ? `${o.sku_name} · ${o.spec}` : String(o.sku_name || o.sku_code || ''),
|
||||
spec: o.spec || '',
|
||||
unit: o.unit || '',
|
||||
origin: o.origin || '',
|
||||
category_code: o.category_code || '',
|
||||
category_name: o.category_name || '',
|
||||
backfill: o.backfill || null,
|
||||
}));
|
||||
this.setData({ productOptions: options });
|
||||
if (this.data.sheetType === 'product') {
|
||||
this.refreshSheetLocal(this.data.sheetQuery, options);
|
||||
}
|
||||
} catch (e) {
|
||||
// 搜索失败静默:不阻断手输 / 新建
|
||||
}
|
||||
}, 300);
|
||||
},
|
||||
|
||||
closeSheet() {
|
||||
this.setData({ sheetOpen: false });
|
||||
},
|
||||
@@ -259,12 +317,75 @@ Component({
|
||||
onSheetPick(e) {
|
||||
const code = String(e.currentTarget.dataset.code || '');
|
||||
const label = String(e.currentTarget.dataset.label || '');
|
||||
if (this.data.sheetType === 'product') {
|
||||
const opt = (this.data.productOptions || []).find((o) => o.code === code);
|
||||
if (opt) {
|
||||
this.applyProductPick(opt);
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.applySheetValue(code, label);
|
||||
},
|
||||
|
||||
/** 选中已有商品:回填 sku + 上次设置(生产日期 / 数量不动) */
|
||||
applyProductPick(opt) {
|
||||
const index = this.data.sheetIndex;
|
||||
if (!Number.isInteger(index) || index < 0) {
|
||||
this.setData({ sheetOpen: false });
|
||||
return;
|
||||
}
|
||||
const line = (this.data.lines || [])[index] || {};
|
||||
const bf = opt.backfill || null;
|
||||
const patch = {
|
||||
[`lines[${index}].sku_code`]: opt.code,
|
||||
[`lines[${index}].item_name`]: opt.sku_name || opt.label,
|
||||
[`lines[${index}].candidates`]: [],
|
||||
sheetOpen: false,
|
||||
};
|
||||
// 规格 / 单位:商品档案优先
|
||||
if (opt.spec) patch[`lines[${index}].spec`] = opt.spec;
|
||||
if (opt.unit) patch[`lines[${index}].unit`] = opt.unit;
|
||||
if (bf) {
|
||||
// 分类:商品档案 / 上次设置优先
|
||||
if (bf.category_code) {
|
||||
patch[`lines[${index}].category_code`] = bf.category_code;
|
||||
patch[`lines[${index}].category_name`] = bf.category_name || '';
|
||||
}
|
||||
// 成本价 / 存放区域 / 保质期 / 生产地区:空才回填
|
||||
if (!String(line.unit_cost ?? '').trim() && bf.last_cost_price != null) {
|
||||
patch[`lines[${index}].unit_cost`] = String(bf.last_cost_price);
|
||||
}
|
||||
if (!String(line.storage_zone || '').trim() && bf.last_storage_zone) {
|
||||
patch[`lines[${index}].storage_zone`] = bf.last_storage_zone;
|
||||
}
|
||||
if (!String(line.shelf_life || '').trim() && bf.last_shelf_life_days != null) {
|
||||
patch[`lines[${index}].shelf_life`] = `${bf.last_shelf_life_days}天`;
|
||||
}
|
||||
if (!String(line.origin || '').trim() && bf.origin) {
|
||||
patch[`lines[${index}].origin`] = bf.origin;
|
||||
}
|
||||
}
|
||||
this.setData(patch);
|
||||
},
|
||||
|
||||
async onSheetCreate() {
|
||||
const q = String(this.data.sheetQuery || '').trim();
|
||||
if (!q) return;
|
||||
// 商品:输入新名 ⇒ 记为待新建(确认入库时才建品/查重)
|
||||
if (this.data.sheetType === 'product') {
|
||||
const index = this.data.sheetIndex;
|
||||
if (Number.isInteger(index) && index >= 0) {
|
||||
this.setData({
|
||||
[`lines[${index}].item_name`]: q,
|
||||
[`lines[${index}].sku_code`]: '',
|
||||
[`lines[${index}].candidates`]: [],
|
||||
sheetOpen: false,
|
||||
});
|
||||
} else {
|
||||
this.setData({ sheetOpen: false });
|
||||
}
|
||||
return;
|
||||
}
|
||||
// 存放区域是自由文本,直接采用输入值
|
||||
if (this.data.sheetType === 'zone') {
|
||||
this.applySheetValue(q, q);
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
</view>
|
||||
|
||||
<view class="line-row">
|
||||
<input class="input cell cell--name" placeholder="品名" data-index="{{index}}" data-field="item_name" value="{{item.item_name}}" bindinput="onLineField" />
|
||||
<view class="cell cell--name cell-pick {{item.item_name ? '' : 'cell-pick--empty'}}" data-index="{{index}}" bindtap="openProductSheet">{{item.item_name || '品名'}} ▾</view>
|
||||
<input class="input cell cell--spec" placeholder="规格" data-index="{{index}}" data-field="spec" value="{{item.spec}}" bindinput="onLineField" />
|
||||
<input class="input cell cell--qty" type="digit" placeholder="数量" data-index="{{index}}" data-field="qty" value="{{item.qty}}" bindinput="onLineField" />
|
||||
<input class="input cell cell--unit" placeholder="单位" data-index="{{index}}" data-field="unit" value="{{item.unit}}" bindinput="onLineField" />
|
||||
@@ -59,8 +59,8 @@
|
||||
|
||||
<view wx:if="{{sheetOpen}}" class="sheet-mask" catchtap="closeSheet">
|
||||
<view class="sheet" catchtap="noop">
|
||||
<view class="sheet-title">{{sheetType === 'category' ? '选择分类' : '选择存放区域'}}</view>
|
||||
<input class="sheet-input" placeholder="{{sheetType === 'category' ? '搜索或输入新分类' : '搜索或输入新区域'}}" value="{{sheetQuery}}" bindinput="onSheetInput" confirm-type="done" />
|
||||
<view class="sheet-title">{{sheetType === 'product' ? '选择商品' : (sheetType === 'category' ? '选择分类' : '选择存放区域')}}</view>
|
||||
<input class="sheet-input" placeholder="{{sheetType === 'product' ? '搜索已有商品,或输入新商品名' : (sheetType === 'category' ? '搜索或输入新分类' : '搜索或输入新区域')}}" value="{{sheetQuery}}" bindinput="onSheetInput" confirm-type="done" />
|
||||
<scroll-view scroll-y class="sheet-list">
|
||||
<view wx:if="{{sheetCanCreate}}" class="sheet-item sheet-item-create" bindtap="onSheetCreate">{{sheetCreateLabel}}</view>
|
||||
<view class="sheet-item" wx:for="{{sheetList}}" wx:key="code" data-code="{{item.code}}" data-label="{{item.label}}" bindtap="onSheetPick">{{item.label}}</view>
|
||||
|
||||
Reference in New Issue
Block a user