Authored by htoooth

refactor

... ... @@ -17,7 +17,10 @@ var html = $tpl.html();
var active;
var $dialogEdit = $('#edit-dialog-tpl');
var $addressManage, $province, $city, $county, $selectList, $name, $phone, $address, $telCode, $tel, isProvinceChecked = false;
var $addressManage, $province, $city,
$county, $selectList, $name, $phone, $address, $telCode, $tel;
var isProvinceChecked = false;
function cancelFactory(id) {
var options = {
... ... @@ -68,9 +71,15 @@ function cancelFactory(id) {
return new Dialog(options);
}
function showMessgaeDialog(message_icon, message_title, message_summary, cb) {
function showMessgaeDialog(messageIcon, messageTitle, messageSummary, cb) {
var template = require('hbs/home/orders/dialog-message.hbs');
var html = template({messageIcon: message_icon, messageTitle: message_title, messageSummary: message_summary});
var messageHtml = template({
messageIcon: messageIcon,
messageTitle: messageTitle,
messageSummary: messageSummary}
);
var options = {
mask: true,
btns: [
... ... @@ -84,112 +93,13 @@ function showMessgaeDialog(message_icon, message_title, message_summary, cb) {
}
}
],
content: html,
content: messageHtml,
className: 'message-dialog'
};
return new Dialog(options);
}
// 编辑订单
function editOrder(id) {
var options = {
mask: true,
btns: [
{
id: 'edit-sure',
name: '确定',
btnClass: ['edit-sure'],
cb: function() {
saveAddress(id);
}
},
{
id: 'edit-no',
name: '取消',
btnClass: ['edit-no'],
cb: function() {
active.close();
}
}
],
content: $dialogEdit.html(),
className: 'edit-order-dialog'
};
return new Dialog(options);
}
$tpl.remove();
$dialogEdit.remove();
// 查看物流
$('.check-logistics').click(function() {
var $this = $(this);
var orderId = $this.data('id');
var time = $this.data('time');
var paytype = $this.data('paytype');
ordersApi.getExpress(orderId, paytype, time).then(function(result) {
var tpl = require('hbs/home/orders/express.hbs');
if (result.logistics.length === 0) {
return;
}
$this.siblings('.logistics').html(tpl(result)).removeClass('hide');
});
});
// 关闭查看物流
$('.logistics').on('click', '.close-logistics', function() {
$(this).closest('.logistics').addClass('hide');
});
// 订单列表&订单详情【取消订单和确认收货】
$('.me-orders, .order-detail').on('click', '.cancel-order', function(e) {
// 取消订单
active = cancelFactory($(this).closest('.order, .order-detail').data('id'));
active.show();
}).on('click', '.confirm-received', function(e) {
var id = $(this).closest('.order, .order-detail').data('id');
// 确认收货
active = new Confirm({
cb: function() {
$.ajax({
type: 'POST',
url: '/home/orders/confirmorder',
data: {
orderCode: id
}
}).then(function(data) {
if (data.code === 200) {
active.close();
history.go(0);
}
});
},
content: '您确定要确认收货吗?'
});
active.show();
}).on('click', '.order-delete', function() {
var id = $(this).closest('.order, .order-detail').data('id');
active = deleteOrder(id, this);
active.show();
}).on('click', '.edit-order', function() {
$(this).addClass('edit-order-active');
active = editOrder($(this).closest('.order, .order-detail').data('id'));
active.show();
newAddress(0);
}).on('click', '.rebuy', function() {
var id = $(this).closest('.order, .order-detail').data('id');
buyAgain(id);
});
// 再次购买
function buyAgain(id) {
$.ajax({
... ... @@ -204,6 +114,7 @@ function buyAgain(id) {
}
});
}
function deleteOrder(id, obj) {
// 是否为彻底删除
var isFullyDelete = true;
... ... @@ -211,7 +122,7 @@ function deleteOrder(id, obj) {
var deleteMessage = isFullyDelete ? '您确定要永久删除订单吗?' : '您确定要删除订单吗?';
var deleteTip = isFullyDelete ? '永久删除后,订单将无法恢复,您将无法对该订单的商品申请售后服务,请谨慎操作。' : '删除后,您可以在订单回收站找回该订单,也可以做永久删除。';
var insert = require('hbs/home/orders/delete-order.hbs');
var html = insert({deleteMessage: deleteMessage, deleteTip: deleteTip});
var deleteMessageHtml = insert({deleteMessage: deleteMessage, deleteTip: deleteTip});
var options = {
mask: false,
btns: [
... ... @@ -248,120 +159,47 @@ function deleteOrder(id, obj) {
}
}
],
content: html,
content: deleteMessageHtml,
className: 'delete-dialog'
};
return new Dialog(options);
}
// 地址操作
function newAddress(id) {
var code, codeId;
var pId = id || 0; // 如果没有传id则获取所有省列表
var addressCodeReg = /[0-9]{2}/gi;
$addressManage = $('.edit-order-dialog');
$province = $addressManage.find('select[name="province"]');
$city = $addressManage.find('select[name="city"]');
$county = $addressManage.find('select[name="county"]');
$selectList = $addressManage.find('select[name="province"],select[name="city"]');
$name = $addressManage.find('.inp[name="name"]');
$phone = $addressManage.find('.inp[name="phone"]');
$address = $addressManage.find('.inp[name="address"]');
$telCode = $addressManage.find('.inp[name="tel-code"]');
$tel = $addressManage.find('.inp[name="tel"]');
var validate = validateForm();
code = $province.data('areacode') + '';
codeId = !!code && code.match(addressCodeReg);
// 获取省
getAddress({
id: pId,
type: 'getProvince',
selectId: codeId[0]
}, function() {
var provinceId = $province.val();
if (provinceId !== '0') {
isProvinceChecked = true;
// 构建select下拉选项
function structureOption($obj, data, selectId) {
// 如果获取的省有默认选中项则获取市
getAddress({
id: provinceId,
type: 'getCity',
selectId: '' + codeId[0] + codeId[1]
}, function() {
var i,
optionHtml = '',
defaultOption,
isStar = '';
var cityId = $city.val();
for (i = 0; i < data.length; i++) {
// 如果获取的市有默认选中项则获取县
if (cityId !== '0') {
getAddress({
id: cityId,
type: 'getCounty',
selectId: code
});
}
});
if (data[i].is_support_express === 'Y') {
isStar = '*';
} else {
$addressManage.find('select[name="city"]').html('<option value="0">请选择城市</option>');
$addressManage.find('select[name="county"]').html('<option value="0">请选择区县</option>');
isStar = '';
}
});
// 发生change事件时获取下一级地址
$selectList.change(function() {
var $this = $(this);
if (data[i].id === selectId) {
optionHtml += '<option selected value="' + data[i].id + '">' + isStar + data[i].caption + '</option>';
} else {
optionHtml += '<option value="' + data[i].id + '">' + isStar + data[i].caption + '</option>';
}
}
if ($this.attr('name') === 'province') {
if ($this.val() === '0') {
$addressManage.find('select[name="city"]').html('<option value="0">请选择城市</option>');
$addressManage.find('select[name="county"]').html('<option value="0">请选择区县</option>');
} else {
getAddress({
id: $this.val(),
type: 'getCity'
}, function() {
isProvinceChecked = true;
});
$addressManage.find('select[name="county"]').html('<option value="0">请选择区县</option>');
}
}
if ($obj.attr('name') === 'province') {
defaultOption = '<option value="0">请选择省份</option>';
} else if ($obj.attr('name') === 'city') {
defaultOption = '<option value="0">请选择城市</option>';
if ($this.attr('name') === 'city' && isProvinceChecked) {
} else if ($obj.attr('name') === 'county') {
defaultOption = '<option value="0">请选择区县</option>';
}
if ($this.val() === '0') {
$addressManage.find('select[name="county"]').html('<option value="0">请选择区县</option>');
} else {
getAddress({
id: $this.val(),
type: 'getCounty'
});
}
}
});
$name.on('blur', function() {
validate.name($.trim($name.val()));
});
$address.on('blur', function() {
validate.address($.trim($address.val()));
});
$phone.on('blur', function() {
validate.mobile($.trim($phone.val()));
});
$tel.on('blur', function() {
validate.tel($.trim($telCode.val()), $.trim($tel.val()));
});
$county.on('change', function() {
validate.city($province.val(), $city.val(), $county.val());
});
$obj.html(defaultOption + optionHtml);
}
/**
... ... @@ -371,7 +209,6 @@ function newAddress(id) {
* id !== 0 && type: 'getProvince' 获取所有省,默认选中用户所在的省
*/
function getAddress(d, callback) {
var $obj;
var selectId = d.selectId;
... ... @@ -396,51 +233,108 @@ function getAddress(d, callback) {
data: {
id: d.id * 1
}
}).then(function(d) {
}).then(function(result) {
structureOption($obj, d.data, selectId);
structureOption($obj, result.data, selectId);
if (typeof callback === 'function') {
callback();
return callback();
}
});
}
// 构建select下拉选项
function structureOption($obj, data, selectId) {
var i,
optionHtml = '',
defaultOption,
isStar = '';
for (i = 0; i < data.length; i++) {
if (data[i].is_support_express === 'Y') {
isStar = '*';
} else {
isStar = '';
}
if (data[i].id === selectId) {
optionHtml += '<option selected value="' + data[i].id + '">' + isStar + data[i].caption + '</option>';
} else {
optionHtml += '<option value="' + data[i].id + '">' + isStar + data[i].caption + '</option>';
}
}
function validateForm() {
var $errName = $name.siblings('.error'),
$errProvince = $province.siblings('.error'),
$errAddress = $address.siblings('.error'),
$errPhone = $phone.siblings('.error'),
$errTel = $tel.siblings('.error');
var nameReg = /^[\u4e00-\u9fa5]{2,5}$/;
var addressReg = /^[a-zA-Z0-9-#()()\u4e00-\u9fa5]+$/;
var phoneReg = /^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
var telCodeReg = /^[0-9]{3,4}$/;
var telReg = /^[0-9]{8}$/;
var _rightHtml = '<i class="order-icon icon-right"></i>';
var _errorHtml = '<i class="order-icon icon-error"></i><b></b>';
if ($obj.attr('name') === 'province') {
defaultOption = '<option value="0">请选择省份</option>';
} else if ($obj.attr('name') === 'city') {
defaultOption = '<option value="0">请选择城市</option>';
return {
name: function(name) {
if (!nameReg.test(name)) {
$errName.html(_errorHtml);
$errName.find('b').html('真实姓名至少2个中文,最多5个中文');
$errName.show();
return false;
} else {
$errName.html(_rightHtml).show();
return true;
}
},
city: function(province, city, county) {
if (province === '0' || city === '0' || county === '0') {
$errProvince.html(_errorHtml);
$errProvince.find('b').html('请填写完整的省市区信息');
$errProvince.show();
return false;
} else {
$errProvince.html(_rightHtml).show();
return true;
}
},
address: function(address) {
var message = '';
} else if ($obj.attr('name') === 'county') {
defaultOption = '<option value="0">请选择区县</option>';
}
if (!addressReg.test(address)) {
message = '详细地址不能为空';
$obj.html(defaultOption + optionHtml);
if ($.trim(address) !== '') {
message = '只能包含数字、字母、汉字、#、-、()及其组合';
}
$errAddress.html(_errorHtml);
$errAddress.find('b').html(message);
$errAddress.show();
return false;
} else {
$errAddress.html(_rightHtml).show();
return true;
}
},
mobile: function(phone) {
var message = '';
if (!phoneReg.test(phone)) {
message = '手机号码不能为空';
if ($.trim(phone) !== '') {
message = '你输入的联系电话格式不正确';
}
$errPhone.html(_errorHtml);
$errPhone.find('b').html(message);
$errPhone.show();
return false;
} else {
$errPhone.html(_rightHtml).show();
return true;
}
},
tel: function(telCode, tel) {
if (telCode === '' && tel === '') {
_rightHtml = '';
}
if ((!!telCode && !telCodeReg.test(telCode)) || (!!tel && !telReg.test(tel)) ||
(telCodeReg.test(telCode) && !telReg.test(tel)) ||
(!telCodeReg.test(telCode) && telReg.test(tel))) {
$errTel.html(_errorHtml);
$errTel.find('b').html('你输入的电话格式不正确');
$errTel.show();
return false;
} else {
$errTel.html(_rightHtml).show();
return true;
}
}
};
}
// 保存地址
... ... @@ -470,7 +364,12 @@ function saveAddress(id) {
phoneNum: tel
};
var validate = validateForm();
if (!validate.name(name) || !validate.city(province, city, county) || !validate.address(address) || !validate.mobile(phone) || !validate.tel(telCode, tel)) {
if (!validate.name(name) ||
!validate.city(province, city, county) ||
!validate.address(address) ||
!validate.mobile(phone) ||
!validate.tel(telCode, tel)) {
return;
}
... ... @@ -490,89 +389,210 @@ function saveAddress(id) {
}
});
}
function validateForm() {
var $err_name = $name.siblings('.error'),
$err_province = $province.siblings('.error'),
$err_address = $address.siblings('.error'),
$err_phone = $phone.siblings('.error'),
$err_tel = $tel.siblings('.error');
var nameReg = /^[\u4e00-\u9fa5]{2,5}$/;
var addressReg = /^[a-zA-Z0-9-#()()\u4e00-\u9fa5]+$/;
var phoneReg = /^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
var telCodeReg = /^[0-9]{3,4}$/;
var telReg = /^[0-9]{8}$/;
var _right_html = '<i class="order-icon icon-right"></i>';
var _error_html = '<i class="order-icon icon-error"></i><b></b>';
return {
name: function(name) {
if (!nameReg.test(name)) {
$err_name.html(_error_html);
$err_name.find('b').html('真实姓名至少2个中文,最多5个中文');
$err_name.show();
return false;
} else {
$err_name.html(_right_html).show();
return true;
}
},
city: function(province, city, county) {
if (province === '0' || city === '0' || county === '0') {
$err_province.html(_error_html);
$err_province.find('b').html('请填写完整的省市区信息');
$err_province.show();
return false;
} else {
$err_province.html(_right_html).show();
return true;
}
},
address: function(address) {
if (!addressReg.test(address)) {
var message = '详细地址不能为空';
if ($.trim(address) !== '') {
message = '只能包含数字、字母、汉字、#、-、()及其组合';
// 编辑订单
function editOrder(id) {
var options = {
mask: true,
btns: [
{
id: 'edit-sure',
name: '确定',
btnClass: ['edit-sure'],
cb: function() {
saveAddress(id);
}
},
{
id: 'edit-no',
name: '取消',
btnClass: ['edit-no'],
cb: function() {
active.close();
}
$err_address.html(_error_html);
$err_address.find('b').html(message);
$err_address.show();
return false;
} else {
$err_address.html(_right_html).show();
return true;
}
},
mobile: function(phone) {
if (!phoneReg.test(phone)) {
var message = '手机号码不能为空';
if ($.trim(phone) !== '') {
message = '你输入的联系电话格式不正确';
],
content: $dialogEdit.html(),
className: 'edit-order-dialog'
};
return new Dialog(options);
}
// 地址操作
function newAddress(id) {
var code, codeId;
var pId = id || 0; // 如果没有传id则获取所有省列表
var addressCodeReg = /[0-9]{2}/gi;
var validate = validateForm();
$addressManage = $('.edit-order-dialog');
$province = $addressManage.find('select[name="province"]');
$city = $addressManage.find('select[name="city"]');
$county = $addressManage.find('select[name="county"]');
$selectList = $addressManage.find('select[name="province"],select[name="city"]');
$name = $addressManage.find('.inp[name="name"]');
$phone = $addressManage.find('.inp[name="phone"]');
$address = $addressManage.find('.inp[name="address"]');
$telCode = $addressManage.find('.inp[name="tel-code"]');
$tel = $addressManage.find('.inp[name="tel"]');
code = $province.data('areacode') + '';
codeId = !!code && code.match(addressCodeReg);
// 获取省
getAddress({
id: pId,
type: 'getProvince',
selectId: codeId[0]
}, function() {
var provinceId = $province.val();
if (provinceId !== '0') {
isProvinceChecked = true;
// 如果获取的省有默认选中项则获取市
getAddress({
id: provinceId,
type: 'getCity',
selectId: '' + codeId[0] + codeId[1]
}, function() {
var cityId = $city.val();
// 如果获取的市有默认选中项则获取县
if (cityId !== '0') {
getAddress({
id: cityId,
type: 'getCounty',
selectId: code
});
}
$err_phone.html(_error_html);
$err_phone.find('b').html(message);
$err_phone.show();
return false;
});
} else {
$addressManage.find('select[name="city"]').html('<option value="0">请选择城市</option>');
$addressManage.find('select[name="county"]').html('<option value="0">请选择区县</option>');
}
});
// 发生change事件时获取下一级地址
$selectList.change(function() {
var $this = $(this);
if ($this.attr('name') === 'province') {
if ($this.val() === '0') {
$addressManage.find('select[name="city"]').html('<option value="0">请选择城市</option>');
$addressManage.find('select[name="county"]').html('<option value="0">请选择区县</option>');
} else {
$err_phone.html(_right_html).show();
return true;
}
},
tel: function(telCode, tel) {
if (telCode === '' && tel === '') {
_right_html = '';
getAddress({
id: $this.val(),
type: 'getCity'
}, function() {
isProvinceChecked = true;
});
$addressManage.find('select[name="county"]').html('<option value="0">请选择区县</option>');
}
if ((!!telCode && !telCodeReg.test(telCode)) || (!!tel && !telReg.test(tel)) ||
(telCodeReg.test(telCode) && !telReg.test(tel)) ||
(!telCodeReg.test(telCode) && telReg.test(tel))) {
$err_tel.html(_error_html);
$err_tel.find('b').html('你输入的电话格式不正确');
$err_tel.show();
return false;
}
if ($this.attr('name') === 'city' && isProvinceChecked) {
if ($this.val() === '0') {
$addressManage.find('select[name="county"]').html('<option value="0">请选择区县</option>');
} else {
$err_tel.html(_right_html).show();
return true;
getAddress({
id: $this.val(),
type: 'getCounty'
});
}
}
};
});
$name.on('blur', function() {
validate.name($.trim($name.val()));
});
$address.on('blur', function() {
validate.address($.trim($address.val()));
});
$phone.on('blur', function() {
validate.mobile($.trim($phone.val()));
});
$tel.on('blur', function() {
validate.tel($.trim($telCode.val()), $.trim($tel.val()));
});
$county.on('change', function() {
validate.city($province.val(), $city.val(), $county.val());
});
}
// 关闭查看物流
$('.logistics').on('click', '.close-logistics', function() {
$(this).closest('.logistics').addClass('hide');
});
// 订单列表&订单详情【取消订单和确认收货】
$('.me-orders, .order-detail').on('click', '.cancel-order', function() {
// 取消订单
active = cancelFactory($(this).closest('.order, .order-detail').data('id'));
active.show();
}).on('click', '.confirm-received', function() {
var id = $(this).closest('.order, .order-detail').data('id');
// 确认收货
active = new Confirm({
cb: function() {
$.ajax({
type: 'POST',
url: '/home/orders/confirmorder',
data: {
orderCode: id
}
}).then(function(data) {
if (data.code === 200) {
active.close();
history.go(0);
}
});
},
content: '您确定要确认收货吗?'
});
active.show();
}).on('click', '.order-delete', function() {
var id = $(this).closest('.order, .order-detail').data('id');
active = deleteOrder(id, this);
active.show();
}).on('click', '.edit-order', function() {
$(this).addClass('edit-order-active');
active = editOrder($(this).closest('.order, .order-detail').data('id'));
active.show();
newAddress(0);
}).on('click', '.rebuy', function() {
var id = $(this).closest('.order, .order-detail').data('id');
buyAgain(id);
});
$tpl.remove();
$dialogEdit.remove();
// 查看物流
$('.check-logistics').click(function() {
var $this = $(this);
var orderId = $this.data('id');
var time = $this.data('time');
var paytype = $this.data('paytype');
ordersApi.getExpress(orderId, paytype, time).then(function(result) {
var tpl = require('hbs/home/orders/express.hbs');
if (result.logistics.length === 0) {
return;
}
$this.siblings('.logistics').html(tpl(result)).removeClass('hide');
});
});
... ...
... ... @@ -7,3 +7,15 @@ exports.getExpress = function(orderId, type, time) {
time: time
});
};
exports.refund = function(orderId, reasonId, reason) {
return $.get('/home/orders/refund', {
orderId: orderId,
reasonId: reasonId,
reason: reason
});
};
exports.refundReason = function() {
return $.get('/home/orders/refundreason');
};
... ...