Authored by 周少峰

Merge branch 'feature/cartAdd' into gray

... ... @@ -8,6 +8,7 @@ const _ = require('lodash');
const co = require('bluebird').coroutine;
const logger = global.yoho.logger;
const yohoHelpers = global.yoho.helpers;
const config = global.yoho.config;
const service = require('../models/cart-service');
const helper = require('../models/cart-helper');
const simpleHeaderModel = require('../../../doraemon/models/simple-header');
... ... @@ -58,6 +59,7 @@ const setShoppingCookie = (req, res) => {
let shoppingKey = helper.getShoppingKeyByCookie(req);
return service.getCartCount(uid, shoppingKey).then(ret => {
if (ret && ret.data && ret.data.cart_goods_count) {
res.cookie('_g', JSON.stringify({
_k: shoppingKey,
... ... @@ -125,6 +127,59 @@ const delCartGoods = (req, res, next) => {
};
/**
* 加入购物车 商品详情页
*
* @param string productSku 商品的SKU
* @param int buyNumber 购买数量
* @param int promotionId 促销ID, 加价购有关
* @param int goodsType 商品类型,0表示普通商品,1表示加价购商品
* @param int isEdit 是否是编辑商品SKU,0表示不是编辑
* @return json
*/
const cartAddIndex = (req, res) => {
co(function * () {
let uid = req.user.uid;
let shoppingKey = helper.getShoppingKeyByCookie(req);
let productSku = req.body.productSku;
let buyNumber = req.body.buyNumber || 1;
let goodsType = req.body.goodsType || 0;
let promotionId = req.body.promotionId || 0;
let isEdit = req.body.isEdit || 0;
// 执行加入购物车操作
let result = yield service.addCart(productSku, buyNumber,
goodsType, isEdit, promotionId,
uid, shoppingKey);
// 设置加入购物车凭证到客户端浏览器
if (!shoppingKey && _.get(result, 'data.shopping_key')) {
res.cookie('_SPK', result.data.shopping_key, {
expires: new Date(Date.now() + 86400 * 360),
domain: config.cookieDomain
});
}
// 更新头部购物车COOKIE
if (_.get(result, 'data.shopping_key')) {
res.cookie('_g', JSON.stringify({
_k: result.data.shopping_key,
_nac: result.data.goods_count,
_ac: 0,
_r: 1
}), {
expires: new Date(Date.now() + 86400 * 360),
domain: config.cookieDomain
});
}
res.send(result);
})();
};
/**
* 我的购物车
*/
const cart = (req, res, next) => {
... ... @@ -184,7 +239,8 @@ const cartAdd = (req, res) => {
// 设置加入购物车凭证到客户端浏览器
if (!shoppingKey && result && result.data && result.data.shopping_key) {
res.cookie('_SPK', result.data.shopping_key, {
expires: new Date(Date.now() + 86400 * 360)
expires: new Date(Date.now() + 86400 * 360),
domain: config.cookieDomain
});
}
... ... @@ -450,6 +506,7 @@ const queryUserPromotionGift = (req, res, next) => {
};
module.exports = {
cartAddIndex,
getProductInfo,
getProductData,
cart,
... ...
... ... @@ -447,6 +447,40 @@ const getCartCount = (uid, shoppingKey) => {
};
/**
* 加入购物车 获取数量
*
* @param int $productSku 商品SKU
* @param int $buyNumber 购买数量
* @param int $goodsType 商品类型,0表示普通商品,1表示加价购商品
* @param int $isEdit 是否是编辑商品SKU,0表示不是编辑
* @param null|int $promotionId 促销id,默认null(加价购有关)
* @param null|int $uid 用户UID,可以不传
* @param string $shoppingKey 购物车在浏览器的唯一识别码,可以不传
* @return array 加入购物车接口返回的数据
*/
const addCart = (productSku, buyNumber, goodsType, isEdit, promotionId, uid, shoppingKey) => {
return co(function * () {
let result = {
code: 400,
message: ERROR_400_MESSAGE
};
if (!productSku) {
return result;
}
let ret = yield cartApi.addToCart(productSku, buyNumber, goodsType, isEdit, promotionId, uid, shoppingKey);
if (ret && ret.code) {
result = ret;
}
return result;
})();
};
/**
* 加入购物车
*
* @param int $productSku 商品SKU
... ... @@ -1021,6 +1055,7 @@ module.exports = {
getProductInfoAsync, /** 获取商品信息 **/
getCartData, /** 购物车数据 **/
getCartCount, /** 得到购物车数量 **/
addCart, /** 加入到购物车 获取数量 **/
addToCart, /** 加入到购物车 **/
selectGoods, /** 选中/取消选中商品 **/
modifyProductNum, /** 修改商品数量 **/
... ...
... ... @@ -17,6 +17,7 @@ const easypay = require(`${cRoot}/easypay`);
const ensure = require(`${cRoot}/order-ensure`);
router.get('/index/getProductInfo', cart.getProductInfo);
router.post('/cart/index/add', cart.cartAddIndex); // 加入购物车 商品详情页
router.get('/coupon/list', cart.getCoupons); // 优惠券列表
... ...
... ... @@ -275,9 +275,11 @@ function addcart(data, cookieList) {
type: 'POST',
url: '/cart/cart/add',
data: data
}).then(function(d) {
if (d.code === 200) {
// window.history.go(0);
refreshCart(d.data);
if (cookieList) {
window.setCookie('cart-del-list', JSON.stringify(cookieList), {
... ...
... ... @@ -125,7 +125,7 @@ function getSku() {
function addCart() {
return $.ajax({
type: 'POST',
url: '/cart/index/add',
url: '/cart/cart/index/add',
data: {
productSku: getSku(),
buyNumber: getNum()
... ... @@ -141,7 +141,9 @@ function addCart() {
$('#cart-num').text(data.data.goods_count); // 更新数目
return $.Deferred().resolve().promise(); // eslint-disable-line
} else if (code === 500) {
if (deposit) {
alert = new Alert('定金预售商品只能在APP端购买');
alert.show();
... ...