cart.js 12.7 KB
/**
 * 购物车模型 model
 * @author: jinhu.dong<jinhu.dong@yoho.cn>
 * @date: 2016/07/04
 */
'use strict';

var Promise = require('bluebird');
var _ = require('lodash');

const api = global.yoho.API;

// const ServiceAPI = require(`${global.library}/api`).ServiceAPI;
// const sign = require(`${global.library}/sign`);
// const logger = require(`${global.library}/logger`);
// const headerModel = require('../../../doraemon/models/header');

// var api = new ServiceAPI();

// 获取预售商品
const getPreSaleProducts = () => {
    return Promise.resolve({
        preSalePros: [{
            gender: '1',
            tags: '[Object]',
            status: 1,
            smallSortId: 115,
            vip1Price: 379.04999999999995,
            isGlobal: 'N',
            vip3Price: 351.12,
            isOutlets: 2,
            isDiscount: 'Y',
            brandDomain: 'allenove',
            isSpecial: 'N',
            goodsList: '[Object]',
            isAdvance: 'N',
            productId: 286135,
            vipDiscountType: 1,
            salesNum: 0,
            cnAlphabet: 'ALLENOVE95S11GeBenHaGen',
            productSkn: 51160998,
            shelveTime: 1455616165,
            vip2Price: 359.1,
            editTime: 1455616165,
            isNew: 'N',
            isLimited: 'N',
            brandName: 'Alle nove',
            maxSortId: 1,
            productName: 'Alle nove 小方领长袖衬衫【哥本哈根】',
            brandId: 1007,
            defaultImages: 'http://img13.static.yhbimg.com/goodsimg/2015/10/21/05/0279bf8ab67a6c962b08e37b3a13ce3401.jpg?imageMogr2/thumbnail/{width}x{height}/extent/{width}x{height}/background/d2hpdGU=/position/center/quality/80',
            stockNumber: 29,
            storageNum: 29,
            isSoonSoldOut: false,
            middleSortId: 12,
            salesPrice: 399,
            isPromotion: 101,
            marketPrice: 1299,
            vipPrice: 0,
            id: 51160998,
            thumb: 'http://img13.static.yhbimg.com/goodsimg/2015/10/21/05/0279bf8ab67a6c962b08e37b3a13ce3401.jpg?imageMogr2/thumbnail/{width}x{height}/extent/{width}x{height}/background/d2hpdGU=/position/center/quality/80',
            url: 'http://www.yohobuy.com/product/pro_286135_367985/ALLENOVE95S11GeBenHaGen.html',
            brandUrl: '//allenove.yohobuy.com',
            orderNum: 1,      // 购买数量
            numIsValid: true, // 库存是否正常
            isValid: true     // 是否没有实效
        }]
    });
};

