Blame view

public/js/cart/cartbuynow/select-coupons.js 6.5 KB
郭成尧 authored
1
import $ from 'yoho-jquery';
郭成尧 authored
2
import qs from 'yoho-qs';
郭成尧 authored
3
import Page from 'yoho-page';
郭成尧 authored
4
import tip from 'plugin/tip';
郭成尧 authored
5 6
import cookie from 'yoho-cookie';
import cookieOption from './cookie-option';
郭成尧 authored
7 8

class SelectCouponController extends Page {
郭成尧 authored
9
    constructor(params) {
郭成尧 authored
10
        super();
郭成尧 authored
11
郭成尧 authored
12
        this.linkUrl = document.referrer;
郭成尧 authored
13 14 15
        this.orderInfo = params.orderInfo;
        this.couponListUrl = params.couponListUrl;
        this.useCouponCodeUrl = params.useCouponCodeUrl;
郭成尧 authored
16
        this.selectCouponCodes = [];
郭成尧 authored
17
        this.isBuyNowPage = params.isBuyNowPage || false;
郭成尧 authored
18
        this.closeCouponRuleTipbyUser = cookie.get('close_coupon_rule_tip_by_user') || 'N';
郭成尧 authored
19
郭成尧 authored
20 21
        if (this.orderInfo('coupon_code')) {
            this.selectCouponCodes = this.orderInfo('coupon_code').split(',');
郭成尧 authored
22 23
        }
郭成尧 authored
24 25 26 27 28 29 30 31 32
        this.page = $('.select-coupons-page');
        this.renderPage();
    }

    /**
     * 渲染页面
     */
    renderPage() {
        let couponCodeStr = this.selectCouponCodes.length > 0 ? this.selectCouponCodes.join(',') : '';
郭成尧 authored
33 34 35 36 37 38 39 40 41
        let postData = {
            delivery_way: this.orderInfo('deliveryId'),
            coupon_code: couponCodeStr
        };

        if (this.isBuyNowPage) {
            postData.product_sku = qs.product_sku;
            postData.buy_number = qs.buy_number;
        }
郭成尧 authored
42 43 44

        this.ajax({
            type: 'POST',
郭成尧 authored
45 46
            url: this.couponListUrl,
            data: postData
郭成尧 authored
47
        }).then(result => {
郭成尧 authored
48
            this.orderInfo('coupon_code', couponCodeStr);
郭成尧 authored
49
郭成尧 authored
50 51 52 53 54 55 56 57 58 59 60
            let resultHtml = $(result);

            this.bindEvents(resultHtml);
            this.page.html(resultHtml);
        });
    }

    /**
     * 事件绑定处理
     */
    bindEvents(resultHtml) {
郭成尧 authored
61
        this.view = {
郭成尧 authored
62 63 64 65 66 67 68
            filterBtn: resultHtml.find('.filter-btn'),
            coupon: resultHtml.find('.coupon'),
            checkbox: resultHtml.find('.checkbox'),
            tipBox: resultHtml.find('.tip-box'),
            tipClose: resultHtml.find('.tip-close'),
            usableCouponList: resultHtml.find('.usable'),
            unusableCouponList: resultHtml.find('.unusable'),
郭成尧 authored
69 70
            useNowBtn: resultHtml.find('#useNowBtn'),
            exchangeCouponBtn: resultHtml.find('#exchangeCouponBtn'),
郭成尧 authored
71
            couponCodeInput: resultHtml.find('input[name=couponCodeInput]'),
郭成尧 authored
72 73
            exchangeBox: resultHtml.find('.exchange-box'),
            useNowBox: resultHtml.find('#useNowBox')
郭成尧 authored
74 75
        };
郭成尧 authored
76
        this.view.filterBtn.on('click', this.tabChange.bind(this));
郭成尧 authored
77 78
        this.view.coupon.on('click', '.checkbox', this.check.bind(this));
        this.view.tipClose.on('click', this.closeTip.bind(this));
郭成尧 authored
79
        this.view.useNowBtn.on('click', this.useCoupon.bind(this));
郭成尧 authored
80 81
        this.view.exchangeCouponBtn.on('click', this.exchangeCoupon.bind(this));
        this.view.couponCodeInput.on('input', this.changeExchangeBtnStatus.bind(this));
郭成尧 authored
82 83 84 85

        if (this.closeCouponRuleTipbyUser === 'Y') {
            this.view.tipBox.addClass('hide');
        }
郭成尧 authored
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    }

    /**
     * 改变兑换按钮状态
     */
    changeExchangeBtnStatus() {
        if (this.view.couponCodeInput.val().length > 0) {
            this.view.exchangeCouponBtn.addClass('active');
        } else {
            this.view.exchangeCouponBtn.removeClass('active');
        }
    }

