订单支付增加优惠券积分

This commit is contained in:
2024-11-18 11:03:39 +08:00
parent d5dbd51c80
commit c9825f8df7
6 changed files with 455 additions and 159 deletions

View File

@@ -119,9 +119,13 @@
</view>
<view class="u-flex">
<view class="color-red u-m-r-24" v-if="youhuiAllPrice>0">
已优惠{{youhuiAllPrice}}
<view class="u-flex u-m-r-24" v-if="youhuiAllPrice>0">
<view class="color-red u-m-r-6 " >
已优惠{{youhuiAllPrice}}
</view>
<up-icon name="info-circle" color="#999" :size="14" @click="youhuiDetailShow"></up-icon>
</view>
<view>
<text>小计</text>
<text class="font-bold u-font-32">{{allPrice}}</text>
@@ -161,12 +165,41 @@
<my-button @tap="printOrder" type="cancel" :color="color.ColorMain">重新打印</my-button>
</view>
</view>
<up-popup :round="10" :show="pop.youhui" mode="center" closeOnClickOverlay @close="youhuiDetailHide" >
<view class="u-p-30" style="width: 80vw;">
<view class="font-bold u-text-center">优惠详情</view>
<view class="u-m-t-32">
<view class="u-flex u-row-between">
<view>满减券抵扣</view>
<view class="color-red">
<text></text>
<text>{{orderInfo.fullCouponDiscountAmount}}</text>
</view>
</view>
<view class="u-flex u-row-between u-m-t-18">
<view>商品券抵扣</view>
<view class="color-red">
<text></text>
<text>{{orderInfo.productCouponDiscountAmount}}</text>
</view>
</view>
<view class="u-flex u-row-between u-m-t-18">
<view>积分抵扣</view>
<view class="color-red">
<text></text>
<text>{{orderInfo.pointsDiscountAmount}}</text>
</view>
</view>
</view>
</view>
</up-popup>
</view>
</template>
<script setup>
import {
computed
computed, reactive
} from 'vue';
import color from '@/commons/color.js'
import {
@@ -178,6 +211,15 @@
canTuicai,
numSum
} from '@/commons/utils/goodsUtil.js'
const pop=reactive({
youhui:false
})
function youhuiDetailShow(){
pop.youhui=true
}
function youhuiDetailHide(){
pop.youhui=false
}
const props = defineProps({
orderInfo: {
type: Object,

View File

@@ -33,7 +33,7 @@
<view class="color-666 u-m-t-8 u-font-24">余额</view>
</view>
<view class="">
<view class="font-bold">{{user.totalScore}}</view>
<view class="font-bold">{{user.accountPoints}}</view>
<view class="color-666 u-m-t-8 u-font-24">积分</view>
</view>
<view class="">
@@ -61,7 +61,7 @@
headImg:'',
telephone:'',
amount:'0.00',
totalScore:'0.00'
accountPoints:'0.00'
}
}
}

View File

