Authored by htoooth

refactor

@@ -17,7 +17,10 @@ var html = $tpl.html(); @@ -17,7 +17,10 @@ var html = $tpl.html();
17 17
18 var active; 18 var active;
19 var $dialogEdit = $('#edit-dialog-tpl'); 19 var $dialogEdit = $('#edit-dialog-tpl');
20 -var $addressManage, $province, $city, $county, $selectList, $name, $phone, $address, $telCode, $tel, isProvinceChecked = false; 20 +var $addressManage, $province, $city,
  21 + $county, $selectList, $name, $phone, $address, $telCode, $tel;
  22 +
  23 +var isProvinceChecked = false;
21 24
22 function cancelFactory(id) { 25 function cancelFactory(id) {
23 var options = { 26 var options = {
@@ -68,9 +71,15 @@ function cancelFactory(id) { @@ -68,9 +71,15 @@ function cancelFactory(id) {
68 71
69 return new Dialog(options); 72 return new Dialog(options);
70 } 73 }
71 -function showMessgaeDialog(message_icon, message_title, message_summary, cb) { 74 +
  75 +function showMessgaeDialog(messageIcon, messageTitle, messageSummary, cb) {
72 var template = require('hbs/home/orders/dialog-message.hbs'); 76 var template = require('hbs/home/orders/dialog-message.hbs');
73 - var html = template({messageIcon: message_icon, messageTitle: message_title, messageSummary: message_summary}); 77 + var messageHtml = template({
  78 + messageIcon: messageIcon,
  79 + messageTitle: messageTitle,
  80 + messageSummary: messageSummary}
  81 + );
  82 +
74 var options = { 83 var options = {
75 mask: true, 84 mask: true,
76 btns: [ 85 btns: [
@@ -84,112 +93,13 @@ function showMessgaeDialog(message_icon, message_title, message_summary, cb) { @@ -84,112 +93,13 @@ function showMessgaeDialog(message_icon, message_title, message_summary, cb) {
84 } 93 }
85 } 94 }
86 ], 95 ],
87 - content: html, 96 + content: messageHtml,
88 className: 'message-dialog' 97 className: 'message-dialog'
89 }; 98 };
90 99
91 return new Dialog(options); 100 return new Dialog(options);
92 -  
93 -}  
94 -  
95 -// 编辑订单  
96 -function editOrder(id) {  
97 - var options = {  
98 - mask: true,  
99 - btns: [  
100 - {  
101 - id: 'edit-sure',  
102 - name: '确定',  
103 - btnClass: ['edit-sure'],  
104 - cb: function() {  
105 - saveAddress(id);  
106 - }  
107 - },  
108 - {  
109 - id: 'edit-no',  
110 - name: '取消',  
111 - btnClass: ['edit-no'],  
112 - cb: function() {  
113 - active.close();  
114 - }  
115 - }  
116 - ],  
117 - content: $dialogEdit.html(),  
118 - className: 'edit-order-dialog'  
119 - };  
120 -  
121 - return new Dialog(options);  
122 } 101 }
123 102
124 -$tpl.remove();  
125 -$dialogEdit.remove();  
126 -  
127 -// 查看物流  
128 -$('.check-logistics').click(function() {  
129 - var $this = $(this);  
130 - var orderId = $this.data('id');  
131 - var time = $this.data('time');  
132 - var paytype = $this.data('paytype');  
133 -  
134 - ordersApi.getExpress(orderId, paytype, time).then(function(result) {  
135 - var tpl = require('hbs/home/orders/express.hbs');  
136 -  
137 - if (result.logistics.length === 0) {  
138 - return;  
139 - }  
140 -  
141 - $this.siblings('.logistics').html(tpl(result)).removeClass('hide');  
142 - });  
143 -  
144 -});  
145 -  
146 -// 关闭查看物流  
147 -$('.logistics').on('click', '.close-logistics', function() {  
148 - $(this).closest('.logistics').addClass('hide');  
149 -});  
150 -  
151 -// 订单列表&订单详情【取消订单和确认收货】  
152 -$('.me-orders, .order-detail').on('click', '.cancel-order', function(e) {  
153 -  
154 - // 取消订单  
155 - active = cancelFactory($(this).closest('.order, .order-detail').data('id'));  
156 - active.show();  
157 -}).on('click', '.confirm-received', function(e) {  
158 - var id = $(this).closest('.order, .order-detail').data('id');  
159 -  
160 - // 确认收货  
161 - active = new Confirm({  
162 - cb: function() {  
163 - $.ajax({  
164 - type: 'POST',  
165 - url: '/home/orders/confirmorder',  
166 - data: {  
167 - orderCode: id  
168 - }  
169 - }).then(function(data) {  
170 - if (data.code === 200) {  
171 - active.close();  
172 - history.go(0);  
173 - }  
174 - });  
175 - },  
176 - content: '您确定要确认收货吗?'  
177 - });  
178 - active.show();  
179 -}).on('click', '.order-delete', function() {  
180 - var id = $(this).closest('.order, .order-detail').data('id');  
181 - active = deleteOrder(id, this);  
182 - active.show();  
183 -}).on('click', '.edit-order', function() {  
184 - $(this).addClass('edit-order-active');  
185 - active = editOrder($(this).closest('.order, .order-detail').data('id'));  
186 - active.show();  
187 - newAddress(0);  
188 -}).on('click', '.rebuy', function() {  
189 - var id = $(this).closest('.order, .order-detail').data('id');  
190 - buyAgain(id);  
191 -});  
192 -  
193 // 再次购买 103 // 再次购买
194 function buyAgain(id) { 104 function buyAgain(id) {
195 $.ajax({ 105 $.ajax({
@@ -204,6 +114,7 @@ function buyAgain(id) { @@ -204,6 +114,7 @@ function buyAgain(id) {
204 } 114 }
205 }); 115 });
206 } 116 }
  117 +
207 function deleteOrder(id, obj) { 118 function deleteOrder(id, obj) {
208 // 是否为彻底删除 119 // 是否为彻底删除
209 var isFullyDelete = true; 120 var isFullyDelete = true;
@@ -211,7 +122,7 @@ function deleteOrder(id, obj) { @@ -211,7 +122,7 @@ function deleteOrder(id, obj) {
211 var deleteMessage = isFullyDelete ? '您确定要永久删除订单吗?' : '您确定要删除订单吗?'; 122 var deleteMessage = isFullyDelete ? '您确定要永久删除订单吗?' : '您确定要删除订单吗?';
212 var deleteTip = isFullyDelete ? '永久删除后,订单将无法恢复,您将无法对该订单的商品申请售后服务,请谨慎操作。' : '删除后,您可以在订单回收站找回该订单,也可以做永久删除。'; 123 var deleteTip = isFullyDelete ? '永久删除后,订单将无法恢复,您将无法对该订单的商品申请售后服务,请谨慎操作。' : '删除后,您可以在订单回收站找回该订单,也可以做永久删除。';
213 var insert = require('hbs/home/orders/delete-order.hbs'); 124 var insert = require('hbs/home/orders/delete-order.hbs');
214 - var html = insert({deleteMessage: deleteMessage, deleteTip: deleteTip}); 125 + var deleteMessageHtml = insert({deleteMessage: deleteMessage, deleteTip: deleteTip});
215 var options = { 126 var options = {
216 mask: false, 127 mask: false,
217 btns: [ 128 btns: [
@@ -248,120 +159,47 @@ function deleteOrder(id, obj) { @@ -248,120 +159,47 @@ function deleteOrder(id, obj) {
248 } 159 }
249 } 160 }
250 ], 161 ],
251 - content: html, 162 + content: deleteMessageHtml,
252 className: 'delete-dialog' 163 className: 'delete-dialog'
253 }; 164 };
  165 +
254 return new Dialog(options); 166 return new Dialog(options);
255 } 167 }
256 168
257 -// 地址操作  
258 -function newAddress(id) {  
259 - var code, codeId;  
260 -  
261 - var pId = id || 0; // 如果没有传id则获取所有省列表  
262 -  
263 - var addressCodeReg = /[0-9]{2}/gi;  
264 -  
265 - $addressManage = $('.edit-order-dialog');  
266 - $province = $addressManage.find('select[name="province"]');  
267 - $city = $addressManage.find('select[name="city"]');  
268 - $county = $addressManage.find('select[name="county"]');  
269 - $selectList = $addressManage.find('select[name="province"],select[name="city"]');  
270 - $name = $addressManage.find('.inp[name="name"]');  
271 - $phone = $addressManage.find('.inp[name="phone"]');  
272 - $address = $addressManage.find('.inp[name="address"]');  
273 - $telCode = $addressManage.find('.inp[name="tel-code"]');  
274 - $tel = $addressManage.find('.inp[name="tel"]');  
275 -  
276 - var validate = validateForm();  
277 -  
278 - code = $province.data('areacode') + '';  
279 - codeId = !!code && code.match(addressCodeReg);  
280 -  
281 - // 获取省  
282 - getAddress({  
283 - id: pId,  
284 - type: 'getProvince',  
285 - selectId: codeId[0]  
286 - }, function() {  
287 -  
288 - var provinceId = $province.val();  
289 -  
290 - if (provinceId !== '0') {  
291 - isProvinceChecked = true; 169 +// 构建select下拉选项
  170 +function structureOption($obj, data, selectId) {
292 171
293 - // 如果获取的省有默认选中项则获取市  
294 - getAddress({  
295 - id: provinceId,  
296 - type: 'getCity',  
297 - selectId: '' + codeId[0] + codeId[1]  
298 - }, function() { 172 + var i,
  173 + optionHtml = '',
  174 + defaultOption,
  175 + isStar = '';
299 176
300 - var cityId = $city.val(); 177 + for (i = 0; i < data.length; i++) {
301 178
302 - // 如果获取的市有默认选中项则获取县  
303 - if (cityId !== '0') {  
304 - getAddress({  
305 - id: cityId,  
306 - type: 'getCounty',  
307 - selectId: code  
308 - });  
309 - }  
310 - }); 179 + if (data[i].is_support_express === 'Y') {
  180 + isStar = '*';
311 } else { 181 } else {
312 - $addressManage.find('select[name="city"]').html('<option value="0">请选择城市</option>');  
313 - $addressManage.find('select[name="county"]').html('<option value="0">请选择区县</option>'); 182 + isStar = '';
314 } 183 }
315 - });  
316 184
317 - // 发生change事件时获取下一级地址  
318 - $selectList.change(function() {  
319 -  
320 - var $this = $(this); 185 + if (data[i].id === selectId) {
  186 + optionHtml += '<option selected value="' + data[i].id + '">' + isStar + data[i].caption + '</option>';
  187 + } else {
  188 + optionHtml += '<option value="' + data[i].id + '">' + isStar + data[i].caption + '</option>';
  189 + }
  190 + }
321 191
322 - if ($this.attr('name') === 'province') {  
323 192
324 - if ($this.val() === '0') {  
325 - $addressManage.find('select[name="city"]').html('<option value="0">请选择城市</option>');  
326 - $addressManage.find('select[name="county"]').html('<option value="0">请选择区县</option>');  
327 - } else {  
328 - getAddress({  
329 - id: $this.val(),  
330 - type: 'getCity'  
331 - }, function() {  
332 - isProvinceChecked = true;  
333 - });  
334 - $addressManage.find('select[name="county"]').html('<option value="0">请选择区县</option>');  
335 - }  
336 - } 193 + if ($obj.attr('name') === 'province') {
  194 + defaultOption = '<option value="0">请选择省份</option>';
  195 + } else if ($obj.attr('name') === 'city') {
  196 + defaultOption = '<option value="0">请选择城市</option>';
337 197
338 - if ($this.attr('name') === 'city' && isProvinceChecked) { 198 + } else if ($obj.attr('name') === 'county') {
  199 + defaultOption = '<option value="0">请选择区县</option>';
  200 + }
339 201
340 - if ($this.val() === '0') {  
341 - $addressManage.find('select[name="county"]').html('<option value="0">请选择区县</option>');  
342 - } else {  
343 - getAddress({  
344 - id: $this.val(),  
345 - type: 'getCounty'  
346 - });  
347 - }  
348 - }  
349 - });  
350 - $name.on('blur', function() {  
351 - validate.name($.trim($name.val()));  
352 - });  
353 - $address.on('blur', function() {  
354 - validate.address($.trim($address.val()));  
355 - });  
356 - $phone.on('blur', function() {  
357 - validate.mobile($.trim($phone.val()));  
358 - });  
359 - $tel.on('blur', function() {  
360 - validate.tel($.trim($telCode.val()), $.trim($tel.val()));  
361 - });  
362 - $county.on('change', function() {  
363 - validate.city($province.val(), $city.val(), $county.val());  
364 - }); 202 + $obj.html(defaultOption + optionHtml);
365 } 203 }
366 204
367 /** 205 /**
@@ -371,7 +209,6 @@ function newAddress(id) { @@ -371,7 +209,6 @@ function newAddress(id) {
371 * id !== 0 && type: 'getProvince' 获取所有省,默认选中用户所在的省 209 * id !== 0 && type: 'getProvince' 获取所有省,默认选中用户所在的省
372 */ 210 */
373 function getAddress(d, callback) { 211 function getAddress(d, callback) {
374 -  
375 var $obj; 212 var $obj;
376 var selectId = d.selectId; 213 var selectId = d.selectId;
377 214
@@ -396,51 +233,108 @@ function getAddress(d, callback) { @@ -396,51 +233,108 @@ function getAddress(d, callback) {
396 data: { 233 data: {
397 id: d.id * 1 234 id: d.id * 1
398 } 235 }
399 - }).then(function(d) { 236 + }).then(function(result) {
400 237
401 - structureOption($obj, d.data, selectId); 238 + structureOption($obj, result.data, selectId);
402 239
403 if (typeof callback === 'function') { 240 if (typeof callback === 'function') {
404 - callback(); 241 + return callback();
405 } 242 }
406 243
407 }); 244 });
408 } 245 }
409 246
410 -// 构建select下拉选项  
411 -function structureOption($obj, data, selectId) {  
412 -  
413 - var i,  
414 - optionHtml = '',  
415 - defaultOption,  
416 - isStar = '';  
417 -  
418 - for (i = 0; i < data.length; i++) {  
419 -  
420 - if (data[i].is_support_express === 'Y') {  
421 - isStar = '*';  
422 - } else {  
423 - isStar = '';  
424 - }  
425 -  
426 - if (data[i].id === selectId) {  
427 - optionHtml += '<option selected value="' + data[i].id + '">' + isStar + data[i].caption + '</option>';  
428 - } else {  
429 - optionHtml += '<option value="' + data[i].id + '">' + isStar + data[i].caption + '</option>';  
430 - }  
431 - } 247 +function validateForm() {
  248 + var $errName = $name.siblings('.error'),
  249 + $errProvince = $province.siblings('.error'),
  250 + $errAddress = $address.siblings('.error'),
  251 + $errPhone = $phone.siblings('.error'),
  252 + $errTel = $tel.siblings('.error');
432 253
  254 + var nameReg = /^[\u4e00-\u9fa5]{2,5}$/;
  255 + var addressReg = /^[a-zA-Z0-9-#()()\u4e00-\u9fa5]+$/;
  256 + var phoneReg = /^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
  257 + var telCodeReg = /^[0-9]{3,4}$/;
  258 + var telReg = /^[0-9]{8}$/;
  259 + var _rightHtml = '<i class="order-icon icon-right"></i>';
  260 + var _errorHtml = '<i class="order-icon icon-error"></i><b></b>';
433 261
434 - if ($obj.attr('name') === 'province') {  
435 - defaultOption = '<option value="0">请选择省份</option>';  
436 - } else if ($obj.attr('name') === 'city') {  
437 - defaultOption = '<option value="0">请选择城市</option>'; 262 + return {
  263 + name: function(name) {
  264 + if (!nameReg.test(name)) {
  265 + $errName.html(_errorHtml);
  266 + $errName.find('b').html('真实姓名至少2个中文,最多5个中文');
  267 + $errName.show();
  268 + return false;
  269 + } else {
  270 + $errName.html(_rightHtml).show();
  271 + return true;
  272 + }
  273 + },
  274 + city: function(province, city, county) {
  275 + if (province === '0' || city === '0' || county === '0') {
  276 + $errProvince.html(_errorHtml);
  277 + $errProvince.find('b').html('请填写完整的省市区信息');
  278 + $errProvince.show();
  279 + return false;
  280 + } else {
  281 + $errProvince.html(_rightHtml).show();
  282 + return true;
  283 + }
  284 + },
  285 + address: function(address) {
  286 + var message = '';
438 287
439 - } else if ($obj.attr('name') === 'county') {  
440 - defaultOption = '<option value="0">请选择区县</option>';  
441 - } 288 + if (!addressReg.test(address)) {
  289 + message = '详细地址不能为空';
442 290
443 - $obj.html(defaultOption + optionHtml); 291 + if ($.trim(address) !== '') {
  292 + message = '只能包含数字、字母、汉字、#、-、()及其组合';
  293 + }
  294 + $errAddress.html(_errorHtml);
  295 + $errAddress.find('b').html(message);
  296 + $errAddress.show();
  297 + return false;
  298 + } else {
  299 + $errAddress.html(_rightHtml).show();
  300 + return true;
  301 + }
  302 + },
  303 + mobile: function(phone) {
  304 + var message = '';
  305 +
  306 + if (!phoneReg.test(phone)) {
  307 + message = '手机号码不能为空';
  308 +
  309 + if ($.trim(phone) !== '') {
  310 + message = '你输入的联系电话格式不正确';
  311 + }
  312 + $errPhone.html(_errorHtml);
  313 + $errPhone.find('b').html(message);
  314 + $errPhone.show();
  315 + return false;
  316 + } else {
  317 + $errPhone.html(_rightHtml).show();
  318 + return true;
  319 + }
  320 + },
  321 + tel: function(telCode, tel) {
  322 + if (telCode === '' && tel === '') {
  323 + _rightHtml = '';
  324 + }
  325 + if ((!!telCode && !telCodeReg.test(telCode)) || (!!tel && !telReg.test(tel)) ||
  326 + (telCodeReg.test(telCode) && !telReg.test(tel)) ||
  327 + (!telCodeReg.test(telCode) && telReg.test(tel))) {
  328 + $errTel.html(_errorHtml);
  329 + $errTel.find('b').html('你输入的电话格式不正确');
  330 + $errTel.show();
  331 + return false;
  332 + } else {
  333 + $errTel.html(_rightHtml).show();
  334 + return true;
  335 + }
  336 + }
  337 + };
444 } 338 }
445 339
446 // 保存地址 340 // 保存地址
@@ -470,7 +364,12 @@ function saveAddress(id) { @@ -470,7 +364,12 @@ function saveAddress(id) {
470 phoneNum: tel 364 phoneNum: tel
471 }; 365 };
472 var validate = validateForm(); 366 var validate = validateForm();
473 - if (!validate.name(name) || !validate.city(province, city, county) || !validate.address(address) || !validate.mobile(phone) || !validate.tel(telCode, tel)) { 367 +
  368 + if (!validate.name(name) ||
  369 + !validate.city(province, city, county) ||
  370 + !validate.address(address) ||
  371 + !validate.mobile(phone) ||
  372 + !validate.tel(telCode, tel)) {
474 return; 373 return;
475 } 374 }
476 375
@@ -490,89 +389,210 @@ function saveAddress(id) { @@ -490,89 +389,210 @@ function saveAddress(id) {
490 } 389 }
491 }); 390 });
492 } 391 }
493 -function validateForm() {  
494 - var $err_name = $name.siblings('.error'),  
495 - $err_province = $province.siblings('.error'),  
496 - $err_address = $address.siblings('.error'),  
497 - $err_phone = $phone.siblings('.error'),  
498 - $err_tel = $tel.siblings('.error');  
499 392
500 - var nameReg = /^[\u4e00-\u9fa5]{2,5}$/;  
501 - var addressReg = /^[a-zA-Z0-9-#()()\u4e00-\u9fa5]+$/;  
502 - var phoneReg = /^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;  
503 - var telCodeReg = /^[0-9]{3,4}$/;  
504 - var telReg = /^[0-9]{8}$/;  
505 - var _right_html = '<i class="order-icon icon-right"></i>';  
506 - var _error_html = '<i class="order-icon icon-error"></i><b></b>';  
507 -  
508 - return {  
509 - name: function(name) {  
510 - if (!nameReg.test(name)) {  
511 - $err_name.html(_error_html);  
512 - $err_name.find('b').html('真实姓名至少2个中文,最多5个中文');  
513 - $err_name.show();  
514 - return false;  
515 - } else {  
516 - $err_name.html(_right_html).show();  
517 - return true;  
518 - }  
519 - },  
520 - city: function(province, city, county) {  
521 - if (province === '0' || city === '0' || county === '0') {  
522 - $err_province.html(_error_html);  
523 - $err_province.find('b').html('请填写完整的省市区信息');  
524 - $err_province.show();  
525 - return false;  
526 - } else {  
527 - $err_province.html(_right_html).show();  
528 - return true;  
529 - }  
530 - },  
531 - address: function(address) {  
532 - if (!addressReg.test(address)) {  
533 - var message = '详细地址不能为空';  
534 - if ($.trim(address) !== '') {  
535 - message = '只能包含数字、字母、汉字、#、-、()及其组合'; 393 +// 编辑订单
  394 +function editOrder(id) {
  395 + var options = {
  396 + mask: true,
  397 + btns: [
  398 + {
  399 + id: 'edit-sure',
  400 + name: '确定',
  401 + btnClass: ['edit-sure'],
  402 + cb: function() {
  403 + saveAddress(id);
  404 + }
  405 + },
  406 + {
  407 + id: 'edit-no',
  408 + name: '取消',
  409 + btnClass: ['edit-no'],
  410 + cb: function() {
  411 + active.close();
536 } 412 }
537 - $err_address.html(_error_html);  
538 - $err_address.find('b').html(message);  
539 - $err_address.show();  
540 - return false;  
541 - } else {  
542 - $err_address.html(_right_html).show();  
543 - return true;  
544 } 413 }
545 - },  
546 - mobile: function(phone) {  
547 - if (!phoneReg.test(phone)) {  
548 - var message = '手机号码不能为空';  
549 - if ($.trim(phone) !== '') {  
550 - message = '你输入的联系电话格式不正确'; 414 + ],
  415 + content: $dialogEdit.html(),
  416 + className: 'edit-order-dialog'
  417 + };
  418 +
  419 + return new Dialog(options);
  420 +}
  421 +
  422 +// 地址操作
  423 +function newAddress(id) {
  424 + var code, codeId;
  425 + var pId = id || 0; // 如果没有传id则获取所有省列表
  426 + var addressCodeReg = /[0-9]{2}/gi;
  427 + var validate = validateForm();
  428 +
  429 + $addressManage = $('.edit-order-dialog');
  430 + $province = $addressManage.find('select[name="province"]');
  431 + $city = $addressManage.find('select[name="city"]');
  432 + $county = $addressManage.find('select[name="county"]');
  433 + $selectList = $addressManage.find('select[name="province"],select[name="city"]');
  434 + $name = $addressManage.find('.inp[name="name"]');
  435 + $phone = $addressManage.find('.inp[name="phone"]');
  436 + $address = $addressManage.find('.inp[name="address"]');
  437 + $telCode = $addressManage.find('.inp[name="tel-code"]');
  438 + $tel = $addressManage.find('.inp[name="tel"]');
  439 +
  440 + code = $province.data('areacode') + '';
  441 + codeId = !!code && code.match(addressCodeReg);
  442 +
  443 + // 获取省
  444 + getAddress({
  445 + id: pId,
  446 + type: 'getProvince',
  447 + selectId: codeId[0]
  448 + }, function() {
  449 +
  450 + var provinceId = $province.val();
  451 +
  452 + if (provinceId !== '0') {
  453 + isProvinceChecked = true;
  454 +
  455 + // 如果获取的省有默认选中项则获取市
  456 + getAddress({
  457 + id: provinceId,
  458 + type: 'getCity',
  459 + selectId: '' + codeId[0] + codeId[1]
  460 + }, function() {
  461 +
  462 + var cityId = $city.val();
  463 +
  464 + // 如果获取的市有默认选中项则获取县
  465 + if (cityId !== '0') {
  466 + getAddress({
  467 + id: cityId,
  468 + type: 'getCounty',
  469 + selectId: code
  470 + });
551 } 471 }
552 - $err_phone.html(_error_html);  
553 - $err_phone.find('b').html(message);  
554 - $err_phone.show();  
555 - return false; 472 + });
  473 + } else {
  474 + $addressManage.find('select[name="city"]').html('<option value="0">请选择城市</option>');
  475 + $addressManage.find('select[name="county"]').html('<option value="0">请选择区县</option>');
  476 + }
  477 + });
  478 +
  479 + // 发生change事件时获取下一级地址
  480 + $selectList.change(function() {
  481 +
  482 + var $this = $(this);
  483 +
  484 + if ($this.attr('name') === 'province') {
  485 +
  486 + if ($this.val() === '0') {
  487 + $addressManage.find('select[name="city"]').html('<option value="0">请选择城市</option>');
  488 + $addressManage.find('select[name="county"]').html('<option value="0">请选择区县</option>');
556 } else { 489 } else {
557 - $err_phone.html(_right_html).show();  
558 - return true;  
559 - }  
560 - },  
561 - tel: function(telCode, tel) {  
562 - if (telCode === '' && tel === '') {  
563 - _right_html = ''; 490 + getAddress({
  491 + id: $this.val(),
  492 + type: 'getCity'
  493 + }, function() {
  494 + isProvinceChecked = true;
  495 + });
  496 + $addressManage.find('select[name="county"]').html('<option value="0">请选择区县</option>');
564 } 497 }
565 - if ((!!telCode && !telCodeReg.test(telCode)) || (!!tel && !telReg.test(tel)) ||  
566 - (telCodeReg.test(telCode) && !telReg.test(tel)) ||  
567 - (!telCodeReg.test(telCode) && telReg.test(tel))) {  
568 - $err_tel.html(_error_html);  
569 - $err_tel.find('b').html('你输入的电话格式不正确');  
570 - $err_tel.show();  
571 - return false; 498 + }
  499 +
  500 + if ($this.attr('name') === 'city' && isProvinceChecked) {
  501 +
  502 + if ($this.val() === '0') {
  503 + $addressManage.find('select[name="county"]').html('<option value="0">请选择区县</option>');
572 } else { 504 } else {
573 - $err_tel.html(_right_html).show();  
574 - return true; 505 + getAddress({
  506 + id: $this.val(),
  507 + type: 'getCounty'
  508 + });
575 } 509 }
576 } 510 }
577 - }; 511 + });
  512 + $name.on('blur', function() {
  513 + validate.name($.trim($name.val()));
  514 + });
  515 + $address.on('blur', function() {
  516 + validate.address($.trim($address.val()));
  517 + });
  518 + $phone.on('blur', function() {
  519 + validate.mobile($.trim($phone.val()));
  520 + });
  521 + $tel.on('blur', function() {
  522 + validate.tel($.trim($telCode.val()), $.trim($tel.val()));
  523 + });
  524 + $county.on('change', function() {
  525 + validate.city($province.val(), $city.val(), $county.val());
  526 + });
578 } 527 }
  528 +
  529 +// 关闭查看物流
  530 +$('.logistics').on('click', '.close-logistics', function() {
  531 + $(this).closest('.logistics').addClass('hide');
  532 +});
  533 +
  534 +// 订单列表&订单详情【取消订单和确认收货】
  535 +$('.me-orders, .order-detail').on('click', '.cancel-order', function() {
  536 +
  537 + // 取消订单
  538 + active = cancelFactory($(this).closest('.order, .order-detail').data('id'));
  539 + active.show();
  540 +}).on('click', '.confirm-received', function() {
  541 + var id = $(this).closest('.order, .order-detail').data('id');
  542 +
  543 + // 确认收货
  544 + active = new Confirm({
  545 + cb: function() {
  546 + $.ajax({
  547 + type: 'POST',
  548 + url: '/home/orders/confirmorder',
  549 + data: {
  550 + orderCode: id
  551 + }
  552 + }).then(function(data) {
  553 + if (data.code === 200) {
  554 + active.close();
  555 + history.go(0);
  556 + }
  557 + });
  558 + },
  559 + content: '您确定要确认收货吗?'
  560 + });
  561 + active.show();
  562 +}).on('click', '.order-delete', function() {
  563 + var id = $(this).closest('.order, .order-detail').data('id');
  564 +
  565 + active = deleteOrder(id, this);
  566 + active.show();
  567 +}).on('click', '.edit-order', function() {
  568 + $(this).addClass('edit-order-active');
  569 + active = editOrder($(this).closest('.order, .order-detail').data('id'));
  570 + active.show();
  571 + newAddress(0);
  572 +}).on('click', '.rebuy', function() {
  573 + var id = $(this).closest('.order, .order-detail').data('id');
  574 +
  575 + buyAgain(id);
  576 +});
  577 +
  578 +$tpl.remove();
  579 +$dialogEdit.remove();
  580 +
  581 +// 查看物流
  582 +$('.check-logistics').click(function() {
  583 + var $this = $(this);
  584 + var orderId = $this.data('id');
  585 + var time = $this.data('time');
  586 + var paytype = $this.data('paytype');
  587 +
  588 + ordersApi.getExpress(orderId, paytype, time).then(function(result) {
  589 + var tpl = require('hbs/home/orders/express.hbs');
  590 +
  591 + if (result.logistics.length === 0) {
  592 + return;
  593 + }
  594 +
  595 + $this.siblings('.logistics').html(tpl(result)).removeClass('hide');
  596 + });
  597 +
  598 +});
@@ -7,3 +7,15 @@ exports.getExpress = function(orderId, type, time) { @@ -7,3 +7,15 @@ exports.getExpress = function(orderId, type, time) {
7 time: time 7 time: time
8 }); 8 });
9 }; 9 };
  10 +
  11 +exports.refund = function(orderId, reasonId, reason) {
  12 + return $.get('/home/orders/refund', {
  13 + orderId: orderId,
  14 + reasonId: reasonId,
  15 + reason: reason
  16 + });
  17 +};
  18 +
  19 +exports.refundReason = function() {
  20 + return $.get('/home/orders/refundreason');
  21 +};