Commit 0e4f9fd4 authored by GuanHua's avatar GuanHua

fix: 解决商品价格区间取值的问题

parent 0b917b15
......@@ -784,7 +784,7 @@ const CommodityDetail = (props) => {
*/
const setCurrentPriceRange = (uniPrice) => {
const initPriceRange = uniPrice
const tempPriceRange = []
let tempPriceRange = []
Object.keys(initPriceRange).forEach((key) => {
const keyArr = key.split('-')
const min = keyArr[0]
......@@ -796,6 +796,11 @@ const CommodityDetail = (props) => {
price: initPriceRange[key]
})
})
try {
tempPriceRange = tempPriceRange.sort((a, b) => a.price < b.price ? 1: -1)
} catch (error) {
console.log(error)
}
setCommodityPriceInfo(tempPriceRange)
}
......@@ -892,12 +897,12 @@ const CommodityDetail = (props) => {
const s1 = arg1.toString()
const s2 = arg2.toString()
try {
m += s1.split(".")[1].length;
m += s1.split(".")[1]? s1.split(".")[1].length : 0
} catch (e) {
console.log(e)
}
try {
m += s2.split(".")[1].length;
m += s2.split(".")[1] ? s2.split(".")[1].length : 0
} catch (e) {
console.log(e)
}
......@@ -905,16 +910,12 @@ const CommodityDetail = (props) => {
}
/**
* 获取金额区间中数量最大的区间
* 获取和购买数量相差最小的区间价格
*/
const getMaxCountRange = () => {
let maxItem: any = {}
for (const item of commodityPriceInfo) {
if (Number(item.max) > Number(maxItem.max || 0)) {
maxItem = item
}
}
return maxItem
const priceList = [...commodityPriceInfo]
const result = priceList.sort((a, b) => Math.abs(Number(b.max) - Number(buyCount)) < Math.abs(Number(a.min) - Number(buyCount)) ? 1 : -1 )
return result[0]
}
/**
......@@ -960,6 +961,23 @@ const CommodityDetail = (props) => {
return result
}
const checkItemInRang = (item) => {
if(Number(item.min) <= Number(buyCount) && Number(item.max) >= Number(buyCount)) {
return true
} else {
const temp = commodityPriceInfo.filter(item => {
return Number(buyCount) >= Number(item.min) && Number(buyCount) <= Number(item.max)
})
if(isEmpty(temp)) {
const nearItem = getMaxCountRange()
if(Number(nearItem.min) === Number(item.min)) {
return true
}
}
return false
}
}
return (
<Spin spinning={spinLoading}>
<div className={styles.commodity_detail}>
......@@ -980,7 +998,7 @@ const CommodityDetail = (props) => {
<span key={`product_info_tags_item_${index}`}>{item}{index !== commodityDetail?.sellingPoint.length - 1 ? ' ' : ''}</span>
))
}
</>
</>
)
}
</div>
......@@ -1000,7 +1018,7 @@ const CommodityDetail = (props) => {
<div className={styles.prompt_goods_price_list}>
{
selectCommodityId ? commodityPriceInfo && commodityPriceInfo.map((item, index) => (
<div className={cx(styles.prompt_goods_price_list_item, (commodityPriceInfo.length > 1 && Number(buyCount) >= item.min && Number(buyCount) <= item.max) ? styles.active : "")} key={`prompt_goods_price_list_item_${index}`}>
<div className={cx(styles.prompt_goods_price_list_item, (commodityPriceInfo.length > 1 && checkItemInRang(item)) ? styles.active : "")} key={`prompt_goods_price_list_item_${index}`}>
<div className={styles.price}>{priceFormat(item.price)}</div>
{
(commodityDetail?.isMemberPrice && (parameter || parameter === 0)) && <div className={styles.member_price}>{priceFormat(item.price * parameter)}</div>
......
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment