|
|
|
// pack-cookbook/pages/comboBuy.js
|
|
|
|
import util from '../../utils/util';
|
|
|
|
import App from '../../utils/api';
|
|
|
|
Page({
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 页面的初始数据
|
|
|
|
*/
|
|
|
|
data: {
|
|
|
|
sid: '',
|
|
|
|
details: {}, //上页面传入的套餐信息
|
|
|
|
seller: {}, //商家信息
|
|
|
|
couponArr: [], //本套餐可用的优惠券列表
|
|
|
|
finalList: [], //勾选的券列表
|
|
|
|
planPrice: '', //套餐价格
|
|
|
|
memberPrice: '', //菜品原价
|
|
|
|
discountPrice: '', //优惠金额
|
|
|
|
actualPrice: '', //抵扣后价格
|
|
|
|
couponSwitch: true, //本单使用优惠券开关
|
|
|
|
dataStr: Date.now() * 1 / 1000,
|
|
|
|
},
|
|
|
|
couponModal: '',
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 生命周期函数--监听页面加载
|
|
|
|
*/
|
|
|
|
onLoad(options) {
|
|
|
|
console.log('comboBuy options----', options);
|
|
|
|
this.couponModal = this.selectComponent("#couponModal");
|
|
|
|
let details = {};
|
|
|
|
if (options.details) {
|
|
|
|
details = JSON.parse(options.details);
|
|
|
|
}
|
|
|
|
let planPrice = details.plan_price || '';
|
|
|
|
let memberPrice = details.member_price || details.menu_price || '';
|
|
|
|
if (planPrice !== '') {
|
|
|
|
planPrice = parseFloat(planPrice).toFixed(2);
|
|
|
|
}
|
|
|
|
if (memberPrice !== '') {
|
|
|
|
memberPrice = parseFloat(memberPrice).toFixed(2);
|
|
|
|
}
|
|
|
|
this.setData({
|
|
|
|
details,
|
|
|
|
planPrice,
|
|
|
|
memberPrice,
|
|
|
|
actualPrice: planPrice || '0.00',
|
|
|
|
})
|
|
|
|
let sid = (details.seller && details.seller.sid) || options.sid || '';
|
|
|
|
if (sid) {
|
|
|
|
this.setData({
|
|
|
|
sid
|
|
|
|
})
|
|
|
|
if (details.seller) {
|
|
|
|
this.setData({
|
|
|
|
seller: details.seller
|
|
|
|
})
|
|
|
|
}
|
|
|
|
this.getSellerDetailFun(sid)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// 获取商家名称、照片
|
|
|
|
getSellerDetailFun(sid) {
|
|
|
|
App.Customer.GetSellerDetail({
|
|
|
|
sid
|
|
|
|
}).then(res => {
|
|
|
|
let data = res.data;
|
|
|
|
this.setData({
|
|
|
|
seller: {
|
|
|
|
seller_image: data.seller_image,
|
|
|
|
sellername: data.sellername,
|
|
|
|
sid: data.id
|
|
|
|
}
|
|
|
|
})
|
|
|
|
this.getCoupon()
|
|
|
|
})
|
|
|
|
},
|
|
|
|
// 获取所有优惠券,只展示本套餐可用的
|
|
|
|
getCoupon() {
|
|
|
|
if (!this.data.sid) return;
|
|
|
|
App.Activity.ActivityListByOpenid({
|
|
|
|
sellerid: this.data.sid
|
|
|
|
}).then(res => {
|
|
|
|
if (res.ret == 200 && res.data && res.data.length > 0) {
|
|
|
|
let list = res.data;
|
|
|
|
list.forEach(detail => {
|
|
|
|
detail.exp_date = detail.exp_date ? util.determine(detail.exp_date) : '';
|
|
|
|
detail.arule = detail.arule ? util.determine(detail.arule) : '';
|
|
|
|
detail.expiration_date = detail.expiration_date ? util.determine(detail.expiration_date) : '';
|
|
|
|
detail.aexpiration = detail.aexpiration ? util.determine(detail.aexpiration) : '';
|
|
|
|
if (detail.arule) {
|
|
|
|
detail.accumulationShow = detail.arule.is_accumulation
|
|
|
|
}
|
|
|
|
if (detail.exp_date && detail.exp_date.week_data) {
|
|
|
|
let mor = this.hasConsecutiveSelect(detail.exp_date.week_data, '1').rArr.join('、')
|
|
|
|
let noon = this.hasConsecutiveSelect(detail.exp_date.week_data, '2').rArr.join('、')
|
|
|
|
let nig = this.hasConsecutiveSelect(detail.exp_date.week_data, '3').rArr.join('、')
|
|
|
|
if (mor) mor = '早餐:' + mor
|
|
|
|
if (noon) noon = '午餐:' + noon
|
|
|
|
if (nig) nig = '晚餐:' + nig
|
|
|
|
detail.morText = mor;
|
|
|
|
detail.noonText = noon;
|
|
|
|
detail.nigText = nig;
|
|
|
|
}
|
|
|
|
detail.checked = false;
|
|
|
|
detail.showCpupon = '0';
|
|
|
|
detail.cdate = detail.cdate * 1;
|
|
|
|
detail.useStime = '';
|
|
|
|
detail.useEtime = '';
|
|
|
|
let condition = detail.aexpiration || {};
|
|
|
|
if (condition.valid_forever === '0' && condition.stime && condition.etime) {
|
|
|
|
detail.useStime = condition.stime;
|
|
|
|
detail.useEtime = condition.etime;
|
|
|
|
}
|
|
|
|
if (condition.valid_forever === '1') {
|
|
|
|
detail.foreverText = '永久有效';
|
|
|
|
}
|
|
|
|
if (condition.valid_forever === '0' && condition.today) {
|
|
|
|
let obj = this.setTimeRange(detail.cdate, 0, parseInt(condition.today) - 1);
|
|
|
|
detail.useStime = obj.useStime;
|
|
|
|
detail.useEtime = obj.useEtime;
|
|
|
|
}
|
|
|
|
if (condition.valid_forever === '0' && condition.tomorrow) {
|
|
|
|
let obj = this.setTimeRange(detail.cdate, 1, condition.tomorrow * 1);
|
|
|
|
detail.useStime = obj.useStime;
|
|
|
|
detail.useEtime = obj.useEtime;
|
|
|
|
}
|
|
|
|
if (condition.valid_forever === '0' && condition.sday && condition.eday) {
|
|
|
|
let obj = this.setTimeRange(detail.cdate, parseInt(condition.sday) - 1, parseInt(condition.eday) - 1);
|
|
|
|
detail.useStime = obj.useStime;
|
|
|
|
detail.useEtime = obj.useEtime;
|
|
|
|
}
|
|
|
|
if ((detail.useStime > 0 && detail.useEtime > 0) || condition.valid_forever === '1') {
|
|
|
|
detail.endDateStr = condition.valid_forever == '1' ? '永久有效' : (util.unixtoTime(detail.useStime, 'YYYY-MM-DD') + '至' + util.unixtoTime(detail.useEtime, 'YYYY-MM-DD'));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// 过滤已过期的券
|
|
|
|
list = list.filter(item => {
|
|
|
|
return (this.data.dataStr < (item.useEtime * 1) || item.aexpiration.valid_forever == '1');
|
|
|
|
})
|
|
|
|
// 判断券是否可用:满足套餐价格门槛的才显示
|
|
|
|
let planPrice = parseFloat(this.data.planPrice) || 0;
|
|
|
|
list = list.filter(item => {
|
|
|
|
let desc = parseFloat(item.desc) || 0;
|
|
|
|
return desc == 0 || planPrice >= desc;
|
|
|
|
})
|
|
|
|
// 相同aid的券合并成一张,用加减号选择张数
|
|
|
|
let couponGroup = {};
|
|
|
|
let couponList = [];
|
|
|
|
list.forEach(item => {
|
|
|
|
let key = item.aid ? item.aid : item.activity_id
|
|
|
|
if (couponGroup[key]) {
|
|
|
|
couponGroup[key].num = (couponGroup[key].num * 1) + 1
|
|
|
|
couponGroup[key].activityIds.push(item.activity_id)
|
|
|
|
} else {
|
|
|
|
item.num = 1
|
|
|
|
item.checkedNum = 0
|
|
|
|
item.groupKey = key
|
|
|
|
item.activityIds = [item.activity_id]
|
|
|
|
couponGroup[key] = item
|
|
|
|
couponList.push(item)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
list = couponList
|
|
|
|
// 单次使用数量限制,0表示不限制,默认0
|
|
|
|
list.forEach(item => {
|
|
|
|
let useLimit = item.single_use_limit ? (item.single_use_limit * 1) : 0
|
|
|
|
item.maxNum = (useLimit > 0 && useLimit < item.num) ? useLimit : item.num
|
|
|
|
item.showCpupon = '1'
|
|
|
|
item.canCheck = '1'
|
|
|
|
item.isOk = '1'
|
|
|
|
})
|
|
|
|
// 面额大的排在前面
|
|
|
|
list.sort((a, b) => (parseFloat(b.price) || 0) - (parseFloat(a.price) || 0))
|
|
|
|
console.log('comboBuy couponArr----', list);
|
|
|
|
this.setData({
|
|
|
|
couponArr: list
|
|
|
|
})
|
|
|
|
if (this.data.couponSwitch) {
|
|
|
|
this.colationData()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.setData({
|
|
|
|
couponArr: [],
|
|
|
|
discountPrice: '',
|
|
|
|
actualPrice: this.data.planPrice || '0.00',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
// 打开优惠券弹框
|
|
|
|
showCouponModal() {
|
|
|
|
this.couponModal.showModal()
|
|
|
|
},
|
|
|
|
// 关闭优惠券弹框
|
|
|
|
closeCouponModal() {
|
|
|
|
this.couponModal.closeModal()
|
|
|
|
},
|
|
|
|
// 使用券开关
|
|
|
|
switchChange(event) {
|
|
|
|
this.setData({
|
|
|
|
couponSwitch: event.detail.value,
|
|
|
|
})
|
|
|
|
if (this.data.couponSwitch) {
|
|
|
|
this.colationData()
|
|
|
|
} else {
|
|
|
|
let list = this.data.couponArr;
|
|
|
|
list.forEach(i => {
|
|
|
|
i.checkedNum = 0;
|
|
|
|
i.showCpupon = '1';
|
|
|
|
i.canAdd = '1';
|
|
|
|
})
|
|
|
|
this.setData({
|
|
|
|
couponArr: list,
|
|
|
|
finalList: [],
|
|
|
|
discountPrice: '',
|
|
|
|
actualPrice: this.data.planPrice || '0.00',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// 加减号选择券的张数
|
|
|
|
changeCouponNum(e) {
|
|
|
|
let type = e.currentTarget.dataset.type;
|
|
|
|
let aid = e.currentTarget.dataset.aid;
|
|
|
|
const list = this.data.couponArr
|
|
|
|
let item = list.find(i => i.aid == aid)
|
|
|
|
if (!item) return
|
|
|
|
if (type == 'plus') {
|
|
|
|
if (item.canAdd != '1') return
|
|
|
|
item.checkedNum = (item.checkedNum * 1) + 1
|
|
|
|
}
|
|
|
|
if (type == 'minus') {
|
|
|
|
if (item.checkedNum <= 0) return
|
|
|
|
item.checkedNum = (item.checkedNum * 1) - 1
|
|
|
|
}
|
|
|
|
this.refreshCouponState()
|
|
|
|
},
|
|
|
|
// 加减号中间输入框
|
|
|
|
numInputChange(e) {
|
|
|
|
let aid = e.currentTarget.dataset.aid;
|
|
|
|
let val = (e.detail.value || '').replace(/[^\d]/g, '');
|
|
|
|
const list = this.data.couponArr
|
|
|
|
let item = list.find(i => i.aid == aid)
|
|
|
|
if (!item) return
|
|
|
|
let maxSelect = this.getMaxSelectNum(item)
|
|
|
|
let num = val === '' ? 0 : parseInt(val, 10)
|
|
|
|
if (isNaN(num)) num = 0
|
|
|
|
if (num > maxSelect) num = maxSelect
|
|
|
|
item.checkedNum = num
|
|
|
|
this.refreshCouponState()
|
|
|
|
},
|
|
|
|
// 计算某张券当前最多可以选到的张数
|
|
|
|
getMaxSelectNum(item) {
|
|
|
|
const list = this.data.couponArr
|
|
|
|
let inputValue = parseFloat(this.data.planPrice)
|
|
|
|
let descStr = 0
|
|
|
|
let val = 0
|
|
|
|
list.forEach(i => {
|
|
|
|
if (i.checkedNum > 0 && i.aid != item.aid) {
|
|
|
|
let d = parseFloat(i.desc) || 0
|
|
|
|
if (d > 0) descStr += d * i.checkedNum
|
|
|
|
val += parseFloat(i.price) * i.checkedNum
|
|
|
|
}
|
|
|
|
})
|
|
|
|
let itemDesc = parseFloat(item.desc) || 0
|
|
|
|
let itemPrice = parseFloat(item.price)
|
|
|
|
let maxSelect = item.maxNum
|
|
|
|
if (item.accumulationShow != '1' && maxSelect > 1) maxSelect = 1
|
|
|
|
if (inputValue > 0) {
|
|
|
|
if (itemDesc > 0) {
|
|
|
|
let byDesc = Math.floor((inputValue - descStr) / itemDesc)
|
|
|
|
if (byDesc < maxSelect) maxSelect = byDesc
|
|
|
|
} else {
|
|
|
|
if (descStr >= inputValue) {
|
|
|
|
maxSelect = 0
|
|
|
|
} else {
|
|
|
|
let byPrice = Math.floor((inputValue - val) / itemPrice)
|
|
|
|
if (byPrice < maxSelect) maxSelect = byPrice
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (maxSelect < 0) maxSelect = 0
|
|
|
|
return maxSelect
|
|
|
|
},
|
|
|
|
// 根据选择的券张数刷新券的可选状态
|
|
|
|
refreshCouponState() {
|
|
|
|
const list = this.data.couponArr
|
|
|
|
let currentCoupon = '0';
|
|
|
|
let inputValue = parseFloat(this.data.planPrice)
|
|
|
|
let val = 0;
|
|
|
|
let descStr = 0;
|
|
|
|
list.forEach(item => {
|
|
|
|
if (item.checkedNum > 0) {
|
|
|
|
val += parseFloat(item.price) * item.checkedNum
|
|
|
|
let itemDesc = parseFloat(item.desc) || 0
|
|
|
|
if (itemDesc > 0) {
|
|
|
|
descStr += itemDesc * item.checkedNum
|
|
|
|
}
|
|
|
|
if (item.accumulationShow != '1') {
|
|
|
|
currentCoupon = '2'
|
|
|
|
} else {
|
|
|
|
currentCoupon = '1'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (currentCoupon == '0') {
|
|
|
|
list.forEach(item => {
|
|
|
|
if (item.canCheck == "1") {
|
|
|
|
item.showCpupon = '1';
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (currentCoupon == '2') {
|
|
|
|
list.map((item) => {
|
|
|
|
if (item.checkedNum > 0) {
|
|
|
|
item.showCpupon = '1'
|
|
|
|
} else {
|
|
|
|
item.showCpupon = '0'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (currentCoupon == '1') {
|
|
|
|
list.forEach(item => {
|
|
|
|
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)))) {
|
|
|
|
item.showCpupon = '1';
|
|
|
|
} else {
|
|
|
|
item.showCpupon = '0';
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
let finalList = []
|
|
|
|
list.forEach(item => {
|
|
|
|
if (item.checkedNum > 0) {
|
|
|
|
for (let k = 0; k < item.checkedNum; k++) {
|
|
|
|
finalList.push({
|
|
|
|
...item,
|
|
|
|
activity_id: item.activityIds[k]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
this.updateCanAdd()
|
|
|
|
let discountPrice = 0;
|
|
|
|
finalList.forEach(i => {
|
|
|
|
discountPrice += parseFloat(i.price)
|
|
|
|
})
|
|
|
|
discountPrice = discountPrice.toFixed(2)
|
|
|
|
let actualPrice = (inputValue - discountPrice).toFixed(2)
|
|
|
|
if (parseFloat(actualPrice) < 0) actualPrice = '0.00'
|
|
|
|
this.setData({
|
|
|
|
couponArr: list,
|
|
|
|
finalList,
|
|
|
|
discountPrice: discountPrice == '0.00' ? '' : discountPrice,
|
|
|
|
actualPrice,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
// 计算每张券还能不能继续加
|
|
|
|
updateCanAdd() {
|
|
|
|
const list = this.data.couponArr
|
|
|
|
let inputValue = parseFloat(this.data.planPrice)
|
|
|
|
let descStr = 0
|
|
|
|
let val = 0
|
|
|
|
list.forEach(item => {
|
|
|
|
if (item.checkedNum > 0) {
|
|
|
|
let itemDesc = parseFloat(item.desc) || 0
|
|
|
|
let itemPrice = parseFloat(item.price)
|
|
|
|
if (itemDesc > 0) descStr += itemDesc * item.checkedNum
|
|
|
|
val += itemPrice * item.checkedNum
|
|
|
|
}
|
|
|
|
})
|
|
|
|
list.forEach(item => {
|
|
|
|
item.canAdd = '0'
|
|
|
|
let maxSelect = item.accumulationShow == '1' ? item.maxNum : 1
|
|
|
|
if (item.showCpupon == '1' && item.checkedNum < maxSelect) {
|
|
|
|
let itemDesc = parseFloat(item.desc) || 0
|
|
|
|
let itemPrice = parseFloat(item.price)
|
|
|
|
if (itemDesc > 0) {
|
|
|
|
if (descStr + itemDesc <= inputValue) item.canAdd = '1'
|
|
|
|
} else {
|
|
|
|
if (descStr < inputValue && val + itemPrice <= inputValue) item.canAdd = '1'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
// 点确认选择
|
|
|
|
checkCertificate() {
|
|
|
|
this.closeCouponModal()
|
|
|
|
let list = this.data.couponArr;
|
|
|
|
let price = 0;
|
|
|
|
list.forEach(i => {
|
|
|
|
if (i.checkedNum > 0) {
|
|
|
|
price += parseFloat(i.price) * i.checkedNum
|
|
|
|
}
|
|
|
|
})
|
|
|
|
let planPrice = parseFloat(this.data.planPrice) || 0;
|
|
|
|
let actualPrice = (planPrice - price).toFixed(2)
|
|
|
|
if (parseFloat(actualPrice) < 0) actualPrice = '0.00'
|
|
|
|
price = price.toFixed(2)
|
|
|
|
this.setData({
|
|
|
|
couponSwitch: price != '0.00',
|
|
|
|
discountPrice: price == '0.00' ? '' : price,
|
|
|
|
actualPrice,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
// 自动勾选最优惠券组合
|
|
|
|
colationData() {
|
|
|
|
let list = this.data.couponArr;
|
|
|
|
if (!list.length) {
|
|
|
|
this.setData({
|
|
|
|
discountPrice: '',
|
|
|
|
actualPrice: this.data.planPrice || '0.00',
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let obj = this.findBestCoupons(list);
|
|
|
|
this.setData({
|
|
|
|
couponArr: obj.arr,
|
|
|
|
})
|
|
|
|
this.refreshCouponState()
|
|
|
|
},
|
|
|
|
// 最优惠的消费券组合
|
|
|
|
findBestCoupons(arr) {
|
|
|
|
const amount = parseFloat(this.data.planPrice) || 0;
|
|
|
|
const validCoupons = arr.filter(coupon => (coupon.isOk == '1' && parseFloat(coupon.desc) <= amount) && amount >= parseFloat(coupon.price));
|
|
|
|
validCoupons.sort((a, b) => parseFloat(b.price) - parseFloat(a.price));
|
|
|
|
arr.forEach(item => {
|
|
|
|
const isValid = validCoupons.some(validCoupon => validCoupon.activity_id == item.activity_id);
|
|
|
|
item.canCheck = isValid ? '1' : '0';
|
|
|
|
item.checkedNum = 0;
|
|
|
|
item.showCpupon = isValid ? '1' : '0';
|
|
|
|
});
|
|
|
|
let stackingCoupons = validCoupons.filter(i => i.accumulationShow == '1')
|
|
|
|
let list1 = [];
|
|
|
|
let list2 = [];
|
|
|
|
let finalList = [];
|
|
|
|
let selectCount = {};
|
|
|
|
stackingCoupons.sort((a, b) => {
|
|
|
|
if (a.desc === '0' || a.desc === '') return -1;
|
|
|
|
if (b.desc === '0' || b.desc === '') return 1;
|
|
|
|
if (parseFloat(b.price) !== parseFloat(a.price)) return parseFloat(b.price) - parseFloat(a.price);
|
|
|
|
return parseFloat(b.desc) - parseFloat(a.desc);
|
|
|
|
});
|
|
|
|
let thresholdDescSum = 0
|
|
|
|
let maxThresholdValue = 0
|
|
|
|
let thresholdCoupons = stackingCoupons.filter(coupon => (parseFloat(coupon.desc) || 0) > 0)
|
|
|
|
thresholdCoupons.sort((a, b) => {
|
|
|
|
if (parseFloat(b.desc) !== parseFloat(a.desc)) return parseFloat(b.desc) - parseFloat(a.desc)
|
|
|
|
return parseFloat(b.price) - parseFloat(a.price)
|
|
|
|
})
|
|
|
|
thresholdCoupons.forEach(coupon => {
|
|
|
|
let couponDesc = parseFloat(coupon.desc) || 0
|
|
|
|
let couponPrice = parseFloat(coupon.price)
|
|
|
|
let maxCopies = Math.floor((amount - thresholdDescSum) / couponDesc)
|
|
|
|
if (maxCopies > coupon.maxNum) maxCopies = coupon.maxNum
|
|
|
|
if (maxCopies > 0) {
|
|
|
|
selectCount[coupon.aid] = maxCopies
|
|
|
|
thresholdDescSum += couponDesc * maxCopies
|
|
|
|
maxThresholdValue += couponPrice * maxCopies
|
|
|
|
}
|
|
|
|
})
|
|
|
|
list1 = stackingCoupons.filter(i => (selectCount[i.aid] || 0) > 0)
|
|
|
|
let noDescValue = 0
|
|
|
|
let noDescCount = {}
|
|
|
|
stackingCoupons.forEach(coupon => {
|
|
|
|
let couponDesc = parseFloat(coupon.desc) || 0
|
|
|
|
let couponPrice = parseFloat(coupon.price)
|
|
|
|
if (couponDesc === 0 && couponPrice > 0) {
|
|
|
|
let num = Math.floor((amount - noDescValue) / couponPrice)
|
|
|
|
if (num > coupon.maxNum) num = coupon.maxNum
|
|
|
|
if (num > 0) {
|
|
|
|
noDescCount[coupon.aid] = num
|
|
|
|
noDescValue += couponPrice * num
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
let maxStackableValue = maxThresholdValue
|
|
|
|
if (thresholdDescSum < amount) {
|
|
|
|
stackingCoupons.forEach(coupon => {
|
|
|
|
let couponDesc = parseFloat(coupon.desc) || 0
|
|
|
|
let couponPrice = parseFloat(coupon.price)
|
|
|
|
if (couponDesc === 0 && couponPrice > 0) {
|
|
|
|
let num = Math.floor((amount - maxStackableValue) / couponPrice)
|
|
|
|
if (num > coupon.maxNum) num = coupon.maxNum
|
|
|
|
if (num > 0) {
|
|
|
|
selectCount[coupon.aid] = num
|
|
|
|
maxStackableValue += couponPrice * num
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
list1 = stackingCoupons.filter(i => (selectCount[i.aid] || 0) > 0)
|
|
|
|
} else if (noDescValue > maxThresholdValue) {
|
|
|
|
list1 = stackingCoupons.filter(i => (noDescCount[i.aid] || 0) > 0)
|
|
|
|
selectCount = noDescCount
|
|
|
|
maxStackableValue = noDescValue
|
|
|
|
} else if (noDescValue == maxThresholdValue && noDescValue > 0) {
|
|
|
|
let listThreshold = stackingCoupons.filter(i => (selectCount[i.aid] || 0) > 0)
|
|
|
|
let listNoDesc = stackingCoupons.filter(i => (noDescCount[i.aid] || 0) > 0)
|
|
|
|
let min1 = listThreshold.length > 0 ? Math.min(...listThreshold.map(item => item.useEtime)) : Infinity
|
|
|
|
let min2 = listNoDesc.length > 0 ? Math.min(...listNoDesc.map(item => item.useEtime)) : Infinity
|
|
|
|
if (min2 < min1) {
|
|
|
|
list1 = listNoDesc
|
|
|
|
selectCount = noDescCount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let stackList = list1
|
|
|
|
let stackCount = selectCount
|
|
|
|
let maxNonStackableValue = 0
|
|
|
|
validCoupons.forEach(coupon => {
|
|
|
|
if (coupon.accumulationShow != '1') {
|
|
|
|
const discountAmount = parseFloat(coupon.price);
|
|
|
|
const finalAmount = amount - discountAmount;
|
|
|
|
if (finalAmount > 0 || finalAmount == 0) {
|
|
|
|
if (maxNonStackableValue < discountAmount) {
|
|
|
|
maxNonStackableValue = discountAmount
|
|
|
|
list2 = [{ ...coupon }]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (maxStackableValue > maxNonStackableValue) {
|
|
|
|
finalList = stackList
|
|
|
|
} else if (maxStackableValue < maxNonStackableValue) {
|
|
|
|
finalList = list2
|
|
|
|
stackCount = {}
|
|
|
|
if (list2[0]) stackCount[list2[0].aid] = 1
|
|
|
|
} else {
|
|
|
|
let copiedList1 = JSON.parse(JSON.stringify(stackList));
|
|
|
|
let copiedList2 = JSON.parse(JSON.stringify(list2));
|
|
|
|
let minList1 = copiedList1.length > 0 ? Math.min(...copiedList1.map(item => item.useEtime)) : Infinity;
|
|
|
|
let minList2 = copiedList2.length > 0 ? Math.min(...copiedList2.map(item => item.useEtime)) : Infinity;
|
|
|
|
if (minList1 <= minList2) {
|
|
|
|
finalList = stackList
|
|
|
|
} else {
|
|
|
|
finalList = list2
|
|
|
|
stackCount = {}
|
|
|
|
if (list2[0]) stackCount[list2[0].aid] = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let discountPrice = 0;
|
|
|
|
finalList.forEach(coupon => {
|
|
|
|
let target = arr.findIndex(obj => obj.aid === coupon.aid || obj.activity_id === coupon.activity_id);
|
|
|
|
if (target > -1) {
|
|
|
|
arr[target].showCpupon = '1';
|
|
|
|
arr[target].checkedNum = stackCount[coupon.aid] || 1;
|
|
|
|
discountPrice += parseFloat(coupon.price) * (stackCount[coupon.aid] || 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
finalList = []
|
|
|
|
arr.forEach(item => {
|
|
|
|
if (item.checkedNum > 0) {
|
|
|
|
for (let k = 0; k < item.checkedNum; k++) {
|
|
|
|
finalList.push({
|
|
|
|
...item,
|
|
|
|
activity_id: item.activityIds[k]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
discountPrice = discountPrice.toFixed(2)
|
|
|
|
return {
|
|
|
|
arr,
|
|
|
|
discountPrice,
|
|
|
|
finalList,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
// 处理优惠券不可用日期数据
|
|
|
|
hasConsecutiveSelect(arr, type) {
|
|
|
|
let firstTitle = null;
|
|
|
|
let lastTitle = null;
|
|
|
|
let rArr = []
|
|
|
|
let num = 0
|
|
|
|
for (let i = 0; i < arr.length; i++) {
|
|
|
|
let str = type == '1' ? arr[i].selectMor : type == '2' ? arr[i].selectNoon : type == '3' ? arr[i].selectNig : '';
|
|
|
|
if (str) {
|
|
|
|
rArr.push(arr[i].title)
|
|
|
|
num++
|
|
|
|
if (num == 1) {
|
|
|
|
firstTitle = arr[i].title
|
|
|
|
}
|
|
|
|
if (num >= 5) {
|
|
|
|
lastTitle = arr[i].title
|
|
|
|
}
|
|
|
|
if (num >= 5 && i >= (arr.length - 1)) {
|
|
|
|
let fi = rArr.findIndex((item) => {
|
|
|
|
return item == firstTitle
|
|
|
|
})
|
|
|
|
rArr.splice(fi, num, firstTitle + '至' + lastTitle)
|
|
|
|
num = 0
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (num < 5) {
|
|
|
|
num = 0
|
|
|
|
firstTitle = null;
|
|
|
|
lastTitle = null;
|
|
|
|
} else {
|
|
|
|
let fi = rArr.findIndex((item) => {
|
|
|
|
return item == firstTitle
|
|
|
|
})
|
|
|
|
rArr.splice(fi, num, firstTitle + '至' + lastTitle)
|
|
|
|
num = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
rArr,
|
|
|
|
lastTitle,
|
|
|
|
firstTitle
|
|
|
|
};
|
|
|
|
},
|
|
|
|
// 计算免费券有效期的起始、结束日期
|
|
|
|
setTimeRange(cdate, startOffset, endOffset) {
|
|
|
|
const startDate = new Date((cdate + startOffset * 86400) * 1000);
|
|
|
|
startDate.setHours(0, 0, 0, 0);
|
|
|
|
let useStime = startDate.getTime() / 1000
|
|
|
|
const endDate = new Date((cdate + endOffset * 86400) * 1000);
|
|
|
|
endDate.setHours(23, 59, 59, 0);
|
|
|
|
let useEtime = endDate.getTime() / 1000
|
|
|
|
return { useStime, useEtime };
|
|
|
|
},
|
|
|
|
// 支付(暂未接入支付流程,先占位)
|
|
|
|
submitPay() {
|
|
|
|
wx.showToast({
|
|
|
|
title: '支付功能开发中',
|
|
|
|
icon: 'none',
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
onReady() {},
|
|
|
|
onShow() {},
|
|
|
|
onHide() {},
|
|
|
|
onUnload() {},
|
|
|
|
onPullDownRefresh() {},
|
|
|
|
onReachBottom() {},
|
|
|
|
onShareAppMessage() {}
|
|
|
|
}) |
...
|
...
|
|