Showing
5 changed files
with
52 additions
and
33 deletions
@@ -15,6 +15,7 @@ exports.index = (req, res, next) => { | @@ -15,6 +15,7 @@ exports.index = (req, res, next) => { | ||
15 | const uid = req.user.uid || null; | 15 | const uid = req.user.uid || null; |
16 | 16 | ||
17 | cartModel.getCartData(shoppingKey, uid).then((result) => { | 17 | cartModel.getCartData(shoppingKey, uid).then((result) => { |
18 | + console.log(JSON.stringify(result, '', 4)); | ||
18 | if (result.code === 200) { | 19 | if (result.code === 200) { |
19 | res.display('cart', _.merge({ | 20 | res.display('cart', _.merge({ |
20 | module: 'shopping', | 21 | module: 'shopping', |
@@ -201,3 +202,22 @@ exports.modifyProduct = (req, res, next) => { | @@ -201,3 +202,22 @@ exports.modifyProduct = (req, res, next) => { | ||
201 | } | 202 | } |
202 | }).catch(next); | 203 | }).catch(next); |
203 | }; | 204 | }; |
205 | + | ||
206 | +// 获取购物车商品信息 | ||
207 | +exports.getCartData = (req, res, next) => { | ||
208 | + const shoppingKey = req.cookies._SPK || null; | ||
209 | + const uid = req.user.uid || null; | ||
210 | + | ||
211 | + cartModel.getCartData(shoppingKey, uid).then((result) => { | ||
212 | + if (result.code === 200) { | ||
213 | + res.json( | ||
214 | + _.merge( | ||
215 | + cartModel.filterCartData(result, uid), | ||
216 | + {code: result.code, message: result.message} | ||
217 | + ) | ||
218 | + ); | ||
219 | + } else { | ||
220 | + res.send(result); | ||
221 | + } | ||
222 | + }).catch(next); | ||
223 | +}; |
@@ -23,6 +23,7 @@ router.post('/cart/add', cartCtrl.addToCart); | @@ -23,6 +23,7 @@ router.post('/cart/add', cartCtrl.addToCart); | ||
23 | router.post('/cart/toggleSelectGoods', cartCtrl.toggleSelectGoods); | 23 | router.post('/cart/toggleSelectGoods', cartCtrl.toggleSelectGoods); |
24 | router.get('/cart/checkStorage', cartCtrl.checkStorage); | 24 | router.get('/cart/checkStorage', cartCtrl.checkStorage); |
25 | router.put('/cart/updateProduct', cartCtrl.modifyProduct); | 25 | router.put('/cart/updateProduct', cartCtrl.modifyProduct); |
26 | +router.get('/cart/data', cartCtrl.getCartData); | ||
26 | 27 | ||
27 | // 结算 | 28 | // 结算 |
28 | router.get('/order', auth, order.index); | 29 | router.get('/order', auth, order.index); |
@@ -151,7 +151,7 @@ | @@ -151,7 +151,7 @@ | ||
151 | <li class="total-price-action"> | 151 | <li class="total-price-action"> |
152 | <span class="price item-total-price">¥ \{{round (multiple sales_price buy_number) 2}}</span> | 152 | <span class="price item-total-price">¥ \{{round (multiple sales_price buy_number) 2}}</span> |
153 | <div class="actions"> | 153 | <div class="actions"> |
154 | - <div class="remove-item action" data-product_id=\{{product_id}}><span class="iconfont"></span> 删 除</div> | 154 | + <div class="remove-item action" data-product_extra_info='{"goodsId": "\{{goods_id}}", "cnAlphabet": "\{{cn_alphabet}}", "productId": "\{{product_id}}", "salesPrice": "\{{round sales_price 2}}", "productName": "\{{product_name}}", "goodType":"ordinary", "selected": "\{{selected}}"}'><span class="iconfont"></span> 删 除</div> |
155 | <div class="send-to-favorite action" data-product_id=\{{product_id}}>移入收藏夹</div> | 155 | <div class="send-to-favorite action" data-product_id=\{{product_id}}>移入收藏夹</div> |
156 | </div> | 156 | </div> |
157 | </li> | 157 | </li> |
@@ -218,7 +218,7 @@ | @@ -218,7 +218,7 @@ | ||
218 | <li class="total-price-action"> | 218 | <li class="total-price-action"> |
219 | <span class="price item-total-price">¥ \{{round (multiple sales_price buy_number) 2}}</span> | 219 | <span class="price item-total-price">¥ \{{round (multiple sales_price buy_number) 2}}</span> |
220 | <div class="actions"> | 220 | <div class="actions"> |
221 | - <div class="remove-item action" data-product_id=\{{product_id}}><span class="iconfont"></span> 删 除</div> | 221 | + <div class="remove-item action" data-product_extra_info='{"goodsId": "\{{goods_id}}", "cnAlphabet": "\{{cn_alphabet}}", "productId": "\{{product_id}}", "salesPrice": "\{{round sales_price 2}}", "productName": "\{{product_name}}", "goodType":"ordinary", "selected": "\{{selected}}"}'><span class="iconfont"></span> 删 除</div> |
222 | <div class="send-to-favorite action" data-product_id=\{{product_id}}>移入收藏夹</div> | 222 | <div class="send-to-favorite action" data-product_id=\{{product_id}}>移入收藏夹</div> |
223 | </div> | 223 | </div> |
224 | </li> | 224 | </li> |
@@ -339,7 +339,7 @@ | @@ -339,7 +339,7 @@ | ||
339 | <div class="product-price">¥ \{{salesPrice}}</div> | 339 | <div class="product-price">¥ \{{salesPrice}}</div> |
340 | <div class="bought-num">\{{buyNumber}}</div> | 340 | <div class="bought-num">\{{buyNumber}}</div> |
341 | <div class="actions"> | 341 | <div class="actions"> |
342 | - <span class="buy-again">重新购买</span> | 342 | + <span class="buy-again" data-rebuy_info='{"buyNumber": \{{buyNumber}}, "productSku": "\{{productSku}}"}'>重新购买</span> |
343 | <span class="send-to-favorite" data-product_info='{"goods_type": "\{{goodType}}", "buy_number": \{{buyNumber}}, "selected": "\{{selected}}", "product_sku": "\{{productSku}}", "promotion_id": 0}'>移入收藏夹</span> | 343 | <span class="send-to-favorite" data-product_info='{"goods_type": "\{{goodType}}", "buy_number": \{{buyNumber}}, "selected": "\{{selected}}", "product_sku": "\{{productSku}}", "promotion_id": 0}'>移入收藏夹</span> |
344 | </div> | 344 | </div> |
345 | </li> | 345 | </li> |
@@ -99,13 +99,28 @@ $(function() { | @@ -99,13 +99,28 @@ $(function() { | ||
99 | }); | 99 | }); |
100 | 100 | ||
101 | $('#removed_products').delegate('.send-to-favorite', 'click', function() { | 101 | $('#removed_products').delegate('.send-to-favorite', 'click', function() { |
102 | + var $removedProduct = $(this).parents('.removed-product'); | ||
103 | + | ||
102 | Cart.sendToFavorite([$.parseJSON($(this).attr('data-product_info'))], function() { | 104 | Cart.sendToFavorite([$.parseJSON($(this).attr('data-product_info'))], function() { |
103 | if ($('#removed_products .removed-product').length > 1) { | 105 | if ($('#removed_products .removed-product').length > 1) { |
104 | - $(this).remove(); | 106 | + $removedProduct.remove(); |
105 | } else { | 107 | } else { |
106 | $('#removed_products').html(''); | 108 | $('#removed_products').html(''); |
107 | } | 109 | } |
108 | }); | 110 | }); |
111 | + }).delegate('.buy-again', 'click', function() { | ||
112 | + var productInfo = $.parseJSON($(this).attr('data-rebuy_info')), | ||
113 | + $removedProduct = $(this).parents('.removed-product'); | ||
114 | + | ||
115 | + Cart.addToCart(productInfo, function() { | ||
116 | + Cart.refreshCart(function() { | ||
117 | + if ($('#removed_products .removed-product').length > 1) { | ||
118 | + $removedProduct.remove(); | ||
119 | + } else { | ||
120 | + $('#removed_products').html(''); | ||
121 | + } | ||
122 | + }); | ||
123 | + }); | ||
109 | }); | 124 | }); |
110 | 125 | ||
111 | // 变动商品数量 | 126 | // 变动商品数量 |
@@ -98,37 +98,20 @@ var Cart = { | @@ -98,37 +98,20 @@ var Cart = { | ||
98 | /* | 98 | /* |
99 | * 根据服务端JSON,刷新购物车信息 | 99 | * 根据服务端JSON,刷新购物车信息 |
100 | * @function [refreshCart] | 100 | * @function [refreshCart] |
101 | - * @params { Object } data 最新购物车数据 | 101 | + * @params { Function } callback 刷新购物车后的回调函数 |
102 | */ | 102 | */ |
103 | - refreshCart: function(data) { | ||
104 | - var template; | ||
105 | - | ||
106 | - hbs.registerHelper('multiple', function(num1, num2) { | ||
107 | - num1 = typeof num1 === 'number' ? num1 : parseFloat(num1, 10); | ||
108 | - num2 = typeof num2 === 'number' ? num2 : parseFloat(num2, 10); | ||
109 | - | ||
110 | - if (num1 && num2) { | ||
111 | - return num1 * num2; | ||
112 | - } | ||
113 | - }); | ||
114 | - | ||
115 | - hbs.registerHelper('isEqual', function(v1, v2, options) { | ||
116 | - if (v1 === v2) { | ||
117 | - return options.fn(this); | 103 | + refreshCart: function(callback) { |
104 | + Util.ajax({ | ||
105 | + url: '/shopping/cart/data', | ||
106 | + success: function(res) { | ||
107 | + Util.refreshCart(res, function() { | ||
108 | + Stepper.init(); | ||
109 | + if (callback) { | ||
110 | + return callback(); | ||
111 | + } | ||
112 | + }); | ||
118 | } | 113 | } |
119 | - return options.inverse(this); | ||
120 | }); | 114 | }); |
121 | - | ||
122 | - hbs.registerHelper('image', function(url, width, height, mode) { | ||
123 | - mode = parseInt(mode, 10) ? mode : 2; | ||
124 | - url = url || ''; | ||
125 | - return url.replace(/{width}/g, width).replace(/{height}/g, height).replace(/{mode}/g, mode); | ||
126 | - }); | ||
127 | - | ||
128 | - template = hbs.compile($('#cart-content-tpl').html()); | ||
129 | - $('#cart_content').html(template(data)); | ||
130 | - | ||
131 | - Stepper.init(); | ||
132 | }, | 115 | }, |
133 | 116 | ||
134 | /* | 117 | /* |
@@ -205,7 +188,7 @@ var Cart = { | @@ -205,7 +188,7 @@ var Cart = { | ||
205 | var dialog; | 188 | var dialog; |
206 | 189 | ||
207 | if (products.length) { | 190 | if (products.length) { |
208 | - //Cart.showRemovedProducts(products, extraInfos); | 191 | + // Cart.showRemovedProducts(products, extraInfos); |
209 | 192 | ||
210 | dialog = new _confirm({ | 193 | dialog = new _confirm({ |
211 | content: '您确定要从购物车中删除该商品吗?', | 194 | content: '您确定要从购物车中删除该商品吗?', |
-
Please register or login to post a comment