buyFreeCoupon.js 5.2 KB
// pack-activity/pack/buyFreeCoupon.js
import util from '../../utils/util';
import App from '../../utils/api';
Page({
    /**
     * 页面的初始数据
     */
    data: {
        options: {},
        available: '1', //输入金额够优惠券最低消费1  不够0
        couponArr: [], //所有券列表
        dataStr: Date.now() * 1 / 1000,
    },
    couponModal: '',
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.couponModal = this.selectComponent("#couponModal");
        this.couponModal.showModal()
        this.setData({
            ...options
        })
        if (options.sid) {
            this.getSellerDetail(options.sid)
        }
    },
    // 打开优惠券弹框
    showCouponModal() {
        this.couponModal.showModal()
    },
    // 关闭优惠券弹框
    closeCouponModal() {
        this.couponModal.closeModal()
    },
    // 获取店铺名称
    getSellerDetail(sid) {
        App.Customer.GetSellerDetail({
            sid
        }).then(res => {
            let data = res.data;
            this.setData({
                seller: {
                    seller_image: data.seller_image,
                    sellername: data.sellername

                }
            })
            this.getCoupon()
        })
    },
    // 点确认选择
    checkCertificate() {
        console.log('1111111111');
        this.closeCouponModal()
    },
    // 获取所有优惠券列表
    getCoupon() {
        App.Activity.ActivityListByOpenid().then(res => {
            if (res.ret == 200 && res.data && res.data.length > 0) {
                let list = res.data.filter(item => {
                    return this.data.dataStr < (item.etime * 1) && item.is_open == '1'
                })
                list.forEach(detail => {
                    detail.stringifyUse = detail.use ? JSON.stringify(detail.use) : '';
                    detail.exp_date = detail.exp_date ? util.determine(detail.exp_date) : '';
                    detail.arule = detail.arule ? util.determine(detail.arule) : '';
                    if (detail.exp_date && detail.exp_date.week_data) {
                        let noon = this.hasConsecutiveSelect(detail.exp_date.week_data, '1').rArr.join('、')
                        let nig = this.hasConsecutiveSelect(detail.exp_date.week_data, '2').rArr.join('、')
                        if (noon) noon = '午餐:' + noon
                        if (nig) nig = '晚餐:' + nig
                        detail.noonText = noon;
                        detail.nigText = nig;
                    }
                    detail.checked = false;
                    detail.endDateStr = util.unixtoTime(detail.stime, 'YYYY-MM-DD') + '至' + util.unixtoTime(detail.etime, 'YYYY-MM-DD')
                })
                console.log(list);

                let couponArr = list
                this.setData({
                    couponArr
                })
            }
        })
    },
    checkboxChange(e) {
        console.log('checkbox发生change事件,携带value值为:', e.detail.value)
    },
    // 输入金额
    bindKeyInput(e) {
        this.setData({
            inputValue: e.detail.value
        })
    },
    // 处理优惠券不可用日期数据
    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].selectNoon : 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
        };
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})