address.js
7.74 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
/**
* @description: 地址管理
* @author: chenglong.wang@yoho.cn
*/
var $ = require('yoho.jquery');
var isProvinceChecked = false;
var $addressManage = $('.address-manage'),
$province = $addressManage.find('.text-input[name="province"]'),
$city = $addressManage.find('.text-input[name="city"]'),
$county = $addressManage.find('.text-input[name="county"]'),
$selectList = $addressManage.find('.text-input[name="province"], .text-input[name="city"]'),
$addressList = $('.address-list'),
$name = $addressManage.find('.text-input[name="name"]'),
$phone = $addressManage.find('.text-input[name="phone"]'),
$telCode = $addressManage.find('.text-input[name="code-tel"]'),
$tel = $addressManage.find('.text-input[name="tel"]'),
$mail = $addressManage.find('.text-input[name="mail"]'),
$address = $addressManage.find('.text-input[name="address"]'),
$code = $addressManage.find('.text-input[name="code"]'),
$saveBtn = $('.save-btn');
function structureOption($obj, data) {
var key,
optionHtml,
defaultOption;
for (key in data) {
if (data[key].checked) {
optionHtml += '<option selected value="' + data[key].value + '">' + data[key].name + '</option>';
} else {
optionHtml += '<option value="' + data[key].value + '">' + data[key].name + '</option>';
}
}
$obj.html(optionHtml);
if ($obj.attr('name') === 'province') {
defaultOption = '<option value="0">请选择省份</option>';
} else if ($obj.attr('name') === 'city') {
defaultOption = '<option value="0">请选择城市</option>';
} else if ($obj.attr('name') === 'county') {
defaultOption = '<option value="0">请选择区县</option>';
}
$obj.prepend($(defaultOption));
}
/**
* @description: 改函数会返回地址信息
* d {Object} type: 'getProvince'获取省 type: 'getCity'获取城市 type: 'getCounty'获取县
* id: 0 && type: 'getProvince' 获取所有省,默认没有选中项
* id !== 0 && type: 'getProvince' 获取所有省,默认选中用户所在的省
*/
function getAddress(d, callback) {
var $obj,
url;
if (d.type === 'getProvince') {
url = 'getProvince';
$obj = $province;
} else if (d.type === 'getCity') {
url = 'getCity';
$obj = $city;
} else if (d.type === 'getCounty') {
url = 'getCounty';
$obj = $county;
}
$.ajax({
type: 'post',
url: '/order/save/' + url,
dataType: 'json',
data: {
id: d.id
}
}).then(function(data) {
structureOption($obj, data);
if (typeof callback === 'function') {
callback();
}
}).fail(function() {
//todo
});
}
function getUserInfo(id) {
$.ajax({
type: 'post',
url: 'getUserInfo',
data: {
id: id
}
}).then(function(data) {
var key;
for (key in data) {
if (data.hasOwnProperty(key)) {
if (!!data[key]) {
eval('$' + key).val(data[key]);
}
}
}
}).fail(function() {
});
}
//添加地址
exports.newAddress = function(id) {
var pId = id !== 'undefined' ? id : 0;
if (!!pId) {
getUserInfo();
}
//获取省
getAddress({
id: pId,
type: 'getProvince'
}, function() {
var provinceId = $province.val();
if (provinceId !== '0') {
//如果获取的省有默认选中项则获取市
getAddress({
id: provinceId,
type: 'getCity'
}, function() {
var cityId = $city.val();
//如果获取的市有默认选中项则获取县
if (cityId !== '0') {
getAddress({
id: cityId,
type: 'getCounty'
});
}
});
}
});
$selectList.change(function() {
var $this = $(this);
if ($this.attr('name') === 'province') {
getAddress({
id: $this.val(),
type: 'getCity'
}, function() {
isProvinceChecked = true;
});
}
if ($this.attr('name') === 'city' && isProvinceChecked) {
getAddress({
id: $this.val(),
type: 'getCounty'
});
}
});
};
//修改地址
exports.modifyAddress = function() {
$addressList.click(function(event) {
var $this = $(event.target),
id = $this.closest('li').find('.radio').attr('id');
if ($this.hasClass('address-modify')) {
$addressManage.removeClass('hide');
exports.newAddress(id);
} else if ($this.hasClass('address-del')) {
$.ajax({
type: 'post',
url: '/order/save/addressDel',
data: {
id: id
}
}).then(function(data) {
if (!!data.status) {
$this.closest('li').remove();
}
}).fail(function() {
//todo
});
} else if ($this.hasClass('default-address')) {
// 设为默认地址
$.ajax({
type: 'post',
url: '/order/save/defaultAddress',
data: {
id: id
}
}).then(function(data) {
if (!!data.status) {
$this.remove();
}
}).fail(function() {
//todo
});
}
});
};
exports.saveAddress = function() {
$saveBtn.click(function() {
var name = $name.val(),
province = $province.val(),
city = $city.val(),
county = $county.val(),
address = $address.val(),
phone = $phone.val(),
telCode = $telCode.val(),
tel = $tel.val(),
mail = $mail.val(),
code = $code.val(),
id = new Date();
$.ajax({
type: 'post',
url: '/order/save/addressSave',
data: {
name: name,
province: province,
city: city,
county: county,
address: address,
phone: phone,
tel: tel,
telCode: telCode,
mail: mail,
code: code,
id: id * 1
}
}).then(function(data) {
var addressStr;
if (!!data.status) {
addressStr = '<li>' +
'<input class="radio" type="radio" name="address" id="' + id * 1 + '" />' +
'<label for="' + id * 1 + '">' +
'<strong>' + name + '</strong>' +
'<span>' + address + '</span>' +
'<a href="javascript:void(0);">设为默认地址</a>' +
'<div class="order-modify-btn">' +
'<span class="address-modify">[修改]</span>' +
'<span class="address-del">[删除]</span>' +
'</div>' +
'</label>' +
'</li>';
$('.use-new-address').before($(addressStr));
}
}).fail(function() {
//todo
});
});
};