|
|
|
// 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,
|
...
|
...
|
|