Authored by yyq

Merge branch 'feature/shoppingCart' of git.yoho.cn:fe/yohobuy-node into feature/shoppingCart

@@ -4,8 +4,10 @@ @@ -4,8 +4,10 @@
4 4
5 'use strict'; 5 'use strict';
6 6
  7 +const co = require('bluebird').coroutine;
7 const service = require('../models/cart-service'); 8 const service = require('../models/cart-service');
8 const helper = require('../models/cart-helper'); 9 const helper = require('../models/cart-helper');
  10 +const ghelper = require('../../guang/models/guang-helper');
9 const simpleHeaderModel = require('../../../doraemon/models/simple-header'); 11 const simpleHeaderModel = require('../../../doraemon/models/simple-header');
10 12
11 const getProductInfo = (req, res, next) => { 13 const getProductInfo = (req, res, next) => {
@@ -18,6 +20,27 @@ const getProductInfo = (req, res, next) => { @@ -18,6 +20,27 @@ const getProductInfo = (req, res, next) => {
18 }).catch(next); 20 }).catch(next);
19 }; 21 };
20 22
  23 +
  24 +/**
  25 + * 设置购物车COOKIE信息
  26 + */
  27 +const setShoppingCookie = (req) => {
  28 +
  29 + let uid = req.user.uid || true;
  30 + let shoppingKey = helper.getShoppingKeyByCookie(req);
  31 +
  32 + return service.getCartCount(uid, shoppingKey).then(ret => {
  33 + if (ret && ret.data && ret.data.cart_goods_count) {
  34 + req.cookies('_g', {
  35 + _k: shoppingKey,
  36 + _nac: ret.data.cart_goods_count,
  37 + _ac: 0,
  38 + _r: 1
  39 + });
  40 + }
  41 + });
  42 +};
  43 +
