新建商品:商品名与分类必填其余选填
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s

- 品名、分类加红色必填标记,提交时拦空
- 分类不再默认选中第一个(-1,取参不再 || 0 兜底)
- 规格、单位、生产地区为空照常提交
This commit is contained in:
雨天
2026-10-23 17:29:26 +08:00
parent 456976df73
commit 3dce355f66
6 changed files with 53 additions and 8 deletions
@@ -293,3 +293,21 @@ Web `ProcessingManagement.standaloneUi.contract.test.ts` 新增 2 条(默认
证据:小程序契约测试新增 1 条(分类区不得含 `picker`、必须有 `cat-chips` / `pickOutputCategory` /
`+ 新建分类` / `toggleOutputCategoryCreate` / `submitOutputCategory`,且确实调 `createStockInboundCategory`);
注入对拍:把「+ 新建分类」的 `catchtap` 改成 `noop` ⇒ 立即变红(已还原)。
## 17. 追加(同日第十三次提交):新建商品「商品名 + 分类」必填,其余选填
开发者:「新增商品 商品的商品名与分类是必填 其他字段都是选填」。
改前的问题:分类默认 `categoryIndex: 0`,即**一打开表单就已经"选中"第一个分类** —— 「必填」形同虚设,
且容易把商品挂到错分类;表单上也没有任何必填标记。
改后(`miniprogram/components/processing-panel/`):
- **必填标记**:只有「品名」「分类」带红色 `*`(`.req-mark`,`#e11d48`),规格/单位/生产地区保持无标记;
- **分类不再默认选中**:`categoryIndex: -1`(`data` 初值与 `openOutputProductForm` 两处),未选就提交会被拦;
提交时的取参改为 `Number.isInteger(catIndex) && catIndex >= 0 ? options[catIndex] : null`,不再 `|| 0` 兜底;
- **校验口径**:`请填写商品名` / `请选择分类` 两个 toast 是唯一拦口;规格/单位/生产地区为空照常提交(不传即不写)。
证据:小程序契约测试新增 1 条(必填标记恰好 2 处、`categoryIndex: -1` 恰好 2 处、不得出现 `categoryIndex: 0`、两个 toast 存在、
不出现「请填写规格/单位/生产地区」);注入对拍两处 —— **把两处 `-1` 改回 `0` ⇒ 变红**、
**去掉分类的必填标记 ⇒ 变红**(均已还原)。
File diff suppressed because one or more lines are too long
@@ -244,7 +244,7 @@ Component({
allOutputSkus: [],
/** 成品:搜不到时的新建商品表单(复用库存入库同一个建品接口) */
showOutputProductModal: false,
outputProductForm: { name: '', categoryIndex: 0, spec: '', unit: '', origin: '' },
outputProductForm: { name: '', categoryIndex: -1, spec: '', unit: '', origin: '' },
outputCategoryOptions: [],
/** 分类:就地新建(不弹滚轮选择器) */
outputCategoryCreating: false,
@@ -1013,7 +1013,7 @@ Component({
const name = String(this.data.outputSkuSearch || '').trim();
this.setData({
showOutputProductModal: true,
outputProductForm: { name, categoryIndex: 0, spec: '', unit: '', origin: '' },
outputProductForm: { name, categoryIndex: -1, spec: '', unit: '', origin: '' },
outputCategoryCreating: false,
outputCategoryName: '',
});
@@ -1091,7 +1091,7 @@ Component({
const index = options.findIndex((o) => o.code === code);
this.setData({
outputCategoryOptions: options,
'outputProductForm.categoryIndex': index < 0 ? 0 : index,
'outputProductForm.categoryIndex': index,
outputCategoryCreating: false,
outputCategoryName: '',
});
@@ -1119,7 +1119,12 @@ Component({
wx.showToast({ title: '请填写商品名', icon: 'none' });
return;
}
const cat = (this.data.outputCategoryOptions || [])[Number(form.categoryIndex) || 0];
// 分类必填:没选(-1)就拦下来,不默认取第一个
const catIndex = Number(form.categoryIndex);
const cat =
Number.isInteger(catIndex) && catIndex >= 0
? (this.data.outputCategoryOptions || [])[catIndex]
: null;
if (!cat || !cat.code) {
wx.showToast({ title: '请选择分类', icon: 'none' });
return;
@@ -269,11 +269,11 @@
<view class="sheet-close" bindtap="closeOutputProductForm">×</view>
</view>
<view class="field">
<text class="field-label">品名</text>
<text class="field-label">品名<text class="req-mark">*</text></text>
<input class="field-input" placeholder="商品名" value="{{outputProductForm.name}}" data-field="name" bindinput="onOutputProductField" />
</view>
<view class="field">
<text class="field-label">分类</text>
<text class="field-label">分类<text class="req-mark">*</text></text>
<!-- 内联分类标签(不再弹滚轮选择器);末尾可就地新建分类 -->
<view class="cat-chips">
<view wx:for="{{outputCategoryOptions}}" wx:key="code" class="cat-chip {{outputProductForm.categoryIndex === index ? 'is-on' : ''}}" data-index="{{index}}" catchtap="pickOutputCategory">{{item.label}}</view>
@@ -500,6 +500,11 @@
}
/* 新建商品:分类内联标签(不弹滚轮选择器)+ 就地新建 */
/* 必填标记(商品名 / 分类) */
.req-mark {
color: #e11d48;
margin-left: 4rpx;
}
.cat-chips {
display: flex;
flex-wrap: wrap;
@@ -150,7 +150,24 @@ it('新建商品的分类框:内联标签 + 就地新建分类(不用滚轮
assert.ok(js.includes('await createStockInboundCategory(name);'));
assert.ok(js.includes('createStockInboundCategory,'), '建分类接口必须从 request 引入');
// 建完直接选中(不关表单)
assert.ok(js.includes("'outputProductForm.categoryIndex': index < 0 ? 0 : index,"));
assert.ok(js.includes("'outputProductForm.categoryIndex': index,"));
});
/**
* 2026-10-23 开发者:「新增商品 商品的商品名与分类是必填 其他字段都是选填」。
* ⇒ 只有品名/分类带必填标记并在提交时拦空;规格/单位/生产地区不参与校验。
* ⇒ 分类**不默认取第一个**(未选 = -1),否则「必填」形同虚设。
*/
it('新建商品:商品名与分类必填,其他字段选填', () => {
assert.strictEqual((wxml.match(/class="req-mark"/g) || []).length, 2, '只有品名与分类带必填标记');
assert.strictEqual((js.match(/categoryIndex: -1/g) || []).length, 2, '数据默认与打开表单两处都必须未选中');
assert.ok(!/categoryIndex: 0[,]/.test(js), '不得默认选中第一个分类');
assert.ok(js.includes('Number.isInteger(catIndex) && catIndex >= 0'), '未选分类必须拦下,不得默认取第一个');
assert.ok(js.includes("wx.showToast({ title: '请填写商品名', icon: 'none' });"));
assert.ok(js.includes("wx.showToast({ title: '请选择分类', icon: 'none' });"));
assert.ok(!js.includes('请填写规格'));
assert.ok(!js.includes('请填写单位'));
assert.ok(!js.includes('请填写生产地区'));
});
it('订单/需求单加工:毛重净重流程保持原样(不许被改)', () => {