Commit da95b8da by 张立顺

fix 订单最低价格校验

1 parent fec45f90
......@@ -456,14 +456,14 @@ Component({
[`orderInfo.vdQuotationGoodsList[${index}]`]: item
})
}
this.setGoodsListTotal()
this.setGoodsListTotal(false)
},
priceBlur(e) {
let { index, childind } = e.currentTarget.dataset
let thisPage = wx.$router.getPageInstance()
let { orderInfo } = thisPage.data
let item = orderInfo.vdQuotationGoodsList[index]
console.log( index, childind,item,getProductStrategy(item))
if (childind !== undefined) {
const subItem = item.packageGoods.goodsList[childind]
subItem.priceInput = null
......@@ -473,9 +473,38 @@ Component({
item.goods.priceInput = null
}
const strategy = getProductStrategy(item)
if (strategy) {
if (childind !== undefined && strategy.getSubItemData) {
const subItem = strategy.getSubItemData(childind)
const currentPrice =
subItem.basePrice !== undefined
? subItem.basePrice
: subItem.sellPrice !== undefined
? subItem.sellPrice
: subItem.price
if (!currentPrice && currentPrice !== 0 && subItem.validMinSellPrice === 'Y') {
strategy.updateSubItemPrice(childind, subItem.minSellPrice)
}
} else {
const currentPrice = strategy.getPrice()
const data = strategy.getData()
if (!currentPrice && currentPrice !== 0 && data.validMinSellPrice === 'Y') {
const minPrice = strategy.getMinPrice()
strategy.setPrice(minPrice)
}
}
const validation = strategy.performValidation()
console.log(validation,'performValidation')
if (!validation.valid) {
wx.$util.showToast(validation.message)
}
}
thisPage.setData({
[`orderInfo.vdQuotationGoodsList[${index}]`]: item
})
this.setGoodsListTotal()
},
//多规格单价修改
changeSpecific(e) {
......@@ -517,7 +546,7 @@ Component({
[`orderInfo.vdQuotationGoodsList[${index}]`]: item
})
}
this.setGoodsListTotal()
this.setGoodsListTotal(false)
},
changePricePackage(e) {
let { value } = e.detail
......@@ -561,7 +590,7 @@ Component({
[`orderInfo.vdQuotationGoodsList[${index}]`]: item
})
}
this.setGoodsListTotal()
this.setGoodsListTotal(false)
return value
},
delGoodsList(e) {
......@@ -584,7 +613,6 @@ Component({
const strategy = getProductStrategy(item)
if (strategy) {
strategy.recalculatePrices()
strategy.performValidation()
total = NP.plus(total, strategy.calculateSubtotal())
// Sync package price if it's a package
......
......@@ -1414,7 +1414,7 @@ Page({
//套餐
if (ele.goodsFlag == 3) {
let item = ele.packageGoods;
console.log(item, "套餐商品");
// console.log(item, "套餐商品");
ele.packageGoods.goodsList = item.goodsList.map(
(item) => {
item.price = this.multipliedByHundred(
......
......@@ -73,7 +73,7 @@ ProductStrategy.prototype.validatePrice = function (price) {
if (
data.validMinSellPrice === 'Y' &&
Number(checkPrice) < Number(data.minSellPrice)
NP.divide(Number(checkPrice), 100) < Number(data.minSellPrice)
) {
return { valid: false, message: '单价不可低于商品最低价' }
}
......@@ -162,9 +162,14 @@ ProductStrategy.prototype.setError = function (isError) {
const data = this.getData()
if (data) {
data.errorTarget = isError
data.checkError = isError
}
}
ProductStrategy.prototype.clearError = function () {
this.setError(false)
}
ProductStrategy.prototype.performValidation = function () {
// 使用 getBasePrice() 验证基础价格,而不是最终价格(this.getPrice())
// 避免因折扣导致最终价格低于最低售价时,基础价格输入框报红
......@@ -272,16 +277,28 @@ PackageProductStrategy.prototype.performValidation = function () {
this.item.packageGoods.goodsList.forEach(subItem => {
if (
subItem.validMinSellPrice === 'Y' &&
Number(subItem.price) < Number(subItem.minSellPrice)
NP.divide(Number(subItem.price), 100) < Number(subItem.minSellPrice)
) {
subItem.errorTarget = true
subItem.checkError = true
hasError = true
} else {
subItem.errorTarget = false
subItem.checkError = false
}
})
}
return { valid: !hasError }
return { valid: !hasError, message: hasError ? '部分商品价格低于最低价' : '' }
}
PackageProductStrategy.prototype.clearError = function () {
ProductStrategy.prototype.clearError.call(this)
if (this.item.packageGoods && this.item.packageGoods.goodsList) {
this.item.packageGoods.goodsList.forEach(subItem => {
subItem.errorTarget = false
subItem.checkError = false
})
}
}
PackageProductStrategy.prototype.recalculatePrices = function () {
......
......@@ -651,12 +651,14 @@ Page({
// 以及 validateSubItemPrice 方法进行价格合规性校验(如最低限价检查)。
// 这样做避免了在业务层直接操作复杂的嵌套数据结构,提高了代码的可读性和可维护性。
if (strategy && strategy.updateSubItemPrice) {
const validation = strategy.validateSubItemPrice(item, number)
if (!validation.valid) {
wx.$util.showToast(validation.message)
// 强制赋值最低价格
number = item.minSellPrice
}
// 移除输入时的校验,改为失焦校验
// const validation = strategy.validateSubItemPrice(item, number)
// if (!validation.valid) {
// wx.$util.showToast(validation.message)
// // 强制赋值最低价格
// number = item.minSellPrice
// }
strategy.clearError()
// 如果价格有变化,清空折扣输入框 (仅针对折扣单和组合单)
if (
......@@ -696,12 +698,14 @@ Page({
// 业务层无需关心具体商品类型的内部数据结构差异,实现了逻辑解耦。
const strategy = getProductStrategy(goodsList[index])
if (strategy) {
const validation = strategy.validatePrice(number)
if (!validation.valid) {
wx.$util.showToast(validation.message)
// 强制赋值最低价格
number = strategy.getMinPrice()
}
// 移除输入时的校验,改为失焦校验
// const validation = strategy.validatePrice(number)
// if (!validation.valid) {
// wx.$util.showToast(validation.message)
// // 强制赋值最低价格
// number = strategy.getMinPrice()
// }
strategy.clearError()
if (
this.data.orderObj.djOrderType == ORDER_TYPE.DISCOUNT ||
......@@ -727,21 +731,47 @@ Page({
return number
},
priceBlur(e) {
let { index } = e.currentTarget.dataset || e.detail
console.log(e,'priceBlur')
let { index, gindex } = e.detail
let { goodsList } = this.data
// 策略模式应用:
// 获取策略实例,利用 strategy.getPrice() 和 strategy.getMinPrice() 统一接口访问商品价格数据。
// 当输入价格为空时,自动恢复为最低售价,保证数据完整性。
const strategy = getProductStrategy(goodsList[index])
console.log(strategy,goodsList[index],index,'priceBlur')
if (strategy) {
const currentPrice = strategy.getPrice()
// 如果价格无效(空字符串等),重置为最低价
if (!currentPrice && currentPrice !== 0) {
const minPrice = strategy.getMinPrice()
strategy.setPrice(minPrice)
this.setGoodsTotal(goodsList)
if (gindex !== undefined && gindex !== null && strategy.getSubItemData) {
// 套餐子商品处理
const subItem = strategy.getSubItemData(gindex)
const currentPrice =
subItem.basePrice !== undefined
? subItem.basePrice
: subItem.sellPrice !== undefined
? subItem.sellPrice
: subItem.price
if (!currentPrice && currentPrice !== 0 && subItem.validMinSellPrice === 'Y') {
strategy.updateSubItemPrice(gindex, subItem.minSellPrice)
}
} else {
// 普通/多规格商品处理
const currentPrice = strategy.getPrice()
const data = strategy.getData()
// 如果价格无效(空字符串等),重置为最低价
if (!currentPrice && currentPrice !== 0 && data.validMinSellPrice === 'Y') {
const minPrice = strategy.getMinPrice()
strategy.setPrice(minPrice)
}
}
// 触发校验逻辑(显示红框)
// performValidation 会自动处理套餐内所有子商品的校验
const validation = strategy.performValidation()
if (!validation.valid) {
wx.$util.showToast(validation.message)
}
this.setGoodsTotal(goodsList)
}
},
setGoodsTotal(goodsList) {
......@@ -754,7 +784,7 @@ Page({
// 从而消除了原有的大量 if-else 判断,使总价计算逻辑清晰简洁。
const strategy = getProductStrategy(item)
if (strategy) {
strategy.performValidation()
// strategy.performValidation() // 移除此处校验,避免输入时触发
total = NP.plus(total, strategy.calculateSubtotal())
}
})
......
......@@ -566,6 +566,7 @@ value="{{ orderObj.remark }}"
bind:discountValueInput="inputDiscountValue"
bind:toggleGift="toggleGift"
bind:showPopup="showPopup"
bind:priceBlur="priceBlur"
data-item="{{ g }}"
data-gindex="{{ gInx }}"
data-index="{{ index }}"
......
......@@ -151,9 +151,14 @@ ProductStrategy.prototype.setError = function (isError) {
const data = this.getData()
if (data) {
data.errorTarget = isError
data.checkError = isError
}
}
ProductStrategy.prototype.clearError = function () {
this.setError(false)
}
ProductStrategy.prototype.performValidation = function () {
// 使用 getBasePrice() 验证基础价格,而不是最终价格(this.getPrice())
// 避免因折扣导致最终价格低于最低售价时,基础价格输入框报红
......@@ -268,13 +273,25 @@ PackageProductStrategy.prototype.performValidation = function () {
Number(checkPrice) < Number(subItem.minSellPrice)
) {
subItem.errorTarget = true
subItem.checkError = true
hasError = true
} else {
subItem.errorTarget = false
subItem.checkError = false
}
})
}
return { valid: !hasError }
return { valid: !hasError, message: hasError ? '部分商品价格低于最低价' : '' }
}
PackageProductStrategy.prototype.clearError = function () {
ProductStrategy.prototype.clearError.call(this)
if (this.item.packageGoods && this.item.packageGoods.goodsList) {
this.item.packageGoods.goodsList.forEach(subItem => {
subItem.errorTarget = false
subItem.checkError = false
})
}
}
PackageProductStrategy.prototype.getSubItemData = function (index) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!