receiveCoupon.js 7.09 KB
// pack-activity/pack/receiveCoupon.js
import util from '../../utils/util';
import App from '../../utils/api';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        opt: {},
        dataStr: Date.now() * 1 / 1000,
        detail: {},
        exp_date: {},
        resultCoupon: '', //0未领取 1已领取
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log('options', options);
        var obj = {};
        if (options.scene) {
            let scene = decodeURIComponent(options.scene);
            console.log('scene', scene);
            for (var i = 0; i < scene.split('&').length; i++) {
                var arr = scene.split('&')[i].split('=');
                obj[arr[0]] = arr[1];
            }
        }
        console.log('obj', {
            ...obj
        });
        this.setData({
            opt: {
                id: options.id || obj.id,
                sid: options.sid || obj.sid,
                oid: options.oid,
                types: options.types || obj.types, //1是邀请函过来的(邀请函的券或者点击意见反馈买券)  2是提前点餐  3是商家APP分享的或者短信的链接
                rid: options.rid ? options.rid : '',
            }
        })
        console.log('opt', {
            ...this.data.opt
        });
    },
    // 购买完成券之后关注公众号
    wananchi() {
        wx.navigateTo({
            url: '/pages/me/wananchi'
        })
    },
    // 提交订单
    submitOrder: util.throttle(function () {
        if (this.data.dataStr > (this.data.detail.etime * 1)) {
            return wx.showToast({
                title: '优惠券已过期,不可购买!',
                duration: 2000,
                mask: true,
                icon: 'none'
            })
        }
        // is_type  1店铺优惠券  需要判断优惠券剩余数量  2个人优惠券不需要判断数量  3免费券 4付费券
        if (this.data.detail.grant_last == '0' && this.data.detail.is_type == '3') {
            return wx.showToast({
                title: '您来晚了,当前优惠券已经售罄,请与商家联系!',
                duration: 2000,
                mask: true,
                icon: 'none'
            })
        }
        let detail = this.data.detail
        detail.name = this.data.sellername;
        detail.oid = this.data.opt.oid;
        App.Activity.SetActivityAsReserve({
            aid: this.data.opt.id,
            rid: this.data.opt.oid,
        }).then((json) => {
            if (json.ret == 200) {
                util.toast('领取成功', 'none');
            } else {
                wx.showToast({
                    title: res.msg,
                    duration: 2000,
                    mask: true
                })
            }
        })
    }, 3000),
    // 获取券信息
    getDetail() {
        App.Activity.GetActivityById({
            id: this.data.opt.id
        }).then(res => {
            if (res.data.length > 0) {
                let detail = res.data[0];
                let exp_date = util.determine(detail.exp_date);
                detail.arule = detail.arule ? util.determine(detail.arule) : '';
                if (exp_date && exp_date.week_data) {
                    let noon = this.hasConsecutiveSelect(exp_date.week_data, '1').rArr.join('、')
                    let nig = this.hasConsecutiveSelect(exp_date.week_data, '2').rArr.join('、')
                    if (noon) noon = '午餐:' + noon
                    if (nig) nig = '晚餐:' + nig
                    detail.noonText = noon;
                    detail.nigText = nig;
                }
                if (detail.arule && detail.arule.is_share_lose == '0') {
                    wx.hideShareMenu()
                }
                this.setData({
                    detail,
                    exp_date,
                })
            }
        })
    },
    // 获取店铺名称
    getSellerDetail() {
        App.Customer.GetSellerDetail({
            sid: this.data.opt.sid
        }).then(res => {
            this.setData({
                sellername: res.data.sellername
            })
        })
    },
    // 处理优惠券不可用日期数据
    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
        };
    },
    sharesetting() {
        let data = this.data.opt;
        let detail = this.data.detail;
        return {
            title: `送你${this.data.sellername}${detail.price}元代金券,点击查看详情,领取到卡包吧!`,
            path: `/pack-activity/pack/receiveCoupon?id=${data.id}&oid=${data.oid}&rid=${data.rid}&sid=${data.sid}&types=${data.types}`,
            imageUrl: 'https://nosqn.colorcun.com/share_money.png',
        }
    },
    // 是否已领取
    getCoupon() {
        App.Activity.ActivityListByOpenid({
            aid: this.data.opt.id,
            sellerid:this.data.opt.sid
        }).then(res => {
            if (res.ret === 200) {
                this.setData({
                    resultCoupon: res.data.length > 0 ? '1' : '0',
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        this.getDetail();
        this.getSellerDetail();
        this.getCoupon()
    },

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

    },

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

    },

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

    },

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

    },

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

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {
        return this.sharesetting();
    }
})