order.page.js
5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
* [购物流程]结算页
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2016/7/8
*/
var $ = require('yoho-jquery'),
lazyLoad = require('yoho-jquery-lazyload');
var Alert = require('../plugins/dialog').Alert;
var minusPlus = {
minus: '',
plus: ''
};
var $printPrice = $('#print-price');
var $invoice = $('#invoice-content');
var $coinRemarkContent = $('.coin-and-remark .coin .content');
var balanceTpl = require('../../tpl/shopping/balance.hbs'),
yohoCoinTpl = require('../../tpl/shopping/yoho-coin.hbs');
var pkgCache = {};
var suredCoin = 0;
var $pkgList;
require('yoho-jquery-placeholder');
require('yoho-jquery-dotdotdot');
require('../plugins/check'); // before 地址和发票
require('./order/address'); // 地址
require('./order/invoice'); // 发票
lazyLoad($('img.lazy'));
// IE8 placeholder
$('[placeholder]').placeholder();
// dot
$('.brand-and-name .name').dotdotdot({
wrap: 'letter'
});
function pkgPageControl($el, flag) {
var id = $el.index(),
$ul = $el.find('.package-goods'),
theCache = pkgCache[id],
curPage = theCache.cur ? theCache.cur : 1,
page = Math.ceil(theCache.total / 5);
curPage += flag;
// 第一页或最后一页
if (curPage < 1 || curPage > page) {
return;
}
$ul.animate({
marginLeft: -(curPage - 1) * $ul.parent('.package-goods-wrap').width()
}, 200);
theCache.cur = curPage;
}
// JIT拆单
if ($('.multi-package-row').length > 0) {
$pkgList = $('.package-list');
// 显示拆单详情面板
$('.show-package').click(function(e) {
$pkgList.toggleClass('hide');
e.stopPropagation();
});
// 绑定document事件,去触发面板关闭
$(document).on('click', function(e) {
if ($pkgList.hasClass('hide') || $(e.target).closest('.package-list').length > 0) {
return;
}
$pkgList.addClass('hide');
});
// 设置左右切换
$('.package-goods').each(function() {
$(this).width($(this).children('li').length * 120);
});
// 初始化cache
$('.package-item').each(function() {
pkgCache[$(this).index()] = {
total: $(this).find('li').length
};
});
$('.package-list').on('click', '.toggle-icon', function() {
var $this = $(this);
var flag = $this.hasClass('left-icon') ? -1 : 1;
pkgPageControl($this.closest('.package-item'), flag);
});
}
// 有货币、备注切换显示
$('.coin-ctrl, .remark-ctrl').click(function() {
var $this = $(this),
$icon = $this.hasClass('iconfont') ? $this : $this.siblings('.iconfont');
if ($icon.hasClass('minus')) {
// hide panel
$icon.html(minusPlus.plus).removeClass('minus');
} else {
// show panel
$icon.html(minusPlus.minus).addClass('minus');
}
$this.parent('.title').siblings('.content').toggleClass('hide');
});
function compute(coin) {
$.ajax({
type: 'POST',
url: '/shopping/order/compute',
data: {
coin: coin
}
}).then(function(data) {
var cost;
if (data.code === 200) {
cost = data.data.last_order_amount;
cost = cost.toFixed(2);
$('#balance-list').html(balanceTpl(data.data));
$coinRemarkContent.html(yohoCoinTpl(data.data));
}
});
}
// 获取已使用的有货币
function getCoinUsed() {
var coin = $('.coin-and-remark').find('#coin-sure').data('yoho_coin_num') || 0;
return coin === '' ? 0 : parseInt(coin, 10); // 使用parseInt可以排除异常情况的转化影响
}
// 添加备注-是否打印价格
$printPrice.printPrice = 'N'; // 默认为false
$('.print-price-radio').check({
type: 'radio',
group: 'print-price',
onChange: function(el, checked, value) {
if (checked) {
$printPrice.printPrice = value === 1 ? 'Y' : 'N';
}
}
});
// 订单提交
$('#balance-list').on('click', '#submit-order', function() {
var reqParam = {
address_id: $('.address.focus').data('id'),
use_yoho_coin: suredCoin,
remark: $('#remark-content').val(),
isPrintPrice: $printPrice.printPrice
};
var $invoiceDetail;
// 发票信息
if ($invoice.find('.checked').length > 0) {
$invoiceDetail = $invoice.find('.invoice-detail');
$.extend(reqParam, {
invoices_type: $invoiceDetail.data('type'),
invoices_mobile: $invoiceDetail.data('mobile'),
invoices_title: $invoiceDetail.data('title'),
invoice_content: $invoiceDetail.data('content')
});
}
$.ajax({
type: 'POST',
url: '/shopping/order/submit',
data: reqParam
}).then(function(data) {
if (data.code === 200) {
location.href = data.data.payUrl;
} else {
new Alert(data.message).show();
}
});
});
$coinRemarkContent.on('mouseover mouseout', '.yoho-coin-help', function(event) {
if (event.type === 'mouseover') {
$('.coin-help-dialog').removeClass('hide');
} else if (event.type === 'mouseout') {
$('.coin-help-dialog').addClass('hide');
}
}).on('click', '#coin-sure', function() { // 确定有货币按钮
if ($(this).hasClass('disable')) {
return;
}
suredCoin = getCoinUsed();
// 切换显示
compute(suredCoin);
// 隐藏有货币面板并重置子面板显示
$('.coin-ctrl.iconfont').trigger('click');
}).on('click', '.cancel', function() { // 取消有货币按钮
suredCoin = 0;
compute(suredCoin);
// 隐藏有货币面板并重置子面板显示
$('.coin-ctrl.iconfont').trigger('click');
});