Authored by biao

购物车编辑功能更改,code review by LZF

@@ -34,6 +34,11 @@ var $chosePanel = $('#chose-panel'), @@ -34,6 +34,11 @@ var $chosePanel = $('#chose-panel'),
34 $soonSoldOut = $('.soonSoldOut-tag'), 34 $soonSoldOut = $('.soonSoldOut-tag'),
35 $yohoPage = $('.yoho-page'); 35 $yohoPage = $('.yoho-page');
36 36
  37 +//购物车编辑标相关变量
  38 +var isEdit,
  39 + isSelected,
  40 + oldSknId;
  41 +
37 //初始化购物车面板显示 42 //初始化购物车面板显示
38 function init() { 43 function init() {
39 hasChooseColor = false; 44 hasChooseColor = false;
@@ -47,6 +52,37 @@ function init() { @@ -47,6 +52,37 @@ function init() {
47 $leftNum = $('#left-num'); 52 $leftNum = $('#left-num');
48 curColorIndex = 0; 53 curColorIndex = 0;
49 curSizeIndex = 0; 54 curSizeIndex = 0;
  55 + isEdit = 0;
  56 +}
  57 +
  58 +/*
  59 + * 设置当前面板为编辑模式
  60 + *
  61 + * @param {String} sknId. 当前编辑商品的sknId
  62 + *
  63 + * @param {Bool} isThisGoodSelected. 当前编辑商品的在购物车中是否被选中
  64 + *
  65 + * @return {undefined}
  66 + */
  67 +function setEditModeWithSknId(sknId, isThisGoodSelected) {
  68 + $('#chose-btn-sure').html('确认');
  69 + isEdit = 1;
  70 + oldSknId = sknId;
  71 + isSelected = isThisGoodSelected;
  72 +}
  73 +
  74 +//删除面板
  75 +function removePannel() {
  76 + var $pannel = $('.chose-panel'),
  77 + $promotionId = $('#promotionId');
  78 +
  79 + if ($pannel) {
  80 + $pannel.remove();
  81 + }
  82 +
  83 + if ($promotionId) {
  84 + $promotionId.remove();
  85 + }
50 } 86 }
51 87
52 function checkColorSizeNum() { 88 function checkColorSizeNum() {
@@ -108,7 +144,7 @@ function updateConformButtonClassAndText() { @@ -108,7 +144,7 @@ function updateConformButtonClassAndText() {
108 if (2 === $chosed.closest('.zero-stock').length) { 144 if (2 === $chosed.closest('.zero-stock').length) {
109 $('#chose-btn-sure').css('background-color', '#c0c0c0').html('已售罄'); 145 $('#chose-btn-sure').css('background-color', '#c0c0c0').html('已售罄');
110 } else { 146 } else {
111 - $('#chose-btn-sure').css('background-color', '#eb0313').html('加入购物车'); 147 + $('#chose-btn-sure').css('background-color', '#eb0313').html(isEdit ? '确认' : '加入购物车');
112 } 148 }
113 } 149 }
114 150
@@ -355,7 +391,8 @@ $yohoPage.on('touchstart', '.btn-minus', function() { @@ -355,7 +391,8 @@ $yohoPage.on('touchstart', '.btn-minus', function() {
355 buyNumber = $('#good-num').val() - 0, 391 buyNumber = $('#good-num').val() - 0,
356 392
357 promotionId, 393 promotionId,
358 - isEdit = 0, 394 + cartGoodData,
  395 + url,
359 num = parseInt($num.val(), 10); 396 num = parseInt($num.val(), 10);
360 397
361 //颜色尺码没有选择 398 //颜色尺码没有选择
@@ -389,21 +426,39 @@ $yohoPage.on('touchstart', '.btn-minus', function() { @@ -389,21 +426,39 @@ $yohoPage.on('touchstart', '.btn-minus', function() {
389 426
390 confirming = true; 427 confirming = true;
391 loading.showLoadingMask(); 428 loading.showLoadingMask();
392 - $.ajax({  
393 - method: 'POST',  
394 - url: '/cart/index/add',  
395 - data: { 429 +
  430 + //针对是否处于编辑模式设置不同的url和需要post的数据
  431 + if (isEdit) {
  432 + cartGoodData = {
  433 + new_product_sku: productSku,
  434 + old_product_sku: oldSknId,
  435 + buy_number: buyNumber,
  436 + selected: isSelected
  437 + };
  438 +
  439 + url = '/cart/index/modify';
  440 +
  441 + } else {
  442 + cartGoodData = {
396 productSku: productSku, 443 productSku: productSku,
397 buyNumber: buyNumber, 444 buyNumber: buyNumber,
398 promotionId: promotionId, 445 promotionId: promotionId,
399 isEdit: isEdit, 446 isEdit: isEdit,
400 cartType: queryString.cartType 447 cartType: queryString.cartType
  448 + };
  449 +
  450 + url = '/cart/index/add';
401 } 451 }
  452 +
  453 + $.ajax({
  454 + method: 'POST',
  455 + url: url,
  456 + data: cartGoodData
402 }).done(function(res) { 457 }).done(function(res) {
403 var cartNum; 458 var cartNum;
404 459
405 loading.hideLoadingMask(); 460 loading.hideLoadingMask();
406 - if (res.code === 200) { 461 + if (res.code === 200 && !isEdit) {
407 cartNum = res.data.goods_count; 462 cartNum = res.data.goods_count;
408 if (cartNum > 99) { 463 if (cartNum > 99) {
409 cartNum = '99+'; 464 cartNum = '99+';
@@ -415,11 +470,20 @@ $yohoPage.on('touchstart', '.btn-minus', function() { @@ -415,11 +470,20 @@ $yohoPage.on('touchstart', '.btn-minus', function() {
415 cbFn(); 470 cbFn();
416 } 471 }
417 } 472 }
418 - if (res.message) { 473 + if (res.message && !isEdit) {
419 tip.show(res.message); 474 tip.show(res.message);
420 } 475 }
421 476
422 hide(); 477 hide();
  478 +
  479 + if (isEdit) {
  480 + loading.showLoadingMask();
  481 +
  482 + //延迟刷新,否则面板可能无法隐藏
  483 + setTimeout(function() {
  484 + window.location.reload();
  485 + }, 1);
  486 + }
423 }).fail(function() { 487 }).fail(function() {
424 tip.show('网络出了点问题~'); 488 tip.show('网络出了点问题~');
425 }).always(function() { 489 }).always(function() {
@@ -431,3 +495,6 @@ $yohoPage.on('touchstart', '.btn-minus', function() { @@ -431,3 +495,6 @@ $yohoPage.on('touchstart', '.btn-minus', function() {
431 495
432 exports.init = init; 496 exports.init = init;
433 exports.show = show; 497 exports.show = show;
  498 +exports.remove = removePannel;
  499 +exports.setEditModeWithSknId = setEditModeWithSknId;
  500 +
@@ -7,6 +7,7 @@ @@ -7,6 +7,7 @@
7 var $ = require('jquery'), 7 var $ = require('jquery'),
8 ellipsis = require('mlellipsis'), 8 ellipsis = require('mlellipsis'),
9 chosePanel = require('../cart/chose-panel'), 9 chosePanel = require('../cart/chose-panel'),
  10 + loading = require('../plugin/loading'),
10 lazyLoad = require('yoho.lazyload'); 11 lazyLoad = require('yoho.lazyload');
11 12
12 var dialog = require('../me/dialog'), 13 var dialog = require('../me/dialog'),
@@ -76,6 +77,69 @@ $('.cart-goods').on('touchstart', '.checkbox', function() { @@ -76,6 +77,69 @@ $('.cart-goods').on('touchstart', '.checkbox', function() {
76 }); 77 });
77 }); 78 });
78 79
  80 +/*
  81 + * 显示购物车编辑面板。
  82 + *
  83 + * @param {String} html. chose-pannel模板,由服务端返回
  84 + *
  85 + * @param {String} id. 商品skuid
  86 + *
  87 + * @param {Bool} isSelected. 所要编辑的商品是否被选中
  88 + *
  89 + * @return false;
  90 + *
  91 + */
  92 +function showEditPannelWithSku(html, id, isSelected) {
  93 +
  94 + if (html.length < 2) {
  95 + tip.show('出错啦!');
  96 + return false;
  97 + }
  98 +
  99 + //删掉页面上原有的pannel
  100 + chosePanel.remove();
  101 +
  102 + $(html).appendTo('#mainCart');
  103 + chosePanel.init();
  104 + chosePanel.setEditModeWithSknId(id, isSelected);
  105 + chosePanel.show();
  106 +
  107 + return false;
  108 +}
  109 +
  110 +
  111 +$('.icon-edit').on('touchstart', function(e) {
  112 + var $this = $(this),
  113 + $checkBox = $this.closest('.info').siblings('.checkbox');
  114 +
  115 + var skn = $this.closest('.shopping-cart-good').data('skn'),
  116 + id = $this.closest('.shopping-cart-good').data('id'),
  117 + count = $this.data('count');
  118 +
  119 + e.stopPropagation();
  120 +
  121 + loading.showLoadingMask();
  122 +
  123 +
  124 + $.ajax({
  125 + url: '/cart/index/goodinfo',
  126 + data: {
  127 + skn: skn,
  128 + buy_num: count
  129 + },
  130 + success: function(res) {
  131 + showEditPannelWithSku(res, id, $checkBox.hasClass('icon-cb-checked'));
  132 + },
  133 + error: function() {
  134 + tip.show('网络异常');
  135 + window.location.reload();
  136 + },
  137 + complete: function() {
  138 + loading.hideLoadingMask();
  139 + }
  140 + });
  141 +});
  142 +
79 $('.icon-del').on('touchstart', function(e) { 143 $('.icon-del').on('touchstart', function(e) {
80 var $this = $(this); 144 var $this = $(this);
81 145
@@ -189,15 +189,19 @@ class IndexController extends AbstractAction @@ -189,15 +189,19 @@ class IndexController extends AbstractAction
189 189
190 if ($this->isAjax()) { 190 if ($this->isAjax()) {
191 $num = $this->get('buy_num', 1); 191 $num = $this->get('buy_num', 1);
192 - $skn = $this->get('id', 1); 192 + $skn = $this->get('skn', 1);
193 $uid = $this->getUid(true); 193 $uid = $this->getUid(true);
194 $result = CartModel::cartProductData($uid, $skn, $num); // 测试skn的ID为51172055 194 $result = CartModel::cartProductData($uid, $skn, $num); // 测试skn的ID为51172055
195 $result['num'] = $num; 195 $result['num'] = $num;
196 } 196 }
197 197
  198 + if (!isset($result['code'])) {
198 $this->_view->display('gift-info', array( 199 $this->_view->display('gift-info', array(
199 'cartInfo' => $result 200 'cartInfo' => $result
200 )); 201 ));
  202 + } else {
  203 + echo ' ';
  204 + }
201 } 205 }
202 206
203 // /* 207 // /*