Authored by 刘传洋

m

... ... @@ -4,7 +4,9 @@
'use strict';
const _ = require('lodash');
const co = require('bluebird').coroutine;
const logger = global.yoho.logger;
const service = require('../models/cart-service');
const helper = require('../models/cart-helper');
const ghelper = require('../../guang/models/guang-helper');
... ... @@ -134,7 +136,8 @@ const cartAdd = (req, res) => {
let goodsType = req.body.goodsType || 0;
let promotionId = req.body.promotionId || 0;
let isEdit = req.body.isEdit || 0;
let cartDelList = req.cookies['cart-del-list'];
let isReAdd = !!req.body.isReAdd || false;
let cartDelList = helper.getCartDelList(req, res, null, isReAdd ? productSku : null);
let result = yield service.addToCart(productSku, buyNumber,
goodsType, isEdit, promotionId,
... ... @@ -182,7 +185,7 @@ const selectProduct = (req, res) => {
let productId = req.body.skuList;
let hasPromotion = req.body.hasPromotion || false;
let shoppingKey = helper.getShoppingKeyByCookie(req);
let cartDelList = req.cookies['cart-del-list'];
let cartDelList = helper.getCartDelList(req, res);
service.selectGoods(uid, productId, shoppingKey, hasPromotion, cartDelList)
.then(ret => {
... ... @@ -204,7 +207,7 @@ const modifyProductNum = (req, res, next) => {
let sku = req.body.sku;
let increaseNum = req.body.increaseNum || null;
let decreaseNum = req.body.decreaseNum || null;
let cartDelList = req.cookies['cart-del-list'];
let cartDelList = helper.getCartDelList(req, res);
return service.modifyProductNum(uid, sku, increaseNum, decreaseNum, shoppingKey, cartDelList)
.then(ret => {
... ... @@ -232,20 +235,17 @@ const removeProduct = (req, res) => {
let shoppingKey = helper.getShoppingKeyByCookie(req);
let skuList = req.body.skuList;
let hasPromotion = true;
let cartDelList = req.body.cartDelList;
// let cartDelList = req.cookies['cart-del-list'];
let cartDelList = helper.getCartDelList(req, res, req.body.cartDelList);
let ret = yield service.removeFromCart(uid, shoppingKey, skuList, hasPromotion, cartDelList);
if (ret && ret.code === 200) {
yield setShoppingCookie(req, res);
res.cookie('cart-del-list', cartDelList, {
/* res.cookie('cart-del-list', cartDelList, {
domain: '.yohobuy.com',
path: '/'
});
});*/
}
return res.send(ret);
... ... @@ -264,9 +264,25 @@ const moveToFav = (req, res) => {
// let shoppingKey = helper.getShoppingKeyByCookie(req);
let skuList = req.body.skuList;
let hasPromotion = req.body.hasPromotion || false;
let cartDelList = req.cookies['cart-del-list'];
let isReFav = !!req.body.isReFav || false;
let productSku = null;
let cartDelList;
let ret;
let ret = yield service.addToFav(uid, skuList, hasPromotion, cartDelList);
if (isReFav) {
try {
let sl = JSON.parse(skuList);
if (sl && sl.length) {
productSku = _.get(sl[0], 'product_sku');
}
} catch (err) {
logger.error(err);
}
}
cartDelList = helper.getCartDelList(req, res, null, productSku);
ret = yield service.addToFav(uid, skuList, hasPromotion, cartDelList);
if (ret && ret.code === 200) {
yield setShoppingCookie(req, res);
... ... @@ -363,7 +379,7 @@ const getIncreasePurchase = (req, res) => {
const modifyProduct = (req, res, next) => {
const uid = req.user && req.user.uid;
const shoppingKey = helper.getShoppingKeyByCookie(req);
let cartDelList = req.cookies['cart-del-list'];
let cartDelList = helper.getCartDelList(req, res);
// swapData => [{"buy_number":"1","selected":"Y","new_product_sku":"735172","old_product_sku":"735171"}]
const swapData = req.body.swapData;
... ... @@ -386,7 +402,7 @@ const swapGift = (req, res, next) => {
let promotionId = req.body.promotionId;
let newSkn = req.body.newSkn;
let newSku = req.body.newSku;
let cartDelList = req.cookies['cart-del-list'];
let cartDelList = helper.getCartDelList(req, res);
service.swapGift(uid, shoppingKey, promotionId, newSkn, newSku, cartDelList)
.then(ret => {
... ...
... ... @@ -54,6 +54,55 @@ const makeToken = (str) => {
};
/**
* cookie cart-del-list 的数据操作
* @param req
* @param res
* @param addsStr 向删除列表中加入数据
* @param delId 删除列表中移除某个 id 的产品
*/
const getCartDelList = (req, res, addsStr, delSkuId) => {
let cartDelListStr = req.cookies['cart-del-list'] || '';
let isMod = false;
let cookieDelStr = '';
try {
let cartDelList = cartDelListStr ? JSON.parse(cartDelListStr) : [];
let addList = addsStr ? JSON.parse(addsStr) : null;
// 追加
if (addList && addList.length && _.isArray(cartDelList)) {
isMod = true;
cartDelList = cartDelList.concat(addList);
}
if (delSkuId) {
isMod = true;
_.remove(cartDelList, it => {
return Number(it.productSku) === Number(delSkuId);
});
}
if (isMod) {
if (cartDelList && cartDelList.length > 0) {
cookieDelStr = JSON.stringify(cartDelList);
}
res.cookie('cart-del-list', cookieDelStr, {
domain: '.yohobuy.com',
path: '/'
});
}
return cartDelList;
} catch (err) {
logger.error(err);
}
return null;
};
/**
* 购物车商品
* @param array cartGoods 购物车商品列表
* @param bool isAdvanceCart 是否是预售购物车(和上市期有关)
... ... @@ -540,14 +589,8 @@ const formatCart = (cartDataRet, uid, shoppingKey, cartDelList) => {
selectedGoodsCount: _.get(advStat, 'selectedGoodsCount', 0) + _.get(ordStat, 'selectedGoodsCount', 0)
};
/* 移除的商品列表 */
if (cartDelList) {
try {
result.deleteShop = JSON.parse(cartDelList);
} catch (err) {
logger.error(err);
}
}
// 移除的商品列表
result.deleteShop = cartDelList;
// 普通购物车和预售购物车都为空
/* if (ordinaryCount === 0 && advanceCount === 0 && ordinarySoldOut && advanceSoldOut) {
... ... @@ -587,5 +630,7 @@ module.exports = {
formatPromotionInfos,
formatOffShelves,
formatSoldOuts,
formatCart
formatCart,
getCartDelList
};
... ...
... ... @@ -495,7 +495,6 @@ const selectGoods = (uid, skuList, shoppingKey, hasPromotion, cartDelList) => {
let select = yield cartApi.selectGoods(uid, skuList, shoppingKey, hasPromotion);
if (select && select.code) {
result = {
code: select.code,
data: chelper.formatCart(select, uid, shoppingKey, cartDelList)
... ...
... ... @@ -227,7 +227,7 @@
{{/priceGifts}}
{{#promotionInfos}}
<p class="gift-sell-info"><code class="order-pay-mark">{{tag}}</code>{{promotionTitle}}
<p class="gift-sell-info">{{#if tag}}<code class="order-pay-mark">{{tag}}</code>{{/if}}{{promotionTitle}}
<!--<a class="btn-clear blue" data-together-id="6">去凑单&nbsp;&gt;</a>-->
</p>
{{/promotionInfos}}
... ...
... ... @@ -308,7 +308,8 @@ Cart = {
capi.addcart({
productSku: $li.data('sku'),
buyNumber: $li.data('num'),
promotionId: $li.data('promotionid')
promotionId: $li.data('promotionid'),
isReAdd: true
});
},
reFav: function() {
... ... @@ -322,7 +323,7 @@ Cart = {
promotion_id: $li.data('promotionid') || 0
};
capi.cartItemDel(item);
capi.cartItemDel(item, null, null, true);
},
_submit: function($t) {
... ...
... ... @@ -135,7 +135,7 @@ function choiceOut(items) {
* data: 数据
* tpe: true - 删除,默认 移入收藏夹
*/
function cartItemDel(items, type, cookieList) {
function cartItemDel(items, type, cookieList, isReFav) {
var selList = $.isArray(items) ? items : [items];
var hasPromotion = false;
... ... @@ -154,7 +154,8 @@ function cartItemDel(items, type, cookieList) {
data: {
skuList: JSON.stringify(selList),
hasPromotion: hasPromotion,
cartDelList: JSON.stringify(cookieList)
cartDelList: JSON.stringify(cookieList),
isReFav: isReFav || false
},
beforeSend: function() {
$('.loading').show();
... ... @@ -342,7 +343,7 @@ function updateCartItem(newSku, oldSku) {
}).then(function(d) {
if (d.code === 200) {
// window.history.go(0);
$('#Y_CartListWrap').html(cartListTpl(d.data));
refreshCart(d.data);
} else {
new Alert(d.message === '' ? '修改商品失败哦~~' : d.message).show();
}
... ... @@ -362,7 +363,7 @@ function updateCartGiftItem(promotionId, newSkn, newSku) {
}).then(function(d) {
if (d.code === 200) {
// window.history.go(0);
$('#Y_CartListWrap').html(cartListTpl(d.data));
refreshCart(d.data);
} else {
new Alert(d.message === '' ? '修改商品失败哦~~' : d.message).show();
}
... ...