|
|
/**
|
|
|
* @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"]'),
|
|
|
$btn = $('.order-modify-btn'),
|
|
|
$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() {
|
|
|
|
|
|
$btn.click(function(event) {
|
|
|
|
|
|
var $this = $(event.target),
|
|
|
id = $this.closest('li').find('.radio').attr('id');
|
|
|
|
|
|
if ($this.hasClass('address-modify')) {
|
|
|
|
|
|
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
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
};
|
|
|
|
|
|
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
|
|
|
});
|
|
|
});
|
|
|
}; |
|
|
\ No newline at end of file |
...
|
...
|
|