修改使用优惠券逻辑

This commit is contained in:
2024-11-19 17:48:48 +08:00
parent 3316c470d2
commit 0069b7c6fd
3 changed files with 62 additions and 17 deletions

View File

@@ -227,7 +227,7 @@
import editDiscount from '@/components/my-components/edit-discount.vue'
import editAccountPoints from './components/edit-accountPoints.vue'
import {
returnGoodsPayPriceMap,returnProCoupStartIndex ,returnProductCoupAllPrice
returnGoodsPayPriceMap,returnProCoupStartIndex ,returnProductCoupAllPrice,returnProductCanUseNum
} from '../quan_util.js'
//商品数量从0到n每一个对应的价格
let $goodsPayPriceMap={}
@@ -396,7 +396,7 @@
uni.$on('choose-quan', (arr) => {
console.log(arr);
const manjianCoup = arr.filter(v => v.type == 1&&v.num>=1)
let productCoup = arr.filter(v => v.type == 2&&v.num>=1)
let productCoup = arr.filter(v => v.type == 2)
console.log(productCoup);
//商品券分组
let coupMap={}
@@ -408,16 +408,20 @@
coupMap[coup.proId]=[coup]
}
}
console.log(coupMap);
for(let key in coupMap){
const arr=coupMap[key]
for(let i in arr){
const coup=arr[i]
const proCoupStartIndex=returnProCoupStartIndex(arr,i)
const num=Math.min($goodsPayPriceMap[coup.proId].length,coup.num)
console.log(proCoupStartIndex);
const coupUseNum=returnProductCanUseNum($goodsPayPriceMap[coup.proId],proCoupStartIndex,coup.num)
const num=Math.min($goodsPayPriceMap[coup.proId].length,coupUseNum)
coup.num=num
coup.discountAmount=returnProductCoupAllPrice($goodsPayPriceMap[coup.proId],proCoupStartIndex,num).toFixed(2)
}
}
productCoup=productCoup.filter(v=>v.num>=1)
console.log(productCoup);
pays.quan = [...manjianCoup, ...productCoup]
})

View File

