Authored by 刘传洋

m

/**
* Created by yoho on 2017-01-05.
*/
var $ = require('yoho-jquery');
var dialog = require('../common/dialog');
var Dialog = dialog.Dialog;
var Alert = dialog.Alert;
var Confirm = dialog.Confirm;
var $cartnewTips = $('.cartnew-tips'),
$payWapper = $('.pay-wapper'),
$cartnewSum = $('.cartnew-sum'),
CART_ITEM_DEL_URL = '/cart/index/remove',
CART_ITEM_FAV_URL = '/cart/index/fav',
selColorWinTpl = require('hbs/cart/select-color-panel.hbs'),
giftsWinTpl = require('hbs/cart/cart-gifts-win-tpl.hbs'),
productInfoTpl = require('hbs/cart/cart-product-info-tpl.hbs'),
$goodsSelWin = $('#Y_goodsSelectWin');
// 关闭温馨提示
$cartnewTips.find('.btn_close').click(function() {
$cartnewTips.fadeOut();
});
// 滚动到第一个选中的商品
function scrollToFirst() {
var $selected = $payWapper.find('li[data-role="pitem"] .cart-item-check.cart-item-checked:eq(0)');
var top = 0;
if ($selected.length > 0) {
top = $selected.offset().top - 36;
$('html,body').scrollTop(top);
}
return false;
}
function toggleCheckAllPros() {
}
// checkbox提交ajax
function choiceOut(items) {
var skuList = $.isArray(items) ? items : [items];
var hasPromotion = false;
$.each(skuList, function(idx, it) {
if (it.promotion_id) {
hasPromotion = true;
return false;
}
});
return $.ajax({
type: 'POST',
dataType: 'json',
url: '/cart/index/select',
data: {
skuList: JSON.stringify(skuList),
hasPromotion: hasPromotion
},
beforeSend: function() {
$('.loading').css({
top: $(document).scrollTop() + 200
});
$('.loading').show();
}
}).then(function(d) {
if (d.code === 200) {
window.history.go(0);
}
});
}
/*
* 1. 删除购物车商品,把删除的商品移入cookie中
* 2. 移到收藏夹
* data: 数据
* tpe: true - 删除,默认 移入收藏夹
*/
function cartItemDel(items, type, cookieList) {
var selList = $.isArray(items) ? items : [items];
var hasPromotion = false;
$.each(selList, function(idx, it) {
if (it.promotion_id) {
hasPromotion = true;
return false;
}
});
return $.ajax({
type: 'POST',
dataType: 'json',
url: type === true ? CART_ITEM_DEL_URL : CART_ITEM_FAV_URL,
data: {
skuList: JSON.stringify(selList),
hasPromotion: hasPromotion
},
beforeSend: function() {
$('.loading').css({
top: $(document).scrollTop() + 200
});
$('.loading').show();
}
}).then(function(d) {
if (d.code === 200) {
if (cookieList) {
window.setCookie('cart-del-list', JSON.stringify(cookieList), {
domain: '.yohobuy.com',
path: '/'
});
}
window.history.go(0);
} else if (d.code === 300) {
$('.loading').hide();
new Alert(d.message).show();
} else if (d.code === 403) {
if (d.data.url) {
window.location = d.data.url;
}
}
});
}
// 购物车商品增减
var cartItemNumChg = (function(data) {
var countBusy = false; // 保证一次只请求完成前不能再次发起
return function(data) {
if (countBusy) {
return;
}
countBusy = true;
$.ajax({
type: 'POST',
dataType: 'json',
url: '/cart/index/modifyNum',
data: data
}).then(function(d) {
if (d.code === 200) {
window.history.go(0);
} else {
new Alert(d.message === '' ? '加入购物车失败哦~~' : d.message).show();
}
countBusy = false;
});
};
})();
function getProductInfo(pid, skn) {
return $.ajax({
type: 'GET',
url: '/cart/index/getProductData', // '/product/item/getProductInfo',
data: {
productId: pid,
skn: skn
}
}).done(function(res) {
return res;
});
}
// 根据id获取商品信息
function getProductHtmlInfo(productId) {
return $.ajax({
type: 'GET',
dataType: 'html',
url: '/cart/index/getProductInfo',
data: {
productId: productId
}
}).then(function(d) {
return d;
/* pacList = 0;
$goodsDetail.html(' ');
$goodsDetail.append(d);
$goodsDetail.show();
$('.detail-bigpic:not(.none) .con li:first').addClass('active');
if ($('.showSizeBox:not(.none) span').length < 2) {
$('.showSizeBox:not(.none) span:first').addClass('atcive');
}
if (Number($('#addToCart').val()) !== 1) {
$('.showSizeBox span').data('num', 0);
}
$.each($('.showSizeBox span'), function() {
if ($(this).data('num') <= 0) {
$(this).addClass('null-atcivec');
console.log($('.showSizeBox:not(.none) span:first'));
$('.showSizeBox:not(.none) span:first').removeClass('atcive');
}
});
if (typeof promotionPrice !== 'undefined') {
oldprice = $('.detail-info .oldprice del').html() ? $('.detail-info .oldprice del').html() : wapperPrice;
htmlInfo = '<span class="oldprice">现价:<del>' + oldprice + '</del></span>' +
'<span class="newprice">活动价:<b class="promotion-price">' + promotionPrice + '</b></span>';
$('.detail-info .price').html(htmlInfo);
}
$('.detail-bigpic:not(.none) .bigpic:gt(0)').hide();
$('.showSizeBox:not(.none)').find('span').each(function() {
if ($(this).hasClass('null-atcivec')) {
$('.addcart').addClass('none');
$('.btn_sellout').removeClass('none');
} else {
$('.addcart').removeClass('none');
$('.btn_sellout').addClass('none');
return false;
}
});*/
});
}
// 加入购物车,弹出框中加入购物车
function addcart(data, cookieList) {
$.ajax({
type: 'POST',
url: '/cart/index/add',
data: data
}).then(function(d) {
if (d.code === 200) {
window.history.go(0);
if (cookieList) {
window.setCookie('cart-del-list', JSON.stringify(cookieList), {
domain: '.yohobuy.com',
path: '/'
});
}
} else {
new Alert(d.message === '' ? '加入购物车失败哦~~' : d.message).show();
}
});
}
function parseProductInfo(productInfo, defaultInfo) {
var index = 0;
var colors;
var colorsLen;
var color;
// 前端处理后的集合
var filterSet = [];
var sizeIdx;
var curColor;
var curSize;
var hasActiveColor = false;
var defaultColor = defaultInfo.color;
var defaultSize = defaultInfo.size;
var defaultImg;
// 没有res.code
if (productInfo.colors) {
// 获取成功
colors = productInfo.colors;
colorsLen = colors.length;
for (index; index < colorsLen; index++) {
color = colors[index];
// 迭代每一种颜色
filterSet.push({
pid: productInfo.productId,
skn: productInfo.skn,
name: color.name,
src: color.src,
focus: color.focus,
title: color.title,
sizes: color.size,
pic: color.thumbs[0].shower,
selectable: color.total > 0
});
}
}
// 默认选中用户选择的sku,若已售罄或下架,则选中列表中第一个非售罄的sku
for (index = 0; index < filterSet.length; index++) {
curColor = filterSet[index];
if (!hasActiveColor && String(curColor.name) === String(defaultInfo.color)) {
curColor.active = true;
curColor.hasActiveColor = hasActiveColor = true;
defaultImg = curColor.pic;
}
curSize = curColor.sizes;
for (sizeIdx = 0; sizeIdx < curSize.length; sizeIdx++) {
if (curColor.hasActiveColor && curSize[sizeIdx].sku === defaultInfo.sku) {
// console.log(curSize[sizeIdx]);
curSize[sizeIdx].sizeActive = true;
break;
}
}
}
// 若无对应颜色,则选中第一个颜色
if (!hasActiveColor) {
filterSet[0].active = true;
defaultColor = filterSet[0].color;
defaultImg = filterSet[0].pic;
}
return {
skn: productInfo.skn,
colors: filterSet,
defaultColor: defaultColor,
defaultSize: defaultSize,
defaultImg: defaultImg
};
}
function updateCartItem(newSku, oldSku) {
$.ajax({
type: 'POST',
url: '/cart/index/updateProduct',
data: {
swapData: JSON.stringify([{
buy_number: '1',
selected: 'Y',
new_product_sku: newSku,
old_product_sku: oldSku
}])
}
}).then(function(d) {
if (d.code === 200) {
window.history.go(0);
} else {
new Alert(d.message === '' ? '修改商品失败哦~~' : d.message).show();
}
});
}
function updateCartGiftItem(promotionId, newSkn, newSku) {
$.ajax({
type: 'POST',
url: '/cart/index/swapGift',
data: {
promotionId: promotionId,
newSkn: newSkn,
newSku: newSku
}
}).then(function(d) {
if (d.code === 200) {
window.history.go(0);
} else {
new Alert(d.message === '' ? '修改商品失败哦~~' : d.message).show();
}
});
}
function renderAndShowSelWin($item, pinfo) {
$item.find('.goods-choose-box').remove();
var $selWin = $(selColorWinTpl(pinfo)).appendTo($item);
$selWin.show();
}
function getProductByPromotionId(promotionId) {
return $.ajax({
type: 'GET',
url: '/cart/index/queryPromotionGift',
data: {
promotionId: promotionId
}
}).done(function(res) {
return res;
});
}
function renderAndShowGiftWin(plist) {
$goodsSelWin.find('.content').empty().html(giftsWinTpl(plist));
$goodsSelWin.show();
/* var d = new Dialog({
content: giftsWinTpl(plist),
className: 'cart-togetherGoods'
});
d.show();*/
// bindGiftWinAction(d.$el);
}
var Cart = {
addToCart: function(params) {
},
toggleSelectOne: function() { // 单选
var $this = $(this);
var $pitem = $this.closest('li[data-role="pitem"]');
var item;
$this.toggleClass('cart-item-checked');
item = {
product_sku: $pitem.data('id'),
selected: $this.hasClass('cart-item-checked') ? 'Y' : 'N',
buy_number: $pitem.data('productnum'),
goods_type: $pitem.data('goodstype'),
promotion_id: $pitem.data('promotionid') ? $pitem.data('promotionid') : 0
};
return choiceOut(item);
},
toggleSelectAll: function() { // 全选
var $this = $(this);
var selected = $this.hasClass('cart-item-checked') ? 'Y' : 'N';
var selectArray = [];
$payWapper.find('.cart-item-check').each(function() {
var $t = $(this);
if ($t.data('id')) {
selectArray.push({
product_sku: $t.data('id'),
selected: selected,
buy_number: $t.data('productnum'),
goods_type: $t.data('goodstype'),
promotion_id: $t.data('promotionid') || 0
});
}
});
choiceOut(selectArray);
},
del: function() {
var $this = $(this);
var $item = $this.closest('li[data-role="pitem"]');
var selectArray = [];
var content = '<div><span></span>删除商品</div><p>确定从购物车中删除此商品?</p>';
var countJSON;
selectArray.push({
product_sku: $item.data('id'),
buy_number: $item.data('productnum'),
promotion_id: $item.data('promotionid') || 0
});
if (!$this.data('gift')) {
countJSON = {
productPrice: $item.find('.productPrice').text(),
productTitle: $item.find('.pay-pro-info a').text(),
link: $item.find('.pay-pro-info a').attr('href'),
productNum: $item.data('productnum'),
productSku: $item.data('id'),
promotionId: $item.data('promotionid')
};
}
new Confirm({
content: content,
cb: function() {
cartItemDel(selectArray, true, countJSON);
}
}).show();
},
delAll: function() {
var selectArray = [];
var PromotionArray = [];
var content = '<div><span></span>删除商品</div><p>确定从购物车中删除所有选中商品?</p>';
$payWapper.find('.cart-item-check').each(function() {
var $item = $(this);
var $chk = $item.find('.cart-item-check');
if ($chk.hasClass('cart-item-checked')) {
if ($item.data('id')) {
selectArray.push({
product_sku: $item.data('id'),
buy_number: $item.data('productnum'),
promotion_id: $item.data('promotionid') || 0
});
PromotionArray.push({
productPrice: $item.find('.productPrice').text(),
productTitle: $item.find('.pay-pro-info a').text(),
link: $item.find('.pay-pro-info a').attr('href'),
productNum: $item.data('productnum'),
productSku: $item.data('id'),
promotionId: $item.data('promotionid')
});
}
}
});
if (!$.isEmptyObject(selectArray)) {
new Confirm({
content: content,
cb: function() {
cartItemDel(selectArray, true, PromotionArray);
}
}).show();
} else {
new Alert('请至少选择一件商品').show();
}
},
toFav: function() {
var $this = $(this);
var $item = $this.closest('li[data-role="pitem"]');
var item = {
product_sku: $item.data('id'),
buy_number: $item.data('productnum'),
promotion_id: $item.data('promotionid') ? $item.data('promotionid') : 0
};
cartItemDel(item);
},
toFavAll: function() {
var selectArray = [];
$payWapper.find('.cart-item-check').each(function() {
var $item = $(this);
var $chk = $item.find('.cart-item-check');
if ($chk.hasClass('cart-item-checked')) {
if ($item.data('id')) {
selectArray.push({
product_sku: $(this).data('id'),
buy_number: $(this).data('productnum'),
promotion_id: $item.data('promotionid') || 0
});
}
}
});
if (!$.isEmptyObject(selectArray)) {
cartItemDel(selectArray);
} else {
new Alert('请至少选择一件商品').show();
}
},
modNum: function() {
var $this = $(this);
var countJSON = {};
var oprType = $this.hasClass('minus') ? 'decreaseNum' : 'increaseNum';
countJSON[oprType] = 1;
if ($this.siblings('input').val() === '1' && $this.hasClass('minus')) {
return false;
}
cartItemNumChg($.extend(countJSON, {
sku: $this.closest('li[data-role="pitem"]').data('id')
}));
},
cleanAllDisable: function() {
var selectArray = [];
var PromotionArray = [];
var content = '<div><span></span>删除商品</div><p>确定从购物车中删除所有选中商品?</p>';
$payWapper.find('.cart-item-check').each(function() {
var $item = $(this);
var $chk = $item.find('.cart-item-check');
if ($chk.hasClass('cart-item-checked')) {
if ($item.data('id')) {
selectArray.push({
product_sku: $item.data('id'),
buy_number: $item.data('productnum'),
promotion_id: $item.data('promotionid') || 0
});
PromotionArray.push({
productPrice: $item.find('.productPrice').text(),
productTitle: $item.find('.pay-pro-info a').text(),
link: $item.find('.pay-pro-info a').attr('href'),
productNum: $item.data('productnum'),
productSku: $item.data('id'),
promotionId: $item.data('promotionid')
});
}
}
});
if (!$.isEmptyObject(selectArray)) {
new Confirm({
content: content,
cb: function() {
cartItemDel(selectArray, true, PromotionArray);
}
}).show();
} else {
new Alert('请至少选择一件商品').show();
}
},
showColorSizePanel: function() {
var $this = $(this);
var $item = $this.closest('li[data-role="pitem"]');
var pinfo = $this.data('_p_info');
var $selWin = $item.find('.goods-choose-box');
var pid = $item.data('pid');
var skn = $item.data('skn');
var sku = $item.data('id');
var defaultInfo = {
color: $item.data('color'),
size: $item.data('size'),
pid: pid,
sku: sku,
skn: skn
};
$payWapper.find('.pay-pro-detail').removeClass('active');
if ($selWin && $selWin.length && $selWin.is(':visible')) {
$selWin.hide();
return;
}
$item.find('.pay-pro-detail').addClass('active');
$payWapper.find('.goods-choose-box').hide();
if (!pinfo) {
getProductInfo(pid, skn).done(function(productInfo) {
pinfo = parseProductInfo(productInfo, defaultInfo);
$this.data('_p_info', pinfo);
renderAndShowSelWin($item, pinfo);
}).fail(function() {
new Alert('此商品无法编辑颜色和尺寸').show();
});
return;
}
renderAndShowSelWin($item, pinfo);
},
editColorOrSize: function() {
var $this = $(this);
var $item = $this.closest('li[data-role="pitem"]');
var pid = $item.data('pid');
var oldSku = $item.data('id');
var $size = $this.closest('.goods-choose-box').find('.choose-size .dt.active');
var newSku = $size.data('sku');
var newSkn = $this.closest('.goods-info').data('skn');
var promotionId = $item.data('promotionid');
// 没有重新选择颜色-尺码,则不用重新请求显示
if (!oldSku || !newSku || oldSku === newSku) {
Cart._hideColorSizePanel($item);
return false;
}
// 加价购更换
if ($item.data('isgift') || $item.data('ispricegift')) {
return updateCartGiftItem(promotionId, newSkn, newSku);
}
updateCartItem(newSku, oldSku);
},
_hideColorSizePanel: function($item) {
$item.find('.goods-choose-box').hide();
$item.find('.pay-pro-detail').removeClass('active');
},
hideColorSizePanel: function(event) {
var $this = $(this);
event.stopPropagation();
Cart._hideColorSizePanel($this.closest('li[data-role="pitem"]'));
},
selectColor: function() {
var $this = $(this);
var index = $this.index($this.parent().find('.dt'));
var $srows = $this.closest('.goods-info').find('.choose-size .size-row');
var $bigImgs = $this.closest('.goods-choose-box').find('.goods-info-bigImg .bigImg');
if ($this.hasClass('active')) return;
$this.siblings('.dt').removeClass('active');
$this.addClass('active');
$srows.find('.dt').removeClass('active');
$srows.addClass('hide');
$srows.eq(index).removeClass('hide');
$bigImgs.addClass('hide');
$bigImgs.eq(index).removeClass('hide');
},
selectSize: function() {
var $this = $(this);
$this.siblings('.dt').removeClass('active');
$this.addClass('active');
},
showGiftWin: function() {
var $this = $(this);
var $wrap = $this.closest('[data-role="promotion-wrap"]');
var promotionid = $wrap.data('promotionid');
var promotionInfo = $wrap.data('_promotionInfo');
var role = $this.data('role');
var isSwap = role === 'pg-resel-btn' || role === 'gift-resel-btn';
if (!promotionInfo) {
getProductByPromotionId(promotionid).done(function(pinfo) {
if (!pinfo && pinfo.code !== 200) {
return new Alert('获取商品失败,请稍后再试!').show();
}
promotionInfo = pinfo.data;
promotionInfo.isSwap = isSwap;
$wrap.data('_promotionInfo', promotionInfo);
renderAndShowGiftWin(promotionInfo);
}).fail(function() {
new Alert('获取商品失败,请稍后再试!').show();
});
return;
}
renderAndShowGiftWin(promotionInfo);
},
submit: function() {
/* understock = '';
if ($('.pay-wapper input:checked').parents('tr').find('.tipNoStore').length > 0) {
shopName = $('.pay-wapper input:checked').parents('tr').find('.tipNoStore');
$.each(shopName.parents('tr').find('.pay-pro-info a'), function() {
understock += $(this).html();
});
new Alert(understock + '库存不足').show();
} else {
if ($('.zp').length > 0 && !$(this).attr('title')) {
$(this).attr('title', '1');
new Alert('您有赠品没有选择,请选择完再结算!').show();
} else {
if ($('input:checked').length > 0) {
// 添加埋点
var productId = [];
$('.pay-wapper input:checked').parents('tr').each(function() {
if ($(this).attr('data-pid')) {
productId.push($(this).attr('data-pid'));
}
});
// 结算点击埋点
window.addPoint('YB_SC_TOBUY_CLICK', {PRD_ID: productId.join(',')});
if ($('.pre-sell-box input:checked').length > 0) {
window.location.href = '/cart/index/orderEnsure?type=2';
} else {
window.location.href = '/cart/index/orderEnsure?type=1';
}
} else {
new Alert('请至少选择一件商品').show();
}
}
}*/
}
};
module.exports = Cart;
$payWapper.one('click', 'li[data-role="pitem"] .cart-item-check', Cart.toggleSelectOne); // 单选
$cartnewSum.one('click', '.cart-item-check', Cart.toggleSelectAll); // 全选
$payWapper.on('click', '.cart-del-btn', Cart.del); // 删除商品
$cartnewSum.on('click', '.delAll', Cart.delAll); // 批量删除商品
$payWapper.on('click', '.cart-remove-btn', Cart.toFav); // 移入收藏夹
$cartnewSum.on('click', '.removeAll', Cart.toFavAll); // 批量移入收藏夹商品
$payWapper.on('click', '.minus, .plus', Cart.modNum); // 修改购物车数量
$cartnewSum.on('click', '.clean-all-disable', Cart.cleanAllDisable);
$('.btn_account').on('click', Cart.submit); // 结算
/** 重新选择商品颜色尺码 **/
$payWapper.on('click', 'li[data-role="pitem"] .pay-pro-detail', Cart.showColorSizePanel);
$payWapper.on('click', 'li[data-role="pitem"] .button-cancel', Cart.hideColorSizePanel);
$payWapper.on('click', 'li[data-role="pitem"] .button-sure', Cart.editColorOrSize);
$payWapper.find('li[data-role="pitem"]').on('click', '.goods-choose-box .choose-color .dt', Cart.selectColor);
$payWapper.find('li[data-role="pitem"]').on('click', '.goods-choose-box .choose-size .dt', Cart.selectSize);
/** 赠品加价购弹窗 **/
// 显示赠品
var giftBtn = ['[data-role=gift-view-btn]',
'[data-role=gift-resel-btn]',
'[data-role=gift-sel-btn]',
'[data-role=pg-sel-btn]',
'[data-role=pg-resel-btn]'];
$payWapper./* find('li[data-role="pitem"]').*/on('click', giftBtn.join(','), Cart.showGiftWin);
/*
$('.shop-cart').on('click', giftBtn.join(','), function() {
var $this = $(this);
var $win = $this.closest('[data-role=promotion-wrap]').find('[data-role=cart-gift-win]');
console.log($win.length);
$win.show();
});
$('.shop-cart').on('click', '[data-role="cart-gift-win"] .close', function() {
$(this).closest('[data-role="cart-gift-win"]').hide();
});*/
var GoodsWinAction = {
closeWin: function() {
// console.log($goodsSelWin);
$goodsSelWin.hide();
},
changeGoods: function() {
var $this = $(this);
var id = $this.data('id');
var skn = $this.data('skn');
$this.sibling('li').removeClass('active');
$this.addClass('active');
getProductInfo(id, skn).then(res => {
$goodsSelWin.find('.product-detail-info').empty().append(productInfoTpl(res));
});
},
selThumb: function() {
var $this = $(this);
var idx = $(this).index();
$goodsSelWin.find('.detail-bigpic:not(.none) .piclist li').removeClass('active');
$this.addClass('active');
$goodsSelWin.find('.detail-bigpic:not(.none) .bigpic').hide().eq(idx).show();
},
selThumbPrevNext: function() {
var $this = $(this);
var $detailBigpic = $this.closest('.detail-bigpic');
var curIndex = Number($detailBigpic.data('_index') || 0);
var $lis = $this.siblings('.con').find('li');
if ($this.hasClass('next')) {
if (curIndex >= $lis.length - 1) {
return false;
}
curIndex++;
} else {
if (curIndex < 1) {
return false;
}
curIndex--;
}
$detailBigpic.data('_index', curIndex);
$lis.removeClass('active').eq(curIndex).addClass('active');
$detailBigpic.find('.bigpic').hide().eq(curIndex).show();
},
selColor: function() {
var $this = $(this);
var idx = $this.index();
var $detail = $this.closest('.detail-goods');
var $sizes = $detail.find('[data-role=sizes] .size-row');
var $detailBigpic = $detail.find('.detail-bigpic');
var $curSize = $sizes.eq(idx);
var $curDetailBig = $detailBigpic.eq(idx);
var bigPicIndex = 0; // 默认显示大图中的第一个图
$curDetailBig.data('_index', bigPicIndex);
$this.siblings('.color').find('p').removeClass('active');
$this.find('p').addClass('active');
$sizes.addClass('none');
$curSize.removeClass('none');
if ($curSize.find('span').length < 2) {
$curSize.find('span:first').addClass('active');
}
$detailBigpic.addClass('none');
$curDetailBig.removeClass('none');
$curDetailBig.find('.bigpic').hide();
$curDetailBig.find('.bigpic').eq(bigPicIndex).show();
$curDetailBig.find('.con li').removeClass('active');
$curDetailBig.find('.con li').eq(bigPicIndex).addClass('active');
$sizes.eq(idx).find('span').each(function() {
if ($(this).hasClass('null-atcivec')) {
$goodsSelWin.find('.addcart').addClass('none');
$goodsSelWin.find('.btn_sellout').removeClass('none');
} else {
$goodsSelWin.find('.addcart').removeClass('none');
$goodsSelWin.find('.btn_sellout').addClass('none');
return false;
}
});
},
selSize: function() {
var $this = $(this);
// var idx = $this.index();
var shopNumAll = $this.data('num');
$this.siblings('span').removeClass('active');
$this.addClass('active');
if (shopNumAll > 0) {
$goodsSelWin.find('.addcart').removeClass('none');
$goodsSelWin.find('.btn_sellout').addClass('none');
} else {
$goodsSelWin.find('.addcart').addClass('none');
$goodsSelWin.find('.btn_sellout').removeClass('none');
}
},
changeNum: function() {
var $this = $(this);
var $pinfo = $this.closest('.product-detail-info');
// var count = $this.hasClass('minus') ? 'decreaseNum' : 'increaseNum';
var promotionId = $pinfo.data('promotionid');
var $num = $goodsSelWin.find('#num');
var shopNum = Number($num.val() || 1);
if (promotionId) {
if ($this.hasClass('cut')) {
new Alert('-_-,已经是最后一件,不能再减了!').show();
} else {
new Alert('最多只能购买一件,您好像购买的太多了!').show();
}
} else {
if ($this.hasClass('add')) {
shopNum++;
} else {
shopNum--;
}
if (shopNum < 1) {
new Alert('-_-,已经是最后一件,不能在减了!').show();
shopNum = 1;
return false;
}
$num.val(shopNum);
}
},
add2Cart: function() {
var $this = $(this);
var $curSize = $goodsSelWin.find('[data-role=sizes] .size-row:not(.none) .active');
var $num = $goodsSelWin.find('#num');
var allNum = $curSize.data('num');
var sku = $curSize.data('sku');
var promotionId = $this.closest('.product-detail-info').data('promotionid') || 0;
var isSwap = $this.closest('.product-detail-info').data('swap');
if ($curSize.length <= 0) {
new Alert('请选择尺码').show();
return false;
}
if ($num.val() > allNum) {
new Alert('库存不足,目前还有' + allNum + '个库存').show();
} else {
/* if (Number($('#addToCart').val()) === 1) {
addcart(dataJSON);
} else {
new Alert('该商品无法加入购物车').show();
}*/
// 替换促销商品
if (isSwap) {
updateCartGiftItem(promotionId, newSkn, sku);
} else {
addcart({
productSku: sku,
buyNumber: $num.val(),
promotionId: promotionId
});
}
}
}
};
/** 弹窗事件绑定 **/
$goodsSelWin.on('click', '.close', GoodsWinAction.closeWin);
$goodsSelWin.on('click', '.slide-img .img-list .img-item', GoodsWinAction.changeGoods);
$goodsSelWin.on('click', '.piclist li', GoodsWinAction.selThumb);
$goodsSelWin.on('click', '.pre, .next', GoodsWinAction.selThumbPrevNext);
$goodsSelWin.on('click', '[data-role=colors] .color', GoodsWinAction.selColor);
$goodsSelWin.on('click', '[data-role=sizes] .size-row span', GoodsWinAction.selSize);
$goodsSelWin.on('click', '.cut, .add', GoodsWinAction.changeNum);
$goodsSelWin.on('click', '.addcart', GoodsWinAction.add2Cart);
/**
* 订单结算页
* @author: gxh<xuhui.ge@yoho.cn>
* @date: 2016/12/27
*/
var $ = require('yoho-jquery'),
lazyLoad = require('yoho-jquery-lazyload'),
dialog = require('../common/dialog'),
rDialog = require('./rdialog');
require('../plugins/slider');
require('../common/center-slider');
require('../common');
var Dialog = dialog.Dialog,
// Confirm = dialog.Confirm,
Alert = dialog.Alert,
RConfirm = rDialog.RConfirm,
RAlert = rDialog.RAlert;
var $cartGoodsDetail = $('#goodsTogetherDetail'),
$paySum = $('#pay-sum'),
shopNum = 1,
productId,
promotionId,
understock,
$sellBox = $('.cart-table .pre-sell-box');
// 优惠券 显示和隐藏
$('.sale-info').mouseenter(function() {
$(this).addClass('active').children('ul').removeClass('none');
}).mouseleave(function() {
$(this).removeClass('active').children('ul').addClass('none');
});
$('.side-img-dd').slider2({
shownum: 5,
isCircle: true
});
// var dialogTpl = require('hbs/cart/cart-togetherGoods.hbs');
lazyLoad($('#details-html img'));
// ----------------底部 为你优选,最近浏览 begin--------------
// 推荐、优选、浏览切换
$(function() {
var $togetherGoods = $('.individual-item-togetherGoods'),
$recommendGoods = $('.individual-item-recommendGoods'),
$latestWalk = $('.individual-item-latestWalk');
if ($togetherGoods.hasClass('none')) {
$recommendGoods.show();
}
$('.bottom-tab-slide').on('click', '.bottom-title', function() {
var $this = $(this),
index = $this.index();
console.log(index);
$togetherGoods.hide();
$recommendGoods.hide();
if ($this.hasClass('change')) {
return;
}
if ($this.hasClass('cur')) {
return;
}
$this.addClass('cur');
$this.siblings('.cur').removeClass('cur');
switch (index) {
case 0: {
$togetherGoods.slideDown();
$recommendGoods.hide();
$latestWalk.hide();
break;
}
case 1: {
$togetherGoods.hide();
$recommendGoods.slideDown();
$latestWalk.hide();
break;
}
case 2: {
$togetherGoods.hide();
$recommendGoods.hide();
$latestWalk.slideDown();
break;
}
}
});
});
// 凑单 底部 轮播
function loadSlideGoods() {
$.ajax({
type: 'GET',
url: '',
data: {
// skn: skn,
size: 20,
num: 1
}
}).then(function(data) {
// var pro = data.data.products;
// if (data.code === 200) {
// if (pro.length === 0) {
// return;
// }
$('#recommend-shop').removeClass('none');
$('.individual-item ').slider2({
shownum: 5,
isCircle: true
});
// } else {
// $('.bottom-title').filter('.change').addClass('none');
// }
});
}
loadSlideGoods();
$(function() {
$('.bottom-tab .change').click(function() {
$('.img-brand-switch .next').trigger('click');
});
// 判断商品是否已收藏,是否全选
function isCheckFav() {
var pidArray = [],
isAllCheck = true;
$sellBox.each(function() {
var $checkItem = $(this).find('.cart-item-check');
//有一个没选中,跳出循环
if (!$checkItem.hasClass('cart-item-checked')) {
isAllCheck = false;
return false;
}
});
if (isAllCheck) {
//头部全选
$('.cart-title .cart-item-check').addClass('cart-item-checked');
//底部全选
$('#cbSelAllGoods').addClass('cart-item-checked');
}
$sellBox.each(function () {
pidArray.push($(this).data('pid'));
});
$.ajax({
type: 'POST',
url: '/cart/index/checkFav',
data: {
pidList: pidArray.join(',')
}
}).then(function(result) {
$.each(result.data,function(pid,obj) {
if (obj.code !== 200) {
return false;
}
if (obj.data) {
$sellBox.each(function () {
if (String($(this).data('pid')) === String(pid)) {
$(this).find('.cart-col-btn').addClass('has-col-btn').html('已收藏');
return false;
}
});
}
});
});
}
isCheckFav();
});
$('body').on('click', '.cut, .add', function() {
count = $(this).hasClass('minus') ? 'decreaseNum' : 'increaseNum';
if (promotionId) {
if ($(this).hasClass('cut')) {
new Alert('-_-,已经是最后一件,不能再减了!').show();
} else {
new Alert('最多只能购买一件,您好像购买的太多了!').show();
}
} else {
if ($(this).hasClass('add')) {
shopNum++;
} else {
shopNum--;
}
if (shopNum < 1) {
new Alert('-_-,已经是最后一件,不能在减了!').show();
shopNum = 1;
return false;
}
$('#num').val(shopNum);
}
});
// 凑单弹框 商品信息弹出框,加入收藏夹
$cartGoodsDetail.on('click', '.btn-favCount', function() {
$.ajax({
type: 'POST',
dataType: 'json',
url: '',
data: {
productId: productId,
type: 'add'
}
}).then(function(d) {
if (d.code === 200) {
new Alert(d.message).show();
} else if (d.code === 403) {
if (d.data.url) {
window.location = d.data.url;
}
} else {
new Alert(d.message).show();
}
});
});
// 结算
$paySum.on('click', '.btn-account', function() {
// 结算
understock = '';
// if ($('.pay-wapper .cart-item-checked').parents('.tr').find('.tipNoStore').length > 0) {
// shopName = $('.pay-wapper .cart-item-checked').parents('.tr').find('.tipNoStore');
// $.each(shopName.parents('.tr').find('.pay-pro-info a'), function() {
// understock += $(this).html();
// });
// new Alert(understock + '库存不足').show();
// } else {
// if ($('.zp').length > 0 && !$(this).attr('title')) {
// $(this).attr('title', '1');
// new Confirm({
// className: 'shopCart-del-dialog',
// content: '<h4><i class="iconfont">&#xe6c2;</i>您还未选择赠品</h4>' +
// '<small>是否去选择赠品?</small>',
// }).show();
// } else {
// if ($('input:checked').length > 0) {
//
// if ($('.pre-sell-box .cart-item-checked').length > 0) {
// window.location.href = '/cart/index/orderEnsure?type=2';
// } else {
// window.location.href = '/cart/index/orderEnsure?type=1';
// }
// } else {
// new Alert('请至少选择一件商品').show();
// }
// }
// }
$('.cart-preSell-dialog').show();
// $('.togethor').show();
$('.cart-togetherGoods').show();
});
$('.shop-cart').on('click', '[data-role="cart-gift-win"] .close', function() {
$(this).closest('[data-role="cart-gift-win"]').hide();
});
require('./cart-action');
require('./cart-color-panel');
require('./cart-goods-win');