ensure.page.js 3.65 KB
/**
 * 订单结算页
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2016/11/30
 */

var $ = require('yoho-jquery');

var order = {};

var payWay,
    deliveryWay;

// 支付方式
payWay = {
    $payType: $('.pay-wrap .check-btn'),
    init: function() {
        var that = this;

        this.payType = {};
        this.$payType.each(function() {
            var $this = $(this),
                data = $this.data();

            that.payType[data.id] = {
                dom: $this,
                id: data.id,
                type: data.type
            };

            // 更新订单支付数据
            if ($this.hasClass('checked')) {
                that.updateOrder(data);
            }
        });
        this.eventBind();
    },
    eventBind: function() {
        var that = this;

        this.$payType.click(function() {
            var $this = $(this);

            if ($this.hasClass('checked')) {
                return;
            }

            that.$payType.removeClass('checked');
            $this.addClass('checked');

            // 更新订单支付数据
            that.updateOrder($this.data());
        });
    },
    updateOrder: function(info) {
        if (!info) {
            return;
        }

        if (info.id) {
            order.paymentId = info.id;
        }

        if (info.type) {
            order.paymentType = info.type;
        }
    }
};

deliveryWay = {
    $deliveryType: $('.delivery-way-wrap .check-btn'),
    $deliveryTime: $('.delivery-time-wrap .check-btn'),
    init: function() {
        var that = this;

        this.deliveryType = {};
        this.$deliveryType.each(function() {
            var $this = $(this),
                data = $this.data();

            that.deliveryType[data.id] = {
                dom: $this,
                id: data.id
            };

            // 更新订单配送方式数据
            if ($this.hasClass('checked')) {
                that.updateOrder({way: data.id});
            }
        });

        this.$deliveryTime.each(function() {
            var $this = $(this);

            // 更新订单送货时间数据
            if ($this.hasClass('checked')) {
                that.updateOrder({time: $this.data('id')});
            }
        });
        this.eventBind();
    },
    eventBind: function() {
        var that = this;

        this.$deliveryType.click(function() {
            var $this = $(this);

            if ($this.hasClass('checked')) {
                return;
            }
            that.$deliveryType.removeClass('checked');
            $this.addClass('checked');

            // 更新订单配送方式数据
            that.updateOrder({way: $this.data('id')});
        });

        this.$deliveryTime.click(function() {
            var $this = $(this);

            if ($this.hasClass('checked')) {
                return;
            }
            that.$deliveryTime.removeClass('checked');
            $this.addClass('checked');

            // 更新订单送货时间数据
            that.updateOrder({time: $this.data('id')});
        });

        // 仅前端展示,该数据无作用,接口不接受此参数
        $('.delivery-time-wrap .radio-btn').click(function() {
            var $this = $(this);

            if ($this.hasClass('on')) {
                return;
            }
            $this.siblings('.on').removeClass('on');
            $this.addClass('on');
        });
    },
    updateOrder: function(info) {
        if (!info) {
            return;
        }

        if (info.time) {
            order.deliveryTime = info.time;
        }

        if (info.way) {
            order.deliveryWay = info.way;
        }
    }
};

payWay.init();
deliveryWay.init();