Authored by biao

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

... ... @@ -34,6 +34,11 @@ var $chosePanel = $('#chose-panel'),
$soonSoldOut = $('.soonSoldOut-tag'),
$yohoPage = $('.yoho-page');
//购物车编辑标相关变量
var isEdit,
isSelected,
oldSknId;
//初始化购物车面板显示
function init() {
hasChooseColor = false;
... ... @@ -47,6 +52,37 @@ function init() {
$leftNum = $('#left-num');
curColorIndex = 0;
curSizeIndex = 0;
isEdit = 0;
}
/*
* 设置当前面板为编辑模式
*
* @param {String} sknId. 当前编辑商品的sknId
*
* @param {Bool} isThisGoodSelected. 当前编辑商品的在购物车中是否被选中
*
* @return {undefined}
*/
function setEditModeWithSknId(sknId, isThisGoodSelected) {
$('#chose-btn-sure').html('确认');
isEdit = 1;
oldSknId = sknId;
isSelected = isThisGoodSelected;
}
//删除面板
function removePannel() {
var $pannel = $('.chose-panel'),
$promotionId = $('#promotionId');
if ($pannel) {
$pannel.remove();
}
if ($promotionId) {
$promotionId.remove();
}
}
function checkColorSizeNum() {
... ... @@ -108,7 +144,7 @@ function updateConformButtonClassAndText() {
if (2 === $chosed.closest('.zero-stock').length) {
$('#chose-btn-sure').css('background-color', '#c0c0c0').html('已售罄');
} else {
$('#chose-btn-sure').css('background-color', '#eb0313').html('加入购物车');
$('#chose-btn-sure').css('background-color', '#eb0313').html(isEdit ? '确认' : '加入购物车');
}
}
... ... @@ -355,7 +391,8 @@ $yohoPage.on('touchstart', '.btn-minus', function() {
buyNumber = $('#good-num').val() - 0,
promotionId,
isEdit = 0,
cartGoodData,
url,
num = parseInt($num.val(), 10);
//颜色尺码没有选择
... ... @@ -389,21 +426,39 @@ $yohoPage.on('touchstart', '.btn-minus', function() {
confirming = true;
loading.showLoadingMask();
$.ajax({
method: 'POST',
url: '/cart/index/add',
data: {
//针对是否处于编辑模式设置不同的url和需要post的数据
if (isEdit) {
cartGoodData = {
new_product_sku: productSku,
old_product_sku: oldSknId,
buy_number: buyNumber,
selected: isSelected
};
url = '/cart/index/modify';
} else {
cartGoodData = {
productSku: productSku,
buyNumber: buyNumber,
promotionId: promotionId,
isEdit: isEdit,
cartType: queryString.cartType
};
url = '/cart/index/add';
}
$.ajax({
method: 'POST',
url: url,
data: cartGoodData
}).done(function(res) {
var cartNum;
loading.hideLoadingMask();
if (res.code === 200) {
if (res.code === 200 && !isEdit) {
cartNum = res.data.goods_count;
if (cartNum > 99) {
cartNum = '99+';
... ... @@ -415,11 +470,20 @@ $yohoPage.on('touchstart', '.btn-minus', function() {
cbFn();
}
}
if (res.message) {
if (res.message && !isEdit) {
tip.show(res.message);
}
hide();
if (isEdit) {
loading.showLoadingMask();
//延迟刷新,否则面板可能无法隐藏
setTimeout(function() {
window.location.reload();
}, 1);
}
}).fail(function() {
tip.show('网络出了点问题~');
}).always(function() {
... ... @@ -431,3 +495,6 @@ $yohoPage.on('touchstart', '.btn-minus', function() {
exports.init = init;
exports.show = show;
exports.remove = removePannel;
exports.setEditModeWithSknId = setEditModeWithSknId;
... ...
... ... @@ -7,6 +7,7 @@
var $ = require('jquery'),
ellipsis = require('mlellipsis'),
chosePanel = require('../cart/chose-panel'),
loading = require('../plugin/loading'),
lazyLoad = require('yoho.lazyload');
var dialog = require('../me/dialog'),
... ... @@ -76,6 +77,69 @@ $('.cart-goods').on('touchstart', '.checkbox', function() {
});
});
/*
* 显示购物车编辑面板。
*
* @param {String} html. chose-pannel模板,由服务端返回
*
* @param {String} id. 商品skuid
*
* @param {Bool} isSelected. 所要编辑的商品是否被选中
*
* @return false;
*
*/
function showEditPannelWithSku(html, id, isSelected) {
if (html.length < 2) {
tip.show('出错啦!');
return false;
}
//删掉页面上原有的pannel
chosePanel.remove();
$(html).appendTo('#mainCart');
chosePanel.init();
chosePanel.setEditModeWithSknId(id, isSelected);
chosePanel.show();
return false;
}
$('.icon-edit').on('touchstart', function(e) {
var $this = $(this),
$checkBox = $this.closest('.info').siblings('.checkbox');
var skn = $this.closest('.shopping-cart-good').data('skn'),
id = $this.closest('.shopping-cart-good').data('id'),
count = $this.data('count');
e.stopPropagation();
loading.showLoadingMask();
$.ajax({
url: '/cart/index/goodinfo',
data: {
skn: skn,
buy_num: count
},
success: function(res) {
showEditPannelWithSku(res, id, $checkBox.hasClass('icon-cb-checked'));
},
error: function() {
tip.show('网络异常');
window.location.reload();
},
complete: function() {
loading.hideLoadingMask();
}
});
});
$('.icon-del').on('touchstart', function(e) {
var $this = $(this);
... ...
... ... @@ -189,15 +189,19 @@ class IndexController extends AbstractAction
if ($this->isAjax()) {
$num = $this->get('buy_num', 1);
$skn = $this->get('id', 1);
$skn = $this->get('skn', 1);
$uid = $this->getUid(true);
$result = CartModel::cartProductData($uid, $skn, $num); // 测试skn的ID为51172055
$result['num'] = $num;
}
if (!isset($result['code'])) {
$this->_view->display('gift-info', array(
'cartInfo' => $result
));
} else {
echo ' ';
}
}
// /*
... ...