address.page.js
10.2 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
/**
* 个人中心页-地址管理
* @author: wsl<shuiling.wang@yoho.cn>
* @date: 2016/02/24
*/
var $ = require('yoho-jquery');
var regionCode = require('./common-address');
var dialog = require('../common/dialog');
var stringHandle = require('../common/stringHandle');
var Alert = dialog.Alert;
var active;
var $del = $('.address-del'),
$edit = $('.address-modify'),
$input = $('.input-1'),
saveFlag = true;
var address = {
evil: function(fn) {
var Fn = Function;
return new Fn('return ' + fn)();
},
clearAddrForm: function() {
$('#addressName').val('');
$('#address').val('');
$('#zipCode').val('');
$('#phone').val('');
$('#mobile').val('');
$('#email').val('');
$('#addrId').val(0);
$('#province').val(0);
$('#city').val(0);
$('#areaCode').val(0).hide();
$('#streets').val(0).hide();
$('#btn-reset').hide();
$('#form-status').html('添加新地址');
},
editAddr: function(id) {
$.getJSON('/home/address/editAddress?id=' + id, function(jsonData) {
if (jsonData.code === 200) {
$('#form-status').html('修改地址');
$('#addressName').val(jsonData.data.consignee);
regionCode.loadAllData(jsonData.data.area_code, {}, true);
$('#address').val(jsonData.data.address);
$('#email').val(jsonData.data.email);
$('#phone').val(jsonData.data.phone);
$('#mobile').val(jsonData.data.mobile);
$('#zipCode').val(jsonData.data.zip_code);
$('#addrId').val(jsonData.data.address_id);
$('#btn-reset').show();
}
});
},
saveAddr: function() {
if (!saveFlag) {
return;
}
saveFlag = false;
$.post('/home/address/saveAddress', $('#address-form').serialize(), function(data) {
if (data.code === 200) {
location.reload();
} else {
active = new Alert(data.message);
active.show();
}
saveFlag = true;
}, 'json');
},
// 删除地址
delAddr: function(addrId) {
$.getJSON('/home/address/delAddress?id=' + addrId, function(jsonData) {
if (jsonData.code === 200) {
$('.address-list').find('li[addressId=' + addrId + ']').remove();
if (addrId === $('#addrId').val()) {
address.clearAddrForm();
}
} else {
active = new Alert('删除失败');
active.show();
}
});
},
setDefaultAddr: function(addrId) {
var oldBtn = $('.address-list').find('.default-btn'),
oldLi = $('.address-list').find('.default-btn').parents('li'),
curLi = $('.address-list').find('li[addressId=' + addrId + ']'),
curBtn = curLi.find('.set-default-btn');
$.getJSON('/home/address/defaultAddress?id=' + addrId, function(jsonData) {
if (jsonData.code === 200) {
oldBtn.attr('class', 'btn-c3 set-default-btn');
oldLi.removeClass('preferred');
oldBtn.html('<span>设为常用地址</span>');
curBtn.attr('class', 'btn-c2 default-btn');
curBtn.html('<span>已设为常用地址</span>');
curLi.addClass('preferred');
} else {
active = new Alert('设置失败');
active.show();
}
});
}
};
require('../common');
if (!Array.prototype.indexOf) {// eslint-disable-line
Array.prototype.indexOf = function(obj, start) {// eslint-disable-line
var i,
j;
for (i = (start || 0), j = this.length; i < j; i++) {
if (this[i] === obj) {
return i;
}
}
return -1;
};
}
// 获取地址id
function getAddressId(dom) {
var li = dom.parents('.address-content'),
id = li.attr('addressId');
return id;
}
// 表单焦点显示
function focusAction(opt) {
var msg = '';
if (opt.inputName === 'addressName') {
msg = '仅支持中英文、数字,20个字符以内';
}
if (opt.inputName === 'address') {
msg = '请输入详细地址';
}
if (opt.inputName === 'zipCode') {
msg = '请输入收货人所在地邮编号';
}
if (opt.inputName === 'phone') {
msg = '格式例如:0577-88888888';
}
if (opt.inputName === 'mobile') {
msg = '请输入手机号,如1360000000';
}
if (opt.inputName === 'email') {
msg = '用来接收订单提醒邮件,便于您及时了解订单状态';
}
opt.$checkInfo.removeClass('form-prompt').addClass('form-focus').html(msg);
}
// 表单失去焦点显示
function blurAction(opt) {
var regular = '',
res = true,
msg = '输入正确',
domClass = 'form-info form-success',
value = opt.val;
if (opt.inputName === 'addressName') {
regular = /^([\u4e00-\u9fa5\w*]{1,20})$/;
if (stringHandle.chinese2(value) > 20 || stringHandle.chinese2(value) < 1) {
msg = '收货人姓名最多支持20个字符';
res = false;
domClass = 'form-info form-error';
} else if (regular.test(opt.val) === false) {
msg = '收货人姓名不支持特殊符号';
res = false;
domClass = 'form-info form-error';
}
}
if (opt.inputName === 'address') {
if (opt.len < 2) {
msg = '请填写详细地址';
res = false;
domClass = 'form-info form-error';
}
}
if (opt.inputName === 'zipCode') {
regular = /^[0-9]{6}$/;
if (opt.len === 0) {
msg = '请输入收货人所在地邮编号';
res = false;
domClass = 'form-info form-error';
} else if (regular.test(opt.val) === false) {
msg = '你输入的邮编式不正确';
res = false;
domClass = 'form-info form-error';
}
}
if (opt.inputName === 'phone') {
regular = /^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$/;
if (opt.len === 0) {
msg = '你真的不想留联系电话了吗?';
res = true;
domClass = 'form-info form-success';
} else if (regular.test(opt.val) === false) {
msg = '你输入的联系电话格式不正确';
res = false;
domClass = 'form-info form-error';
}
}
if (opt.inputName === 'mobile') {
regular = /^[0-9]{11}$/; // 手机号11位校验 by 新飞 2017.7.11
if (regular.test(opt.val) === false) {
msg = '您输入的手机号格式不正确';
res = false;
domClass = 'form-info form-error';
}
}
if (opt.inputName === 'email') {
regular = '^[a-z0-9_\-]+(\.[_a-z0-9\-]+)*@([_a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|' +
'edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)$';
if (value.match(regular) === null || opt.len === 0) {
msg = '你输入的邮件地址格式不正确';
res = false;
domClass = 'form-info form-error';
}
}
opt.$checkInfo.removeClass().addClass(domClass).html(msg);
return res;
}
// 地址表单验证
function checkAddressForm(dom, action) {
var val = dom.val(),
$domParent = dom.parent(),
opt = {
val: val,
len: val.length,
$domParent: dom.parent(),
$checkInfo: $domParent.find('span'),
inputName: dom.attr('name')
};
if (action === 'focus') {
focusAction(opt);
}
if (action === 'blur') {
return blurAction(opt);
}
}
function fullOneValidator() {
var phone = $('#phone').next(),
mobile = $('#mobile').next();
if (mobile.hasClass('form-success') && !phone.hasClass('form-error') && $('#phone').val().length === 0) {
phone.removeClass().addClass('form-prompt form-info').html('请输入你的联系电话,可以为空哦');
return true;
} else if (phone.html() === '输入正确' && !mobile.hasClass('form-error') && $('#mobile').val().length === 0) {
mobile.removeClass().addClass('form-prompt form-info').html('填写手机号便于接收发货和收货通知');
return true;
} else if (!mobile.hasClass('form-success') && phone.html() !== '输入正确') {
phone.removeClass().addClass('form-success form-info').html('你真的不想留联系电话了吗?');
mobile.removeClass().addClass('form-prompt form-info').html('填写手机号便于接收发货和收货通知');
$('#mobile').focus();
return false;
}
}
// 校验所有内容
function checkAllForm() {
var arr = [];
$.each($input, function(key, item) {
arr.push(checkAddressForm($(item), 'blur'));
});
arr.push(fullOneValidator());
$.each($('.form-info'), function(key, item) {
if ($(item).hasClass('form-error')) {
$(item).prev().focus();
return false;
}
});
if (arr.indexOf(false) === -1) {
return true;
} else {
return false;
}
}
$(document).on('click', '.set-default-btn', function() {
var id = getAddressId($(this));
address.setDefaultAddr(id);
});
$del.on('click', function() {
var id = getAddressId($(this));
address.delAddr(id);
});
$edit.on('click', function() {
var id = getAddressId($(this));
address.editAddr(id);
$('#btn-reset').show();
});
$input.on('focus', function() {
checkAddressForm($(this), 'focus');
});
$input.on('blur', function() {
checkAddressForm($(this), 'blur');
});
$('#address-info').on('click', function() {
if (checkAllForm()) {
address.saveAddr();
}
});
$(document).ready(function() {
regionCode.loadAllData(
0,
{
provinceDomId: 'province',
cityDomId: 'city',
areaDomId: 'areaCode',
streetsDomId: 'streets'
}
);
address.clearAddrForm();
$('#btn-reset').click(function() {
address.clearAddrForm();
});
});