Commit ccfd609b by wangzhihui

```

feat(stock): 完善库存数量输入校验功能

- 移除 el-input 的 @input 事件监听,改用自定义校验器
- 添加 validateStockNum 校验方法,支持必填、数字格式、数值范围校验
- 集成表单验证,在提交时统一校验表单数据
- 优化输入处理逻辑,自动格式化为最多两位小数的数字
- 增加库存数量不能超过当前库存上限的业务校验
```
1 parent 6e0cdf90
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
<span>{{ row.upgradeCommodityCode || '-' }}</span> <span>{{ row.upgradeCommodityCode || '-' }}</span>
</el-form-item> </el-form-item>
<el-form-item v-if="row.isFilm" :error="errorMsg" label="库存数量:" prop="stockNum"> <el-form-item v-if="row.isFilm" :error="errorMsg" label="库存数量:" prop="stockNum">
<el-input v-model="form.stockNum" placeholder="请输入" style="width: 200px" @input="handleInput"> <el-input v-model="form.stockNum" placeholder="请输入" style="width: 200px">
<template #append> <template #append>
米 米
</template> </template>
...@@ -45,9 +45,12 @@ export default { ...@@ -45,9 +45,12 @@ export default {
stockNum: '' stockNum: ''
}, },
rules: { rules: {
stockNum: [{ required: true, message: '请输入库存数量', trigger: 'blur' }] stockNum: [
{ required: true, message: '请输入库存数量' },
{ validator: this.validateStockNum, trigger: 'blur' },
{ validator: this.validateStockNum, trigger: 'change' }
]
}, },
errorMsg: '',
isLoading: false isLoading: false
} }
}, },
...@@ -62,71 +65,90 @@ export default { ...@@ -62,71 +65,90 @@ export default {
} }
}, },
methods: { methods: {
handleClose(done) { validateStockNum(rule, val, callback) {
this.dialogVisible = false // 1. 必填校验
if (typeof done === 'function') done() if (!val) {
}, return callback(new Error('请输入库存数量'))
handleInput(val) { }
// 2. 格式化输入:只能输入数字,保留2位小数
let formatted = val.replace(/[^\d.]/g, '') let formatted = val.replace(/[^\d.]/g, '')
formatted = limitPrice(formatted, this.form.stockNum, 10, 2, 0) formatted = limitPrice(formatted, this.form.stockNum, 10, 2, 0)
this.form.stockNum = formatted
if (this.form.stockNum && Number(this.form.stockNum) > Number(this.row.stock)) { // 使用 $nextTick 更新表单数据,保持视图同步
this.errorMsg = '输入值不能高于当前库存数量' this.$nextTick(() => {
} else { this.form.stockNum = formatted
this.errorMsg = '' })
}
},
handleConfirm() {
if (this.isLoading) return
if (this.row.isFilm) { const numVal = Number(formatted)
if (!this.form.stockNum) {
this.$message.warning('请输入库存数量') // 3. 校验数值有效性
return if (isNaN(numVal)) {
} return callback(new Error('请输入有效的数字'))
if (Number(this.form.stockNum) > Number(this.row.stock)) {
this.errorMsg = '输入值不能高于当前库存数量'
this.$message.error('输入值不能高于当前库存数量')
return
}
} else {
if (!this.row.stock || Number(this.row.stock) <= 0) {
this.$message.error('当前产品库存必须大于0,才允许补库存标签码')
return
}
} }
const params = { if (numVal <= 0) {
// merchantId: this.row.merchantId, return callback(new Error('库存数量必须大于0'))
upgradeGoodsId: this.row.upgradeGoodsId,
goodsType: this.row.goodsType,
merchantId: this.row.yqMerchantId
} }
if (this.row.isFilm) { // 4. 校验是否超过当前库存上限
params.stockNum = this.form.stockNum const maxStock = Number(this.row.stock)
if (!isNaN(maxStock) && numVal > maxStock) {
return callback(new Error('输入值不能高于当前库存数量'))
} }
this.isLoading = true // 校验通过
this.$apis.stockManage callback()
.labelCodeRecode({ dataString: JSON.stringify(params) }) },
.then(res => { handleClose(done) {
this.isLoading = false this.dialogVisible = false
if (res && res.code == 0) { if (typeof done === 'function') done()
const url = res.data.labelImageUrl },
if (url) {
this.downloadFile(url) handleConfirm() {
this.dialogVisible = false this.$refs.form.validate(valid => {
this.$message.success('补库存标签码成功并开始下载') if (valid) {
if (this.isLoading) return
if (!this.row.isFilm) {
if (!this.row.stock || Number(this.row.stock) <= 0) {
this.$message.error('当前产品库存必须大于0,才允许补库存标签码')
return
} }
} else {
this.$message.error(res.message)
} }
})
.catch(() => { const params = {
this.isLoading = false // merchantId: this.row.merchantId,
}) upgradeGoodsId: this.row.upgradeGoodsId,
goodsType: this.row.goodsType,
merchantId: this.row.yqMerchantId
}
if (this.row.isFilm) {
params.stockNum = this.form.stockNum
}
this.isLoading = true
this.$apis.stockManage
.labelCodeRecode({ dataString: JSON.stringify(params) })
.then(res => {
this.isLoading = false
if (res && res.code == 0) {
const url = res.data.labelImageUrl
if (url) {
this.downloadFile(url)
this.dialogVisible = false
this.$message.success('补库存标签码成功并开始下载')
}
} else {
this.$message.error(res.message)
}
})
.catch(() => {
this.isLoading = false
})
}
})
}, },
downloadFile(url, fileName) { downloadFile(url, fileName) {
const link = document.createElement('a') const link = document.createElement('a')
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!