Commit 1b6629e30f2f49acd2bf96e61e4b2359085a5dcf

Authored by pengye
1 parent 607d6909

优惠券扫码购买页4

1 // pack-activity/pack/buyFreeCoupon.js 1 // pack-activity/pack/buyFreeCoupon.js
2 import util from '../../utils/util'; 2 import util from '../../utils/util';
3 import App from '../../utils/api'; 3 import App from '../../utils/api';
  4 +// import {monidata} from './aa';
4 Page({ 5 Page({
5 /** 6 /**
6 * 页面的初始数据 7 * 页面的初始数据
@@ -9,6 +10,7 @@ Page({ @@ -9,6 +10,7 @@ Page({
9 options: {}, 10 options: {},
10 available: '1', //输入金额够优惠券最低消费1 不够0 11 available: '1', //输入金额够优惠券最低消费1 不够0
11 couponArr: [], //所有券列表 12 couponArr: [], //所有券列表
  13 + finalList: [], //勾选的券列表
12 dataStr: Date.now() * 1 / 1000, 14 dataStr: Date.now() * 1 / 1000,
13 discountPrice: '', //优惠价格 15 discountPrice: '', //优惠价格
14 actualPrice: '', //实付价格 16 actualPrice: '', //实付价格
@@ -59,6 +61,8 @@ Page({ @@ -59,6 +61,8 @@ Page({
59 }).then(res => { 61 }).then(res => {
60 if (res.ret == 200 && res.data && res.data.length > 0) { 62 if (res.ret == 200 && res.data && res.data.length > 0) {
61 let list = res.data 63 let list = res.data
  64 + // console.log('listlistlist',JSON.stringify(list));
  65 + // list= monidata
62 list.forEach(detail => { 66 list.forEach(detail => {
63 detail.stringifyUse = detail.use ? JSON.stringify(detail.use) : ''; 67 detail.stringifyUse = detail.use ? JSON.stringify(detail.use) : '';
64 detail.exp_date = detail.exp_date ? util.determine(detail.exp_date) : ''; 68 detail.exp_date = detail.exp_date ? util.determine(detail.exp_date) : '';
@@ -97,7 +101,7 @@ Page({ @@ -97,7 +101,7 @@ Page({
97 let arr = [] 101 let arr = []
98 let currentCoupon = '0'; //没选券 为0 可叠加券1 不可叠加券2 102 let currentCoupon = '0'; //没选券 为0 可叠加券1 不可叠加券2
99 for (let i = 0, lenI = list.length; i < lenI; ++i) { 103 for (let i = 0, lenI = list.length; i < lenI; ++i) {
100 - list[i].checked = false 104 + list[i].checked = false;
101 for (let j = 0, lenJ = values.length; j < lenJ; ++j) { 105 for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
102 if (list[i].activity_id === values[j]) { 106 if (list[i].activity_id === values[j]) {
103 arr.push({ 107 arr.push({
@@ -113,7 +117,7 @@ Page({ @@ -113,7 +117,7 @@ Page({
113 } 117 }
114 } 118 }
115 } 119 }
116 - console.log('没选券 为0 可叠加券1 不可叠加券2', currentCoupon); 120 + console.log('没选券为0可叠加券1不可叠加券2', currentCoupon);
117 //没选券 所有可选的打开 121 //没选券 所有可选的打开
118 if (currentCoupon == '0') { 122 if (currentCoupon == '0') {
119 list.forEach(item => { 123 list.forEach(item => {
@@ -175,7 +179,7 @@ Page({ @@ -175,7 +179,7 @@ Page({
175 price = price.toFixed(2) 179 price = price.toFixed(2)
176 this.setData({ 180 this.setData({
177 discountPrice: price, 181 discountPrice: price,
178 - actualPrice: actualPrice > 0 ? actualPrice : 0.1, 182 + actualPrice: actualPrice > 0 ? actualPrice : 0.01,
179 }) 183 })
180 console.log('点确认选择后', list); 184 console.log('点确认选择后', list);
181 }, 185 },
@@ -195,6 +199,7 @@ Page({ @@ -195,6 +199,7 @@ Page({
195 }, 199 },
196 // 付款 200 // 付款
197 confirmPayment() { 201 confirmPayment() {
  202 + let that = this;
198 let actualPrice = this.data.actualPrice 203 let actualPrice = this.data.actualPrice
199 if (actualPrice <= 0) { 204 if (actualPrice <= 0) {
200 return 205 return
@@ -220,9 +225,9 @@ Page({ @@ -220,9 +225,9 @@ Page({
220 paySign: res.data.paySign, 225 paySign: res.data.paySign,
221 success(ress) { 226 success(ress) {
222 if (ress.errMsg == 'requestPayment:ok') { 227 if (ress.errMsg == 'requestPayment:ok') {
223 - this.writeOff() 228 + that.writeOff()
224 console.log('支付成功', ress); 229 console.log('支付成功', ress);
225 - wx.showToast({ 230 + return wx.showToast({
226 title: '支付成功', 231 title: '支付成功',
227 duration: 1000, 232 duration: 1000,
228 icon: 'none', 233 icon: 'none',
@@ -257,6 +262,14 @@ Page({ @@ -257,6 +262,14 @@ Page({
257 }).then(res => { 262 }).then(res => {
258 if (res.ret == '200') { 263 if (res.ret == '200') {
259 console.log('核销成功', str); 264 console.log('核销成功', str);
  265 + this.setData({
  266 + inputValue:'',
  267 + finalList:[],
  268 + couponArr:[],
  269 + discountPrice:'0.00',
  270 + actualPrice:'',
  271 + })
  272 + this.getCoupon()
260 } 273 }
261 }) 274 })
262 } 275 }
@@ -285,6 +298,7 @@ Page({ @@ -285,6 +298,7 @@ Page({
285 const isUseDate = (currentDate > startUseDate && currentDate < endUseDate) && 298 const isUseDate = (currentDate > startUseDate && currentDate < endUseDate) &&
286 (this.data.dataStr < (coupon.etime * 1) && coupon.is_open == '1') 299 (this.data.dataStr < (coupon.etime * 1) && coupon.is_open == '1')
287 let obj = this.data.canSolt 300 let obj = this.data.canSolt
  301 + console.log('当前餐段、是不是法定节假日',{...obj});
288 let selectedValue = true; 302 let selectedValue = true;
289 let str = true 303 let str = true
290 if (coupon.exp_date && coupon.exp_date.week_data && coupon.exp_date.week_data.length > 0) { 304 if (coupon.exp_date && coupon.exp_date.week_data && coupon.exp_date.week_data.length > 0) {
@@ -339,6 +353,8 @@ Page({ @@ -339,6 +353,8 @@ Page({
339 let inputValue = this.data.inputValue; 353 let inputValue = this.data.inputValue;
340 const amount = parseFloat(inputValue); 354 const amount = parseFloat(inputValue);
341 const validCoupons = arr.filter(coupon => (coupon.isOk == '1' && parseFloat(coupon.desc) <= amount)); 355 const validCoupons = arr.filter(coupon => (coupon.isOk == '1' && parseFloat(coupon.desc) <= amount));
  356 + // 排序后,优先选择最大优惠的券
  357 + validCoupons.sort((a, b) => parseInt(b.price) - parseInt(a.price));
342 arr.forEach(item => { 358 arr.forEach(item => {
343 const isValid = validCoupons.some(validCoupon => validCoupon.activity_id == item.activity_id); 359 const isValid = validCoupons.some(validCoupon => validCoupon.activity_id == item.activity_id);
344 item.canCheck = isValid ? '1' : '0'; // 如果在 validCoupons 中,设置 canCheck 为 1,否则为 0 360 item.canCheck = isValid ? '1' : '0'; // 如果在 validCoupons 中,设置 canCheck 为 1,否则为 0
@@ -348,34 +364,37 @@ Page({ @@ -348,34 +364,37 @@ Page({
348 let list2 = []; // 最优惠的不可叠加的优惠券 364 let list2 = []; // 最优惠的不可叠加的优惠券
349 let finalList = []; //最终用的优惠券组合 365 let finalList = []; //最终用的优惠券组合
350 // 计算可叠加的组合 366 // 计算可叠加的组合
351 - let remainingValue = inputValue; // 剩余金额 367 + let remainingValue = amount; // 剩余金额
352 let totalDiscount = 0; // 总优惠金额 368 let totalDiscount = 0; // 总优惠金额
353 // 尝试组合可叠加的优惠券 369 // 尝试组合可叠加的优惠券
354 validCoupons.forEach(coupon => { 370 validCoupons.forEach(coupon => {
355 - if (coupon.accumulationShow === '1') { 371 + // 可叠加券
  372 + if (coupon.accumulationShow == '1') {
356 const discountAmount = parseInt(coupon.price); 373 const discountAmount = parseInt(coupon.price);
357 - if (remainingValue > 0) { 374 + // 只有在添加优惠券后剩余金额仍大于或等于0时才添加
  375 + if (remainingValue - discountAmount >= 0) {
358 list1.push(coupon); 376 list1.push(coupon);
359 totalDiscount += discountAmount; // 累加优惠金额 377 totalDiscount += discountAmount; // 累加优惠金额
360 remainingValue -= discountAmount; // 减少应付金额 378 remainingValue -= discountAmount; // 减少应付金额
361 } 379 }
362 } 380 }
363 }); 381 });
  382 + console.log('可叠加的优惠券组合1:', list1);
364 // 检查可叠加组合是否有效 383 // 检查可叠加组合是否有效
365 if (remainingValue < 0) { 384 if (remainingValue < 0) {
366 list1 = []; // 如果剩余金额小于0,清空可叠加列表 385 list1 = []; // 如果剩余金额小于0,清空可叠加列表
367 } 386 }
368 // 重置剩余金额,用于不可叠加的优惠券 387 // 重置剩余金额,用于不可叠加的优惠券
369 - remainingValue = inputValue; 388 + remainingValue = amount;
370 // 遍历有效优惠券,寻找不可叠加的优惠券 389 // 遍历有效优惠券,寻找不可叠加的优惠券
371 validCoupons.forEach(coupon => { 390 validCoupons.forEach(coupon => {
372 if (coupon.accumulationShow === '0') { 391 if (coupon.accumulationShow === '0') {
373 const discountAmount = parseInt(coupon.price); 392 const discountAmount = parseInt(coupon.price);
374 // 计算实际支付金额 393 // 计算实际支付金额
375 - const finalAmount = inputValue - discountAmount; 394 + const finalAmount = amount - discountAmount;
376 // 确保实际支付金额不小于0,并且是最优惠的 395 // 确保实际支付金额不小于0,并且是最优惠的
377 if (finalAmount >= 0) { 396 if (finalAmount >= 0) {
378 - if (!list2.length || discountAmount > parseInt(bestCoupon.price)) { 397 + if (!list2.length || discountAmount > parseInt(coupon.price)) {
379 list2.push({ 398 list2.push({
380 ...coupon 399 ...coupon
381 }); // 更新最优惠的不可叠加券 400 }); // 更新最优惠的不可叠加券
@@ -383,11 +402,11 @@ Page({ @@ -383,11 +402,11 @@ Page({
383 } 402 }
384 } 403 }
385 }); 404 });
386 - console.log('可叠加的优惠券组合', list1); 405 + console.log('可叠加的优惠券组合2', list1);
387 console.log('不可叠加的优惠券', list2); 406 console.log('不可叠加的优惠券', list2);
388 // 计算两个列表的最大优惠值 407 // 计算两个列表的最大优惠值
389 - let maxStackableValue = list1.reduce((total, coupon) => total + coupon.price, 0);  
390 - let maxNonStackableValue = list2.reduce((total, coupon) => total + coupon.price, 0); 408 + let maxStackableValue = list1.reduce((total, coupon) => total + parseInt(coupon.price), 0);
  409 + let maxNonStackableValue = list2.reduce((total, coupon) => total + parseInt(coupon.price), 0);
391 410
392 // 比较两个最大值并输出结果 411 // 比较两个最大值并输出结果
393 if (maxStackableValue > maxNonStackableValue) { 412 if (maxStackableValue > maxNonStackableValue) {
@@ -431,7 +450,7 @@ Page({ @@ -431,7 +450,7 @@ Page({
431 console.log('实际支付金额:', actualPrice); 450 console.log('实际支付金额:', actualPrice);
432 this.setData({ 451 this.setData({
433 discountPrice, 452 discountPrice,
434 - actualPrice: actualPrice > 0 ? actualPrice : '0.1', // 确保实际支付金额不小于0.01 453 + actualPrice: actualPrice > 0 ? actualPrice : '0.01', // 确保实际支付金额不小于0.01
435 }); 454 });
436 return { 455 return {
437 arr, 456 arr,
@@ -8,12 +8,12 @@ @@ -8,12 +8,12 @@
8 <view class="font32r">付款金额</view> 8 <view class="font32r">付款金额</view>
9 <view class="payView"> 9 <view class="payView">
10 <text>¥</text> 10 <text>¥</text>
11 - <input type="digit" bindchange="bindKeyInput" /> 11 + <input type="digit" value="{{inputValue}}" bindchange="bindKeyInput" />
12 </view> 12 </view>
13 <view bindtap="showCouponModal" class="couponsBox {{available == '1' ? 'colorOrange':'colorNine'}}"> 13 <view bindtap="showCouponModal" class="couponsBox {{available == '1' ? 'colorOrange':'colorNine'}}">
14 <view class="couponLeft">优惠金额</view> 14 <view class="couponLeft">优惠金额</view>
15 <view class="couponRight"> 15 <view class="couponRight">
16 - <text wx:if="{{discountPrice}}">{{discountPrice}}</text> 16 + <text wx:if="{{discountPrice && discountPrice!='0.00'}}">{{discountPrice}}</text>
17 <view class="more" style="margin-top: 6rpx;"></view> 17 <view class="more" style="margin-top: 6rpx;"></view>
18 </view> 18 </view>
19 </view> 19 </view>
@@ -32,22 +32,22 @@ @@ -32,22 +32,22 @@
32 <text class="font28r">使用规则</text> 32 <text class="font28r">使用规则</text>
33 </view> 33 </view>
34 <view class="info font24r"> 34 <view class="info font24r">
35 - <view class="drop"></view>{{detail.is_same_day == '1' ? '领取当日可用' : '领取当日不可用'}} 35 + <view class="drop"></view>{{detail.arule.is_same_day == '1' ? '领取当日可用' : '领取当日不可用'}}
36 </view> 36 </view>
37 <view class="info font24r"> 37 <view class="info font24r">
38 - <view class="drop"></view>{{detail.is_accumulation == '1' ? '可以叠加使用其他优惠券' : '不可以叠加使用其他优惠券'}} 38 + <view class="drop"></view>{{detail.arule.is_accumulation == '1' ? '可以叠加使用其他优惠券' : '不可以叠加使用其他优惠券'}}
39 </view> 39 </view>
40 <view class="info font24r"> 40 <view class="info font24r">
41 - <view class="drop"></view>{{detail.is_compartment == '1' ? '可用于包间消费' : '不可用于包间消费'}} 41 + <view class="drop"></view>{{detail.arule.is_compartment == '1' ? '可用于包间消费' : '不可用于包间消费'}}
42 </view> 42 </view>
43 <view class="info font24r"> 43 <view class="info font24r">
44 - <view class="drop"></view>{{detail.is_eatin == '1' ? '堂食可用' : '不可用于堂食消费'}} 44 + <view class="drop"></view>{{detail.arule.is_eatin == '1' ? '堂食可用' : '不可用于堂食消费'}}
45 </view> 45 </view>
46 <view class="info font24r"> 46 <view class="info font24r">
47 - <view class="drop"></view>{{detail.is_takeout == '1' ? '外带可用' : '不可用于外带'}} 47 + <view class="drop"></view>{{detail.arule.is_takeout == '1' ? '外带可用' : '不可用于外带'}}
48 </view> 48 </view>
49 <view class="info font24r"> 49 <view class="info font24r">
50 - <view class="drop"></view>{{detail.is_appointment == '1' ? '需要提前预约' : '不需要提前预约'}} 50 + <view class="drop"></view>{{detail.arule.is_appointment == '1' ? '需要提前预约' : '不需要提前预约'}}
51 </view> 51 </view>
52 </view> 52 </view>
53 <view class="centerMain"> 53 <view class="centerMain">