重构小程序成品搜索框并支持搜不到建品
- 输入处理器只回写用户输入的字符串,不再把候选或结果写进输入框 - 候选瘦身到 4 个字段,本地过滤优先,本地没命中再回服务端 - 搜不到时给「用该名称新建商品」入口,复用库存入库建品接口 - 建好自动加入成品行并刷新候选
This commit is contained in:
@@ -8,6 +8,8 @@ const {
|
||||
completeStandaloneProcessingTask,
|
||||
listStandaloneLines,
|
||||
fetchStockInboundSkuOptions,
|
||||
fetchStockInboundCategories,
|
||||
createStockInboundProduct,
|
||||
uploadProcessingInspectPhoto,
|
||||
submitProcessingInspection,
|
||||
} = require('../../utils/request');
|
||||
@@ -17,6 +19,30 @@ const { prepareOrderImageBase64 } = require('../../utils/orderImage');
|
||||
|
||||
const WAREHOUSE_ROLES = ['warehouse', 'inspector'];
|
||||
|
||||
/**
|
||||
* 成品候选:只留展示需要的字段。
|
||||
* 服务端 `listInboundSkuOptions` 每行还带 `backfill` 等大对象,整块 setData 会拖慢输入框(真机上表现为卡顿/丢字)。
|
||||
*/
|
||||
function toOutputSkuOption(row) {
|
||||
const code = String((row && row.sku_code) || '').trim();
|
||||
if (!code) return null;
|
||||
return {
|
||||
sku_code: code,
|
||||
sku_name: String((row && row.sku_name) || code),
|
||||
spec: row && row.spec ? String(row.spec) : '',
|
||||
unit: row && row.unit ? String(row.unit) : '',
|
||||
};
|
||||
}
|
||||
|
||||
function matchesOutputSku(option, keyword) {
|
||||
const kw = String(keyword || '').toLowerCase();
|
||||
if (!kw) return true;
|
||||
return (
|
||||
String((option && option.sku_name) || '').toLowerCase().includes(kw) ||
|
||||
String((option && option.sku_code) || '').toLowerCase().includes(kw)
|
||||
);
|
||||
}
|
||||
|
||||
function formatLocalDate(date = new Date()) {
|
||||
const y = date.getFullYear();
|
||||
const m = String(date.getMonth() + 1).padStart(2, '0');
|
||||
@@ -215,6 +241,11 @@ Component({
|
||||
outputSkuOptions: [],
|
||||
/** 打开弹窗时一次性载入的在售商品(与 Web 同交互:输入本地即时过滤) */
|
||||
allOutputSkus: [],
|
||||
/** 成品:搜不到时的新建商品表单(复用库存入库同一个建品接口) */
|
||||
showOutputProductModal: false,
|
||||
outputProductForm: { name: '', categoryIndex: 0, spec: '', unit: '', origin: '' },
|
||||
outputCategoryOptions: [],
|
||||
creatingOutputProduct: false,
|
||||
/**
|
||||
* ⚠️ 2026-10-23 复查结论:搜索框必须**受控**(wxml 绑定 `value="{{outputSkuSearch}}"`)。
|
||||
* 之前为绕开 `undefined` 改成"非受控 + epoch 重建输入框",真机上表现为**输入什么都被清空**,
|
||||
@@ -281,6 +312,10 @@ Component({
|
||||
|
||||
lifetimes: {
|
||||
attached() {
|
||||
// 防呆:搜索关键字必须是字符串(历史上出现过数据层 undefined 被当成关键字搜出去)
|
||||
if (typeof this.data.outputSkuSearch !== 'string') {
|
||||
this.setData({ outputSkuSearch: '' });
|
||||
}
|
||||
this.onPanelShow();
|
||||
},
|
||||
},
|
||||
@@ -822,7 +857,8 @@ Component({
|
||||
try {
|
||||
const res = await fetchStockInboundSkuOptions({ limit: 50 });
|
||||
const rows = res && Array.isArray(res.data) ? res.data : [];
|
||||
this.setData({ allOutputSkus: rows, outputSkuOptions: rows });
|
||||
const all = rows.map(toOutputSkuOption).filter(Boolean);
|
||||
this.setData({ allOutputSkus: all, outputSkuOptions: all });
|
||||
} catch (err) {
|
||||
this.setData({ allOutputSkus: [], outputSkuOptions: [] });
|
||||
}
|
||||
@@ -892,55 +928,69 @@ Component({
|
||||
);
|
||||
},
|
||||
|
||||
async searchOutputSkus(e) {
|
||||
const search = String((e && e.detail && e.detail.value) || '');
|
||||
const keyword = search.trim().toLowerCase();
|
||||
this.setData({ outputSkuSearch: search });
|
||||
/**
|
||||
* 成品搜索输入(2026-10-23 重构)。
|
||||
* 这里**只做一件事**:把用户敲的字原样写回输入框,然后刷新候选。
|
||||
* ⚠️ 绝不能在这里(或任何搜索回调里)把「搜索结果」写回输入框——
|
||||
* 历史 bug:搜不到时把空结果/异常值回填,导致输入内容被清空、框里出现字面量 `undefined`,
|
||||
* 还被当成关键字又搜一次(Network 里可见 `search=undefined`)。
|
||||
*/
|
||||
onOutputSkuInput(e) {
|
||||
const raw = e && e.detail ? e.detail.value : '';
|
||||
const keyword = typeof raw === 'string' ? raw : '';
|
||||
this.setData({ outputSkuSearch: keyword });
|
||||
this.refreshOutputSkuOptions(keyword);
|
||||
},
|
||||
|
||||
/** 候选:本地过滤优先(输入即时),本地没命中才回服务端搜全量(丢过期响应) */
|
||||
refreshOutputSkuOptions(keyword) {
|
||||
const text = typeof keyword === 'string' ? keyword : '';
|
||||
const kw = text.trim().toLowerCase();
|
||||
const all = this.data.allOutputSkus || [];
|
||||
if (!keyword) {
|
||||
if (!kw) {
|
||||
this.setData({ outputSkuOptions: all });
|
||||
return;
|
||||
}
|
||||
// 与 Web 同交互:先在已载入的商品里本地过滤(输入即时响应,不等网络)
|
||||
const local = all.filter(
|
||||
(o) =>
|
||||
String(o.sku_name || '').toLowerCase().includes(keyword) ||
|
||||
String(o.sku_code || '').toLowerCase().includes(keyword)
|
||||
);
|
||||
const local = all.filter((o) => matchesOutputSku(o, kw));
|
||||
if (local.length) {
|
||||
this.setData({ outputSkuOptions: local });
|
||||
return;
|
||||
}
|
||||
// 已载入的那批里没有 ⇒ 回服务端搜全量(防商品多时漏掉)
|
||||
const seq = (this._outputSkuSearchSeq || 0) + 1;
|
||||
this._outputSkuSearchSeq = seq;
|
||||
try {
|
||||
const res = await fetchStockInboundSkuOptions({ search: search.trim(), limit: 50 });
|
||||
if (seq !== this._outputSkuSearchSeq) return;
|
||||
this.setData({ outputSkuOptions: res && Array.isArray(res.data) ? res.data : [] });
|
||||
} catch (err) {
|
||||
if (seq !== this._outputSkuSearchSeq) return;
|
||||
this.setData({ outputSkuOptions: [] });
|
||||
}
|
||||
const seq = (this._outputSkuSeq || 0) + 1;
|
||||
this._outputSkuSeq = seq;
|
||||
fetchStockInboundSkuOptions({ search: text.trim(), limit: 20 })
|
||||
.then((res) => {
|
||||
if (seq !== this._outputSkuSeq) return;
|
||||
const rows = res && Array.isArray(res.data) ? res.data : [];
|
||||
this.setData({ outputSkuOptions: rows.map(toOutputSkuOption).filter(Boolean) });
|
||||
})
|
||||
.catch(() => {
|
||||
if (seq !== this._outputSkuSeq) return;
|
||||
this.setData({ outputSkuOptions: [] });
|
||||
});
|
||||
},
|
||||
|
||||
addOutputSku(e) {
|
||||
const skuCode = e.currentTarget.dataset.sku;
|
||||
const opt = (this.data.outputSkuOptions || []).find((o) => o.sku_code === skuCode);
|
||||
if (!opt) return;
|
||||
if (this.data.outputRows.some((r) => r.sku_code === skuCode)) {
|
||||
this.setData({ outputSkuOptions: this.data.allOutputSkus || [], outputSkuSearch: '' });
|
||||
return;
|
||||
}
|
||||
this.appendOutputRow(opt);
|
||||
// 选中后清空关键字、恢复全量候选(与 Web 一致)
|
||||
this.setData({ outputSkuSearch: '', outputSkuOptions: this.data.allOutputSkus || [] });
|
||||
},
|
||||
|
||||
/** 加一行成品(两个报告继承默认勾选;同 SKU 不重复加) */
|
||||
appendOutputRow(opt) {
|
||||
const code = String((opt && opt.sku_code) || '');
|
||||
if (!code) return;
|
||||
if ((this.data.outputRows || []).some((r) => r.sku_code === code)) return;
|
||||
this.setData(
|
||||
{
|
||||
outputSkuOptions: this.data.allOutputSkus || [],
|
||||
outputSkuSearch: '',
|
||||
outputRows: [
|
||||
outputRows: [
|
||||
...this.data.outputRows,
|
||||
{
|
||||
sku_code: opt.sku_code,
|
||||
sku_name: opt.sku_name,
|
||||
sku_code: code,
|
||||
sku_name: String((opt && opt.sku_name) || code),
|
||||
qty: '',
|
||||
unit_cost: '',
|
||||
unit_cost_manual: false,
|
||||
@@ -953,9 +1003,94 @@ Component({
|
||||
);
|
||||
},
|
||||
|
||||
/** 搜不到 → 用当前搜索词新建商品(与 Web「搜不到就用该名称新建」同口径) */
|
||||
async openOutputProductForm() {
|
||||
const name = String(this.data.outputSkuSearch || '').trim();
|
||||
this.setData({
|
||||
showOutputProductModal: true,
|
||||
outputProductForm: { name, categoryIndex: 0, spec: '', unit: '', origin: '' },
|
||||
});
|
||||
if (!(this.data.outputCategoryOptions || []).length) await this.loadOutputCategoryOptions();
|
||||
},
|
||||
|
||||
async loadOutputCategoryOptions() {
|
||||
try {
|
||||
const res = await fetchStockInboundCategories();
|
||||
const rows = (res && (res.data || res)) || [];
|
||||
const options = (Array.isArray(rows) ? rows : [])
|
||||
.map((row) => ({
|
||||
code: String((row && row.category_code) || ''),
|
||||
label: String((row && (row.category_name || row.label)) || (row && row.category_code) || ''),
|
||||
}))
|
||||
.filter((o) => o.code);
|
||||
this.setData({ outputCategoryOptions: options });
|
||||
} catch (err) {
|
||||
this.setData({ outputCategoryOptions: [] });
|
||||
}
|
||||
},
|
||||
|
||||
onOutputProductField(e) {
|
||||
const field = e.currentTarget.dataset.field;
|
||||
if (!field) return;
|
||||
this.setData({ [`outputProductForm.${field}`]: e.detail.value });
|
||||
},
|
||||
|
||||
onOutputProductCategory(e) {
|
||||
this.setData({ 'outputProductForm.categoryIndex': Number(e.detail.value) || 0 });
|
||||
},
|
||||
|
||||
closeOutputProductForm() {
|
||||
if (this.data.creatingOutputProduct) return;
|
||||
this.setData({ showOutputProductModal: false });
|
||||
},
|
||||
|
||||
async submitOutputProduct() {
|
||||
if (this.data.creatingOutputProduct) return;
|
||||
const form = this.data.outputProductForm || {};
|
||||
const name = String(form.name || '').trim();
|
||||
if (!name) {
|
||||
wx.showToast({ title: '请填写商品名', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
const cat = (this.data.outputCategoryOptions || [])[Number(form.categoryIndex) || 0];
|
||||
if (!cat || !cat.code) {
|
||||
wx.showToast({ title: '请选择分类', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
this.setData({ creatingOutputProduct: true });
|
||||
try {
|
||||
const res = await createStockInboundProduct({
|
||||
name,
|
||||
parent_code: cat.code,
|
||||
spec: String(form.spec || '').trim() || undefined,
|
||||
unit: String(form.unit || '').trim() || undefined,
|
||||
origin: String(form.origin || '').trim() || undefined,
|
||||
});
|
||||
const data = (res && (res.data || res)) || {};
|
||||
const code = String(data.skuCode || data.sku_code || '');
|
||||
if (!code) {
|
||||
wx.showToast({ title: '新建商品失败', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
this.setData({ showOutputProductModal: false, outputSkuSearch: '' });
|
||||
this.appendOutputRow({
|
||||
sku_code: code,
|
||||
sku_name: String(data.name || data.sku_name || name),
|
||||
unit: String(form.unit || ''),
|
||||
});
|
||||
this.loadOutputSkuOptions();
|
||||
wx.showToast({ title: '商品已创建并加入', icon: 'success' });
|
||||
} catch (err) {
|
||||
wx.showToast({ title: err.message || '新建商品失败', icon: 'none' });
|
||||
} finally {
|
||||
this.setData({ creatingOutputProduct: false });
|
||||
}
|
||||
},
|
||||
|
||||
closeOutputForm() {
|
||||
this.setData({
|
||||
showOutputModal: false,
|
||||
showOutputProductModal: false,
|
||||
outputTaskId: '',
|
||||
outputTaskCode: '',
|
||||
outputRows: [],
|
||||
|
||||
@@ -204,12 +204,17 @@
|
||||
</view>
|
||||
<view class="field">
|
||||
<text class="field-label">成品明细(可多行)</text>
|
||||
<!-- 受控输入框:必须绑定 value,否则真机上输入内容会被框架丢弃(2026-10-23 复查:非受控 + epoch 重建 ⇒ 输入始终为空) -->
|
||||
<input class="field-input" placeholder="搜索成品商品,点下方加入" value="{{outputSkuSearch}}" bindinput="searchOutputSkus" />
|
||||
<!-- 受控输入框:只写回用户输入;搜不到时给「用该名称新建商品」入口(绝不用搜索结果覆盖输入框) -->
|
||||
<input class="field-input" placeholder="搜索成品商品,点下方加入" value="{{outputSkuSearch}}" bindinput="onOutputSkuInput" />
|
||||
<view wx:if="{{outputSkuOptions.length}}" class="sku-options">
|
||||
<view wx:for="{{outputSkuOptions}}" wx:key="sku_code" class="sku-option" catchtap="addOutputSku" data-sku="{{item.sku_code}}">
|
||||
<text class="sku-name">{{item.sku_name}}</text>
|
||||
<text class="sku-meta">{{item.sku_code}}</text>
|
||||
<text class="sku-meta">{{item.sku_code}}{{item.spec ? ' · ' + item.spec : ''}}</text>
|
||||
</view>
|
||||
</view>
|
||||
<view wx:elif="{{outputSkuSearch}}" class="sku-options">
|
||||
<view class="sku-option sku-option-create" catchtap="openOutputProductForm">
|
||||
<text class="sku-name">搜不到「{{outputSkuSearch}}」,点此用该名称新建商品</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
@@ -254,6 +259,46 @@
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 搜不到 → 新建成品商品(复用库存入库同一个建品接口与字段) -->
|
||||
<view wx:if="{{showOutputProductModal}}" class="sheet-mask" catchtouchmove="stopCreateTap" bindtap="closeOutputProductForm">
|
||||
<view class="sheet-card" catchtap="stopCreateTap">
|
||||
<view class="sheet-head">
|
||||
<view>
|
||||
<view class="sheet-title">新建商品</view>
|
||||
</view>
|
||||
<view class="sheet-close" bindtap="closeOutputProductForm">×</view>
|
||||
</view>
|
||||
<view class="field">
|
||||
<text class="field-label">品名</text>
|
||||
<input class="field-input" placeholder="商品名" value="{{outputProductForm.name}}" data-field="name" bindinput="onOutputProductField" />
|
||||
</view>
|
||||
<view class="field">
|
||||
<text class="field-label">分类</text>
|
||||
<picker mode="selector" range="{{outputCategoryOptions}}" range-key="label" value="{{outputProductForm.categoryIndex}}" bindchange="onOutputProductCategory">
|
||||
<view class="field-input">{{outputCategoryOptions[outputProductForm.categoryIndex].label || '选择分类'}} ▾</view>
|
||||
</picker>
|
||||
</view>
|
||||
<view class="field-grid">
|
||||
<view class="field">
|
||||
<text class="field-label">规格</text>
|
||||
<input class="field-input" placeholder="规格" value="{{outputProductForm.spec}}" data-field="spec" bindinput="onOutputProductField" />
|
||||
</view>
|
||||
<view class="field">
|
||||
<text class="field-label">单位</text>
|
||||
<input class="field-input" placeholder="单位" value="{{outputProductForm.unit}}" data-field="unit" bindinput="onOutputProductField" />
|
||||
</view>
|
||||
</view>
|
||||
<view class="field">
|
||||
<text class="field-label">生产地区</text>
|
||||
<input class="field-input" placeholder="生产地区" value="{{outputProductForm.origin}}" data-field="origin" bindinput="onOutputProductField" />
|
||||
</view>
|
||||
<view class="sheet-actions">
|
||||
<button class="btn-ghost" bindtap="closeOutputProductForm">取消</button>
|
||||
<button class="btn-primary btn-block" loading="{{creatingOutputProduct}}" bindtap="submitOutputProduct">确定</button>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view wx:if="{{showWorkModal}}" class="sheet-mask" catchtouchmove="stopCreateTap" bindtap="closeWorkForm">
|
||||
<view class="sheet-card" catchtap="stopCreateTap">
|
||||
<view class="sheet-head">
|
||||
|
||||
@@ -494,6 +494,11 @@
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
/* 「搜不到 → 用该名称新建商品」入口(可点,绿色) */
|
||||
.sku-option-create .sku-name {
|
||||
color: #047857;
|
||||
}
|
||||
|
||||
.sku-name {
|
||||
font-size: 26rpx;
|
||||
font-weight: 600;
|
||||
|
||||
@@ -81,22 +81,23 @@ it('成品搜索:input 绑定的键必须在 data 中声明', () => {
|
||||
const dataBlock = js.slice(dataStart, dataEnd);
|
||||
assert.ok(/\boutputSkuSearch:\s*''/.test(dataBlock), 'data 缺少 outputSkuSearch');
|
||||
assert.ok(/\boutputSkuOptions:\s*\[\]/.test(dataBlock), 'data 缺少 outputSkuOptions');
|
||||
assert.ok(wxml.includes('bindinput="searchOutputSkus"'));
|
||||
assert.ok(wxml.includes('bindinput="onOutputSkuInput"'));
|
||||
});
|
||||
|
||||
/**
|
||||
* 2026-10-23 真机两轮复现:
|
||||
* ① 先出现字面量 `undefined`(受控但数据层被写入 undefined,又被 bindinput 当关键字提交);
|
||||
* ① 先出现字面量 `undefined`(数据层被写入 undefined,又被 bindinput 当关键字提交);
|
||||
* ② 改成"非受控 + epoch 重建"后,真机表现为**输入什么都被清空**。
|
||||
* 现行修法:**受控输入 + data 声明 + 处理器 `|| ''` 兜底**(不再重建输入框)。
|
||||
* 现行修法:**受控输入 + data 声明 + 写入时严格取字符串**(不再重建输入框)。
|
||||
*/
|
||||
it('搜索框必须**受控**(绑定 value),且处理器对空事件兜底', () => {
|
||||
// 2026-10-23 复查:改成"非受控 + epoch 重建"后真机表现为"输入什么都被清空" ⇒ 必须受控
|
||||
it('搜索框必须受控(绑定 value),且处理器只回写字符串', () => {
|
||||
assert.ok(/value="\{\{outputSkuSearch\}\}"/.test(wxml), '搜索框必须绑定 value(否则真机上输入会被丢弃)');
|
||||
assert.ok(!js.includes('outputSearchEpochList'), '不应再用 epoch 重建输入框');
|
||||
// 只认「搜索处理器内部」的兜底(整个文件里别处也有同样写法,全局 includes 会误判)
|
||||
const searchFn = js.slice(js.indexOf('async searchOutputSkus(e)'), js.indexOf('addOutputSku(e)'));
|
||||
assert.ok(searchFn.includes("String((e && e.detail && e.detail.value) || '')"), '搜索处理器必须对 undefined 事件兜底');
|
||||
// 处理器只认字符串;非字符串一律当空串(不再出现 `undefined` 字面量)
|
||||
const inputFn = js.slice(js.indexOf('onOutputSkuInput(e)'), js.indexOf('refreshOutputSkuOptions(keyword)'));
|
||||
assert.ok(inputFn.includes("typeof raw === 'string' ? raw : ''"), '输入处理器必须只回写字符串');
|
||||
// 输入处理器里**不得**触碰候选/搜索结果(历史上就是把结果写回输入框导致清空/undefined)
|
||||
assert.ok(!inputFn.includes('outputSkuOptions'), '输入处理器不得把候选列表写回输入框');
|
||||
const dataBlock = js.slice(js.indexOf(' data: {'), js.indexOf('\n observers:'));
|
||||
assert.ok(/outputSkuSearch:\s*''/.test(dataBlock), 'data 必须声明 outputSkuSearch');
|
||||
});
|
||||
@@ -109,11 +110,30 @@ it('成品搜索与 Web 同交互:打开即载入候选 + 本地即时过滤 +
|
||||
assert.ok(js.includes('this.loadOutputSkuOptions();'));
|
||||
assert.ok(js.includes('const rows = res && Array.isArray(res.data) ? res.data : [];'));
|
||||
// 输入时本地过滤
|
||||
assert.ok(js.includes('const local = all.filter('));
|
||||
assert.ok(js.includes('.includes(keyword)'));
|
||||
assert.ok(js.includes('const local = all.filter((o) => matchesOutputSku(o, kw));'));
|
||||
// 本地没命中才请求服务端,且丢弃过期响应
|
||||
assert.ok(js.includes('const seq = (this._outputSkuSearchSeq || 0) + 1;'));
|
||||
assert.ok(js.includes('if (seq !== this._outputSkuSearchSeq) return;'));
|
||||
assert.ok(js.includes('const seq = (this._outputSkuSeq || 0) + 1;'));
|
||||
assert.ok(js.includes('if (seq !== this._outputSkuSeq) return;'));
|
||||
// 候选只留展示字段(不把服务端的大对象整块 setData)
|
||||
assert.ok(js.includes('function toOutputSkuOption(row)'));
|
||||
});
|
||||
|
||||
/**
|
||||
* 2026-10-23 开发者:「没有搜索到应该是新增商品呀」。
|
||||
* 搜不到时不再是一片空白,而是给「搜不到「xx」,点此用该名称新建商品」入口,
|
||||
* 建品复用库存入库同一个接口/字段(`fetchStockInboundCategories` + `createStockInboundProduct`),建好直接加入成品行。
|
||||
*/
|
||||
it('搜不到 → 用该名称新建商品(复用库存入库建品接口)', () => {
|
||||
assert.ok(wxml.includes("catchtap=\"openOutputProductForm\""), '搜不到入口必须真的渲染出来');
|
||||
assert.ok(wxml.includes('搜不到「{{outputSkuSearch}}」'));
|
||||
assert.ok(wxml.includes('wx:elif="{{outputSkuSearch}}"'), '候选为空且有关键字时才显示建品入口');
|
||||
assert.ok(js.includes('async openOutputProductForm()'));
|
||||
assert.ok(js.includes('fetchStockInboundCategories()'));
|
||||
assert.ok(js.includes('await createStockInboundProduct({'));
|
||||
assert.ok(js.includes('parent_code: cat.code'));
|
||||
// 建好直接加入成品行(默认勾选两个继承开关)
|
||||
assert.ok(js.includes('this.appendOutputRow({'));
|
||||
assert.ok(wxml.includes('showOutputProductModal'));
|
||||
});
|
||||
|
||||
it('订单/需求单加工:毛重净重流程保持原样(不许被改)', () => {
|
||||
|
||||
Reference in New Issue
Block a user