Authored by jinhu.tung

increase and decrease product

... ... @@ -66,29 +66,55 @@ exports.checkInventory = (req, res) => {
// 修改数量
exports.changeProductNum = (req, res) => {
const changeType = req.body.changeType;
const changeTo = req.body.changeTo;
// const changeTo = req.body.changeTo;
const uid = req.user.uid;
const shoppingKey = req.cookies._SPK;
const sku = req.body.sku;
if (changeType === 'INCREASE') {
// TODO
if (changeTo === '4') {
res.json({
code: '1000',
num: parseInt(changeTo, 10) - 1,
changed: false
});
} else {
res.json({
code: '0',
num: changeTo,
changed: true
});
}
cartModel.modifyProductNum({
increaseNum: 1, // 默认是1
uid,
shoppingKey,
sku
}).then(result => {
console.log('changeProductNum-INCREASE-result:', result);
if (result.code === 200) {
cartModel.getCartData(shoppingKey, uid).then(cartData => {
res.json(_.merge(
cartModel.filterCartData(cartData, uid),
{code: cartData.code, message: result.message})
);
});
} else {
res.json(result);
}
});
// res.json({
// code: '1000',
// num: parseInt(changeTo, 10) - 1,
// changed: false
// });
} else if (changeType === 'DECREASE') {
// TODO
res.json({
code: '0',
num: changeTo,
changed: true
cartModel.modifyProductNum({
decreaseNum: 1, // 默认是1
uid,
shoppingKey,
sku
}).then(result => {
console.log('changeProductNum-DECREASE-result:', result);
if (result.code === 200) {
cartModel.getCartData(shoppingKey, uid).then(cartData => {
res.json(_.merge(
cartModel.filterCartData(cartData, uid),
{code: cartData.code, message: result.message})
);
});
} else {
res.json(result);
}
});
}
};
... ...
... ... @@ -265,7 +265,7 @@ const filterCartData = (result, uid) => {
* @param { Number } increaseNum 增加的数目
* @param { Number } decreaseNum 减少的数目
* @param { String } shoppingKey 未登录用户唯一识别码
* @return { Array } 接口返回数据
* @return { Array } 接口返回单个商品的增减数据
*/
const modifyProductNum = (options) => {
const increaseNum = options.increaseNum;
... ... @@ -310,6 +310,7 @@ const modifyProductNum = (options) => {
});
}
console.log('modifyProductNum---params:', params);
return api.get('', params);
};
... ...
... ... @@ -16,7 +16,7 @@ const order = require(`${cRoot}/order`); // 下单
// 购物车
router.get('/cart', cartCtrl.index);
router.get('/cart/product/check_inventory', cartCtrl.checkInventory);
router.post('/cart/product/:productId/change_num', cartCtrl.changeProductNum);
router.post('/cart/product/change_num', cartCtrl.changeProductNum);
router.delete('/cart/product/remove', cartCtrl.removeProduct);
router.post('/cart/product/send_to_favorite', cartCtrl.sendToFavorite);
router.get('/cart/product/:productId/edit', cartCtrl.editProduct);
... ...
... ... @@ -6,24 +6,27 @@
*/
var Util = require('./util');
var $ = require('yoho-jquery');
var Stepper = {
// 减少商品数量
decrease: function(productId, currNum, callback) {
decrease: function(sku, currNum) {
if (parseInt(currNum, 10) <= 1) {
return callback(1);
//return callback(1);
} else {
Util.ajax({
url: '/shopping/cart/product/' + productId + '/change_num',
url: '/shopping/cart/product/change_num',
type: 'POST',
data: {
changeType: 'DECREASE',
changeTo: parseInt(currNum, 10) - 1
sku: sku
// changeTo: parseInt(currNum, 10) - 1
},
success: function(res) {
if (res.code === '0') {
return callback(res.num, res.changed);
}
Util.refreshCart(res, function() {
Stepper.init();
});
}
});
}
... ... @@ -31,21 +34,25 @@ var Stepper = {
},
// 增加商品数量
increase: function(productId, currNum, callback) {
increase: function(sku) {
Util.ajax({
url: '/shopping/cart/product/' + productId + '/change_num',
url: '/shopping/cart/product/change_num',
type: 'POST',
data: {
changeType: 'INCREASE',
changeTo: parseInt(currNum, 10) + 1
sku: sku
// changeTo: parseInt(currNum, 10) + 1
},
success: function(res) {
if (res.code === '0') {
return callback(res.num, res.changed);
} else {
return callback(res.num, res.changed, res.code === '1000');
}
// if (res.code === '0') {
// return callback(res.num, res.changed);
// } else {
// return callback(res.num, res.changed, res.code === '1000');
// }
Util.refreshCart(res, function() {
Stepper.init();
});
}
});
},
... ... @@ -53,7 +60,7 @@ var Stepper = {
init: function() {
var _this = this,
$target,
productId;
sku;
var steppers = $('.stepper'),
$input,
... ... @@ -77,8 +84,8 @@ var Stepper = {
plus = $input.parent().next();
if (!$target.hasClass('disable')) {
productId = $target.parent().attr('data-productId');
_this.decrease(productId, currNum, function(num, changed) {
sku = $.parseJSON($target.parents('ul').children().first().attr('data-product_info')).product_sku;
_this.decrease(sku, currNum, function(num, changed) {
if (num === 1) {
$input.val(1);
$target.addClass('disable');
... ... @@ -103,8 +110,8 @@ var Stepper = {
minus = $input.parent().prev();
if (!$target.hasClass('disable')) {
productId = $target.parent().attr('data-productId');
_this.increase(productId, currNum, function(num, changed, overflow) {
sku = $.parseJSON($target.parents('ul').children().first().attr('data-product_info')).product_sku;
_this.increase(sku, currNum, function(num, changed, overflow) {
if (overflow) {
$target.addClass('disable');
}
... ...
... ... @@ -6,6 +6,7 @@
*/
var dialog = require('../../plugins/dialog');
var _alert = dialog.Alert;
var hbs = require('yoho-handlebars');
var Util = {
ajax: function(options) {
... ... @@ -27,6 +28,45 @@ var Util = {
new _alert('网络异常,稍后请重试').show();
}
});
},
/*
* 根据服务端JSON,刷新购物车信息
* @function [refreshCart]
*/
refreshCart: function(data, callback) {
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;
} else {
console.error('multiplication needs two number parameters');
}
});
hbs.registerHelper('isEqual', function(v1, v2, options) {
if (v1 === v2) {
return options.fn(this);
}
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));
if (callback) {
return callback();
}
}
};
... ...