address.js
11.7 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/**
* 订单确认页地址相关
* @author:xuqi<qi.xu@yoho.cn>
* @date: 2016/7/12
*/
var $ = require('yoho-jquery'),
Hbs = require('yoho-handlebars'),
cascadingAddress = require('../../plugins/cascading-address'),
popup = require('../../plugins/dialog');
var $receiver = $('#receiver');
var Dialog = popup.Dialog,
Confirm = popup.Confirm;
var addressDialogTpl;
var addressTpl;
// 判断是否是Y
Hbs.registerHelper('isY', function(value, options) {
if (value === 'Y') {
return options.fn(this);
} else {
return options.inverse(this);
}
});
addressDialogTpl = Hbs.compile($('#address-dialog-tpl').html());
addressTpl = Hbs.compile($('#address-list-tpl').html());
// address dialog 数据验证
function validateAddress($el) {
var field = {
name: [
{
noEmpty: true,
err: '收货人不能为空'
},
{
regx: /[\u4e00-\u9fa5a-zA-Z\d]{2,12}/,
err: '请输入2-12个汉字、英文或数字'
}
],
detail: [
{
noEmpty: true,
err: '详细地址不能为空'
},
{
regx: /[\u4e00-\u9fa5a-zA-Z\d#-()]+/,
err: '只能包含数字、字母、汉字、#、-、()及其组合'
}
],
mobile: [
{
noEmpty: true,
err: '手机号码不能为空'
},
{
regx: /\d+/,
err: '手机号码格式不正确'
}
],
phone: [
{
regx: /[\d-]+/,
err: '只能包含数字、-组合',
skipWhenEmpty: true
}
]
};
var key,
$cur,
cur,
vaKey,
vaRegx;
var pass = true;
for (key in field) {
if (field.hasOwnProperty(key)) {
$cur = $el.find('.address-' + key);
cur = $cur.val();
// 按顺序去验证对应filed的值
for (vaKey = 0; vaKey < field[key].length; vaKey++) {
vaRegx = field[key][vaKey];
// 非空验证、非空下正则验证、其他正则验证
if ((vaRegx.noEmpty && cur === '') || (vaRegx.regx &&
(vaRegx.skipWhenEmpty ? !(cur === '' || vaRegx.regx.test(cur)) : !vaRegx.regx.test(cur))
)) {
pass = false;
$cur.siblings('.error-tips').find('em').text(vaRegx.err).end().removeClass('hide');
break;
}
}
}
}
return pass;
}
// 更新收货信息:姓名,手机号码,区域,详细
function receiver(ad) {
$receiver.html(ad.consignee + ' ' + ad.mobile + ' ' + ad.area + ' ' + ad.address);
}
// 地址弹窗Factory
function addressDialogFactory(opt, $the) {
var address = new Dialog({
closeIcon: false,
className: 'address',
content: addressDialogTpl(opt),
btns: [
{
id: 'save-address',
btnClass: ['save-address'],
name: '保存',
cb: function() {
var $el = address.$el,
consignee,
detail,
mobile,
phone,
areaCode;
// 验证输入
if (validateAddress(address.$el)) {
// form value
consignee = $el.find('.address-name').val();
detail = $el.find('.address-detail').val();
mobile = $el.find('.address-mobile').val();
phone = $el.find('.address-phone').val();
areaCode = $el.address.getAreaIds().split(',')[2];
if (opt && opt.id) {
// update
$.ajax({
type: 'POST',
url: '/me/address/update',
data: {
id: opt.id,
uid: '8050484', // todo
consignee: consignee,
address: detail,
mobile: mobile,
phone: phone,
'area_code': areaCode // eslint-disable-line
}
}).then(function(data) {
var updated;
if (data.code === 200) {
updated = {
consignee: consignee,
address: detail,
mobile: mobile,
phone: phone,
area_code: data.data.area_code, // eslint-disable-line
id: opt.id,
focus: $the.hasClass('focus'),
area: $el.address.getAreaLabels().replace(/,/g, ' ')
};
$the.before(addressTpl({
address: [updated]
}));
// 如果当前地址正在被使用,则更新收货信息
if ($the.hasClass('focus')) {
receiver(updated);
}
$the.remove();
address.close();
}
});
} else {
// add
$.ajax({
type: 'POST',
url: '/me/address/add',
data: {
uid: '8050484', // todo
consignee: consignee,
address: detail,
mobile: mobile,
phone: phone,
'area_code': areaCode, // eslint-disable-line
init: opt.init
}
}).then(function(data) {
var the;
if (data.code === 200) {
the = $.extend(data.data, {
focus: true
});
$('.address.focus').removeClass('focus');
$('#address-list').prepend(addressTpl({
address: [the]
}));
// 新地址默认使用,更新收货信息
receiver(the);
address.close();
}
});
}
}
}
},
{
id: 'cancel-address',
btnClass: ['cancel-address', 'white'],
name: '取消',
cb: function() {
address.close();
}
}
]
});
return address;
}
/**
* 初始化弹窗内容
* @param $el dialog的jquery对象
* @param areaCode 区码,初始化选择区域的组件
*/
function initAddressContent($el, areaCode) {
// 初始化地址组件/将组件attr到$el方便操作
$el.address = cascadingAddress({
el: '#address'
});
if (areaCode) {
$el.address.setAddress(areaCode + ''); // need convert to string
}
}
/**
* 新增地址
* @param isInit Boolean 是否地址列表无地址(首次添加不显示取消按钮)
*/
function newAddress(isInit) {
var address;
address = addressDialogFactory({
init: isInit
});
if (isInit) {
address.$el.addClass('is-init');
}
initAddressContent(address.$el);
address.show();
}
// 显示全部地址
$('.address-all').click(function() {
$(this).siblings('.address-list').removeClass('shrink').end().addClass('vhide');
});
// 新增地址
$('.new-address').click(newAddress);
$('.address-list').on('click', '.address', function() {
// 地址切换
var $this = $(this);
if ($this.hasClass('focus')) {
return;
}
$this.addClass('focus');
$this.siblings('.focus').removeClass('focus');
// 切换地址后切换收货信息
receiver({
consignee: $this.data('name'),
area: $this.data('area'),
mobile: $this.data('mobile'),
address: $this.data('address')
});
}).on('click', '.modify', function(e) {
// 修改地址
var $this = $(this).closest('.address');
var areaCode = $this.data('areacode');
var address = addressDialogFactory({
updateAddress: true,
id: $this.data('id'),
name: $this.data('name'),
mobile: $this.data('mobile'),
phone: $this.data('phone'),
areacode: areaCode,
detail: $this.data('address')
}, $this);
initAddressContent(address.$el, areaCode);
address.show();
e.stopPropagation();
}).on('click', '.delete', function(e) {
// 删除地址
var $this = $(this).closest('.address');
var delConfirm = new Confirm({
className: 'address-confirm-dialog',
content: '<p class="main">删除地址</p><p class="sub">您确定要删除该收货地址吗?</p>',
cb: function() {
// 确认删除,do something
$.ajax({
type: 'POST',
url: '/me/address/del',
data: {
uid: '8050484',
id: $this.data('id')
}
}).then(function(data) {
if (data.code === 200) {
// 若当前选中,则移除后选中默认地址
if ($this.hasClass('focus')) {
$this.siblings('.default').addClass('focus');
}
$this.remove();
delConfirm.close();
}
});
}
}).show();
e.stopPropagation();
}).on('click', '.set-default', function(e) {
// 设置为默认地址
var $this = $(this).closest('.address');
$.ajax({
type: 'POST',
url: '/me/address/default',
data: {
uid: '8050484',
id: $this.data('id')
}
}).then(function(data) {
if (data.code === 200) {
// 切换default和focus状态
$this.addClass('default focus');
$this.siblings('.default, .focus').removeClass('default focus');
}
});
e.stopPropagation();
});
// 页面加载时请求地址列表,若有则展示列表;若无则直接显示新建弹窗并不可被关闭
$.ajax({
url: '/me/address/list'
}).then(function(data) {
var list;
if (data && data.code === 200) {
list = data.data;
if (list.length === 0) {
// new address
newAddress(true);
} else {
$('#address-list').append(addressTpl({
address: data.data
}));
// 填收货人信息
receiver(data.data[0]);
}
}
});