@@ -316,7 +316,7 @@
headImg:'',
telephone:'',
amount:'0.00',
totalScore:'0.00'
accountPoints:'0.00'
})
//更新选择用户
async function setUser(par) {

View File

@@ -24,7 +24,7 @@
<view @click="discountShow">修改</view>
</view>
</template>
</view>
<view class="content bg-fff border-r-12">
<view class=" u-p-t-30 u-p-l-26 u-p-r-26 card top u-m-t-30">
@@ -44,6 +44,7 @@
<view class="u-flex">
<view class="hui"></view>
<view class="u-m-l-18">{{item.name}}</view>
<view class="u-m-l-18 color-999">x{{item.num}}</view>
</view>
<view class="color-red">
-{{item.discountAmount}}
@@ -53,7 +54,7 @@
</view>
<view class="border-bottom u-p-b-30" v-if="payPrice!=order.amount||accountPoints.sel">
<view class="border-bottom u-p-b-30" v-if="discount.price||accountPoints.sel">
<view class="u-flex u-p-l-24 u-p-r-24 u-row-between u-p-t-30 "
v-if="discount.price&&discount.currentPrice!=order.amount">
<view>服务员改价</view>
@@ -69,7 +70,7 @@
</view>
</view>
</view>
</view>
<view class="bg-fff border-r-12 ">
<view class="u-p-t-30 u-p-l-50 u-p-r-50 card bottom">
@@ -111,14 +112,14 @@
<view class="u-flex ">
<view class="">积分抵扣</view>
<view class="color-999 u-m-l-10">
<text>(</text>
<text>(</text>
<text>{{user.accountPoints||'0'}}</text>
<text>)</text>
</view>
<!-- <view><text class="color-red font-bold">{{accountPoints.price}}</text></view> -->
</view>
<view class="u-flex">
<view class="u-flex" @click.stop="refPointsOpen">
<view><text>{{accountPoints.num}}</text></view>
<view v-if="accountPoints.calcRes.usable">
@@ -126,22 +127,22 @@
</view>
</view>
<view class="u-m-l-32 u-relative" v-if="accountPoints.calcRes.usable" >
<view class="u-m-l-32 u-relative" v-if="accountPoints.calcRes.usable">
<view class="u-absolute position-all"></view>
<my-radio
:modelValue="accountPoints.sel">
<my-radio :modelValue="accountPoints.sel">
</my-radio>
</view>
</view>
</view>
<view class="color-999 u-font-24 u-m-t-16" >
<view class="color-999 u-font-24 u-m-t-16">
<view class="" v-if="accountPoints.calcRes.unusableReason">
<text class="color-red">*</text>
<text >{{accountPoints.calcRes.unusableReason}}</text>
<text>{{accountPoints.calcRes.unusableReason}}</text>
</view>
<view class="" v-if="accountPoints.calcRes.usable">
<text class="color-red">*</text>
<text class="" v-if="accountPoints.calcRes.equivalentPoints">100积分等于{{to2(accountPoints.calcRes.equivalentPoints*100)}},</text>
<text class=""
v-if="accountPoints.calcRes.equivalentPoints">100积分等于{{to2(accountPoints.calcRes.equivalentPoints*100)}},</text>
<text>
最大抵扣积分{{accountPoints.calcRes.maxUsablePoints}}
</text>
@@ -225,10 +226,16 @@
import infoBox from '@/commons/utils/infoBox.js'
import editDiscount from '@/components/my-components/edit-discount.vue'
import editAccountPoints from './components/edit-accountPoints.vue'
import {
returnGoodsPayPriceMap,returnProCoupStartIndex ,returnProductCoupAllPrice
} from '../quan_util.js'
//商品数量从0到n每一个对应的价格
let $goodsPayPriceMap={}
const refPoints=ref(null)
function refPointsOpen(){
if(!accountPoints.calcRes.usable&&accountPoints.sel){
const refPoints = ref(null)
function refPointsOpen() {
if (!accountPoints.calcRes.usable && accountPoints.sel) {
return
}
refPoints.value.open()
@@ -236,50 +243,50 @@
const accountPoints = reactive({
sel: false,
num: 0,
calcRes:{
usable:false,
unusableReason:'',
minDeductionPoints:0,
maxUsablePoints:0
calcRes: {
usable: false,
unusableReason: '',
minDeductionPoints: 0,
maxUsablePoints: 0
},
price:0
price: 0
})
async function calcUsablePoints(){
if(!order.memberId){
return
async function calcUsablePoints() {
if (!order.memberId) {
return
}
const res=await Api.$calcUsablePoints({
memberId:order.memberId,
orderAmount:pointCanDicountPrice.value
const res = await Api.$calcUsablePoints({
memberId: order.memberId,
orderAmount: pointCanDicountPrice.value
})
accountPoints.calcRes=res
accountPoints.num=res.maxUsablePoints
accountPoints.calcRes = res
accountPoints.num = res.maxUsablePoints
return res;
}
watch(()=>accountPoints.sel,(newval)=>{
if(newval){
watch(() => accountPoints.sel, (newval) => {
if (newval) {
calcUsablePoints()
}
})
async function calcDeDuctionPoints(){
const res=await Api.$calcDeDuctionPoints({
async function calcDeDuctionPoints() {
const res = await Api.$calcDeDuctionPoints({
memberId: order.memberId,
orderAmount: order.amount,
points: accountPoints.num
})
accountPoints.price=res
accountPoints.price = res
return res
}
watch(()=>accountPoints.num,(newval)=>{
if(!newval){
accountPoints.price=0
return
watch(() => accountPoints.num, (newval) => {
if (!newval) {
accountPoints.price = 0
return
}
calcDeDuctionPoints()
})
function changeAccountPoints() {
if(!accountPoints.calcRes.usable){
if (!accountPoints.calcRes.usable) {
return
}
accountPoints.sel = !accountPoints.sel
@@ -298,7 +305,7 @@
go.to('PAGES_ORDER_QUAN', {
orderId: order.id,
memberId: order.memberId,
orderPrice:payPrice.value
orderPrice: payPrice.value*1+coupAllPrice.value*1
})
}
async function discountShow() {
@@ -321,7 +328,6 @@
// }
// return false
})
let payStatus = ref(null) //loading success
let timer = null
@@ -386,7 +392,32 @@
uni.$off('choose-quan')
uni.$on('choose-quan', (arr) => {
console.log(arr);
pays.quan = arr
const manjianCoup = arr.filter(v => v.type == 1)
let productCoup = arr.filter(v => v.type == 2)
console.log(productCoup);
//商品券分组
let coupMap={}
for(let i in productCoup){
const coup=productCoup[i]
if(coupMap.hasOwnProperty(coup.proId)){
coupMap[coup.proId].push(coup)
}else{
coupMap[coup.proId]=[coup]
}
}
for(let key in coupMap){
const arr=coupMap[key]
for(let i in arr){
const coup=arr[i]
const proCoupStartIndex=returnProCoupStartIndex(arr,i)
console.log(proCoupStartIndex);
console.log($goodsPayPriceMap[coup.proId]);
coup.discountAmount=returnProductCoupAllPrice($goodsPayPriceMap[coup.proId],proCoupStartIndex,coup.num)
console.log(coup.discountAmount);
}
}
console.log(productCoup);
pays.quan = [...manjianCoup, ...productCoup]
})
}
@@ -428,13 +459,13 @@
const discount = reactive({
})
function editDiscountConfirm(form) {
console.log(form);
Object.assign(discount, form)
getPayUrl()
}
async function getPayType() {
const payTypeList = await Api.$getPayType()
pays.payTypes.list = payTypeList
@@ -446,9 +477,11 @@
chooseUser()
}
}
let payStatus = '';
//支付成功回调
function paySuccess() {
infoBox.showToast('支付成功')
payStatus = 'success'
setTimeout(() => {
// uni.$emit('orderDetail:update')
payFinish.value = true
@@ -468,35 +501,44 @@
}
payOrder()
}
const tipsMap = {
paying: '支付中',
success: '已支付成功'
}
async function payOrder() {
const payType = pays.payTypes.list[pays.payTypes.selIndex].payType
if (payType == 'vipPay' && user.value.amount * 1 < order.amount * 1) {
infoBox.showToast('余额不足')
return
}
await Api.$payOrder({
tableId: order.tableId,
masterId: order.masterId,
orderId: order.id || order.orderId,
payType,
vipUserId: order.memberId,
discount: 1,
code: '',
pointsNum:accountPoints.sel?accountPoints.num:0,
userCouponInfos:pays.quan.map(v=>{
return {
userCouponId:v.id,
num:v.num
}
if (payStatus) {
return infoBox.showToast(tipsMap[payStatus])
}
try {
payStatus = 'paying'
await Api.$payOrder({
tableId: order.tableId,
masterId: order.masterId,
orderId: order.id || order.orderId,
payType,
vipUserId: order.memberId,
discount: 1,
code: '',
pointsNum: accountPoints.sel ? accountPoints.num : 0,
userCouponInfos: pays.quan.map(v => {
return {
userCouponId: v.id,
num: v.num
}
})
})
})
paySuccess()
paySuccess()
} catch (e) {
//TODO handle the exception
payStatus = ''
}
}
onMounted(() => {
getPayType()
})
const order = reactive({
amount: 0
})
@@ -540,6 +582,8 @@
async function init() {
const orderRes = await orderApi.tbOrderInfoDetail(order.orderId)
Object.assign(order, orderRes)
$goodsPayPriceMap=returnGoodsPayPriceMap(order.detailList)
console.log($goodsPayPriceMap);
if (orderRes.memberId) {
calcUsablePoints()
queryAllShopUser({
@@ -550,7 +594,6 @@
}
})
}
getPayUrl()
}
function getPayUrl() {
@@ -561,37 +604,36 @@
payCodeUrl.value = res
})
}
const coupAllPrice=computed(()=>{
return pays.quan.reduce((prve,cur)=>{
return prve+cur.discountAmount*1
},0)
const coupAllPrice = computed(() => {
return pays.quan.reduce((prve, cur) => {
return prve + cur.discountAmount * 1
}, 0)
})
const payPrice=computed(()=>{
const discountPrice=discount.currentPrice?discount.currentPrice:order.amount
const calcPrice=discountPrice-coupAllPrice.value-accountPoints.price*(accountPoints.sel?1:0)
return (calcPrice<=0?0:calcPrice).toFixed(2)
const payPrice = computed(() => {
const discountPrice = discount.currentPrice ? discount.currentPrice : order.amount
const calcPrice = discountPrice - coupAllPrice.value - accountPoints.price * (accountPoints.sel ? 1 : 0)
return (calcPrice <= 0 ? 0 : calcPrice).toFixed(2)
})
watch(()=>payPrice.value,()=>{
watch(() => payPrice.value, () => {
getPayUrl()
})
const pointCanDicountPrice=computed(()=>{
const discountPrice=discount.currentPrice?discount.currentPrice:order.amount
const calcPrice=discountPrice-coupAllPrice.value
return (calcPrice<=0?0:calcPrice).toFixed(2)
const pointCanDicountPrice = computed(() => {
const discountPrice = discount.currentPrice ? discount.currentPrice : order.amount
const calcPrice = discountPrice - coupAllPrice.value
return (calcPrice <= 0 ? 0 : calcPrice).toFixed(2)
})
watch(()=>pointCanDicountPrice.value,(newval)=>{
watch(() => pointCanDicountPrice.value, (newval) => {
calcUsablePoints()
})
onLoad(async (opt) => {
console.log(opt);
option = opt
Object.assign(order, opt)
const payTypeList = await Api.$getPayType()
pays.payTypes.list = payTypeList
getPayType()
init()
})
onBeforeUnmount(() => {
console.log('onBeforeUnmount');
clear()

View File

@@ -6,9 +6,11 @@
<view class="u-m-t-32">
<template v-if="myQuan.types.sel==0">
<view class="" @click="changeFullReductionCouponSel(item)"
v-for="(item,index) in myQuan.res.fullReductionCoupon" :class="{filtergray:item.status}"
:key="index">
v-for="(item,index) in myQuan.res.fullReductionCoupon" :class="{filtergray:!item.use}" :key="index">
<view class="quan u-row-between u-flex u-col-center u-m-b-32 border-r-10 ">
<view class="no-use" v-if="!item.use">
<image class="img" src="/pagesOrder/static/image/no-use.svg" mode=""></image>
</view>
<view class="sel u-abso" v-if="item.id==myQuan.fullReductionCouponSel.id ">
<up-icon name="checkbox-mark" color="#fff"></up-icon>
</view>
@@ -70,9 +72,9 @@
</view>
</view>
<view class="right u-flex u-flex-col u-col-bottom u-row-center">
<view class="u-flex u-row-center w-full">
<!-- <view class="u-flex u-row-center w-full">
<view class="color-red font-bold u-m-b-24 ">{{item.discountAmount}}</view>
</view>
</view> -->
<view class="u-flex ">
<view class="use-btn" @click.stop="toEmitChooseQuan(item)">去使用</view>
</view>
@@ -90,10 +92,24 @@
<view :style="{height:safebottomHeight+'px'}"></view>
<view class="fixed-b bottom safe-bottom border-top">
<view class="u-m-b-32">
<text>抵扣金额</text>
<text class="color-red"></text>
<text class="color-red">{{discountAmount}}</text>
<view class="u-m-b-32 u-flex u-row-between u-p-t-10">
<view class="u-flex">
<text>抵扣金额</text>
<text class="color-red"></text>
<text class="color-red">{{discountAmount}}</text>
</view>
<view class="u-flex u-relative">
<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-flex line-th color-999">
<text class=""></text>
<text class="">{{option.orderPrice}}</text>
</view>
</view>
</view>
</view>
<view class="u-flex gap-20">
<up-button shape="circle" plain @click="back">
@@ -105,7 +121,12 @@
</view>
</view>
<up-modal :title="modal.title" :content="modal.content" :show="modal.clear" :confirmText="modal.confirmText"
:cancelText="modal.cancelText" showCancelButton closeOnClickOverlay @confirm="confirmModelConfirm"
@cancel="confirmModelCancel" @close="setModalShow('clear',false)" width="300px" />
</view>
</template>
<script setup>
@@ -129,6 +150,7 @@
import {
$activateByOrderId
} from '@/http/yskApi/Instead.js'
import infoBox from '@/commons/utils/infoBox.js'
import {
queryAllShopUser
} from '@/http/yskApi/shop-user.js'
@@ -136,9 +158,48 @@
returnNewGoodsList,
returnCoupCanUse,
returnCouponAllPrice,
returnProductCoupon
returnProductCoupon,
returnCanUseFullReductionCoupon
} from '../quan_util.js'
const modal = reactive({
title: '提示',
cancelText: '取消',
confirmText: '确定',
content: '',
key: '',
clear: false,
data: ''
})
function confirmModelCancel() {
setModalShow('clear', false, '')
}
async function confirmModelConfirm() {
if (modal.key == 'clear') {
myQuan.fullReductionCouponSel = {
id: ''
}
const item = modal.data
item.checked = !item.checked
const CheckedArr = myQuan.res.productCoupon.filter(v => v.checked)
const noCheckedArr = myQuan.res.productCoupon.filter(v => !v.checked)
noCheckedArr.map(v => {
console.log(returnCoupCanUse(canDikouGoodsArr, v, CheckedArr));
v.use = returnCoupCanUse(canDikouGoodsArr, v, CheckedArr)
})
setModalShow('clear', false, '')
}
}
function setModalShow(key = 'show', show = true, data) {
modal.key = key
modal[key] = show
modal.data = data
}
function back() {
uni.navigateBack()
}
@@ -167,6 +228,22 @@
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 (myQuan.fullReductionCouponSel.fullAmount > payPrice) {
modal.content = '选择该商品券后将不满足选择抵扣券的最低满减需求,继续选择将取消选择的满减券'
modal.cancelText = '取消'
modal.confirmText = '继续选择'
setModalShow('clear', true, item)
return
}
}
item.checked = !item.checked
const CheckedArr = myQuan.res.productCoupon.filter(v => v.checked)
const noCheckedArr = myQuan.res.productCoupon.filter(v => !v.checked)
@@ -177,7 +254,18 @@
}
function changeFullReductionCouponSel(item) {
myQuan.fullReductionCouponSel = item
if (!item.use) {
return
}
console.log(item);
if (item.id == myQuan.fullReductionCouponSel.id) {
myQuan.fullReductionCouponSel = {
id: ''
}
}else{
myQuan.fullReductionCouponSel = item
}
myQuan.res.fullReductionCoupon=returnCanUseFullReductionCoupon($fullReductionCoupon, payPrice.value, myQuan.fullReductionCouponSel)
}
function formatStr(str) {
@@ -190,6 +278,9 @@
let user = reactive({
isVip: false
})
let $fullReductionCoupon = []
let $productCoupon = []
async function getQuan() {
order.value = await orderApi.tbOrderInfoDetail(option.orderId)
if (order.value.memberId) {
@@ -202,9 +293,13 @@
}
console.log(order.value);
const res = await $activateByOrderId(option)
const res = await $activateByOrderId({
orderId: option.orderId,
memberId: option.memberId
})
canDikouGoodsArr = returnNewGoodsList(order.value.detailList || [])
res.fullReductionCoupon = res.fullReductionCoupon.filter((v) => v.use&&option.orderPrice*1>=v.fullAmount*1)
res.fullReductionCoupon = res.fullReductionCoupon.filter((v) => v.use && option.orderPrice * 1 >= v
.fullAmount * 1)
res.productCoupon = res.productCoupon.map(v => {
const calcCoup = returnProductCoupon(v, canDikouGoodsArr, user.value)
return {
@@ -212,12 +307,15 @@
checked: false
}
}).filter((v) => v.use);
$fullReductionCoupon = res.fullReductionCoupon
$productCoupon = res.productCoupon
myQuan.res = res
myQuan.hasAjax = true;
}
const option = reactive({
orderId: '',
memberId: ''
memberId: '',
orderPrice:0
})
function toEmitChooseQuan(item) {
@@ -240,6 +338,18 @@
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)
})
watch(() => myQuan.types.sel, (newval) => {
if (newval == 0) {
myQuan.res.fullReductionCoupon = returnCanUseFullReductionCoupon($fullReductionCoupon, payPrice.value, myQuan.fullReductionCouponSel)
}
if (newval == 1) {
}
})
onLoad((opt) => {
Object.assign(option, opt)
getQuan()
@@ -247,6 +357,7 @@
let safebottomHeight = ref(0)
onReady(() => {
getSafeBottomHeight('bottom', 0).then(height => {
console.log(height);
safebottomHeight.value = height
})
})

View File

@@ -1,58 +1,66 @@
export function isTui(item){
return item.status=='return'||item.status=='refund'||item.status=='refunding'
export function isTui(item) {
return item.status == 'return' || item.status == 'refund' || item.status == 'refunding'
}
//计算商品券优惠价格
export function returnProductCouponPrice(coup, goodsArr, vipUser) {
const item = goodsArr.find(v => v.productId == coup.proId);
if(!item){
return 0
}
const memberPrice = item.memberPrice ? item.memberPrice : item.price;
const price = item ? (vipUser.isVip ? memberPrice : item.price) : 0;
return price*coup.num
const item = goodsArr.find(v => v.productId == coup.proId);
if (!item) {
return 0
}
const memberPrice = item.memberPrice ? item.memberPrice : item.price;
const price = item ? (vipUser.isVip ? memberPrice : item.price) : 0;
return price * coup.num
}
//返回新的商品列表,过滤掉退菜的,退单的商品
export function returnNewGoodsList(arr) {
let goodsMap={}
return arr.filter(v => !isTui(v))
let goodsMap = {}
return arr.filter(v => !isTui(v))
}
//根据当前购物车商品以及数量,已选券对应商品数量,判断该商品券是否可用
export function returnCoupCanUse(goodsArr=[],coup,selCoupArr=[]) {
export function returnCoupCanUse(goodsArr = [], coup, selCoupArr = []) {
// if(!coup.use){
// return false
// }
const findGoods=goodsArr.filter(v=>v.productId==coup.proId)
if(!findGoods.length){
const findGoods = goodsArr.filter(v => v.productId == coup.proId)
if (!findGoods.length) {
return false
}
const findGoodsTotalNumber=findGoods.reduce((prve,cur)=>{
return prve+cur.num*1
},0)
const selCoupNumber=selCoupArr.filter(v=>v.proId==coup.proId).reduce((prve,cur)=>{
return prve+cur.num*1
},0)
if(selCoupNumber>=findGoodsTotalNumber){
const findGoodsTotalNumber = findGoods.reduce((prve, cur) => {
return prve + cur.num * 1
}, 0)
const selCoupNumber = selCoupArr.filter(v => v.proId == coup.proId).reduce((prve, cur) => {
return prve + cur.num * 1
}, 0)
if (selCoupNumber >= findGoodsTotalNumber) {
return false
}
return findGoodsTotalNumber<(coup.num+selCoupNumber)?false:true
return findGoodsTotalNumber < (coup.num + selCoupNumber) ? false : true
}
//查找购物车商品根据购物车商品数据返回商品券信息(抵扣价格以及是否满足可用需求)
export function returnProductCoupon(coup, goodsArr, vipUser,selCoupArr=[]) {
const newGoodsArr = returnNewGoodsList(goodsArr)
const item = newGoodsArr.find(v => v.productId == coup.proId);
if(!item){
return {...coup, discountAmount: 0,use:false}
}
const memberPrice = item.memberPrice ? item.memberPrice : item.price;
const price = item ? (vipUser.isVip ? memberPrice : item.price) : 0;
const discountAmount=(price*coup.num).toFixed(2)
export function returnProductCoupon(coup, goodsArr, vipUser, selCoupArr = []) {
const newGoodsArr = returnNewGoodsList(goodsArr)
const item = newGoodsArr.find(v => v.productId == coup.proId);
if (!item) {
return {
...coup,
discountAmount: 0,
use: false
}
}
const memberPrice = item.memberPrice ? item.memberPrice : item.price;
const price = item ? (vipUser.isVip ? memberPrice : item.price) : 0;
const discountAmount = (price * coup.num).toFixed(2)
console.log(discountAmount);
const canUse=!coup.use?false:(discountAmount>0&&returnCoupCanUse(goodsArr,coup,selCoupArr))
// const canUse=discountAmount>0
return { ...coup, discountAmount: discountAmount,use:canUse}
const canUse = !coup.use ? false : (discountAmount > 0 && returnCoupCanUse(goodsArr, coup, selCoupArr))
// const canUse=discountAmount>0
return {
...coup,
discountAmount: discountAmount,
use: canUse
}
}
/**
@@ -64,38 +72,131 @@ export function returnProductCoupon(coup, goodsArr, vipUser,selCoupArr=[]) {
* @param {*} vipUser
* @returns
*/
export function returnProductAllCoup(coupArr, goodsArr, vipUser){
return coupArr.map((v) => {
return returnProductCoupon(v, goodsArr, vipUser)
})
export function returnProductAllCoup(coupArr, goodsArr, vipUser) {
return coupArr.map((v) => {
return returnProductCoupon(v, goodsArr, vipUser)
})
}
//返回商品实际支付价格
export function returnProductPayPrice(goods,vipUser){
const memberPrice = goods.memberPrice ? goods.memberPrice : goods.price;
const price = vipUser.isVip ? memberPrice : item.price;
return price
}
//返回商品券抵扣的商品价格
export function returnProductCoupAllPrice(productPriceArr,startIndex,num){
return productPriceArr.slice(startIndex,startIndex+num).reduce((prve,cur)=>{
let curPrice=0
if(typeof cur==='object'){
curPrice=cur.memberPrice*1
}else{
curPrice=cur*1
}
return prve+curPrice
},0)
}
//返回同类商品券在同类商品价格数组里的开始位置
export function returnProCoupStartIndex(coupArr,index){
return coupArr.slice(0,index).reduce((prve,cur)=>{
return prve+cur.num*1
},0)
}
//返回商品数量从0到n每一个对应的价格对照表
export function returnGoodsPayPriceMap(goodsArr){
return goodsArr.reduce((prve,cur)=>{
if(!prve.hasOwnProperty(cur.productId)){
prve[cur.productId]=[]
}
const arr=new Array(cur.num).fill(cur).map(v=>{
return {
memberPrice:v.memberPrice?v.memberPrice:v.price,
price:v.price
}
})
prve[cur.productId].push(...arr)
return prve
},{})
}
//计算商品券总优惠价格
export function returnProductCouponAllPrice(coupArr, goodsArr, vipUser) {
if(coupArr.length == 0){
return 0;
}
return coupArr.reduce((a, b) => {
const price = returnProductCouponPrice(b, goodsArr, vipUser)
return a + price;
}, 0).toFixed(2);
if (coupArr.length == 0) {
return 0;
}
//商品分组
const goodsMap={}
//商品数量从0到n每一个对应的价格
const goodsPayPriceMap={}
//商品券分组
let coupMap={}
for(let i in coupArr){
const coup=coupArr[i]
if(coupMap.hasOwnProperty(coup.proId)){
coupMap[coup.proId].push(coup)
}else{
coupMap[coup.proId]=[coup]
}
}
let total=0
for(let key in coupMap){
const arr=coupMap[key]
for(let i in arr){
const coup=arr[i]
if(!goodsMap.hasOwnProperty(coup.proId)){
goodsMap[coup.proId]=goodsArr.filter(v=>v.productId==coup.proId).map(v=>{
return {
...v,
payPrice:returnProductPayPrice(v,vipUser)
}
}).sort((a,b)=>{
const aPrice=a.payPrice
const bPrice=b.payPrice
return aPrice-bPrice
})
goodsPayPriceMap[coup.proId]=goodsMap[coup.proId].reduce((prve,cur)=>{
const arr=new Array(cur.num).fill(cur.payPrice)
console.log(arr);
prve.push(...arr)
return prve
},[])
}
const proCoupStartIndex=returnProCoupStartIndex(arr,i)
console.log(proCoupStartIndex);
total+=returnProductCoupAllPrice(goodsPayPriceMap[coup.proId],proCoupStartIndex,coup.num)
}
}
return total.toFixed(2);
}
//计算满减券总优惠价格
export function returnFullReductionCouponAllPrice(coupArr) {
if(coupArr.length == 0){
return 0;
}
return coupArr.filter(v => v.type == 1).reduce((a, b) => {
const price = b.discountAmount
return a + price;
}, 0).toFixed(2);
if (coupArr.length == 0) {
return 0;
}
return coupArr.filter(v => v.type == 1).reduce((a, b) => {
const price = b.discountAmount
return a + price;
}, 0).toFixed(2);
}
//计算优惠券总价格
export function returnCouponAllPrice(coupArr, goodsArr, vipUser) {
const poductAllprice=returnProductCouponAllPrice(coupArr, goodsArr, vipUser)
const pointAllPrice=returnFullReductionCouponAllPrice(coupArr)
return (poductAllprice*1+pointAllPrice*1).toFixed(2);
const poductAllprice = returnProductCouponAllPrice(coupArr, goodsArr, vipUser)
const pointAllPrice = returnFullReductionCouponAllPrice(coupArr)
return (poductAllprice * 1 + pointAllPrice * 1).toFixed(2);
}
//返回当前满减券列表可用状态
export function returnCanUseFullReductionCoupon(coupArr, payPrice, selCoup) {
return coupArr.map(v => {
if (v.id == selCoup.id) {
return v
}
const isfullAmount = payPrice >= v.fullAmount * 1
return {
...v,
use: v.use && isfullAmount
}
})
}