|
@@ -736,49 +736,127 @@ Page({ |
|
@@ -736,49 +736,127 @@ Page({ |
|
736
|
return (this.data.dataStr < (item.useEtime * 1) || item.aexpiration.valid_forever == '1');
|
736
|
return (this.data.dataStr < (item.useEtime * 1) || item.aexpiration.valid_forever == '1');
|
|
737
|
})
|
737
|
})
|
|
738
|
list.sort((a, b) => {
|
738
|
list.sort((a, b) => {
|
|
739
|
- return (a.etime * 1) - (b.etime * 1)
|
739
|
+ if (a.aexpiration.valid_forever == '1' && b.aexpiration.valid_forever != '1') return 1;
|
|
|
|
740
|
+ if (a.aexpiration.valid_forever != '1' && b.aexpiration.valid_forever == '1') return -1;
|
|
|
|
741
|
+ return (a.useEtime * 1) - (b.useEtime * 1)
|
|
740
|
})
|
742
|
})
|
|
741
|
let couponArr = list
|
743
|
let couponArr = list
|
|
|
|
744
|
+ // 相同aid的券合并成一张,用加减号选择张数
|
|
|
|
745
|
+ let couponGroup = {}
|
|
|
|
746
|
+ let couponList = []
|
|
|
|
747
|
+ couponArr.forEach(item => {
|
|
|
|
748
|
+ let key = item.aid ? item.aid : item.activity_id
|
|
|
|
749
|
+ if (couponGroup[key]) {
|
|
|
|
750
|
+ couponGroup[key].num = (couponGroup[key].num * 1) + 1
|
|
|
|
751
|
+ couponGroup[key].activityIds.push(item.activity_id)
|
|
|
|
752
|
+ } else {
|
|
|
|
753
|
+ item.num = 1
|
|
|
|
754
|
+ item.checkedNum = 0
|
|
|
|
755
|
+ item.activityIds = [item.activity_id]
|
|
|
|
756
|
+ couponGroup[key] = item
|
|
|
|
757
|
+ couponList.push(item)
|
|
|
|
758
|
+ }
|
|
|
|
759
|
+ })
|
|
|
|
760
|
+ couponArr = couponList
|
|
|
|
761
|
+ // 单次使用数量限制,0表示不限制,默认0;合并完成后统一计算
|
|
|
|
762
|
+ couponArr.forEach(item => {
|
|
|
|
763
|
+ let useLimit = item.single_use_limit ? (item.single_use_limit * 1) : 0
|
|
|
|
764
|
+ item.maxNum = (useLimit > 0 && useLimit < item.num) ? useLimit : item.num
|
|
|
|
765
|
+ })
|
|
|
|
766
|
+ console.log('couponArr----',couponArr);
|
|
|
|
767
|
+
|
|
742
|
this.setData({
|
768
|
this.setData({
|
|
743
|
couponArr
|
769
|
couponArr
|
|
744
|
})
|
770
|
})
|
|
745
|
}
|
771
|
}
|
|
746
|
})
|
772
|
})
|
|
747
|
},
|
773
|
},
|
|
748
|
- // 勾选券
|
|
|
|
749
|
- checkboxChange(e) {
|
|
|
|
750
|
- console.log('checkbox发生change事件,携带value值为:', e.detail.value)
|
774
|
+ // 加减号选择券的张数
|
|
|
|
775
|
+ changeCouponNum(e) {
|
|
|
|
776
|
+ let type = e.currentTarget.dataset.type;
|
|
|
|
777
|
+ let aid = e.currentTarget.dataset.aid;
|
|
|
|
778
|
+ const list = this.data.couponArr
|
|
|
|
779
|
+ let item = list.find(i => i.aid == aid)
|
|
|
|
780
|
+ if (!item) return
|
|
|
|
781
|
+ if (type == 'plus') {
|
|
|
|
782
|
+ if (item.canAdd != '1') return
|
|
|
|
783
|
+ item.checkedNum = (item.checkedNum * 1) + 1
|
|
|
|
784
|
+ }
|
|
|
|
785
|
+ if (type == 'minus') {
|
|
|
|
786
|
+ if (item.checkedNum <= 0) return
|
|
|
|
787
|
+ item.checkedNum = (item.checkedNum * 1) - 1
|
|
|
|
788
|
+ }
|
|
|
|
789
|
+ this.refreshCouponState()
|
|
|
|
790
|
+ },
|
|
|
|
791
|
+ // 加减号中间输入框:只允许正整数或0,且不能超过当前可用张数
|
|
|
|
792
|
+ numInputChange(e) {
|
|
|
|
793
|
+ let aid = e.currentTarget.dataset.aid;
|
|
|
|
794
|
+ let val = (e.detail.value || '').replace(/[^\d]/g, ''); // 只保留数字
|
|
|
|
795
|
+ const list = this.data.couponArr
|
|
|
|
796
|
+ let item = list.find(i => i.aid == aid)
|
|
|
|
797
|
+ if (!item) return
|
|
|
|
798
|
+ let maxSelect = this.getMaxSelectNum(item)
|
|
|
|
799
|
+ let num = val === '' ? 0 : parseInt(val, 10)
|
|
|
|
800
|
+ if (isNaN(num)) num = 0
|
|
|
|
801
|
+ if (num > maxSelect) num = maxSelect
|
|
|
|
802
|
+ item.checkedNum = num
|
|
|
|
803
|
+ this.refreshCouponState()
|
|
|
|
804
|
+ },
|
|
|
|
805
|
+ // 计算某张券当前最多可以选到的张数
|
|
|
|
806
|
+ getMaxSelectNum(item) {
|
|
|
|
807
|
+ const list = this.data.couponArr
|
|
|
|
808
|
+ let inputValue = parseFloat(this.data.inputValue)
|
|
|
|
809
|
+ let descStr = 0
|
|
|
|
810
|
+ let val = 0
|
|
|
|
811
|
+ list.forEach(i => {
|
|
|
|
812
|
+ if (i.checkedNum > 0 && i.aid != item.aid) {
|
|
|
|
813
|
+ let d = parseFloat(i.desc) || 0
|
|
|
|
814
|
+ if (d > 0) descStr += d * i.checkedNum
|
|
|
|
815
|
+ val += parseFloat(i.price) * i.checkedNum
|
|
|
|
816
|
+ }
|
|
|
|
817
|
+ })
|
|
|
|
818
|
+ let itemDesc = parseFloat(item.desc) || 0
|
|
|
|
819
|
+ let itemPrice = parseFloat(item.price)
|
|
|
|
820
|
+ let maxSelect = item.maxNum
|
|
|
|
821
|
+ if (item.accumulationShow != '1' && maxSelect > 1) maxSelect = 1
|
|
|
|
822
|
+ if (inputValue > 0) {
|
|
|
|
823
|
+ if (itemDesc > 0) {
|
|
|
|
824
|
+ let byDesc = Math.floor((inputValue - descStr) / itemDesc)
|
|
|
|
825
|
+ if (byDesc < maxSelect) maxSelect = byDesc
|
|
|
|
826
|
+ } else {
|
|
|
|
827
|
+ // 无门槛券:满减门槛之和小于输入金额时才能叠加
|
|
|
|
828
|
+ if (descStr >= inputValue) {
|
|
|
|
829
|
+ maxSelect = 0
|
|
|
|
830
|
+ } else {
|
|
|
|
831
|
+ let byPrice = Math.floor((inputValue - val) / itemPrice)
|
|
|
|
832
|
+ if (byPrice < maxSelect) maxSelect = byPrice
|
|
|
|
833
|
+ }
|
|
|
|
834
|
+ }
|
|
|
|
835
|
+ }
|
|
|
|
836
|
+ if (maxSelect < 0) maxSelect = 0
|
|
|
|
837
|
+ return maxSelect
|
|
|
|
838
|
+ },
|
|
|
|
839
|
+ // 根据选择的券张数刷新券的可选状态
|
|
|
|
840
|
+ refreshCouponState() {
|
|
751
|
const list = this.data.couponArr
|
841
|
const list = this.data.couponArr
|
|
752
|
- const values = e.detail.value
|
|
|
|
753
|
- let arr = []
|
|
|
|
754
|
let currentCoupon = '0'; //没选券 为0 可叠加券1 不可叠加券2
|
842
|
let currentCoupon = '0'; //没选券 为0 可叠加券1 不可叠加券2
|
|
755
|
let inputValue = parseFloat(this.data.inputValue)
|
843
|
let inputValue = parseFloat(this.data.inputValue)
|
|
756
|
let val = 0; //勾选的券 优惠金额的和
|
844
|
let val = 0; //勾选的券 优惠金额的和
|
|
757
|
let descStr = 0; //勾选的券 每满金额的和
|
845
|
let descStr = 0; //勾选的券 每满金额的和
|
|
758
|
- for (let i = 0, lenI = list.length; i < lenI; ++i) {
|
|
|
|
759
|
- list[i].checked = false;
|
|
|
|
760
|
- for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
|
|
|
|
761
|
- if (list[i].activity_id === values[j]) {
|
|
|
|
762
|
- val += parseFloat(list[i].price)
|
|
|
|
763
|
- if (!list[i].desc || list[i].desc == 0) {
|
|
|
|
764
|
- descStr += parseFloat(list[i].price)
|
|
|
|
765
|
- } else {
|
|
|
|
766
|
- descStr += parseFloat(list[i].desc)
|
|
|
|
767
|
- }
|
|
|
|
768
|
- arr.push({
|
|
|
|
769
|
- ...list[i]
|
|
|
|
770
|
- })
|
|
|
|
771
|
- list[i].checked = true
|
|
|
|
772
|
- if (list[i].accumulationShow == '1') {
|
|
|
|
773
|
- currentCoupon = '1'
|
|
|
|
774
|
- break
|
|
|
|
775
|
- } else {
|
|
|
|
776
|
- currentCoupon = '2'
|
|
|
|
777
|
- break
|
|
|
|
778
|
- }
|
846
|
+ list.forEach(item => {
|
|
|
|
847
|
+ if (item.checkedNum > 0) {
|
|
|
|
848
|
+ val += parseFloat(item.price) * item.checkedNum
|
|
|
|
849
|
+ let itemDesc = parseFloat(item.desc) || 0
|
|
|
|
850
|
+ if (itemDesc > 0) {
|
|
|
|
851
|
+ descStr += itemDesc * item.checkedNum
|
|
|
|
852
|
+ }
|
|
|
|
853
|
+ if (item.accumulationShow != '1') {
|
|
|
|
854
|
+ currentCoupon = '2'
|
|
|
|
855
|
+ } else {
|
|
|
|
856
|
+ currentCoupon = '1'
|
|
779
|
}
|
857
|
}
|
|
780
|
}
|
858
|
}
|
|
781
|
- }
|
859
|
+ })
|
|
782
|
let remaining = inputValue - val; //剩余金额
|
860
|
let remaining = inputValue - val; //剩余金额
|
|
783
|
console.log('没选券为0可叠加券1不可叠加券2', currentCoupon, remaining);
|
861
|
console.log('没选券为0可叠加券1不可叠加券2', currentCoupon, remaining);
|
|
784
|
//没选券 所有可选的打开
|
862
|
//没选券 所有可选的打开
|
|
@@ -792,7 +870,7 @@ Page({ |
|
@@ -792,7 +870,7 @@ Page({ |
|
792
|
//选的不可叠加券 其他所有的可叠加券都不可选
|
870
|
//选的不可叠加券 其他所有的可叠加券都不可选
|
|
793
|
if (currentCoupon == '2') {
|
871
|
if (currentCoupon == '2') {
|
|
794
|
list.map((item) => {
|
872
|
list.map((item) => {
|
|
795
|
- if (item.checked) {
|
873
|
+ if (item.checkedNum > 0) {
|
|
796
|
item.showCpupon = '1'
|
874
|
item.showCpupon = '1'
|
|
797
|
} else {
|
875
|
} else {
|
|
798
|
item.showCpupon = '0'
|
876
|
item.showCpupon = '0'
|
|
@@ -802,17 +880,79 @@ Page({ |
|
@@ -802,17 +880,79 @@ Page({ |
|
802
|
//选的可叠加券 其他所有的不可叠加券都不可选
|
880
|
//选的可叠加券 其他所有的不可叠加券都不可选
|
|
803
|
if (currentCoupon == '1') {
|
881
|
if (currentCoupon == '1') {
|
|
804
|
list.forEach(item => {
|
882
|
list.forEach(item => {
|
|
805
|
- if (item.checked || (item.accumulationShow == "1" && (inputValue - descStr) >= parseFloat(item.desc) && parseFloat(item.price) <= (inputValue - val))) {
|
883
|
+ if (item.checkedNum > 0 || (item.accumulationShow == "1" && parseFloat(item.price) <= (inputValue - val) && (((parseFloat(item.desc) || 0) > 0 && (inputValue - descStr) >= parseFloat(item.desc)) || ((parseFloat(item.desc) || 0) === 0 && descStr < inputValue)))) {
|
|
806
|
item.showCpupon = '1';
|
884
|
item.showCpupon = '1';
|
|
807
|
} else {
|
885
|
} else {
|
|
808
|
item.showCpupon = '0';
|
886
|
item.showCpupon = '0';
|
|
809
|
}
|
887
|
}
|
|
810
|
})
|
888
|
})
|
|
811
|
}
|
889
|
}
|
|
812
|
- let finalList = list.filter(i=>{return i.checked})
|
|
|
|
813
|
- this.setData({
|
|
|
|
814
|
- couponArr: list,
|
|
|
|
815
|
- finalList
|
890
|
+ let finalList = []
|
|
|
|
891
|
+ list.forEach(item => {
|
|
|
|
892
|
+ if (item.checkedNum > 0) {
|
|
|
|
893
|
+ for (let k = 0; k < item.checkedNum; k++) {
|
|
|
|
894
|
+ finalList.push({
|
|
|
|
895
|
+ ...item,
|
|
|
|
896
|
+ activity_id: item.activityIds[k]
|
|
|
|
897
|
+ })
|
|
|
|
898
|
+ }
|
|
|
|
899
|
+ }
|
|
|
|
900
|
+ })
|
|
|
|
901
|
+ this.updateCanAdd()
|
|
|
|
902
|
+ if (inputValue) {
|
|
|
|
903
|
+ let discountPrice = 0;
|
|
|
|
904
|
+ finalList.forEach(i => {
|
|
|
|
905
|
+ discountPrice += parseFloat(i.price)
|
|
|
|
906
|
+ })
|
|
|
|
907
|
+ discountPrice = discountPrice.toFixed(2)
|
|
|
|
908
|
+ console.log('优惠金额----', discountPrice);
|
|
|
|
909
|
+ let actualPrice = (inputValue - discountPrice - this.data.consumeDiscount).toFixed(2)
|
|
|
|
910
|
+ let myBalance = this.data.myBalance * 1;
|
|
|
|
911
|
+ let balancePay = (this.data.payType == '1' && actualPrice > myBalance) ? myBalance : actualPrice;
|
|
|
|
912
|
+ let soothPrice = this.data.payType == '1' ? (actualPrice - balancePay).toFixed(2) : actualPrice;
|
|
|
|
913
|
+ this.setData({
|
|
|
|
914
|
+ couponArr: list,
|
|
|
|
915
|
+ finalList,
|
|
|
|
916
|
+ discountPrice,
|
|
|
|
917
|
+ actualPrice,
|
|
|
|
918
|
+ balancePay,
|
|
|
|
919
|
+ soothPrice,
|
|
|
|
920
|
+ })
|
|
|
|
921
|
+ } else {
|
|
|
|
922
|
+ this.setData({
|
|
|
|
923
|
+ couponArr: list,
|
|
|
|
924
|
+ finalList
|
|
|
|
925
|
+ })
|
|
|
|
926
|
+ }
|
|
|
|
927
|
+ },
|
|
|
|
928
|
+ // 计算每张券还能不能继续加:满减门槛之和不能超过输入金额
|
|
|
|
929
|
+ updateCanAdd() {
|
|
|
|
930
|
+ const list = this.data.couponArr
|
|
|
|
931
|
+ let inputValue = parseFloat(this.data.inputValue)
|
|
|
|
932
|
+ let descStr = 0
|
|
|
|
933
|
+ let val = 0
|
|
|
|
934
|
+ list.forEach(item => {
|
|
|
|
935
|
+ if (item.checkedNum > 0) {
|
|
|
|
936
|
+ let itemDesc = parseFloat(item.desc) || 0
|
|
|
|
937
|
+ let itemPrice = parseFloat(item.price)
|
|
|
|
938
|
+ if (itemDesc > 0) descStr += itemDesc * item.checkedNum
|
|
|
|
939
|
+ val += itemPrice * item.checkedNum
|
|
|
|
940
|
+ }
|
|
|
|
941
|
+ })
|
|
|
|
942
|
+ list.forEach(item => {
|
|
|
|
943
|
+ item.canAdd = '0'
|
|
|
|
944
|
+ // 不可叠加的券最多只能选一张
|
|
|
|
945
|
+ let maxSelect = item.accumulationShow == '1' ? item.maxNum : 1
|
|
|
|
946
|
+ if (item.showCpupon == '1' && item.checkedNum < maxSelect) {
|
|
|
|
947
|
+ let itemDesc = parseFloat(item.desc) || 0
|
|
|
|
948
|
+ let itemPrice = parseFloat(item.price)
|
|
|
|
949
|
+ if (itemDesc > 0) {
|
|
|
|
950
|
+ if (descStr + itemDesc <= inputValue) item.canAdd = '1'
|
|
|
|
951
|
+ } else {
|
|
|
|
952
|
+ // 无门槛券:满减门槛之和小于输入金额时才能叠加
|
|
|
|
953
|
+ if (descStr < inputValue && val + itemPrice <= inputValue) item.canAdd = '1'
|
|
|
|
954
|
+ }
|
|
|
|
955
|
+ }
|
|
816
|
})
|
956
|
})
|
|
817
|
},
|
957
|
},
|
|
818
|
// 输入金额
|
958
|
// 输入金额
|
|
@@ -821,7 +961,7 @@ Page({ |
|
@@ -821,7 +961,7 @@ Page({ |
|
821
|
let list = this.data.couponArr;
|
961
|
let list = this.data.couponArr;
|
|
822
|
// 所有的券重置
|
962
|
// 所有的券重置
|
|
823
|
list.forEach(i => {
|
963
|
list.forEach(i => {
|
|
824
|
- i.checked = false;
|
964
|
+ i.checkedNum = 0;
|
|
825
|
i.showCpupon = '0'
|
965
|
i.showCpupon = '0'
|
|
826
|
})
|
966
|
})
|
|
827
|
this.setData({
|
967
|
this.setData({
|
|
@@ -867,11 +1007,11 @@ Page({ |
|
@@ -867,11 +1007,11 @@ Page({ |
|
867
|
if (!inputValue) return
|
1007
|
if (!inputValue) return
|
|
868
|
let list = this.data.couponArr;
|
1008
|
let list = this.data.couponArr;
|
|
869
|
list = list.filter(i => {
|
1009
|
list = list.filter(i => {
|
|
870
|
- return i.checked
|
1010
|
+ return i.checkedNum > 0
|
|
871
|
})
|
1011
|
})
|
|
872
|
let price = 0;
|
1012
|
let price = 0;
|
|
873
|
list.forEach(i => {
|
1013
|
list.forEach(i => {
|
|
874
|
- price += parseFloat(i.price)
|
1014
|
+ price += parseFloat(i.price) * i.checkedNum
|
|
875
|
})
|
1015
|
})
|
|
876
|
console.log('优惠金额----', price);
|
1016
|
console.log('优惠金额----', price);
|
|
877
|
let val = parseFloat(this.data.inputValue);
|
1017
|
let val = parseFloat(this.data.inputValue);
|
|
@@ -1235,9 +1375,7 @@ Page({ |
|
@@ -1235,9 +1375,7 @@ Page({ |
|
1235
|
let list1 = []; // 可叠加的优惠券组合
|
1375
|
let list1 = []; // 可叠加的优惠券组合
|
|
1236
|
let list2 = []; // 最优惠的不可叠加的优惠券
|
1376
|
let list2 = []; // 最优惠的不可叠加的优惠券
|
|
1237
|
let finalList = []; //最终用的优惠券组合
|
1377
|
let finalList = []; //最终用的优惠券组合
|
|
1238
|
- // 计算可叠加的组合
|
|
|
|
1239
|
- let remainingPrice = amount; // 剩余金额
|
|
|
|
1240
|
- let totalDiscount = 0; // 总优惠金额
|
1378
|
+ let selectCount = {}; // 每种券选中的张数
|
|
1241
|
// 1. 优先排序 desc 为空或为 0 的,再根据 price 从大到小排序,再根据满减条件 desc 从大到小排序
|
1379
|
// 1. 优先排序 desc 为空或为 0 的,再根据 price 从大到小排序,再根据满减条件 desc 从大到小排序
|
|
1242
|
stackingCoupons.sort((a, b) => {
|
1380
|
stackingCoupons.sort((a, b) => {
|
|
1243
|
if (a.desc === '0' || a.desc === '') return -1; // 优先选择 desc 为空或为 0 的
|
1381
|
if (a.desc === '0' || a.desc === '') return -1; // 优先选择 desc 为空或为 0 的
|
|
@@ -1246,35 +1384,82 @@ Page({ |
|
@@ -1246,35 +1384,82 @@ Page({ |
|
1246
|
return parseFloat(b.desc) - parseFloat(a.desc); // 按满减条件 desc 从大到小
|
1384
|
return parseFloat(b.desc) - parseFloat(a.desc); // 按满减条件 desc 从大到小
|
|
1247
|
});
|
1385
|
});
|
|
1248
|
console.log('排序后的可叠加券列表',stackingCoupons);
|
1386
|
console.log('排序后的可叠加券列表',stackingCoupons);
|
|
1249
|
- // 2. 从排序后的券中选择
|
|
|
|
1250
|
- for (let coupon of stackingCoupons) {
|
|
|
|
1251
|
- let couponDesc = parseFloat(coupon.desc);
|
|
|
|
1252
|
- let couponPrice = parseFloat(coupon.price);
|
|
|
|
1253
|
- let kyNum = Math.floor(remainingPrice / couponDesc);// 计算券可用张数
|
|
|
|
1254
|
- if (couponDesc === 0) {// 如果满减条件为 0,直接选择
|
|
|
|
1255
|
- list1.push(coupon);
|
|
|
|
1256
|
- remainingPrice -= couponPrice; // 减去价格
|
|
|
|
1257
|
- totalDiscount += couponPrice; // 计算优惠金额
|
|
|
|
1258
|
- continue;
|
1387
|
+ // 2. 可叠加满减券:先选满减门槛最大的券,再叠加其他可用的券(门槛之和不能超过输入金额)
|
|
|
|
1388
|
+ let thresholdDescSum = 0
|
|
|
|
1389
|
+ let maxThresholdValue = 0
|
|
|
|
1390
|
+ let thresholdCoupons = stackingCoupons.filter(coupon => (parseFloat(coupon.desc) || 0) > 0)
|
|
|
|
1391
|
+ // 按满减门槛从大到小排序,门槛相同再按优惠金额从大到小
|
|
|
|
1392
|
+ thresholdCoupons.sort((a, b) => {
|
|
|
|
1393
|
+ if (parseFloat(b.desc) !== parseFloat(a.desc)) return parseFloat(b.desc) - parseFloat(a.desc)
|
|
|
|
1394
|
+ return parseFloat(b.price) - parseFloat(a.price)
|
|
|
|
1395
|
+ })
|
|
|
|
1396
|
+ thresholdCoupons.forEach(coupon => {
|
|
|
|
1397
|
+ let couponDesc = parseFloat(coupon.desc) || 0
|
|
|
|
1398
|
+ let couponPrice = parseFloat(coupon.price)
|
|
|
|
1399
|
+ let maxCopies = Math.floor((amount - thresholdDescSum) / couponDesc)
|
|
|
|
1400
|
+ if (maxCopies > coupon.maxNum) maxCopies = coupon.maxNum
|
|
|
|
1401
|
+ if (maxCopies > 0) {
|
|
|
|
1402
|
+ selectCount[coupon.aid] = maxCopies
|
|
|
|
1403
|
+ thresholdDescSum += couponDesc * maxCopies
|
|
|
|
1404
|
+ maxThresholdValue += couponPrice * maxCopies
|
|
|
|
1405
|
+ }
|
|
|
|
1406
|
+ })
|
|
|
|
1407
|
+ list1 = stackingCoupons.filter(i => (selectCount[i.aid] || 0) > 0)
|
|
|
|
1408
|
+ console.log('可叠加优惠券组合', list1);
|
|
|
|
1409
|
+ // 3. 无门槛可叠加券:满减门槛之和小于输入金额时可以叠加,等于时和满减组合取优惠更大的
|
|
|
|
1410
|
+ // 先算无门槛券单独使用的优惠总额
|
|
|
|
1411
|
+ let noDescValue = 0
|
|
|
|
1412
|
+ let noDescCount = {}
|
|
|
|
1413
|
+ stackingCoupons.forEach(coupon => {
|
|
|
|
1414
|
+ let couponDesc = parseFloat(coupon.desc) || 0
|
|
|
|
1415
|
+ let couponPrice = parseFloat(coupon.price)
|
|
|
|
1416
|
+ if (couponDesc === 0 && couponPrice > 0) {
|
|
|
|
1417
|
+ let num = Math.floor((amount - noDescValue) / couponPrice)
|
|
|
|
1418
|
+ if (num > coupon.maxNum) num = coupon.maxNum
|
|
|
|
1419
|
+ if (num > 0) {
|
|
|
|
1420
|
+ noDescCount[coupon.aid] = num
|
|
|
|
1421
|
+ noDescValue += couponPrice * num
|
|
|
|
1422
|
+ }
|
|
1259
|
}
|
1423
|
}
|
|
1260
|
- // 如果剩余金额足够,选择券
|
|
|
|
1261
|
- if (remainingPrice > couponDesc || remainingPrice == couponDesc) {
|
|
|
|
1262
|
- // 计算实际使用的张数,不能超过 kyNum
|
|
|
|
1263
|
- let usedCount = Math.min(kyNum, 1); // 假设每次只使用一张
|
|
|
|
1264
|
- let potentialDiscount = couponDesc * usedCount; // 计算潜在的优惠金额
|
|
|
|
1265
|
- if ((totalDiscount + potentialDiscount < amount) || (totalDiscount + potentialDiscount == amount)) {// 检查总优惠金额是否会超过总金额
|
|
|
|
1266
|
- list1.push(coupon);
|
|
|
|
1267
|
- remainingPrice -= couponPrice * usedCount; // 减去价格
|
|
|
|
1268
|
- totalDiscount += potentialDiscount; // 计算优惠金额
|
1424
|
+ })
|
|
|
|
1425
|
+ let maxStackableValue = maxThresholdValue
|
|
|
|
1426
|
+ if (thresholdDescSum < amount) {
|
|
|
|
1427
|
+ // 可以叠加:无门槛券按剩余优惠额度叠加上去
|
|
|
|
1428
|
+ stackingCoupons.forEach(coupon => {
|
|
|
|
1429
|
+ let couponDesc = parseFloat(coupon.desc) || 0
|
|
|
|
1430
|
+ let couponPrice = parseFloat(coupon.price)
|
|
|
|
1431
|
+ if (couponDesc === 0 && couponPrice > 0) {
|
|
|
|
1432
|
+ let num = Math.floor((amount - maxStackableValue) / couponPrice)
|
|
|
|
1433
|
+ if (num > coupon.maxNum) num = coupon.maxNum
|
|
|
|
1434
|
+ if (num > 0) {
|
|
|
|
1435
|
+ selectCount[coupon.aid] = num
|
|
|
|
1436
|
+ maxStackableValue += couponPrice * num
|
|
|
|
1437
|
+ }
|
|
1269
|
}
|
1438
|
}
|
|
|
|
1439
|
+ })
|
|
|
|
1440
|
+ list1 = stackingCoupons.filter(i => (selectCount[i.aid] || 0) > 0)
|
|
|
|
1441
|
+ } else if (noDescValue > maxThresholdValue) {
|
|
|
|
1442
|
+ // 门槛等于输入金额,不能叠加,取无门槛券或满减券中优惠更大的
|
|
|
|
1443
|
+ list1 = stackingCoupons.filter(i => (noDescCount[i.aid] || 0) > 0)
|
|
|
|
1444
|
+ selectCount = noDescCount
|
|
|
|
1445
|
+ maxStackableValue = noDescValue
|
|
|
|
1446
|
+ } else if (noDescValue == maxThresholdValue && noDescValue > 0) {
|
|
|
|
1447
|
+ // 优惠相同取最早到期
|
|
|
|
1448
|
+ let listThreshold = stackingCoupons.filter(i => (selectCount[i.aid] || 0) > 0)
|
|
|
|
1449
|
+ let listNoDesc = stackingCoupons.filter(i => (noDescCount[i.aid] || 0) > 0)
|
|
|
|
1450
|
+ let min1 = listThreshold.length > 0 ? Math.min(...listThreshold.map(item => item.useEtime)) : Infinity
|
|
|
|
1451
|
+ let min2 = listNoDesc.length > 0 ? Math.min(...listNoDesc.map(item => item.useEtime)) : Infinity
|
|
|
|
1452
|
+ if (min2 < min1) {
|
|
|
|
1453
|
+ list1 = listNoDesc
|
|
|
|
1454
|
+ selectCount = noDescCount
|
|
1270
|
}
|
1455
|
}
|
|
1271
|
- // 如果剩余金额已经小于等于 0,停止选择
|
|
|
|
1272
|
- if (remainingPrice < 0 || remainingPrice == 0) break;
|
|
|
|
1273
|
}
|
1456
|
}
|
|
|
|
1457
|
+ let stackList = list1
|
|
|
|
1458
|
+ let stackCount = selectCount
|
|
1274
|
// 遍历有效优惠券,寻找不可叠加的优惠券
|
1459
|
// 遍历有效优惠券,寻找不可叠加的优惠券
|
|
1275
|
let maxNonStackableValue = 0
|
1460
|
let maxNonStackableValue = 0
|
|
1276
|
validCoupons.forEach(coupon => {
|
1461
|
validCoupons.forEach(coupon => {
|
|
1277
|
- if (coupon.accumulationShow === '0') {
|
1462
|
+ if (coupon.accumulationShow != '1') {
|
|
1278
|
const discountAmount = parseFloat(coupon.price);
|
1463
|
const discountAmount = parseFloat(coupon.price);
|
|
1279
|
// 计算实际支付金额
|
1464
|
// 计算实际支付金额
|
|
1280
|
const finalAmount = amount - discountAmount;
|
1465
|
const finalAmount = amount - discountAmount;
|
|
@@ -1287,27 +1472,25 @@ Page({ |
|
@@ -1287,27 +1472,25 @@ Page({ |
|
1287
|
}
|
1472
|
}
|
|
1288
|
}
|
1473
|
}
|
|
1289
|
});
|
1474
|
});
|
|
1290
|
- console.log('可叠加优惠券组合', list1);
|
|
|
|
1291
|
console.log('不可叠加的优惠券', list2);
|
1475
|
console.log('不可叠加的优惠券', list2);
|
|
1292
|
// 计算两个列表的最大优惠值
|
1476
|
// 计算两个列表的最大优惠值
|
|
1293
|
- let maxStackableValue = list1.reduce((total, coupon) => total + parseFloat(coupon.price), 0);
|
|
|
|
1294
|
console.log('可叠加优惠券组合总金额', maxStackableValue);
|
1477
|
console.log('可叠加优惠券组合总金额', maxStackableValue);
|
|
1295
|
console.log('不可叠加的优惠券总金额', maxNonStackableValue);
|
1478
|
console.log('不可叠加的优惠券总金额', maxNonStackableValue);
|
|
1296
|
// 比较两个最大值并输出结果
|
1479
|
// 比较两个最大值并输出结果
|
|
1297
|
if (maxStackableValue > maxNonStackableValue) {
|
1480
|
if (maxStackableValue > maxNonStackableValue) {
|
|
1298
|
- finalList = list1
|
1481
|
+ finalList = stackList
|
|
1299
|
} else if (maxStackableValue < maxNonStackableValue) {
|
1482
|
} else if (maxStackableValue < maxNonStackableValue) {
|
|
1300
|
finalList = list2
|
1483
|
finalList = list2
|
|
1301
|
} else {
|
1484
|
} else {
|
|
1302
|
console.log(`两者的优惠值相同: ${maxStackableValue}`);
|
1485
|
console.log(`两者的优惠值相同: ${maxStackableValue}`);
|
|
1303
|
- let copiedList1 = JSON.parse(JSON.stringify(list1));
|
1486
|
+ let copiedList1 = JSON.parse(JSON.stringify(stackList));
|
|
1304
|
let copiedList2 = JSON.parse(JSON.stringify(list2));
|
1487
|
let copiedList2 = JSON.parse(JSON.stringify(list2));
|
|
1305
|
let minList1 = copiedList1.length > 0 ? Math.min(...copiedList1.map(item => item.useEtime)) : Infinity;
|
1488
|
let minList1 = copiedList1.length > 0 ? Math.min(...copiedList1.map(item => item.useEtime)) : Infinity;
|
|
1306
|
let minList2 = copiedList2.length > 0 ? Math.min(...copiedList2.map(item => item.useEtime)) : Infinity;
|
1489
|
let minList2 = copiedList2.length > 0 ? Math.min(...copiedList2.map(item => item.useEtime)) : Infinity;
|
|
1307
|
// 判断最小的 useEtime 是在 list1 还是 list2
|
1490
|
// 判断最小的 useEtime 是在 list1 还是 list2
|
|
1308
|
if (minList1 <= minList2) {
|
1491
|
if (minList1 <= minList2) {
|
|
1309
|
- console.log('最小的在 list1 中', list1);
|
|
|
|
1310
|
- finalList = list1
|
1492
|
+ console.log('最小的在 list1 中', stackList);
|
|
|
|
1493
|
+ finalList = stackList
|
|
1311
|
} else {
|
1494
|
} else {
|
|
1312
|
console.log('最小的在 list2 中', list2);
|
1495
|
console.log('最小的在 list2 中', list2);
|
|
1313
|
finalList = list2
|
1496
|
finalList = list2
|
|
@@ -1320,19 +1503,32 @@ Page({ |
|
@@ -1320,19 +1503,32 @@ Page({ |
|
1320
|
let target = arr.findIndex(obj => obj.activity_id === coupon.activity_id);
|
1503
|
let target = arr.findIndex(obj => obj.activity_id === coupon.activity_id);
|
|
1321
|
if (target>-1) {
|
1504
|
if (target>-1) {
|
|
1322
|
arr[target].showCpupon = 1;
|
1505
|
arr[target].showCpupon = 1;
|
|
1323
|
- arr[target].checked = true;
|
|
|
|
1324
|
- discountPrice += parseFloat(coupon.price); // 使用 parseFloat 确保类型正确
|
1506
|
+ arr[target].checkedNum = stackCount[coupon.aid] || 1;
|
|
|
|
1507
|
+ discountPrice += parseFloat(coupon.price) * (stackCount[coupon.aid] || 1); // 使用 parseFloat 确保类型正确
|
|
1325
|
}
|
1508
|
}
|
|
1326
|
console.log('选中的券', {
|
1509
|
console.log('选中的券', {
|
|
1327
|
...coupon
|
1510
|
...coupon
|
|
1328
|
});
|
1511
|
});
|
|
1329
|
});
|
1512
|
});
|
|
|
|
1513
|
+ // 根据选中的张数展开最终券列表
|
|
|
|
1514
|
+ finalList = []
|
|
|
|
1515
|
+ arr.forEach(item => {
|
|
|
|
1516
|
+ if (item.checkedNum > 0) {
|
|
|
|
1517
|
+ for (let k = 0; k < item.checkedNum; k++) {
|
|
|
|
1518
|
+ finalList.push({
|
|
|
|
1519
|
+ ...item,
|
|
|
|
1520
|
+ activity_id: item.activityIds[k]
|
|
|
|
1521
|
+ })
|
|
|
|
1522
|
+ }
|
|
|
|
1523
|
+ }
|
|
|
|
1524
|
+ })
|
|
1330
|
discountPrice = discountPrice.toFixed(2)
|
1525
|
discountPrice = discountPrice.toFixed(2)
|
|
1331
|
console.log('总优惠金额:', discountPrice);
|
1526
|
console.log('总优惠金额:', discountPrice);
|
|
1332
|
console.log('消费金抵扣:',this.data.consumeDiscount);
|
1527
|
console.log('消费金抵扣:',this.data.consumeDiscount);
|
|
1333
|
this.setData({
|
1528
|
this.setData({
|
|
1334
|
discountPrice,
|
1529
|
discountPrice,
|
|
1335
|
});
|
1530
|
});
|
|
|
|
1531
|
+ this.updateCanAdd()
|
|
1336
|
return {
|
1532
|
return {
|
|
1337
|
arr,
|
1533
|
arr,
|
|
1338
|
discountPrice,
|
1534
|
discountPrice,
|
|
@@ -1425,42 +1621,83 @@ Page({ |
|
@@ -1425,42 +1621,83 @@ Page({ |
|
1425
|
let obj = this.checkTimeInInterval(currentTime, lunchDis, lunchDinnerList);
|
1621
|
let obj = this.checkTimeInInterval(currentTime, lunchDis, lunchDinnerList);
|
|
1426
|
//节假日数组
|
1622
|
//节假日数组
|
|
1427
|
var holidays = [
|
1623
|
var holidays = [
|
|
1428
|
- '2025-1-1', // 元旦
|
|
|
|
1429
|
- '2025-1-28', // 春节
|
|
|
|
1430
|
- '2025-1-29', // 春节
|
|
|
|
1431
|
- '2025-1-30', // 春节
|
|
|
|
1432
|
- '2025-1-31', // 春节
|
|
|
|
1433
|
- '2025-2-1', // 春节
|
|
|
|
1434
|
- '2025-2-2', // 春节
|
|
|
|
1435
|
- '2025-2-3', // 春节
|
|
|
|
1436
|
- '2025-2-4', // 春节
|
|
|
|
1437
|
- '2025-4-4', //清明
|
|
|
|
1438
|
- '2025-4-5', //清明
|
|
|
|
1439
|
- '2025-4-6', //清明
|
|
|
|
1440
|
- '2025-5-1', // 劳动节
|
|
|
|
1441
|
- '2025-5-2', // 劳动节
|
|
|
|
1442
|
- '2025-5-3', // 劳动节
|
|
|
|
1443
|
- '2025-5-4', // 劳动节
|
|
|
|
1444
|
- '2025-5-5', // 劳动节
|
|
|
|
1445
|
- '2025-5-31', // 端午节
|
|
|
|
1446
|
- '2025-6-1', // 端午节
|
|
|
|
1447
|
- '2025-6-2', // 端午节
|
|
|
|
1448
|
- '2025-10-1', // 国庆节、中秋节
|
|
|
|
1449
|
- '2025-10-2', // 国庆节、中秋节
|
|
|
|
1450
|
- '2025-10-3', // 国庆节、中秋节
|
|
|
|
1451
|
- '2025-10-4', // 国庆节、中秋节
|
|
|
|
1452
|
- '2025-10-5', // 国庆节、中秋节
|
|
|
|
1453
|
- '2025-10-6', // 国庆节、中秋节
|
|
|
|
1454
|
- '2025-10-7', // 国庆节、中秋节
|
|
|
|
1455
|
- '2025-10-8', // 国庆节、中秋节
|
1624
|
+ '2026-1-1', // 元旦
|
|
|
|
1625
|
+ '2026-1-2', // 元旦
|
|
|
|
1626
|
+ '2026-1-3', // 元旦
|
|
|
|
1627
|
+ '2026-2-15', // 春节
|
|
|
|
1628
|
+ '2026-2-16', // 春节
|
|
|
|
1629
|
+ '2026-2-17', // 春节
|
|
|
|
1630
|
+ '2026-2-18', // 春节
|
|
|
|
1631
|
+ '2026-2-19', // 春节
|
|
|
|
1632
|
+ '2026-2-20', // 春节
|
|
|
|
1633
|
+ '2026-2-21', // 春节
|
|
|
|
1634
|
+ '2026-2-22', // 春节
|
|
|
|
1635
|
+ '2026-2-23', // 春节
|
|
|
|
1636
|
+ '2026-4-4', //清明
|
|
|
|
1637
|
+ '2026-4-5', //清明
|
|
|
|
1638
|
+ '2026-4-6', //清明
|
|
|
|
1639
|
+ '2026-5-1', // 劳动节
|
|
|
|
1640
|
+ '2026-5-2', // 劳动节
|
|
|
|
1641
|
+ '2026-5-3', // 劳动节
|
|
|
|
1642
|
+ '2026-5-4', // 劳动节
|
|
|
|
1643
|
+ '2026-5-5', // 劳动节
|
|
|
|
1644
|
+ '2026-6-19', // 端午节
|
|
|
|
1645
|
+ '2026-6-20', // 端午节
|
|
|
|
1646
|
+ '2026-6-21', // 端午节
|
|
|
|
1647
|
+ '2026-9-25', // 中秋节
|
|
|
|
1648
|
+ '2026-9-26', // 中秋节
|
|
|
|
1649
|
+ '2026-9-27', // 中秋节
|
|
|
|
1650
|
+ '2026-10-1', // 国庆节
|
|
|
|
1651
|
+ '2026-10-2', // 国庆节
|
|
|
|
1652
|
+ '2026-10-3', // 国庆节
|
|
|
|
1653
|
+ '2026-10-4', // 国庆节
|
|
|
|
1654
|
+ '2026-10-5', // 国庆节
|
|
|
|
1655
|
+ '2026-10-6', // 国庆节
|
|
|
|
1656
|
+ '2026-10-7', // 国庆节
|
|
|
|
1657
|
+ '2027-1-1', // 元旦
|
|
|
|
1658
|
+ '2027-1-2', // 元旦
|
|
|
|
1659
|
+ '2027-1-3', // 元旦
|
|
|
|
1660
|
+ '2027-2-5', // 春节
|
|
|
|
1661
|
+ '2027-2-6', // 春节
|
|
|
|
1662
|
+ '2027-2-7', // 春节
|
|
|
|
1663
|
+ '2027-2-8', // 春节
|
|
|
|
1664
|
+ '2027-2-9', // 春节
|
|
|
|
1665
|
+ '2027-2-10', // 春节
|
|
|
|
1666
|
+ '2027-2-11', // 春节
|
|
|
|
1667
|
+ '2027-2-12', // 春节
|
|
|
|
1668
|
+ '2027-2-13', // 春节
|
|
|
|
1669
|
+ '2027-4-3', //清明
|
|
|
|
1670
|
+ '2027-4-4', //清明
|
|
|
|
1671
|
+ '2027-4-5', //清明
|
|
|
|
1672
|
+ '2027-5-1', // 劳动节
|
|
|
|
1673
|
+ '2027-5-2', // 劳动节
|
|
|
|
1674
|
+ '2027-5-3', // 劳动节
|
|
|
|
1675
|
+ '2027-5-4', // 劳动节
|
|
|
|
1676
|
+ '2027-5-5', // 劳动节
|
|
|
|
1677
|
+ '2027-6-9', // 端午节
|
|
|
|
1678
|
+ '2027-9-15', // 中秋节
|
|
|
|
1679
|
+ '2027-10-1', // 国庆节
|
|
|
|
1680
|
+ '2027-10-2', // 国庆节
|
|
|
|
1681
|
+ '2027-10-3', // 国庆节
|
|
|
|
1682
|
+ '2027-10-4', // 国庆节
|
|
|
|
1683
|
+ '2027-10-5', // 国庆节
|
|
|
|
1684
|
+ '2027-10-6', // 国庆节
|
|
|
|
1685
|
+ '2027-10-7', // 国庆节
|
|
1456
|
];
|
1686
|
];
|
|
1457
|
//周末上班日期数组
|
1687
|
//周末上班日期数组
|
|
1458
|
var nWeekend = [
|
1688
|
var nWeekend = [
|
|
1459
|
- '2025-1-26', //春节调整
|
|
|
|
1460
|
- '2025-2-8', //春节调整
|
|
|
|
1461
|
- '2025-4-27', //五一调整
|
|
|
|
1462
|
- '2025-9-28', //国庆调整
|
|
|
|
1463
|
- '2025-10-11', //国庆调整
|
1689
|
+ '2026-1-4', //元旦调整
|
|
|
|
1690
|
+ '2026-2-14', //春节调整
|
|
|
|
1691
|
+ '2026-2-28', //春节调整
|
|
|
|
1692
|
+ '2026-5-9', //五一调整
|
|
|
|
1693
|
+ '2026-9-20', //国庆调整
|
|
|
|
1694
|
+ '2026-10-10', //国庆调整
|
|
|
|
1695
|
+ '2027-1-31', //春节调整
|
|
|
|
1696
|
+ '2027-2-14', //春节调整
|
|
|
|
1697
|
+ '2027-4-25', //五一调整
|
|
|
|
1698
|
+ '2027-5-8', //五一调整
|
|
|
|
1699
|
+ '2027-9-26', //国庆调整
|
|
|
|
1700
|
+ '2027-10-9', //国庆调整
|
|
1464
|
];
|
1701
|
];
|
|
1465
|
var curdate = new Date();
|
1702
|
var curdate = new Date();
|
|
1466
|
var year = curdate.getFullYear();
|
1703
|
var year = curdate.getFullYear();
|
|
@@ -1644,4 +1881,4 @@ Page({ |
|
@@ -1644,4 +1881,4 @@ Page({ |
|
1644
|
onShareAppMessage() {
|
1881
|
onShareAppMessage() {
|
|
1645
|
|
1882
|
|
|
1646
|
}
|
1883
|
}
|
|
1647
|
-}) |
|
|
|
|
|
1884
|
+}) |