    /**
     * 使用优惠券码
     */
    exchangeCoupon() {
        let couponCode = this.view.couponCodeInput.val();

        if (!couponCode) {
            tip.show('请输入优惠券码');
            return false;
        }

        this.ajax({
            method: 'POST',
郭成尧 authored
112
            url: this.useCouponCodeUrl,
郭成尧 authored
113 114 115
            data: {
                couponCode: couponCode
            }
郭成尧 authored
116
        }).then(res => {
郭成尧 authored
117 118 119 120
            if (res.message) {
                tip.show(res.message);
            }
            if (res.code === 200) {
郭成尧 authored
121 122
                this.selectCouponCodes.push(couponCode);
                setTimeout(() => {
郭成尧 authored
123
                    location.reload();
郭成尧 authored
124
                }, 500);
郭成尧 authored
125 126
            }
        });
郭成尧 authored
127 128 129 130 131 132
    }

    /**
     * 使用优惠券
     */
    useCoupon() {
郭成尧 authored
133 134 135 136 137
        if (this.linkUrl) {
            window.location.href = this.linkUrl;
        } else {
            history.go(-1);
        }
郭成尧 authored
138 139 140
    }

    /**
郭成尧 authored
141 142
     * tab 切换
     */
郭成尧 authored
143
    tabChange(event) {
郭成尧 authored
144 145
        let itemClicked = $(event.currentTarget);
郭成尧 authored
146
        // Tab 按钮状态
郭成尧 authored
147
        if (!itemClicked.hasClass('active')) {
郭成尧 authored
148 149 150
            this.view.filterBtn.removeClass('active');
            itemClicked.addClass('active');
        }
郭成尧 authored
151
郭成尧 authored
152
        // 优惠券列表切换,兑换模块、立即使用模块显示控制
郭成尧 authored
153 154 155
        if (itemClicked.hasClass('valid')) {
            this.view.unusableCouponList.addClass('hide');
            this.view.usableCouponList.removeClass('hide');
郭成尧 authored
156
            this.view.exchangeBox.removeClass('hide');
郭成尧 authored
157 158 159 160 161 162

            if (itemClicked.data('num') === 0) {
                this.view.useNowBox.addClass('hide');
            } else {
                this.view.useNowBox.removeClass('hide');
            }
郭成尧 authored
163 164 165
        } else {
            this.view.usableCouponList.addClass('hide');
            this.view.unusableCouponList.removeClass('hide');
郭成尧 authored
166
            this.view.exchangeBox.addClass('hide');
郭成尧 authored
167
            this.view.useNowBox.addClass('hide');
郭成尧 authored
168
        }
郭成尧 authored
169
郭成尧 authored
170
        // Tip 控制
郭成尧 authored
171
        if (this.closeCouponRuleTipbyUser === 'Y' || itemClicked.hasClass('invalid')) {
郭成尧 authored
172
            this.view.tipBox.addClass('hide');
郭成尧 authored
173 174 175 176 177 178
        } else {
            if (itemClicked.data('num') === 0) {
                this.view.tipBox.addClass('hide');
            } else {
                this.view.tipBox.removeClass('hide');
            }
郭成尧 authored
179
        }
郭成尧 authored
180 181 182
    }

    /**
郭成尧 authored
183 184 185
     * 优惠券勾选处理
     */
    check(e) {
郭成尧 authored
186 187 188
        let theCouponCheck = $(e.currentTarget);
        let theCoupon = $(e.delegateTarget);
        let theCouponData = theCoupon.data();
郭成尧 authored
189
郭成尧 authored
190 191 192
        if (!this.orderInfo('user_check_coupon')) {
            this.orderInfo('user_check_coupon', 'Y');
        }
郭成尧 authored
193 194 195 196

        if (theCouponCheck.hasClass('icon-cb-radio')) {
            this.selectCouponCodes.splice($.inArray(theCouponData.code, this.selectCouponCodes), 1);
            theCouponCheck.removeClass('icon-cb-radio').addClass('icon-radio');
郭成尧 authored
197
        } else {
郭成尧 authored
198 199
            this.selectCouponCodes.push(theCouponData.code);
            theCouponCheck.removeClass('icon-radio').addClass('icon-cb-radio');
郭成尧 authored
200
        }
郭成尧 authored
201 202

        this.renderPage();
郭成尧 authored
203 204 205 206 207 208
    }

    /**
     * 关闭提示框
     */
    closeTip() {
郭成尧 authored
209 210
        this.closeCouponRuleTipbyUser = 'Y';
        cookie.set('close_coupon_rule_tip_by_user', 'Y', cookieOption);
郭成尧 authored
211
        this.view.tipBox.hide();
郭成尧 authored
212 213 214 215
    }
}

export default SelectCouponController;