item.js 4 KB
/**
 * 商品详情页model
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2016/7/11
 */
'use strict';

const _ = require('lodash');
const itemAPI = require('./item-api');
const brandAPI = require('./brand-api');
const itemFUN = require('./item-handler');

const search = require('./search-api');


/**
 * 根据商品基本信息获取商品品牌、材质、尺码、描述、详情等
 * @function _getMultiResourceByBaseInfo
 * @param { Object } 商品的基本信息
 * @return { Object } 返回单个商品品牌、材质、尺码、描述、详情等信息
 */
const _getMultiResourceByBaseInfo = (base) => {
    let productId = base.id;
    let skn = base.erpProductId;
    let brandId = base.brand.id ? base.brand.id : 0;

    // 获取相关数据
    let promiseData = [
        itemAPI.getsizeInfoAsync(skn),
        itemAPI.getComfortAsync(productId),
        itemAPI.getModelTryAsync(skn),
        search.getSortAsync({sort: base.smallSortId})
    ];

    if (base.uid) {
        promiseData.push(itemAPI.getUserIsFav(base.uid, productId));
        promiseData.push(brandAPI.getBrandInfo(brandId, base.uid));
    }

    return Promise.all(promiseData).then(result => {
        return {
            sizeInfo: result[0],
            comfort: result[1].data,
            modelTry: result[2].data,
            sort: result[3].data,
            productFav: (result[4] && result[4].data),
            brandFav: (result[5] && result[5].data && result[5].data.is_favorite === 'Y')
        };
    });
};

/**
 * 获取商品基本信息、品牌、材质、尺码、描述、详情等
 * @function getProductItemData
 * @param { Object } 商品的参数
 * @param { String } url 用户ID
 * @param { String } uid 用户ID
 * @return { Object } 返回单个商品的基本信息及品牌、材质、尺码、描述、详情等
 */
const getProductItemData = (params, url, uid) => {
    let pid = params[0];

    // let gid = params[1];

    return itemAPI.getProductBaseAsync(pid).then(result => {
        let resData = {};
        let data = {};

        if (!result.productName &&
            !result.erpProductId &&
            !result.productPriceBo) {
            return data;
        }

        result.uid = uid;
        result.mainPath = url;

        // 商品基本信息
        data.goodInfo = itemFUN.setProductData(result);

        if (result.brand) {
            Object.assign(data, itemFUN.setBrandBanner(result.brand));

            // BRAND品牌简介
            Object.assign(data, itemFUN.setBrandIntro(result.brand));
        }

        return _getMultiResourceByBaseInfo(result).then(mulRes => {

            // 收藏状态
            _.set(data, 'brandBanner.brandFav', mulRes.brandFav);
            _.set(data, 'goodInfo.productFav', mulRes.productFav);

            // 面包屑导航
            Object.assign(data, itemFUN.setPathNav(mulRes.sort, result.productName));

            // DESCRIPTION商品描述
            Object.assign(data, itemFUN.setDescriptionData(mulRes.sizeInfo, mulRes.comfort));

            // MATERIALS材料洗涤
            Object.assign(data, itemFUN.setMaterialData(mulRes.sizeInfo));

            // SIZEINFO尺码信息
            Object.assign(data, itemFUN.setSizeData(mulRes.sizeInfo, mulRes.modelTry));

            // DETAILS商品详情
            Object.assign(data, itemFUN.setDetailData(mulRes.sizeInfo));

            // 商品详情SEO
            Object.assign(resData, itemFUN.setSeoInfo(data.goodInfo, data.nav));

            resData.content = data;

            return resData;
        });

    });
};

/**
 * 获取商品尺寸,颜色,和缩略图
 * @function getProductInfo
 * @param { Number } productId 商品ID
 * @param { String } uid 用户ID
 * @param { Number } skn 商品skn
 * @return { Object } 接口返回单个商品的基本信息
 */
const getProductInfo = (productId, uid, skn) => {
    return itemAPI.getProductBaseAsync(productId, uid, skn).then(result => {
        return itemFUN.setProductData(result);
    });
};

module.exports = {
    getProductItemData,
    getProductInfo
};