Authored by 刘传洋

m

@@ -4,7 +4,9 @@ @@ -4,7 +4,9 @@
4 4
5 'use strict'; 5 'use strict';
6 6
  7 +const _ = require('lodash');
7 const co = require('bluebird').coroutine; 8 const co = require('bluebird').coroutine;
  9 +const logger = global.yoho.logger;
8 const service = require('../models/cart-service'); 10 const service = require('../models/cart-service');
9 const helper = require('../models/cart-helper'); 11 const helper = require('../models/cart-helper');
10 const ghelper = require('../../guang/models/guang-helper'); 12 const ghelper = require('../../guang/models/guang-helper');
@@ -134,7 +136,8 @@ const cartAdd = (req, res) => { @@ -134,7 +136,8 @@ const cartAdd = (req, res) => {
134 let goodsType = req.body.goodsType || 0; 136 let goodsType = req.body.goodsType || 0;
135 let promotionId = req.body.promotionId || 0; 137 let promotionId = req.body.promotionId || 0;
136 let isEdit = req.body.isEdit || 0; 138 let isEdit = req.body.isEdit || 0;
137 - let cartDelList = req.cookies['cart-del-list']; 139 + let isReAdd = !!req.body.isReAdd || false;
  140 + let cartDelList = helper.getCartDelList(req, res, null, isReAdd ? productSku : null);
138 141
139 let result = yield service.addToCart(productSku, buyNumber, 142 let result = yield service.addToCart(productSku, buyNumber,
140 goodsType, isEdit, promotionId, 143 goodsType, isEdit, promotionId,
@@ -182,7 +185,7 @@ const selectProduct = (req, res) => { @@ -182,7 +185,7 @@ const selectProduct = (req, res) => {
182 let productId = req.body.skuList; 185 let productId = req.body.skuList;
183 let hasPromotion = req.body.hasPromotion || false; 186 let hasPromotion = req.body.hasPromotion || false;
184 let shoppingKey = helper.getShoppingKeyByCookie(req); 187 let shoppingKey = helper.getShoppingKeyByCookie(req);
185 - let cartDelList = req.cookies['cart-del-list']; 188 + let cartDelList = helper.getCartDelList(req, res);
186 189
187 service.selectGoods(uid, productId, shoppingKey, hasPromotion, cartDelList) 190 service.selectGoods(uid, productId, shoppingKey, hasPromotion, cartDelList)
188 .then(ret => { 191 .then(ret => {
@@ -204,7 +207,7 @@ const modifyProductNum = (req, res, next) => { @@ -204,7 +207,7 @@ const modifyProductNum = (req, res, next) => {
204 let sku = req.body.sku; 207 let sku = req.body.sku;
205 let increaseNum = req.body.increaseNum || null; 208 let increaseNum = req.body.increaseNum || null;
206 let decreaseNum = req.body.decreaseNum || null; 209 let decreaseNum = req.body.decreaseNum || null;
207 - let cartDelList = req.cookies['cart-del-list']; 210 + let cartDelList = helper.getCartDelList(req, res);
208 211
209 return service.modifyProductNum(uid, sku, increaseNum, decreaseNum, shoppingKey, cartDelList) 212 return service.modifyProductNum(uid, sku, increaseNum, decreaseNum, shoppingKey, cartDelList)
210 .then(ret => { 213 .then(ret => {
@@ -232,20 +235,17 @@ const removeProduct = (req, res) => { @@ -232,20 +235,17 @@ const removeProduct = (req, res) => {
232 let shoppingKey = helper.getShoppingKeyByCookie(req); 235 let shoppingKey = helper.getShoppingKeyByCookie(req);
233 let skuList = req.body.skuList; 236 let skuList = req.body.skuList;
234 let hasPromotion = true; 237 let hasPromotion = true;
235 - let cartDelList = req.body.cartDelList;  
236 -  
237 - // let cartDelList = req.cookies['cart-del-list'];  
238 - 238 + let cartDelList = helper.getCartDelList(req, res, req.body.cartDelList);
239 239
240 let ret = yield service.removeFromCart(uid, shoppingKey, skuList, hasPromotion, cartDelList); 240 let ret = yield service.removeFromCart(uid, shoppingKey, skuList, hasPromotion, cartDelList);
241 241
242 if (ret && ret.code === 200) { 242 if (ret && ret.code === 200) {
243 yield setShoppingCookie(req, res); 243 yield setShoppingCookie(req, res);
244 244
245 - res.cookie('cart-del-list', cartDelList, { 245 + /* res.cookie('cart-del-list', cartDelList, {
246 domain: '.yohobuy.com', 246 domain: '.yohobuy.com',
247 path: '/' 247 path: '/'
248 - }); 248 + });*/
249 } 249 }
250 250
251 return res.send(ret); 251 return res.send(ret);
@@ -264,9 +264,25 @@ const moveToFav = (req, res) => { @@ -264,9 +264,25 @@ const moveToFav = (req, res) => {
264 // let shoppingKey = helper.getShoppingKeyByCookie(req); 264 // let shoppingKey = helper.getShoppingKeyByCookie(req);
265 let skuList = req.body.skuList; 265 let skuList = req.body.skuList;
266 let hasPromotion = req.body.hasPromotion || false; 266 let hasPromotion = req.body.hasPromotion || false;
267 - let cartDelList = req.cookies['cart-del-list']; 267 + let isReFav = !!req.body.isReFav || false;
  268 + let productSku = null;
  269 + let cartDelList;
  270 + let ret;
268 271
269 - let ret = yield service.addToFav(uid, skuList, hasPromotion, cartDelList); 272 + if (isReFav) {
  273 + try {
  274 + let sl = JSON.parse(skuList);
  275 +
  276 + if (sl && sl.length) {
  277 + productSku = _.get(sl[0], 'product_sku');
  278 + }
  279 + } catch (err) {
  280 + logger.error(err);
  281 + }
  282 + }
  283 +
  284 + cartDelList = helper.getCartDelList(req, res, null, productSku);
  285 + ret = yield service.addToFav(uid, skuList, hasPromotion, cartDelList);
270 286
271 if (ret && ret.code === 200) { 287 if (ret && ret.code === 200) {
272 yield setShoppingCookie(req, res); 288 yield setShoppingCookie(req, res);
@@ -363,7 +379,7 @@ const getIncreasePurchase = (req, res) => { @@ -363,7 +379,7 @@ const getIncreasePurchase = (req, res) => {
363 const modifyProduct = (req, res, next) => { 379 const modifyProduct = (req, res, next) => {
364 const uid = req.user && req.user.uid; 380 const uid = req.user && req.user.uid;
365 const shoppingKey = helper.getShoppingKeyByCookie(req); 381 const shoppingKey = helper.getShoppingKeyByCookie(req);
366 - let cartDelList = req.cookies['cart-del-list']; 382 + let cartDelList = helper.getCartDelList(req, res);
367 383
368 // swapData => [{"buy_number":"1","selected":"Y","new_product_sku":"735172","old_product_sku":"735171"}] 384 // swapData => [{"buy_number":"1","selected":"Y","new_product_sku":"735172","old_product_sku":"735171"}]
369 const swapData = req.body.swapData; 385 const swapData = req.body.swapData;
@@ -386,7 +402,7 @@ const swapGift = (req, res, next) => { @@ -386,7 +402,7 @@ const swapGift = (req, res, next) => {
386 let promotionId = req.body.promotionId; 402 let promotionId = req.body.promotionId;
387 let newSkn = req.body.newSkn; 403 let newSkn = req.body.newSkn;
388 let newSku = req.body.newSku; 404 let newSku = req.body.newSku;
389 - let cartDelList = req.cookies['cart-del-list']; 405 + let cartDelList = helper.getCartDelList(req, res);
390 406
391 service.swapGift(uid, shoppingKey, promotionId, newSkn, newSku, cartDelList) 407 service.swapGift(uid, shoppingKey, promotionId, newSkn, newSku, cartDelList)
392 .then(ret => { 408 .then(ret => {
@@ -54,6 +54,55 @@ const makeToken = (str) => { @@ -54,6 +54,55 @@ const makeToken = (str) => {
54 }; 54 };
55 55
56 /** 56 /**
  57 + * cookie cart-del-list 的数据操作
  58 + * @param req
  59 + * @param res
  60 + * @param addsStr 向删除列表中加入数据
  61 + * @param delId 删除列表中移除某个 id 的产品
  62 + */
  63 +const getCartDelList = (req, res, addsStr, delSkuId) => {
  64 +
  65 + let cartDelListStr = req.cookies['cart-del-list'] || '';
  66 + let isMod = false;
  67 + let cookieDelStr = '';
  68 +
  69 + try {
  70 + let cartDelList = cartDelListStr ? JSON.parse(cartDelListStr) : [];
  71 + let addList = addsStr ? JSON.parse(addsStr) : null;
  72 +
  73 + // 追加
  74 + if (addList && addList.length && _.isArray(cartDelList)) {
  75 + isMod = true;
  76 + cartDelList = cartDelList.concat(addList);
  77 + }
  78 +
  79 + if (delSkuId) {
  80 + isMod = true;
  81 + _.remove(cartDelList, it => {
  82 + return Number(it.productSku) === Number(delSkuId);
  83 + });
  84 + }
  85 +
  86 + if (isMod) {
  87 +
  88 + if (cartDelList && cartDelList.length > 0) {
  89 + cookieDelStr = JSON.stringify(cartDelList);
  90 + }
  91 +
  92 + res.cookie('cart-del-list', cookieDelStr, {
  93 + domain: '.yohobuy.com',
  94 + path: '/'
  95 + });
  96 + }
  97 + return cartDelList;
  98 + } catch (err) {
  99 + logger.error(err);
  100 + }
  101 +
  102 + return null;
  103 +};
  104 +
  105 +/**
57 * 购物车商品 106 * 购物车商品
58 * @param array cartGoods 购物车商品列表 107 * @param array cartGoods 购物车商品列表
59 * @param bool isAdvanceCart 是否是预售购物车(和上市期有关) 108 * @param bool isAdvanceCart 是否是预售购物车(和上市期有关)
@@ -540,14 +589,8 @@ const formatCart = (cartDataRet, uid, shoppingKey, cartDelList) => { @@ -540,14 +589,8 @@ const formatCart = (cartDataRet, uid, shoppingKey, cartDelList) => {
540 selectedGoodsCount: _.get(advStat, 'selectedGoodsCount', 0) + _.get(ordStat, 'selectedGoodsCount', 0) 589 selectedGoodsCount: _.get(advStat, 'selectedGoodsCount', 0) + _.get(ordStat, 'selectedGoodsCount', 0)
541 }; 590 };
542 591
543 - /* 移除的商品列表 */  
544 - if (cartDelList) {  
545 - try {  
546 - result.deleteShop = JSON.parse(cartDelList);  
547 - } catch (err) {  
548 - logger.error(err);  
549 - }  
550 - } 592 + // 移除的商品列表
  593 + result.deleteShop = cartDelList;
551 594
552 // 普通购物车和预售购物车都为空 595 // 普通购物车和预售购物车都为空
553 /* if (ordinaryCount === 0 && advanceCount === 0 && ordinarySoldOut && advanceSoldOut) { 596 /* if (ordinaryCount === 0 && advanceCount === 0 && ordinarySoldOut && advanceSoldOut) {
@@ -587,5 +630,7 @@ module.exports = { @@ -587,5 +630,7 @@ module.exports = {
587 formatPromotionInfos, 630 formatPromotionInfos,
588 formatOffShelves, 631 formatOffShelves,
589 formatSoldOuts, 632 formatSoldOuts,
590 - formatCart 633 + formatCart,
  634 +
  635 + getCartDelList
591 }; 636 };
@@ -495,7 +495,6 @@ const selectGoods = (uid, skuList, shoppingKey, hasPromotion, cartDelList) => { @@ -495,7 +495,6 @@ const selectGoods = (uid, skuList, shoppingKey, hasPromotion, cartDelList) => {
495 let select = yield cartApi.selectGoods(uid, skuList, shoppingKey, hasPromotion); 495 let select = yield cartApi.selectGoods(uid, skuList, shoppingKey, hasPromotion);
496 496
497 if (select && select.code) { 497 if (select && select.code) {
498 -  
499 result = { 498 result = {
500 code: select.code, 499 code: select.code,
501 data: chelper.formatCart(select, uid, shoppingKey, cartDelList) 500 data: chelper.formatCart(select, uid, shoppingKey, cartDelList)
@@ -227,7 +227,7 @@ @@ -227,7 +227,7 @@
227 {{/priceGifts}} 227 {{/priceGifts}}
228 228
229 {{#promotionInfos}} 229 {{#promotionInfos}}
230 - <p class="gift-sell-info"><code class="order-pay-mark">{{tag}}</code>{{promotionTitle}} 230 + <p class="gift-sell-info">{{#if tag}}<code class="order-pay-mark">{{tag}}</code>{{/if}}{{promotionTitle}}
231 <!--<a class="btn-clear blue" data-together-id="6">去凑单&nbsp;&gt;</a>--> 231 <!--<a class="btn-clear blue" data-together-id="6">去凑单&nbsp;&gt;</a>-->
232 </p> 232 </p>
233 {{/promotionInfos}} 233 {{/promotionInfos}}
@@ -308,7 +308,8 @@ Cart = { @@ -308,7 +308,8 @@ Cart = {
308 capi.addcart({ 308 capi.addcart({
309 productSku: $li.data('sku'), 309 productSku: $li.data('sku'),
310 buyNumber: $li.data('num'), 310 buyNumber: $li.data('num'),
311 - promotionId: $li.data('promotionid') 311 + promotionId: $li.data('promotionid'),
  312 + isReAdd: true
312 }); 313 });
313 }, 314 },
314 reFav: function() { 315 reFav: function() {
@@ -322,7 +323,7 @@ Cart = { @@ -322,7 +323,7 @@ Cart = {
322 promotion_id: $li.data('promotionid') || 0 323 promotion_id: $li.data('promotionid') || 0
323 }; 324 };
324 325
325 - capi.cartItemDel(item); 326 + capi.cartItemDel(item, null, null, true);
326 }, 327 },
327 _submit: function($t) { 328 _submit: function($t) {
328 329
@@ -135,7 +135,7 @@ function choiceOut(items) { @@ -135,7 +135,7 @@ function choiceOut(items) {
135 * data: 数据 135 * data: 数据
136 * tpe: true - 删除,默认 移入收藏夹 136 * tpe: true - 删除,默认 移入收藏夹
137 */ 137 */
138 -function cartItemDel(items, type, cookieList) { 138 +function cartItemDel(items, type, cookieList, isReFav) {
139 139
140 var selList = $.isArray(items) ? items : [items]; 140 var selList = $.isArray(items) ? items : [items];
141 var hasPromotion = false; 141 var hasPromotion = false;
@@ -154,7 +154,8 @@ function cartItemDel(items, type, cookieList) { @@ -154,7 +154,8 @@ function cartItemDel(items, type, cookieList) {
154 data: { 154 data: {
155 skuList: JSON.stringify(selList), 155 skuList: JSON.stringify(selList),
156 hasPromotion: hasPromotion, 156 hasPromotion: hasPromotion,
157 - cartDelList: JSON.stringify(cookieList) 157 + cartDelList: JSON.stringify(cookieList),
  158 + isReFav: isReFav || false
158 }, 159 },
159 beforeSend: function() { 160 beforeSend: function() {
160 $('.loading').show(); 161 $('.loading').show();
@@ -342,7 +343,7 @@ function updateCartItem(newSku, oldSku) { @@ -342,7 +343,7 @@ function updateCartItem(newSku, oldSku) {
342 }).then(function(d) { 343 }).then(function(d) {
343 if (d.code === 200) { 344 if (d.code === 200) {
344 // window.history.go(0); 345 // window.history.go(0);
345 - $('#Y_CartListWrap').html(cartListTpl(d.data)); 346 + refreshCart(d.data);
346 } else { 347 } else {
347 new Alert(d.message === '' ? '修改商品失败哦~~' : d.message).show(); 348 new Alert(d.message === '' ? '修改商品失败哦~~' : d.message).show();
348 } 349 }
@@ -362,7 +363,7 @@ function updateCartGiftItem(promotionId, newSkn, newSku) { @@ -362,7 +363,7 @@ function updateCartGiftItem(promotionId, newSkn, newSku) {
362 }).then(function(d) { 363 }).then(function(d) {
363 if (d.code === 200) { 364 if (d.code === 200) {
364 // window.history.go(0); 365 // window.history.go(0);
365 - $('#Y_CartListWrap').html(cartListTpl(d.data)); 366 + refreshCart(d.data);
366 } else { 367 } else {
367 new Alert(d.message === '' ? '修改商品失败哦~~' : d.message).show(); 368 new Alert(d.message === '' ? '修改商品失败哦~~' : d.message).show();
368 } 369 }