// 获取普通商品
const getNormalProducts = () => {
    return Promise.resolve({
        commonPros: [{ gender: '1',
       tags: '[Object]',
       status: 1,
       smallSortId: 115,
       vip1Price: 284.05,
       isGlobal: 'N',
       vip3Price: 263.12,
       isOutlets: 2,
       isDiscount: 'Y',
       brandDomain: 'allenove',
       isSpecial: 'N',
       goodsList: [Object],
       isAdvance: 'N',
       productId: 286141,
       vipDiscountType: 1,
       salesNum: 0,
       cnAlphabet: 'ALLENOVE95S14WeiYeNa',
       productSkn: 51161001,
       shelveTime: 1455616165,
       vip2Price: 269.1,
       editTime: 1455616165,
       isNew: 'N',
       isLimited: 'N',
       brandName: 'Alle nove',
       maxSortId: 1,
       productName: 'Alle nove 翼型领长袖衬衫【维也纳】',
       brandId: 1007,
       defaultImages: 'http://img10.static.yhbimg.com/goodsimg/2015/10/21/02/0128dc014524ccf208b4f6f7760c9b9cf2.jpg?imageMogr2/thumbnail/{width}x{height}/extent/{width}x{height}/background/d2hpdGU=/position/center/quality/80',
       stockNumber: 21,
       storageNum: 21,
       isSoonSoldOut: false,
       middleSortId: 12,
       salesPrice: 299,
       isPromotion: 101,
       marketPrice: 1299,
       vipPrice: 0,
       id: 51161001,
       thumb: 'http://img10.static.yhbimg.com/goodsimg/2015/10/21/02/0128dc014524ccf208b4f6f7760c9b9cf2.jpg?imageMogr2/thumbnail/{width}x{height}/extent/{width}x{height}/background/d2hpdGU=/position/center/quality/80',
       url: 'http://www.yohobuy.com/product/pro_286141_367991/ALLENOVE95S14WeiYeNa.html',
       brandUrl: '//allenove.yohobuy.com',
       orderNum: 2,      // 购买数量
       numIsValid: true, // 库存是否正常
       isValid: true,     // 是否没有实效
       selectColor: '蓝',
       selectSize: 'M'
     }, { gender: '1',
       tags: '[Object]',
       status: 1,
       smallSortId: 115,
       vip1Price: 379.04999999999995,
       isGlobal: 'N',
       vip3Price: 351.12,
       isOutlets: 2,
       isDiscount: 'Y',
       brandDomain: 'allenove',
       isSpecial: 'N',
       goodsList: '[Object]',
       isAdvance: 'N',
       productId: 286143,
       vipDiscountType: 1,
       salesNum: 0,
       cnAlphabet: 'ALLENOVE95S20BaSaiLuoNa',
       productSkn: 51161002,
       shelveTime: 1455616165,
       vip2Price: 359.1,
       editTime: 1455616165,
       isNew: 'N',
       isLimited: 'N',
       brandName: 'Foobar',
       maxSortId: 1,
       productName: 'Foobar 纽扣领绅士长袖衬衫【巴塞罗那】',
       brandId: 1007,
       defaultImages: 'http://img12.static.yhbimg.com/goodsimg/2015/10/21/05/024f60a070ab61981c139684f147d41f17.jpg?imageMogr2/thumbnail/{width}x{height}/extent/{width}x{height}/background/d2hpdGU=/position/center/quality/80',
       stockNumber: 25,
       storageNum: 25,
       isSoonSoldOut: false,
       middleSortId: 12,
       salesPrice: 399,
       isPromotion: 101,
       marketPrice: 1299,
       vipPrice: 0,
       id: 51161002,
       thumb: 'http://img12.static.yhbimg.com/goodsimg/2015/10/21/05/024f60a070ab61981c139684f147d41f17.jpg?imageMogr2/thumbnail/{width}x{height}/extent/{width}x{height}/background/d2hpdGU=/position/center/quality/80',
       url: 'http://www.yohobuy.com/product/pro_286143_367993/ALLENOVE95S20BaSaiLuoNa.html',
       brandUrl: '//allenove.yohobuy.com',
       orderNum: 1,      // 购买数量
       numIsValid: false, // 库存是否正常
       isValid: true     // 是否没有实效
       },
       { gender: '1',
       tags: [Object],
       status: 1,
       smallSortId: 115,
       vip1Price: 379.04999999999995,
       isGlobal: 'N',
       vip3Price: 351.12,
       isOutlets: 2,
       isDiscount: 'Y',
       brandDomain: 'allenove',
       isSpecial: 'N',
       goodsList: [Object],
       isAdvance: 'N',
       productId: 286169,
       vipDiscountType: 1,
       salesNum: 0,
       cnAlphabet: 'ALLENOVE95S09BiSa',
       productSkn: 51161015,
       shelveTime: 1455616145,
       vip2Price: 359.1,
       editTime: 1455616145,
       isNew: 'N',
       isLimited: 'N',
       brandName: 'Alle nove',
       maxSortId: 1,
       productName: 'Alle nove 高支牛津休闲长袖衬衫【比萨】',
       brandId: 1007,
       defaultImages: 'http://img13.static.yhbimg.com/goodsimg/2015/10/21/05/027f47db67ca5cdb9fa996c8300947b975.jpg?imageMogr2/thumbnail/{width}x{height}/extent/{width}x{height}/background/d2hpdGU=/position/center/quality/80',
       stockNumber: 27,
       storageNum: 27,
       isSoonSoldOut: false,
       middleSortId: 12,
       salesPrice: 399,
       isPromotion: 101,
       marketPrice: 1299,
       vipPrice: 0,
       id: 51161015,
       thumb: 'http://img13.static.yhbimg.com/goodsimg/2015/10/21/05/027f47db67ca5cdb9fa996c8300947b975.jpg?imageMogr2/thumbnail/{width}x{height}/extent/{width}x{height}/background/d2hpdGU=/position/center/quality/80',
       url: 'http://www.yohobuy.com/product/pro_286169_368019/ALLENOVE95S09BiSa.html',
       brandUrl: '//allenove.yohobuy.com',
       orderNum: 2,      // 购买数量
       numIsValid: true, // 库存是否正常
       isValid: true     // 是否没有实效
       }]
    });
};

