...
|
...
|
@@ -5,14 +5,105 @@ |
|
|
*/
|
|
|
|
|
|
var $ = require('yoho-jquery');
|
|
|
var Hbs = require('yoho-handlebars');
|
|
|
|
|
|
var order = {};
|
|
|
var $balanceDetail = $('#balance-detail'),
|
|
|
$orderPrice = $('#order-price');
|
|
|
|
|
|
var order = {
|
|
|
printPrice: 'Y'
|
|
|
},
|
|
|
iconCode = {
|
|
|
on: '',
|
|
|
off: ''
|
|
|
};
|
|
|
|
|
|
var payWay,
|
|
|
deliveryWay;
|
|
|
deliveryWay,
|
|
|
coupon,
|
|
|
yohoCoin;
|
|
|
var address = require('./order-new/address'),
|
|
|
invoice = require('./order-new/invoice');
|
|
|
|
|
|
var coinTpl = Hbs.compile($('#yoho-coin-tpl').html());
|
|
|
var promotionTpl = Hbs.compile($('#promotion-list-tpl').html());
|
|
|
|
|
|
require('../common');
|
|
|
|
|
|
order.cartType = window.queryString().type || 1;
|
|
|
|
|
|
// FE Helper
|
|
|
Hbs.registerHelper('round', function(num, precision) {
|
|
|
precision = parseFloat('0' + precision, 10) || 2;
|
|
|
|
|
|
return (+num).toFixed(precision);
|
|
|
});
|
|
|
|
|
|
// 订单计算
|
|
|
// 传有货币数量则使用有货币,否者不使用,更改订单总价有货币使用数量重置为0
|
|
|
function compute(coin) {
|
|
|
var reqData = {
|
|
|
cartType: order.cartType,
|
|
|
coin: coin ? coin : 0
|
|
|
};
|
|
|
|
|
|
require('./order-new/address');
|
|
|
require('./order-new/invoice');
|
|
|
order.coin = reqData.coin;
|
|
|
|
|
|
// 支付方式
|
|
|
if (order.paymentType) {
|
|
|
reqData.paymentType = order.paymentType;
|
|
|
}
|
|
|
|
|
|
// 配送方式
|
|
|
if (order.deliveryWay) {
|
|
|
reqData.deliveryWay = order.deliveryWay;
|
|
|
}
|
|
|
|
|
|
// 优惠券
|
|
|
if (order.couponCode) {
|
|
|
reqData.couponCode = order.couponCode;
|
|
|
}
|
|
|
|
|
|
// 红包
|
|
|
if (order.redEnvelopes) {
|
|
|
reqData.redEnvelopes = order.redEnvelopes;
|
|
|
}
|
|
|
|
|
|
$.ajax({
|
|
|
type: 'POST',
|
|
|
url: '/cart/ensure/compute',
|
|
|
data: reqData
|
|
|
}).then(function(data) {
|
|
|
var res;
|
|
|
|
|
|
if (data.code === 200) {
|
|
|
res = data.data;
|
|
|
|
|
|
// update yoho coin max use num
|
|
|
yohoCoin.maxCoin = res.canUseCoinNum;
|
|
|
|
|
|
// update yoho coin using
|
|
|
yohoCoin.$el.html(coinTpl(res));
|
|
|
if (!reqData.coin) {
|
|
|
yohoCoin.$el.removeClass('used');
|
|
|
}
|
|
|
|
|
|
// update last order amount
|
|
|
$orderPrice.html('¥ ' + res.last_order_amount);
|
|
|
|
|
|
// update promotion formula list
|
|
|
$balanceDetail.html(promotionTpl(res));
|
|
|
} else if (data.code === 317) {
|
|
|
if (coupon.$errorTip) {
|
|
|
coupon.$errorTip.text(data.message);
|
|
|
|
|
|
if (order.couponCode) {
|
|
|
delete order.couponCode;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
// 支付方式
|
|
|
payWay = {
|
...
|
...
|
@@ -53,6 +144,9 @@ payWay = { |
|
|
|
|
|
// 更新订单支付数据
|
|
|
that.updateOrder($this.data());
|
|
|
|
|
|
// 重新计算订单价格
|
|
|
compute();
|
|
|
});
|
|
|
},
|
|
|
updateOrder: function(info) {
|
...
|
...
|
@@ -116,6 +210,9 @@ deliveryWay = { |
|
|
|
|
|
// 更新订单配送方式数据
|
|
|
that.updateOrder({way: $this.data('id')});
|
|
|
|
|
|
// 重新计算订单价格
|
|
|
compute();
|
|
|
});
|
|
|
|
|
|
this.$deliveryTime.click(function() {
|
...
|
...
|
@@ -146,5 +243,239 @@ deliveryWay = { |
|
|
}
|
|
|
};
|
|
|
|
|
|
coupon = {
|
|
|
$el: $('#use-coupons'),
|
|
|
init: function() {
|
|
|
if (!this.$el.length) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
this.$couponWrap = this.$el.next();
|
|
|
|
|
|
this.$errorTip = $('.coupon-err-tip', this.$el);
|
|
|
this.$ticketUseTip = $('.ticket-use-tip', this.$couponWrap);
|
|
|
|
|
|
this.$couponCode = $('.coupon-code', this.$couponWrap);
|
|
|
this.$couponList = $('.coupon-list-wrap', this.$couponWrap);
|
|
|
|
|
|
this.couponsTpl = Hbs.compile($('#coupons-tpl').html());
|
|
|
|
|
|
this.getList();
|
|
|
this.eventBind();
|
|
|
},
|
|
|
getList: function() {
|
|
|
var that = this;
|
|
|
|
|
|
$.ajax({
|
|
|
type: 'GET',
|
|
|
url: '/cart/ensure/coupons'
|
|
|
}).then(function(data) {
|
|
|
if (data.code === 200) {
|
|
|
$('.coupon-list-wrap', that.$couponWrap).html(that.couponsTpl(data.data));
|
|
|
that.$radios = that.$couponWrap.find('.coupon-radio');
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
eventBind: function() {
|
|
|
var that = this;
|
|
|
|
|
|
this.$couponWrap.on('click', '.radio-btn', function() {
|
|
|
var $this = $(this);
|
|
|
|
|
|
if ($this.hasClass('on')) {
|
|
|
return;
|
|
|
}
|
|
|
$this.siblings('.radio-btn.on').removeClass('on');
|
|
|
$this.addClass('on').parent().toggleClass('using-code');
|
|
|
|
|
|
if ($this.hasClass('code')) {
|
|
|
that.cancelTicketUse();
|
|
|
that.$couponList.slideUp();
|
|
|
} else {
|
|
|
that.cancelCodeUse();
|
|
|
that.$couponList.slideDown();
|
|
|
}
|
|
|
}).on('click', '.cancel-use-ticket', function() {
|
|
|
that.cancelTicketUse();
|
|
|
}).on('change', '.coupon-code', function() {
|
|
|
that.$errorTip.empty();
|
|
|
}).on('click', '.opt-area > label', function() {
|
|
|
var $this = $(this);
|
|
|
var couponId, data;
|
|
|
|
|
|
if ($this.hasClass('sure-use-ticket')) {
|
|
|
data = that.$radios.filter('.on').data();
|
|
|
|
|
|
if (data) {
|
|
|
that.$ticketUseTip.find('.price').text('- ¥' + data.price);
|
|
|
that.$ticketUseTip.removeClass('hide');
|
|
|
couponId = data.id;
|
|
|
}
|
|
|
} else {
|
|
|
couponId = $this.siblings('.coupon-code').val();
|
|
|
}
|
|
|
|
|
|
if (couponId) {
|
|
|
order.couponCode = couponId;
|
|
|
|
|
|
// 重新计算订单价格
|
|
|
compute();
|
|
|
}
|
|
|
}).on('click', '.coupon-radio', function() {
|
|
|
var $this = $(this);
|
|
|
|
|
|
if ($this.hasClass('on')) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
if (that.$radios) {
|
|
|
that.$radios.filter('.on').removeClass('on');
|
|
|
}
|
|
|
$this.addClass('on');
|
|
|
});
|
|
|
},
|
|
|
cancelTicketUse: function() {
|
|
|
if (order.couponCode) {
|
|
|
this.$ticketUseTip.addClass('hide');
|
|
|
this.$radios.filter('.on').removeClass('on');
|
|
|
|
|
|
delete order.couponCode;
|
|
|
compute(); // 重新计算订单价格
|
|
|
}
|
|
|
},
|
|
|
cancelCodeUse: function() {
|
|
|
this.$errorTip.empty();
|
|
|
this.$couponCode.val('');
|
|
|
|
|
|
if (order.couponCode) {
|
|
|
delete order.couponCode;
|
|
|
compute(); // 重新计算订单价格
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
|
|
yohoCoin = {
|
|
|
$el: $('#yoho-coin-box'),
|
|
|
init: function() {
|
|
|
var data;
|
|
|
|
|
|
if (!this.$el.length) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
data = this.$el.data();
|
|
|
|
|
|
if (data) {
|
|
|
order.coin = data.coin || 0;
|
|
|
this.maxCoin = data.max;
|
|
|
}
|
|
|
|
|
|
this.eventBind();
|
|
|
},
|
|
|
eventBind: function() {
|
|
|
var that = this;
|
|
|
|
|
|
this.$el.on('click', '.coin-use-btn', function() {
|
|
|
if (order.coin * 1 === this.maxCoin * 1) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
compute(that.maxCoin);
|
|
|
}).on('click', '.coin-cancel-btn', function() {
|
|
|
if (order.coin * 1 === 0) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
compute();
|
|
|
});
|
|
|
}
|
|
|
};
|
|
|
|
|
|
$('.locker-switch').click(function() {
|
|
|
var $this = $(this),
|
|
|
$par = $this.parent();
|
|
|
|
|
|
$par.toggleClass('open');
|
|
|
|
|
|
if ($par.hasClass('open')) {
|
|
|
$this.html(iconCode.on);
|
|
|
} else {
|
|
|
$this.html(iconCode.off);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
$('#red-packet-box').on('click', '.radio-btn', function() {
|
|
|
$(this).toggleClass('on');
|
|
|
}).on('click', '.sure-btn', function() {
|
|
|
var $this = $(this),
|
|
|
num = $this.data('num');
|
|
|
|
|
|
if (!num) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
if ($this.siblings('.on').length) {
|
|
|
order.redEnvelopes = num;
|
|
|
} else if (order.redEnvelopes) {
|
|
|
delete order.redEnvelopes;
|
|
|
}
|
|
|
compute(); // 重新计算订单价格
|
|
|
});
|
|
|
|
|
|
$('#remark-box').on('keyup', '.note-text', function() {
|
|
|
var $this = $(this);
|
|
|
var val = $.trim($this.val());
|
|
|
|
|
|
if (val) {
|
|
|
$this.addClass('has-text');
|
|
|
|
|
|
if (val.length > 100) {
|
|
|
val = val.substring(0, 99);
|
|
|
$this.val(val);
|
|
|
}
|
|
|
|
|
|
order.remark = val;
|
|
|
} else {
|
|
|
$this.removeClass('has-text');
|
|
|
}
|
|
|
}).on('click', '.radio-btn', function() {
|
|
|
var $this = $(this);
|
|
|
|
|
|
if ($this.hasClass('on')) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
if ($this.hasClass('unprint')) {
|
|
|
order.printPrice = 'N';
|
|
|
} else {
|
|
|
order.printPrice = 'Y';
|
|
|
}
|
|
|
|
|
|
$this.siblings('.on').removeClass('on');
|
|
|
$this.addClass('on');
|
|
|
});
|
|
|
|
|
|
$('#order-submit').click(function() {
|
|
|
var invoiceInfo = invoice.getInvoice();
|
|
|
|
|
|
order.addressId = address.getAddress();
|
|
|
|
|
|
// 发票信息
|
|
|
if (invoiceInfo) {
|
|
|
$.extend(order, invoiceInfo);
|
|
|
}
|
|
|
$.ajax({
|
|
|
type: 'POST',
|
|
|
url: '/cart/ensure/submit',
|
|
|
data: order
|
|
|
}).then(function(data) {
|
|
|
if (data.code === 200) {
|
|
|
location.href = data.data.url;
|
|
|
}
|
|
|
});
|
|
|
});
|
|
|
|
|
|
payWay.init();
|
|
|
deliveryWay.init();
|
|
|
coupon.init();
|
|
|
yohoCoin.init(); |
...
|
...
|
|