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

const itemAPI = require('./item-api');
const itemFUN = require('./item-handler');

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

const _getMultiResourceByBaseInfo = (base, uid) => {
    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 (uid) {
        console.log(uid);

        // promiseData.push(favoriteBrandService.isFavoriteAsync(uid, productId));
    }

    return Promise.all(promiseData).then(result => {
        return {
            sizeInfo: result[0],
            comfort: result[1].data,
            modelTry: result[2].data,
            sort: result[3].data
        };
    });
};

let getProductItemData = (params) => {
    let pid = params[0];

    // let gid = params[1];

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

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

        // 商品基本信息
        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 => {
            // 面包屑导航
            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));

            return data;
        });

    });
};

module.exports = {
    getProductItemData
};