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

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

var order = {};

var payWay,
    deliveryWay;

require('./order-new/address');
require('./order-new/invoice');

// 支付方式
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')});
        });
    },
    updateOrder: function(info) {
        if (!info) {
            return;
        }

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

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

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