Commit 1b6629e30f2f49acd2bf96e61e4b2359085a5dcf

Authored by pengye
1 parent 607d6909

优惠券扫码购买页4

// pack-activity/pack/buyFreeCoupon.js
import util from '../../utils/util';
import App from '../../utils/api';
// import {monidata} from './aa';
Page({
/**
* 页面的初始数据
... ... @@ -9,6 +10,7 @@ Page({
options: {},
available: '1', //输入金额够优惠券最低消费1 不够0
couponArr: [], //所有券列表
finalList: [], //勾选的券列表
dataStr: Date.now() * 1 / 1000,
discountPrice: '', //优惠价格
actualPrice: '', //实付价格
... ... @@ -59,6 +61,8 @@ Page({
}).then(res => {
if (res.ret == 200 && res.data && res.data.length > 0) {
let list = res.data
// console.log('listlistlist',JSON.stringify(list));
// list= monidata
list.forEach(detail => {
detail.stringifyUse = detail.use ? JSON.stringify(detail.use) : '';
detail.exp_date = detail.exp_date ? util.determine(detail.exp_date) : '';
... ... @@ -97,7 +101,7 @@ Page({
let arr = []
let currentCoupon = '0'; //没选券 为0 可叠加券1 不可叠加券2
for (let i = 0, lenI = list.length; i < lenI; ++i) {
list[i].checked = false
list[i].checked = false;
for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
if (list[i].activity_id === values[j]) {
arr.push({
... ... @@ -113,7 +117,7 @@ Page({
}
}
}
console.log('没选券 为0 可叠加券1 不可叠加券2', currentCoupon);
console.log('没选券为0可叠加券1不可叠加券2', currentCoupon);
//没选券 所有可选的打开
if (currentCoupon == '0') {
list.forEach(item => {
... ... @@ -175,7 +179,7 @@ Page({
price = price.toFixed(2)
this.setData({
discountPrice: price,
actualPrice: actualPrice > 0 ? actualPrice : 0.1,
actualPrice: actualPrice > 0 ? actualPrice : 0.01,
})
console.log('点确认选择后', list);
},
... ... @@ -195,6 +199,7 @@ Page({
},
// 付款
confirmPayment() {
let that = this;
let actualPrice = this.data.actualPrice
if (actualPrice <= 0) {
return
... ... @@ -220,9 +225,9 @@ Page({
paySign: res.data.paySign,
success(ress) {
if (ress.errMsg == 'requestPayment:ok') {
this.writeOff()
that.writeOff()
console.log('支付成功', ress);
wx.showToast({
return wx.showToast({
title: '支付成功',
duration: 1000,
icon: 'none',
... ... @@ -257,6 +262,14 @@ Page({
}).then(res => {
if (res.ret == '200') {
console.log('核销成功', str);
this.setData({
inputValue:'',
finalList:[],
couponArr:[],
discountPrice:'0.00',
actualPrice:'',
})
this.getCoupon()
}
})
}
... ... @@ -285,6 +298,7 @@ Page({
const isUseDate = (currentDate > startUseDate && currentDate < endUseDate) &&
(this.data.dataStr < (coupon.etime * 1) && coupon.is_open == '1')
let obj = this.data.canSolt
console.log('当前餐段、是不是法定节假日',{...obj});
let selectedValue = true;
let str = true
if (coupon.exp_date && coupon.exp_date.week_data && coupon.exp_date.week_data.length > 0) {
... ... @@ -339,6 +353,8 @@ Page({
let inputValue = this.data.inputValue;
const amount = parseFloat(inputValue);
const validCoupons = arr.filter(coupon => (coupon.isOk == '1' && parseFloat(coupon.desc) <= amount));
// 排序后,优先选择最大优惠的券
validCoupons.sort((a, b) => parseInt(b.price) - parseInt(a.price));
arr.forEach(item => {
const isValid = validCoupons.some(validCoupon => validCoupon.activity_id == item.activity_id);
item.canCheck = isValid ? '1' : '0'; // 如果在 validCoupons 中,设置 canCheck 为 1,否则为 0
... ... @@ -348,34 +364,37 @@ Page({
let list2 = []; // 最优惠的不可叠加的优惠券
let finalList = []; //最终用的优惠券组合
// 计算可叠加的组合
let remainingValue = inputValue; // 剩余金额
let remainingValue = amount; // 剩余金额
let totalDiscount = 0; // 总优惠金额
// 尝试组合可叠加的优惠券
validCoupons.forEach(coupon => {
if (coupon.accumulationShow === '1') {
// 可叠加券
if (coupon.accumulationShow == '1') {
const discountAmount = parseInt(coupon.price);
if (remainingValue > 0) {
// 只有在添加优惠券后剩余金额仍大于或等于0时才添加
if (remainingValue - discountAmount >= 0) {
list1.push(coupon);
totalDiscount += discountAmount; // 累加优惠金额
remainingValue -= discountAmount; // 减少应付金额
}
}
});
console.log('可叠加的优惠券组合1:', list1);
// 检查可叠加组合是否有效
if (remainingValue < 0) {
list1 = []; // 如果剩余金额小于0,清空可叠加列表
}
// 重置剩余金额,用于不可叠加的优惠券
remainingValue = inputValue;
remainingValue = amount;
// 遍历有效优惠券,寻找不可叠加的优惠券
validCoupons.forEach(coupon => {
if (coupon.accumulationShow === '0') {
const discountAmount = parseInt(coupon.price);
// 计算实际支付金额
const finalAmount = inputValue - discountAmount;
const finalAmount = amount - discountAmount;
// 确保实际支付金额不小于0,并且是最优惠的
if (finalAmount >= 0) {
if (!list2.length || discountAmount > parseInt(bestCoupon.price)) {
if (!list2.length || discountAmount > parseInt(coupon.price)) {
list2.push({
...coupon
}); // 更新最优惠的不可叠加券
... ... @@ -383,11 +402,11 @@ Page({
}
}
});
console.log('可叠加的优惠券组合', list1);
console.log('可叠加的优惠券组合2', list1);
console.log('不可叠加的优惠券', list2);
// 计算两个列表的最大优惠值
let maxStackableValue = list1.reduce((total, coupon) => total + coupon.price, 0);
let maxNonStackableValue = list2.reduce((total, coupon) => total + coupon.price, 0);
let maxStackableValue = list1.reduce((total, coupon) => total + parseInt(coupon.price), 0);
let maxNonStackableValue = list2.reduce((total, coupon) => total + parseInt(coupon.price), 0);
// 比较两个最大值并输出结果
if (maxStackableValue > maxNonStackableValue) {
... ... @@ -431,7 +450,7 @@ Page({
console.log('实际支付金额:', actualPrice);
this.setData({
discountPrice,
actualPrice: actualPrice > 0 ? actualPrice : '0.1', // 确保实际支付金额不小于0.01
actualPrice: actualPrice > 0 ? actualPrice : '0.01', // 确保实际支付金额不小于0.01
});
return {
arr,
... ...
... ... @@ -8,12 +8,12 @@
<view class="font32r">付款金额</view>
<view class="payView">
<text>¥</text>
<input type="digit" bindchange="bindKeyInput" />
<input type="digit" value="{{inputValue}}" bindchange="bindKeyInput" />
</view>
<view bindtap="showCouponModal" class="couponsBox {{available == '1' ? 'colorOrange':'colorNine'}}">
<view class="couponLeft">优惠金额</view>
<view class="couponRight">
<text wx:if="{{discountPrice}}">{{discountPrice}}</text>
<text wx:if="{{discountPrice && discountPrice!='0.00'}}">{{discountPrice}}</text>
<view class="more" style="margin-top: 6rpx;"></view>
</view>
</view>
... ...
... ... @@ -32,22 +32,22 @@
<text class="font28r">使用规则</text>
</view>
<view class="info font24r">
<view class="drop"></view>{{detail.is_same_day == '1' ? '领取当日可用' : '领取当日不可用'}}
<view class="drop"></view>{{detail.arule.is_same_day == '1' ? '领取当日可用' : '领取当日不可用'}}
</view>
<view class="info font24r">
<view class="drop"></view>{{detail.is_accumulation == '1' ? '可以叠加使用其他优惠券' : '不可以叠加使用其他优惠券'}}
<view class="drop"></view>{{detail.arule.is_accumulation == '1' ? '可以叠加使用其他优惠券' : '不可以叠加使用其他优惠券'}}
</view>
<view class="info font24r">
<view class="drop"></view>{{detail.is_compartment == '1' ? '可用于包间消费' : '不可用于包间消费'}}
<view class="drop"></view>{{detail.arule.is_compartment == '1' ? '可用于包间消费' : '不可用于包间消费'}}
</view>
<view class="info font24r">
<view class="drop"></view>{{detail.is_eatin == '1' ? '堂食可用' : '不可用于堂食消费'}}
<view class="drop"></view>{{detail.arule.is_eatin == '1' ? '堂食可用' : '不可用于堂食消费'}}
</view>
<view class="info font24r">
<view class="drop"></view>{{detail.is_takeout == '1' ? '外带可用' : '不可用于外带'}}
<view class="drop"></view>{{detail.arule.is_takeout == '1' ? '外带可用' : '不可用于外带'}}
</view>
<view class="info font24r">
<view class="drop"></view>{{detail.is_appointment == '1' ? '需要提前预约' : '不需要提前预约'}}
<view class="drop"></view>{{detail.arule.is_appointment == '1' ? '需要提前预约' : '不需要提前预约'}}
</view>
</view>
<view class="centerMain">
... ...