21 /** 44 /**
22 * 我的购物车 45 * 我的购物车
23 */ 46 */
@@ -27,6 +50,10 @@ const cart = (req, res, next) => { @@ -27,6 +50,10 @@ const cart = (req, res, next) => {
27 let shoppingKey = helper.getShoppingKeyByCookie(req); 50 let shoppingKey = helper.getShoppingKeyByCookie(req);
28 let cartDelList = req.cookies['cart-del-list']; 51 let cartDelList = req.cookies['cart-del-list'];
29 52
  53 + if (cartDelList) {
  54 + req.cookies['cart-del-list'] = '';
  55 + }
  56 +
30 service.getCartData(uid, shoppingKey, cartDelList) 57 service.getCartData(uid, shoppingKey, cartDelList)
31 .then(ret => { 58 .then(ret => {
32 console.log(JSON.stringify(ret)); 59 console.log(JSON.stringify(ret));
@@ -48,6 +75,35 @@ const cart = (req, res, next) => { @@ -48,6 +75,35 @@ const cart = (req, res, next) => {
48 */ 75 */
49 const cartAdd = () => { 76 const cartAdd = () => {
50 77
  78 + co(function * (){
  79 + let uid = req.user.uid;
  80 + let shoppingKey = helper.getShoppingKeyByCookie(req);
  81 + let productSku = req.body.productSku;
  82 + let buyNumber = req.body.buyNumber || 1;
  83 + let goodsType = req.body.goodsType || 0;
  84 + let promotionId = req.body.promotionId || 0
  85 + let isEdit = req.body.isEdit || 0;
  86 +
  87 + let result = yield service.addToCart(productSku, buyNumber, goodsType, isEdit, promotionId, uid, shoppingKey);
  88 +
  89 + // 设置加入购物车凭证到客户端浏览器
  90 + if(!shoppingKey && result && result.data && result.data.shopping_key) {
  91 + // req.cookies['_SPK'] = result.data.shopping_key
  92 + //$this->setCookie('_SPK', $result['data']['shopping_key'], time() + 86400 * 360);
  93 + }
  94 +
  95 + // 老站购物车需要的COOKIE
  96 + if (result && result.data && result.data.shopping_key) {
  97 + /*$this->setCookie('_g', json_encode(array(
  98 + '_k' => $result['data']['shopping_key'],
  99 + '_nac' => $result['data']['goods_count'],
  100 + '_ac' => 0,
  101 + '_r' => 1
  102 + )));*/
  103 + }
  104 +
  105 + res.send(result);
  106 + });
51 }; 107 };
52 108
53 /** 109 /**
@@ -55,63 +111,162 @@ const cartAdd = () => { @@ -55,63 +111,162 @@ const cartAdd = () => {
55 */ 111 */
56 const cartTotal = () => { 112 const cartTotal = () => {
57 113
58 -}; 114 + co(function * (){
59 115
60 -/**  
61 - * 设置购物车COOKIE信息  
62 - */  
63 -const setShoppingCookie = () => { 116 + let uid = req.user.uid;
  117 + let shoppingKey = helper.getShoppingKeyByCookie(req);
  118 + let callback = req.query.callback;
  119 + let ret = yield service.getCartCount(uid, shoppingKey);
64 120
  121 + return res.send(callback + '(' + JSON.stringify(ret) + ')');
  122 + });
65 }; 123 };
66 124
67 /** 125 /**
68 * 购物车商品选择与取消 126 * 购物车商品选择与取消
69 */ 127 */
70 -const selectProduct = () => { 128 +const selectProduct = (req, res) => {
71 129
  130 + let uid = req.user.uid;
  131 + let productId = req.body.skuList;
  132 + let hasPromotion = req.body.hasPromotion || false;
  133 + let shoppingKey = helper.getShoppingKeyByCookie(req);
  134 +
  135 + service.selectGoods(uid, productId, shoppingKey, hasPromotion)
  136 + .then(ret => {
  137 + res.send(ret);
  138 + }).catch(() => {
  139 + res.send({
  140 + code: 400
  141 + });
  142 + });
72 }; 143 };
73 144
74 /** 145 /**
75 * 修改购物车商品数量 146 * 修改购物车商品数量
76 */ 147 */
77 -const modifyProduct = () => { 148 +const modifyProduct = (req, res) => {
  149 +
  150 + let uid = req.user.uid;
  151 + let shoppingKey = helper.getShoppingKeyByCookie(req);
  152 + let sku = req.body.sku;
  153 + let increaseNum = req.body.increaseNum || null;
  154 + let decreaseNum = req.body.decreaseNum || null;
78 155
  156 + return service.modifyProductNum(uid, sku, increaseNum, decreaseNum, shoppingKey)
  157 + .then(ret => {
  158 + if (ret && ret.code === 200) {
  159 + return setShoppingCookie().then(() => {
  160 + return res.send(ret);
  161 + });
  162 + }
  163 + })
  164 + .catch(next);
79 }; 165 };
80 166
81 /** 167 /**
82 * 移出购物车 168 * 移出购物车
83 */ 169 */
84 -const removeProduct = () => { 170 +const removeProduct = (req, res) => {
  171 +
  172 + co(function * (){
  173 + let uid = req.user.uid;
  174 + let shoppingKey = helper.getShoppingKeyByCookie(req);
  175 + let skuList = req.body.skuList;
  176 + let hasPromotion = true;
  177 +
  178 + let ret = yield service.removeFromCart(uid, shoppingKey, skuList, hasPromotion);
85 179
  180 + if(ret && ret.code === 200) {
  181 + yield setShoppingCookie();
  182 + }
  183 +
  184 + return res.send(ret);
  185 + });
86 }; 186 };
87 187
88 /** 188 /**
89 * 移入收藏夹 189 * 移入收藏夹
90 * 支持批量移入收藏夹 190 * 支持批量移入收藏夹
91 */ 191 */
92 -const moveToFav = () => { 192 +const moveToFav = (req, res) => {
  193 +
  194 + co(function * (){
  195 + let uid = req.user.uid;
  196 + // let shoppingKey = helper.getShoppingKeyByCookie(req);
  197 + let skuList = req.body.skuList;
  198 + let hasPromotion = req.body.hasPromotion || false;
  199 +
  200 + let ret = yield service.addToFav(uid, skuList, hasPromotion);
93 201
  202 + if(ret && ret.code === 200) {
  203 + yield setShoppingCookie();
  204 + }
  205 +
  206 + return res.send(ret);
  207 + });
94 }; 208 };
95 209
96 /** 210 /**
97 * 检查是否收藏 211 * 检查是否收藏
98 */ 212 */
99 -const checkFav = () => { 213 +const checkFav = (req, res) => {
  214 +
  215 + co(function * (){
  216 +
  217 + let uid = req.user.uid;
  218 + let pids = req.body.pidList;
  219 + let ret = {
  220 + code: 200,
  221 + message: '是否收藏',
  222 + data: {}
  223 + };
100 224
  225 + ret.data = yield service.checkUserIsFav(uid, pids);
  226 +
  227 + return res.send(ret);
  228 + });
101 }; 229 };
102 230
103 /** 231 /**
104 * 凑单商品异步请求 232 * 凑单商品异步请求
105 */ 233 */
106 -const getTogetherProduct = () => { 234 +const getTogetherProduct = (req, res) => {
  235 +
  236 + co(function * (){
  237 +
  238 + let page = req.query.page;
  239 + /*let ret = {
  240 + code: 200,
  241 + message: '凑单商品'
  242 + };*/
107 243
  244 + ret = yield service.getTogetherProduct(page);
  245 +
  246 + return res.send(ret);
  247 + });
108 }; 248 };
109 249
110 /** 250 /**
111 * 为你优选商品异步请求 251 * 为你优选商品异步请求
112 */ 252 */
113 -const getRecommendProductAction = () => { 253 +const getRecommendProductAction = (req, res) => {
  254 +
  255 + co(function * (){
  256 +
  257 + let channel = req.yoho.channel;
  258 + let uid = req.user.uid;
  259 + let udid = ghelper.getUdid(req, res);
  260 + let page = req.query.page;
  261 +
  262 + if(page === '6') {
  263 + page = 1;
  264 + }
  265 +
  266 + let ret = yield service.getRecommendProduct(channel, uid, udid, page);
114 267
  268 + res.send(ret);
  269 + });
115 }; 270 };
116 271
117 module.exports = { 272 module.exports = {
@@ -384,7 +384,92 @@ const getCartData = (uid, shoppingKey, cartDelList) => { @@ -384,7 +384,92 @@ const getCartData = (uid, shoppingKey, cartDelList) => {
384 }); 384 });
385 }; 385 };
386 386
  387 +const getCartCount = (uid, shoppingKey) => {
  388 +
  389 + return Promise.resolve({
  390 + uid,
  391 + shoppingKey
  392 + });
  393 +};
  394 +
  395 +const addToCart = (productSku, buyNumber, goodsType, isEdit, promotionId, uid, shoppingKey) => {
  396 +
  397 + return Promise.resolve({
  398 + productSku,
  399 + buyNumber,
  400 + goodsType,
  401 + isEdit,
  402 + promotionId,
  403 + uid,
  404 + shoppingKey
  405 + });
  406 +};
  407 +
  408 +const selectGoods = (uid, productId, shoppingKey, hasPromotion) => {
  409 +
  410 + return Promise.resolve({
  411 + uid,
  412 + productId,
  413 + shoppingKey,
  414 + hasPromotion
  415 + });
  416 +};
  417 +
  418 +const modifyProductNum = (uid, sku, increaseNum, decreaseNum, shoppingKey) => {
  419 + return Promise.resolve({
  420 + uid,
  421 + sku,
  422 + increaseNum,
  423 + decreaseNum,
  424 + shoppingKey
  425 + });
  426 +};
  427 +
  428 +const removeFromCart = (uid, shoppingKey, skuList, hasPromotion) => {
  429 +
  430 + return Promise.resolve({
  431 + uid,
  432 + shoppingKey,
  433 + skuList,
  434 + hasPromotion
  435 + });
  436 +};
  437 +
  438 +const addToFav = (uid, skuList, hasPromotion) => {
  439 +
  440 + return Promise.resolve({
  441 + uid,
  442 + skuList,
  443 + hasPromotion
  444 + });
  445 +};
  446 +
  447 +const checkUserIsFav = (uid, pids) => {
  448 + return Promise.resolve({
  449 + uid,
  450 + pids
  451 + });
  452 +};
  453 +
  454 +const getRecommendProduct = (channel, uid, udid, page) => {
  455 +
  456 + return Promise.resolve({
  457 + channel,
  458 + uid,
  459 + udid,
  460 + page
  461 + });
  462 +};
  463 +
387 module.exports = { 464 module.exports = {
388 getProductInfoAsync, // 获取某一个商品详情主页面 465 getProductInfoAsync, // 获取某一个商品详情主页面
389 - getCartData 466 + getCartData,
  467 + getCartCount,
  468 + addToCart,
  469 + selectGoods,
  470 + modifyProductNum,
  471 + removeFromCart,
  472 + addToFav,
  473 + checkUserIsFav,
  474 + getRecommendProduct
390 }; 475 };