address.page.js
10.8 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
/**
* [个人中心]收货地址
* @author: jiangmin
* @date: 2016/07/05
*/
// todo 编辑时设置默认;手机号码逆向显示
var cascadingAddress = require('../plugins/cascading-address');
var dialog = require('../plugins/dialog');
var _alert = dialog.Alert;
var _confirm = dialog.Confirm;
var $addressId = $('#address_id');
var $consignee = $('#consignee');
var $address = $('#addressDetail');
var $mobile = $('#mobile');
var $phone = $('#phone');
var addressForm = $('.form-group-address');
var currentLength = $('.a-table').find('tr').length - 1; // 当前地址条数
var leftLength = 7 - currentLength; // 还剩地址条数
var reg = new RegExp(/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/); // 手机号码校验
require('./me');
require('../plugins/check');
require('yoho-jquery-placeholder');
$('[placeholder]').placeholder();
$(function() {
var address = cascadingAddress({
el: '#address'
});
/**
* 公共方法
*/
var Bll = {
// 获取输入框输入的值
getInfo: function() {
return {
id: $addressId.val(),
aid: $('#tr_' + $addressId.val()).data('addressid'),
consignee: $consignee.val(),
address: $address.val(),
mobile: $mobile.val(),
phone: $phone.val(),
default: $('#tr_' + $addressId.val()).data('default') === 'true'
};
},
// 清空输入框
clearInput: function() {
$consignee.val('');
$address.val('');
$mobile.val('');
$phone.val('');
address.reset();
},
// 校验
check: function(info) {
var flag = true;
info.consignee === '' ? $consignee.next().show() : $consignee.next().hide();
info.address === '' ? $address.next().show() : $address.next().hide();
typeof(info.area_code) === 'undefined' ?
addressForm.css('margin-bottom', '20px').find('.error-tips').show() :
addressForm.css('margin-bottom', '70px').find('.error-tips').hide();
if (info.id) {
info.mobile === '' ? $mobile.next().show() : $mobile.next().hide();
if (info.consignee === '' || info.address === '' || info.mobile === '' ||
typeof(info.area_code) === 'undefined') {
flag = false;
}
return flag;
} else {
if (info.mobile === '') {
$mobile.next().show();
} else {
(!reg.test(info.mobile) ? $mobile.next().html('手机号码格式不对').show() : $mobile.next().hide());
}
// info.mobile === '' ? $mobile.next().show() :
// (!reg.test(info.mobile) ? $mobile.next().html('手机号码格式不对').show() : $mobile.next().hide());
if (info.consignee === '' || info.address === '' || info.mobile === '' || !reg.test(info.mobile) ||
typeof(info.area_code) === 'undefined') {
flag = false;
}
return flag;
}
},
// 拼接一条数据的html
getHtml: function(info) {
var html = '<tr class="table-body">';
html += '<input type="hidden" id="tr_' + info.address_id + '" value="' + info.address_id +
'" data-addressid="' + info.aid + '">' +
'<input type="hidden" id="tr_' + info.area_code + '" value="' + info.area_code + '">' +
'<td class=\'width-name\'>' + info.consignee + '</td>' +
'<td class=\'width-address\'>' + info.area + '</td>' +
'<td class=\'width-fulladdress\'>' + info.address + '</td>' +
'<td class=\'width-mobile\'><p>' + info.mobile + '</p><p>' + info.phone + '</p></td>' +
'<td class=\'width-opearte\'><div><span class=\'blue opreation update-address\' data-id=\'' +
info.address_id + '\'>修改</span>\n<em class="op-sep">|</em>\n';
if (!info.default) {
html += '<span class=\'blue opreation del-address\' data-id=\'' + info.aid + '\'>删除</span>\n';
}
html += '<span class=\'btn set-default opreation set\' data-id=\'' + info.aid + '\'>设为默认</span>' +
'</div></td>';
html += '</tr>';
return html;
},
// 获取一条数据
setInfo: function(id, td) {
$addressId.val(id);
$consignee.val(td.eq(0).text());
$address.val(td.eq(2).text());
$mobile.val(td.eq(3).children().eq(0).text());
$phone.val(td.eq(3).children().eq(1).text());
},
// 设置表格头部
setTableTile: function() {
$('.table-title').text('已保存了' + currentLength +
'条地址,还能保存' + leftLength + '条地址');
}
};
// 保存收货地址
$(document).on('click', '#save-address', function() {
var info,
area,
areaInfo, info2;
info = Bll.getInfo();
area = address.getAreaIds();
areaInfo = address.getAreaLabels();
info.area_code = area.split(',')[2];
info.area = areaInfo.split(',').join(' ');
info2 = $.extend({}, info);
info2.id = info.aid;
delete info2.aid;
if (Bll.check(info) === true) {
// console.log(info.id);
// 新增
if (info.id === '') {
if (currentLength >= 7) {
new _alert('您最多添加7个收货地址,可删除不需要的地址后再添加新地址!').show();
Bll.clearInput();
} else {
$.ajax({
type: 'POST',
url: '/me/address/add',
dataType: 'json',
data: info,
success: function(data) {
if (data.code === 200) {
location.reload();
} else {
new _alert(data.message).show();
}
}
});
}
} else { // 修改
if (new RegExp(/^\d{3}[*]{4}\d{4}/).test(info.mobile)) {
$.ajax({
type: 'POST',
url: '/me/address/update',
dataType: 'json',
data: info2,
success: function(data) {
if (data.code === 200) {
location.reload();
} else {
new _alert(data.message).show();
}
}
});
} else if (reg.test(info.mobile)) {
$mobile.next().hide();
$.ajax({
type: 'POST',
url: '/me/address/update',
dataType: 'json',
data: info2,
success: function(data) {
if (data.code === 200) {
location.reload();
} else {
new _alert(data.message).show();
}
}
});
} else {
$mobile.next().html('手机号码格式不对').show();
}
}
}
});
// 修改收货地址
$(document).on('click', '.update-address', function() {
var id = $(this).data('id');
var tr = $(this).parents('.table-body');
var td = tr.find('td');
var areaCode = tr.find('input').eq(1).val();
address.setAddress(areaCode);
addressForm.css('margin-bottom', '70px');
$('.error-tips').hide();
$('.tip em').html('修改地址');
Bll.setInfo(id, td);
});
// 删除收货地址
$(document).on('click', '.del-address', function() {
var id = $(this).data('id');
var tr = $(this).parents('.table-body');
var a = new _confirm({
content: '您确定要删除收货地址吗?',
cb: function() {
$.ajax({
type: 'POST',
url: '/me/address/del',
dataType: 'json',
data: {
id: id
},
success: function() {
currentLength--;
leftLength++;
tr.remove();
Bll.setTableTile();
a.close();
Bll.clearInput();
}
});
}
}).show();
});
// 设置默认收货地址
$(document).on('click', '.set-default', function() {
var tr = $(this).parents('.table-body');
var tbody = tr.parent();
var id = $(this).data('id');
var self = this;
$.ajax({
type: 'POST',
url: '/me/address/default',
dataType: 'json',
data: {
id: id
},
success: function(data) {
if (data.code === 200) {
$('.set-default').addClass('set');
$('.del-address').removeClass('hide');
$('.op-sep').removeClass('hide');
$('.current-default').removeClass('current-default').text('设为默认');
$(self).addClass('current-default').text('默认地址');
$(self).siblings('.del-address').addClass('hide');
$(self).siblings('.op-sep').addClass('hide');
$(self).removeClass('set');
tbody.find('.table-body').eq(0).before('<tr class=\'table-body select-row\'>' + tr.html() +
'</tr>');
tr.remove();
} else {
new _alert(data.message).show();
}
}
});
});
// 选中某一行
$(document).on('mousemove', '.table-body', function() {
$('.table-body').removeClass('select-row');
$(this).addClass('select-row');
$('.set').hide();
$(this).find('.set').css('display', 'inline-block');
});
$('.table-body').on('mouseout', function() {
var tmpset = $(this).find('.set');
if (tmpset.css('display') === 'inline-block') {
tmpset.hide();
}
});
});