// 获取失效商品
const getInvalidProducts = () => {
    return Promise.resolve({
        invalidPros: [{ gender: '1',
       tags: ['Object'],
       status: 1,
       smallSortId: 115,
       vip1Price: 379.04999999999995,
       isGlobal: 'N',
       vip3Price: 351.12,
       isOutlets: 2,
       isDiscount: 'Y',
       brandDomain: 'allenove',
       isSpecial: 'N',
       goodsList: ['Object'],
       isAdvance: 'N',
       productId: 286139,
       vipDiscountType: 1,
       salesNum: 0,
       cnAlphabet: 'ALLENOVE95S13PuLuoWangSi',
       productSkn: 51161000,
       shelveTime: 1455616165,
       vip2Price: 359.1,
       editTime: 1455616165,
       isNew: 'N',
       isLimited: 'N',
       brandName: 'Alle nove',
       maxSortId: 1,
       productName: 'Alle nove 复古圆领长袖衬衫【普罗旺斯】',
       brandId: 1007,
       defaultImages: 'http://img12.static.yhbimg.com/goodsimg/2015/10/21/03/02705334cacca0a06edec049918290f7b3.jpg?imageMogr2/thumbnail/{width}x{height}/extent/{width}x{height}/background/d2hpdGU=/position/center/quality/80',
       stockNumber: 12,
       storageNum: 12,
       isSoonSoldOut: false,
       middleSortId: 12,
       salesPrice: 399,
       isPromotion: 101,
       marketPrice: 1299,
       vipPrice: 0,
       id: 51161000,
       thumb: 'http://img12.static.yhbimg.com/goodsimg/2015/10/21/03/02705334cacca0a06edec049918290f7b3.jpg?imageMogr2/thumbnail/{width}x{height}/extent/{width}x{height}/background/d2hpdGU=/position/center/quality/80',
       url: 'http://www.yohobuy.com/product/pro_286139_367989/ALLENOVE95S13PuLuoWangSi.html',
       brandUrl: '//allenove.yohobuy.com',
       orderNum: 1,       // 购买数量
       numIsValid: true,  // 库存是否正常
       isValid: false     // 是否没有实效
       }]
    });
};

// 获取购物车信息
const getCartInfo = () => {
    return Promise.all([
        getPreSaleProducts(),
        getNormalProducts(),
        getInvalidProducts()
    ]);
};

/*
 * 加入购物车
 * @function addToCart
 * @param { String } productSku 商品SKU
 * @param { Number } buyNumber 购买商品数量
 * @param { Number } goodType 商品类型,0表示普通商品,1表示加价购商品
 * @param { Number } isEdit 是否是编辑商品SKU,0表示不是编辑
 * @param { Number } uid 用户UID,可以不传
 * @param { String } shoppingKey 未登录用户唯一识别码,可以不传
 * @return { Array } 加入购物车接口返回的数据
 */
const addToCart = (options) => {
    const productSku = options.productSku;
    const buyNumber = options.buyNumber;
    const goodType = options.goodType || 0;
    const isEdit = options.isEdit || 0;
    const uid = options.uid;
    const shoppingKey = options.shoppingKey;

    let params = {
        method: 'app.Shopping.add',
        product_sku: productSku,
        buy_number: buyNumber,
        goods_type: goodType,
        edit_product_sku: isEdit,
        selected: 'Y',
        promotion_id: null
    };

    if (uid) {
        _.merge(params, {
            uid: uid
        });
    }

    if (shoppingKey) {
        _.merge(params, {
            shoppingKey: shoppingKey
        });
    }

    return api.get('', params);
};

/*
 * 获取购物车数据
 * @function getCartData
 * @param { Number } uid 用户UID,可以不传
 * @param { String } shoppingKey 未登录用户唯一识别码,客户端从cookie中获取
 * @return { Object }
 * {
 *     advance_cart_data: {                  // 预售商品
 *         gift_list: [],                    // 赠品
 *         goods_list: []                    // 商品列表
 *         off_shelves_goods_list: [],       // 下架商品, 失效商品
 *         price_gift: [],                   // 加价购商品
 *         promotion_info: [],               // 促销信息
 *         shopping_cart_data: [],           // 商品价格计算相关信息
 *         sold_out_goods_list: []           // 售罄商品
 *     },
 *     ordinary_cart_data: {                 // 普通商品
 *         gift_list: [],
 *         goods_list: [],
 *         off_shelves_goods_list: [],
 *         price_gift: [],
 *         promotion_info: [],
 *         shopping_cart_data: [],
 *         sold_out_goods_list: []
 *     },
 *     md5: '...',
 *     message: '...'
 * }
 */
const getCartData = (shoppingKey, uid) => {
    let params = {
        method: 'app.Shopping.cart'
    };

    if (shoppingKey) {
        _.merge(params, {
            shopping_key: shoppingKey
        });
    }

    if (uid) {
        _.merge(params, {
            uid: uid
        });
    }

    return api.get('', params);
};

/*
 * 移入收藏夹
 * @function transferToFavorite
 * @param { Number } uid 用户UID
 * @param { String } sku 商品sku列表
 * @param { Boolean } hasPromotion 是否有促销ID, 默认值是false
 * @return { Array }
 */
const transferToFavorite = (uid, sku, hasPromotion) => {
    let method = hasPromotion ? 'app.Shopping.addfavoriteAndCart' : 'app.Shopping.addfavorite';

    let params = { method };

    _.merge(params, {
        product_sku_list: sku,
        uid // 用户必须登录吗 TODO
    });

    return api.get('', params);
};

module.exports = {
    addToCart,
    getCartData,
    getCartInfo,
    transferToFavorite
};