Authored by 梁志锋

Merge branch 'fix/cart' into 'feature/cart'

购物车增加全选/全取消 功能

See merge request !39
@@ -13,7 +13,10 @@ var dialog = require('../me/dialog'), @@ -13,7 +13,10 @@ var dialog = require('../me/dialog'),
13 tip = require('../plugin/tip'); 13 tip = require('../plugin/tip');
14 14
15 15
16 -var $names; 16 +var $names,
  17 + $selectAllBtn = $('.balance .iconfont');
  18 +
  19 +var requesting = false;
17 20
18 //var $curDelPanel; 21 //var $curDelPanel;
19 22
@@ -167,6 +170,122 @@ $('.cart-goods').on('touchstart', '.checkbox', function() { @@ -167,6 +170,122 @@ $('.cart-goods').on('touchstart', '.checkbox', function() {
167 // }); 170 // });
168 //}) 171 //})
169 172
  173 +function requestUpdateAllGoodsCheckStatus(theGoods, successHandeler) {
  174 + if (requesting) {
  175 + return;
  176 + }
  177 + requesting = true;
  178 + $.ajax({
  179 + url: 'select',
  180 + type: 'post',
  181 + data: {
  182 + skuList: JSON.stringify(theGoods)
  183 + },
  184 + success: function(res) {
  185 + if (res.code === 200) {
  186 + successHandeler();
  187 + } else {
  188 + tip.show(res.message);
  189 + }
  190 + },
  191 + error: function(err) {
  192 + tip.show('网络异常');
  193 + },
  194 + complete: function() {
  195 + requesting = false;
  196 + }
  197 + });
  198 +}
  199 +
  200 +function didUpdateAllGoodsCheckStatus() {
  201 + var $checkedBoxs = $('.shopping-cart-good .icon-cb-checked'),
  202 + $uncheckedBoxs = $('.shopping-cart-good .icon-checkbox');
  203 +
  204 + var shouldSelectAll;
  205 +
  206 + if ($selectAllBtn.hasClass('icon-cb-checked')) {
  207 + $selectAllBtn.removeClass('icon-cb-checked').addClass('icon-checkbox');
  208 + shouldSelectAll = true;
  209 + } else {
  210 + $selectAllBtn.removeClass('icon-checkbox').addClass('icon-cb-checked');
  211 + shouldSelectAll = false;
  212 + }
  213 +
  214 + if (!shouldSelectAll) {
  215 + $uncheckedBoxs.each(function(idx, uncheckedBox) {
  216 + $(uncheckedBox).removeClass('icon-checkbox').addClass('icon-cb-checked');
  217 + });
  218 + } else {
  219 + $checkedBoxs.each(function(idx, checkedBox) {
  220 + $(checkedBox).removeClass('icon-cb-checked').addClass('icon-checkbox');
  221 + });
  222 + }
  223 +}
  224 +
  225 +function bottomCheckBoxHandeler(isSelected, type, handlerAfterTouch) {
  226 + var goodInfo = {};
  227 + var $goods = $('.cart-content:not(.hide) .shopping-cart-good');
  228 + var $good = null;
  229 + var goodsList = [];
  230 +
  231 + function GoodInfo(properties) {
  232 + this.goods_type = properties.goods_type;
  233 + this.buy_number = properties.buy_number;
  234 + this.product_sku = properties.product_sku;
  235 + this.selected = properties.selected;
  236 + }
  237 +
  238 + goodInfo.goods_type = type;
  239 + goodInfo.selected = isSelected ? 'Y' : 'N';
  240 +
  241 + $goods.each(function(idx, good) {
  242 + $good = $(good);
  243 +
  244 + goodInfo.product_sku = $(good).data('id');
  245 + goodInfo.buy_number = $good.find('.count').eq(0).text().trim().replace('×', '');
  246 +
  247 + goodsList.push(new GoodInfo(goodInfo));
  248 + });
  249 + console.log(goodsList);
  250 +
  251 + requestUpdateAllGoodsCheckStatus(goodsList, handlerAfterTouch);
  252 +}
  253 +
  254 +//获取当前购物车类型
  255 +function getCartType() {
  256 + var $navItem = $('.cart-nav ').find('li'),
  257 + type = 'ordinary';
  258 +
  259 + if ($navItem.eq(0).hasClass('active')) {
  260 + type = 'ordinary';
  261 + } else {
  262 + type = 'advance';
  263 + }
  264 +
  265 + return type;
  266 +}
  267 +
  268 +//是否要全选
  269 +function willBeSelected() {
  270 + var isSelected = true;
  271 + var $this = $(this);
  272 +
  273 + if ($this.hasClass('icon-cb-checked')) {
  274 + isSelected = true;
  275 + } else {
  276 + isSelected = false;
  277 + }
  278 +
  279 + return isSelected;
  280 +}
  281 +
  282 +//全选按钮点击事件
  283 +$selectAllBtn.on('touchend', function() {
  284 + bottomCheckBoxHandeler(willBeSelected(), getCartType(), didUpdateAllGoodsCheckStatus);
  285 +});
  286 +
  287 +
  288 +
170 $('.down').on('touchend', function() { 289 $('.down').on('touchend', function() {
171 chosePanel.show(); 290 chosePanel.show();
172 }); 291 });
@@ -78,7 +78,7 @@ class IndexController extends AbstractAction @@ -78,7 +78,7 @@ class IndexController extends AbstractAction
78 $result = array(); 78 $result = array();
79 79
80 if ($this->isAjax()) { 80 if ($this->isAjax()) {
81 - $productId = $this->post('id', 0); 81 + $productId = $this->post('skuList', 0);
82 $uid = $this->getUid(true); 82 $uid = $this->getUid(true);
83 $shoppingKey = $this->getSession('shoppingKey'); 83 $shoppingKey = $this->getSession('shoppingKey');
84 $result = CartModel::selectGoods($uid, $productId, $shoppingKey); 84 $result = CartModel::selectGoods($uid, $productId, $shoppingKey);