@@ -21,7 +21,8 @@
</view>
<view class=" u-m-t-20 u-flex">
<view>有效期:</view>
<view class="u-font-24 u-m-l-6"> {{dayjs(item.endTime).format('YYYY-MM-DD HH:mm:ss') }}</view>
<view class="u-font-24 u-m-l-6"> {{dayjs(item.endTime).format('YYYY-MM-DD HH:mm:ss') }}
</view>
</view>
<view class="u-m-t-10 color-999 u-font-24">
{{ formatStr(item.useRestrictions)}}
@@ -65,7 +66,8 @@
</view>
<view class=" u-m-t-14 u-flex">
<view>有效期:</view>
<view class="u-font-24 u-m-l-6"> {{dayjs(item.endTime).format('YYYY-MM-DD HH:mm:ss') }}</view>
<view class="u-font-24 u-m-l-6"> {{dayjs(item.endTime).format('YYYY-MM-DD HH:mm:ss') }}
</view>
</view>
<view class="u-m-t-10 color-999 u-font-24">
{{ formatStr(item.useRestrictions)}}
@@ -92,7 +94,7 @@
<view :style="{height:safebottomHeight+'px'}"></view>
<view class="fixed-b bottom safe-bottom border-top">
<view class="u-m-b-32 u-flex u-row-between u-p-t-10">
<view class="u-flex">
<text>抵扣金额</text>
@@ -103,7 +105,8 @@
<text>支付金额</text>
<text class="color-red"></text>
<text class="color-red">{{payPrice }}</text>
<view class="u-absolute u-flex u-row-between" style="bottom: 100%;right: 0;" v-if="payPrice*1!=option.orderPrice*1">
<view class="u-absolute u-flex u-row-between" style="bottom: 100%;right: 0;"
v-if="payPrice*1!=option.orderPrice*1">
<view class="u-flex line-th color-999">
<text class=""></text>
<text class="">{{option.orderPrice}}</text>
@@ -229,13 +232,18 @@
if (!item.use) {
return
}
console.log();
if (myQuan.fullReductionCouponSel.id && !item.checked) {
const goodsQuan = myQuan.res.productCoupon.filter(v => v.checked)
const fullReductionCoupon = myQuan.fullReductionCouponSel.id ? [myQuan.fullReductionCouponSel] : []
let coupArr = [...goodsQuan, item]
const payPrice = option.orderPrice - returnCouponAllPrice(coupArr, canDikouGoodsArr, user.value)
console.log(payPrice);
if (payPrice<=0) {
modal.content = '选择该商品券后支付金额将为0继续选择将取消选择的满减券'
modal.cancelText = '取消'
modal.confirmText = '继续选择'
setModalShow('clear', true, item)
return
}
if (myQuan.fullReductionCouponSel.fullAmount > payPrice) {
modal.content = '选择该商品券后将不满足选择抵扣券的最低满减需求,继续选择将取消选择的满减券'
modal.cancelText = '取消'
@@ -247,11 +255,18 @@
item.checked = !item.checked
const CheckedArr = myQuan.res.productCoupon.filter(v => v.checked)
if (CheckedArr.length <= 0) {
return myQuan.res.productCoupon.map(v => {
v.use = true
})
}
const noCheckedArr = myQuan.res.productCoupon.filter(v => !v.checked)
noCheckedArr.map(v => {
console.log(returnCoupCanUse(canDikouGoodsArr, v, CheckedArr));
v.use = returnCoupCanUse(canDikouGoodsArr, v, CheckedArr)
})
}
function changeFullReductionCouponSel(item) {
@@ -263,10 +278,11 @@
myQuan.fullReductionCouponSel = {
id: ''
}
}else{
} else {
myQuan.fullReductionCouponSel = item
}
myQuan.res.fullReductionCoupon=returnCanUseFullReductionCoupon($fullReductionCoupon, payPrice.value, myQuan.fullReductionCouponSel)
myQuan.res.fullReductionCoupon = returnCanUseFullReductionCoupon($fullReductionCoupon, payPrice.value, myQuan
.fullReductionCouponSel)
}
function formatStr(str) {
@@ -316,7 +332,7 @@
const option = reactive({
orderId: '',
memberId: '',
orderPrice:0
orderPrice: 0
})
function toEmitChooseQuan(item) {
@@ -339,13 +355,14 @@
let coupArr = [...fullReductionCoupon, ...goodsQuan]
return returnCouponAllPrice(coupArr, canDikouGoodsArr, user.value)
})
const payPrice=computed(()=>{
const pay= option.orderPrice-discountAmount.value
return (pay<0?0:pay).toFixed(2)
const payPrice = computed(() => {
const pay = option.orderPrice - discountAmount.value
return (pay < 0 ? 0 : pay).toFixed(2)
})
watch(() => myQuan.types.sel, (newval) => {
if (newval == 0) {
myQuan.res.fullReductionCoupon = returnCanUseFullReductionCoupon($fullReductionCoupon, payPrice.value, myQuan.fullReductionCouponSel)
myQuan.res.fullReductionCoupon = returnCanUseFullReductionCoupon($fullReductionCoupon, payPrice.value,
myQuan.fullReductionCouponSel)
}
if (newval == 1) {

View File

@@ -40,7 +40,8 @@ export function returnCoupCanUse(goodsArr = [], coup, selCoupArr = []) {
if (selCoupNumber >= findGoodsTotalNumber) {
return false
}
return findGoodsTotalNumber < (coup.num + selCoupNumber) ? false : true
console.log(selCoupNumber,findGoodsTotalNumber);
return findGoodsTotalNumber < selCoupNumber ? false : true
}
//查找购物车商品根据购物车商品数据返回商品券信息(抵扣价格以及是否满足可用需求)
export function returnProductCoupon(coup, goodsArr, vipUser, selCoupArr = []) {
@@ -101,6 +102,23 @@ export function returnProductCoupAllPrice(productPriceArr,startIndex,num){
return prve+curPrice
},0)
}
//返回商品券可抵扣的商品数量
export function returnProductCanUseNum(productPriceArr,startIndex,num){
console.log(productPriceArr);
console.log(num);
let n=0;
for(let i=0;i<num;i++){
if(productPriceArr[startIndex*1+i]){
n+=1
console.log(n);
}else{
break
}
}
return n
}
//返回同类商品券在同类商品价格数组里的开始位置
export function returnProCoupStartIndex(coupArr,index){
return coupArr.slice(0,index).reduce((prve,cur)=>{
@@ -201,6 +219,12 @@ export function returnCanUseFullReductionCoupon(coupArr, payPrice, selCoup) {
return v
}
const isfullAmount = payPrice >= v.fullAmount * 1
if(payPrice<=0){
return {
...v,
use: false
}
}
return {
...v,
use: v.use && isfullAmount