address.js
4.81 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
/**
* 地址管理
* @author: bikai<kai.bi@yoho.cn>
* @date: 2015/11/17
*/
var $ = require('jquery'),
tip = require('../plugin/tip');
var $action = $('.action'),
$addressForm = $('.edit-address'),
$submit = $('.submit'),
$addAddress = $('.add-address'),
$editAddressPage = $('.my-edit-address-page'),
$addressListPage = $('.my-address-list-page'),
$area = $('.area'),
$footer = $('#yoho-footer'),
$confim = $('.confim-mask'),
$pageWrap = $('.page-wrap'),
$backBtn = $('.nav-back'),
isSubmiting,
deleteId,
currentPage = 'address',
newArea = [];
// 清除原有链接
$backBtn.attr('href', 'javascript:void(0);');
// 自定义事件
$backBtn.on('touchend', function(e) {
console.log(currentPage);
if (currentPage === 'edit') {
$pageWrap.hide();
$pageWrap.first().show();
e.preventDefault();
currentPage = 'address';
} else if (currentPage === 'list') {
$pageWrap.hide();
$editAddressPage.show();
e.preventDefault();
currentPage = 'edit';
} else {
window.history.go(-1);
}
});
function editAddress(data) {
data = data || {};
$addressForm.find('[name="id"]').val(data.id || '');
$addressForm.find('[name="consignee"]').val(data.consignee || '');
$addressForm.find('[name="mobile"]').val(data.mobile || '');
$addressForm.find('[name="area_code"]').val(data.areaCode || '');
$addressForm.find('[name="area"]').val(data.area || '');
$addressForm.find('[name="address"]').val(data.address || '');
currentPage = 'edit';
$footer.hide();
$pageWrap.hide();
$editAddressPage.show();
}
$confim.on('touchend', '.cancel', function() {
deleteId = null;
$confim.hide();
}).on('touchend', '.confim', function() {
$.ajax({
method: 'POST',
url: '/home/deladdress',
data: {
id: deleteId
}
}).then(function(res) {
if ($.type(res) !== 'object') {
res = {};
}
if (res.code !== 200) {
tip.show(res.message || '网络出了点问题~');
} else {
window.location.reload();
}
}).fail(function() {
tip.show('网络出了点问题~');
}).always(function() {
deleteId = null;
$confim.hide();
});
});
// 添加地址
$addAddress.on('touchend', function() {
editAddress();
});
// 编辑或删除
$action.on('touchend', '.edit', function() {
editAddress($(this).data());
}).on('touchend', '.del', function() {
deleteId = $(this).data('id');
$confim.show();
});
$submit.on('touchend', function() {
$addressForm.submit();
return false;
});
$addressForm.on('submit', function() {
if (isSubmiting) {
return false;
}
// 简单的表单校验
if (!$(this).find('[name="consignee"]').val()) {
tip.show('收件人不能为空');
$(this).find('[name="consignee"]').focus();
return false;
}
if (!$(this).find('[name="mobile"]').val()) {
tip.show('手机号不能为空');
$(this).find('[name="mobile"]').focus();
return false;
}
if (!$(this).find('[name="area_code"]').val() || !$(this).find('[name="area"]').val()) {
tip.show('省市区不能为空');
return false;
}
if (!$(this).find('[name="address"]').val()) {
tip.show('地址不能为空');
$(this).find('[name="address"]').focus();
return false;
}
isSubmiting = true;
$.ajax({
method: 'POST',
url: '/home/saveaddress',
data: $(this).serialize()
}).then(function(res) {
if ($.type(res) !== 'object') {
res = {};
}
if (res.code !== 200) {
tip.show(res.message || '网络出了点问题~');
} else {
window.location.reload();
}
}).fail(function() {
tip.show('网络出了点问题~');
}).always(function() {
isSubmiting = false;
});
return false;
});
// 省市区
$area.on('touchend', function() {
$footer.hide();
$pageWrap.hide();
$addressListPage.show();
currentPage = 'list';
});
$addressListPage.on('touchend', '.address', function() {
var caption = $(this).children('.caption').text();
newArea.push(caption);
$(this).siblings().hide();
$(this).children('ul').show();
return false;
}).on('touchend', '.address-last', function() {
// 填结果到 html
newArea.push($(this).children('.caption').text());
$('[name="area"]').val(newArea.join(' '));
$('[name="area_code"]').val($(this).data('id'));
$editAddressPage.show();
currentPage = 'edit';
// 恢复默认的三级选择
$addressListPage.hide();
$addressListPage.find('ul').hide();
$addressListPage.children('ul').show().children('li').show();
newArea = [];
return false;
});