...
|
...
|
@@ -13,6 +13,10 @@ var Util = require('./util'); |
|
|
var hbs = require('yoho-handlebars');
|
|
|
var Stepper = require('./stepper');
|
|
|
|
|
|
// 删除商品信息
|
|
|
var removedProSkus = [];
|
|
|
var removedProsInfo = [];
|
|
|
|
|
|
var Cart = {
|
|
|
/*
|
|
|
* 添加到购物车
|
...
|
...
|
@@ -94,37 +98,20 @@ var Cart = { |
|
|
/*
|
|
|
* 根据服务端JSON,刷新购物车信息
|
|
|
* @function [refreshCart]
|
|
|
* @params { Object } data 最新购物车数据
|
|
|
* @params { Function } callback 刷新购物车后的回调函数
|
|
|
*/
|
|
|
refreshCart: function(data) {
|
|
|
var template;
|
|
|
|
|
|
hbs.registerHelper('multiple', function(num1, num2) {
|
|
|
num1 = typeof num1 === 'number' ? num1 : parseFloat(num1, 10);
|
|
|
num2 = typeof num2 === 'number' ? num2 : parseFloat(num2, 10);
|
|
|
|
|
|
if (num1 && num2) {
|
|
|
return num1 * num2;
|
|
|
}
|
|
|
});
|
|
|
|
|
|
hbs.registerHelper('isEqual', function(v1, v2, options) {
|
|
|
if (v1 === v2) {
|
|
|
return options.fn(this);
|
|
|
refreshCart: function(callback) {
|
|
|
Util.ajax({
|
|
|
url: '/shopping/cart/data',
|
|
|
success: function(res) {
|
|
|
Util.refreshCart(res, function() {
|
|
|
Stepper.init();
|
|
|
if (callback) {
|
|
|
return callback();
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
return options.inverse(this);
|
|
|
});
|
|
|
|
|
|
hbs.registerHelper('image', function(url, width, height, mode) {
|
|
|
mode = parseInt(mode, 10) ? mode : 2;
|
|
|
url = url || '';
|
|
|
return url.replace(/{width}/g, width).replace(/{height}/g, height).replace(/{mode}/g, mode);
|
|
|
});
|
|
|
|
|
|
template = hbs.compile($('#cart-content-tpl').html());
|
|
|
$('#cart_content').html(template(data));
|
|
|
|
|
|
Stepper.init();
|
|
|
},
|
|
|
|
|
|
/*
|
...
|
...
|
@@ -195,11 +182,14 @@ var Cart = { |
|
|
* 删除商品
|
|
|
* @function [removePro]
|
|
|
* @params { Array } products 商品列表
|
|
|
* @params { Array } extraInfos 删除商品额外信息
|
|
|
*/
|
|
|
removePro: function(products) {
|
|
|
removePro: function(products, extraInfos) {
|
|
|
var dialog;
|
|
|
|
|
|
if (products.length) {
|
|
|
// Cart.showRemovedProducts(products, extraInfos);
|
|
|
|
|
|
dialog = new _confirm({
|
|
|
content: '您确定要从购物车中删除该商品吗?',
|
|
|
cb: function() {
|
...
|
...
|
@@ -211,6 +201,9 @@ var Cart = { |
|
|
success: function(res) {
|
|
|
Util.refreshCart(res, function() {
|
|
|
Stepper.init();
|
|
|
|
|
|
// 显示或者更新删除商品模块
|
|
|
Cart.showRemovedProducts(products, extraInfos);
|
|
|
});
|
|
|
}
|
|
|
});
|
...
|
...
|
@@ -222,11 +215,68 @@ var Cart = { |
|
|
},
|
|
|
|
|
|
/*
|
|
|
* 显示或者更新删除商品模块
|
|
|
* @function [removePro]
|
|
|
* @params { Array } products 已删除商品列表
|
|
|
*/
|
|
|
showRemovedProducts: function(products, extraInfos) {
|
|
|
var productsLen = products.length,
|
|
|
index = 0,
|
|
|
currentPro,
|
|
|
currentExtraInfo,
|
|
|
currentSku;
|
|
|
|
|
|
var currentRemovedLen = removedProsInfo.length;
|
|
|
var currentItemBuyNumber;
|
|
|
|
|
|
var template;
|
|
|
|
|
|
if (extraInfos.length) {
|
|
|
for (index; index < productsLen; index++) {
|
|
|
currentPro = products[index];
|
|
|
currentExtraInfo = extraInfos[index];
|
|
|
currentSku = currentPro.product_sku;
|
|
|
|
|
|
if (removedProSkus.indexOf(currentPro.product_sku) === -1) {
|
|
|
// 当前删除商品不存在
|
|
|
removedProSkus.push(currentSku);
|
|
|
removedProsInfo.push({
|
|
|
productName: currentExtraInfo.productName,
|
|
|
salesPrice: currentExtraInfo.salesPrice,
|
|
|
buyNumber: currentPro.buy_number,
|
|
|
productSku: currentSku,
|
|
|
productId: currentExtraInfo.productId,
|
|
|
goodsId: currentExtraInfo.goodsId,
|
|
|
cnAlphabet: currentExtraInfo.cnAlphabet,
|
|
|
goodType: currentExtraInfo.goodType,
|
|
|
selected: currentExtraInfo.selected
|
|
|
});
|
|
|
} else {
|
|
|
// 当前删除商品是否已经存在,如果存在就累计数量
|
|
|
while (currentRemovedLen--) {
|
|
|
currentItemBuyNumber = removedProsInfo[currentRemovedLen].buyNumber;
|
|
|
if (removedProsInfo[currentRemovedLen].productSku === currentSku) {
|
|
|
removedProsInfo[currentRemovedLen].buyNumber = currentItemBuyNumber + currentPro.buy_number;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 刷新
|
|
|
template = hbs.compile($('#removed-products').html());
|
|
|
$('#removed_products').html(template({
|
|
|
removedProducts: removedProsInfo
|
|
|
}));
|
|
|
},
|
|
|
|
|
|
/*
|
|
|
* 商品移入收藏夹
|
|
|
* @function [sendToFavorite]
|
|
|
* @params { Array } products 商品列表
|
|
|
* @params { Function } callback 移入收藏夹成功后回调
|
|
|
*/
|
|
|
sendToFavorite: function(products) {
|
|
|
sendToFavorite: function(products, callback) {
|
|
|
var msg = '确定要将该商品从购物车中移入收藏吗?<br/>移入收藏后该商品将不在购物车中显示';
|
|
|
var dialog;
|
|
|
|
...
|
...
|
@@ -237,22 +287,39 @@ var Cart = { |
|
|
msg = '确定要将已选中的商品从购物车中移入收藏吗?<br/>移入收藏后已选中的商品将不在购物车中显示';
|
|
|
}
|
|
|
|
|
|
dialog = new _confirm({
|
|
|
content: msg,
|
|
|
cb: function() {
|
|
|
dialog.close();
|
|
|
Util.ajax({
|
|
|
url: '/shopping/cart/product/send_to_favorite',
|
|
|
type: 'POST',
|
|
|
data: {skuList: JSON.stringify(products)},
|
|
|
success: function(res) {
|
|
|
Util.refreshCart(res, function() {
|
|
|
Stepper.init();
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
}).show();
|
|
|
// callback存在说明从删除模块收藏
|
|
|
if (callback) {
|
|
|
Util.ajax({
|
|
|
url: '/shopping/cart/product/send_to_favorite',
|
|
|
type: 'POST',
|
|
|
data: {skuList: JSON.stringify(products)},
|
|
|
success: function(res) {
|
|
|
Util.refreshCart(res, function() {
|
|
|
Stepper.init();
|
|
|
if (callback) {
|
|
|
return callback();
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
} else {
|
|
|
dialog = new _confirm({
|
|
|
content: msg,
|
|
|
cb: function() {
|
|
|
dialog.close();
|
|
|
Util.ajax({
|
|
|
url: '/shopping/cart/product/send_to_favorite',
|
|
|
type: 'POST',
|
|
|
data: {skuList: JSON.stringify(products)},
|
|
|
success: function(res) {
|
|
|
Util.refreshCart(res, function() {
|
|
|
Stepper.init();
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
}).show();
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
|
...
|
...
|
|