shop.js 4.81 KB
/**
 * Created by PhpStorm.
 * User: Targaryen
 * Date: 2016/7/26
 * Time: 10:34
 */
'use strict';
const logger = global.yoho.logger;
const api = global.yoho.API;
const shopApi = require('./shop-api');
const processProductList = require(`${global.utils}/beautify/product`);
const camelCase = global.yoho.camelCase;
const _ = require('lodash');
const prettyFilter = require(`${global.utils}/beautify/filters`);

/**
 * 获取品牌店铺数据
 * @param params
 * @returns {*|Promise.<TResult>}
 */
const getShopData = params => {
    let finalResult = {};

    return api.all([
        shopApi.getBrandInfoByDomain({domain: params.domain})
    ]).then(result => {

        if (result[0].code === 200) {

            /* 品牌名称 */
            Object.assign(finalResult, {
                brandName: result[0].data.brand_name
            });

            /* 品牌是否有店铺 */
            if (result[0].data.shop_id) {

                /* 是 BLK 的店铺 */
                Object.assign(finalResult, {
                    isBlkShop: true,
                    shopId: result[0].data.shop_id
                });

                return api.all([
                    shopApi.getShopInfoData({
                        shopId: result[0].data.shop_id,
                        uid: params.uid

                        // uid: '8050882'
                    })
                ]).then(subResult => {
                    if (subResult[0].code === 200) {

                        /* 取店铺的基本信息 */
                        Object.assign(finalResult, {
                            shopLogo: subResult[0].data.shop_logo,
                            shopName: subResult[0].data.shop_name,
                            shopIntro: subResult[0].data.shop_intro,
                            isFav: subResult[0].data.is_favorite === 'Y'
                        });

                        /* 取资源位店铺背景图 */
                        return api.all([
                            shopApi.getShopsDecoratorList({shopId: result[0].data.shop_id})
                        ]).then(thResult => {
                            if (thResult[0].code === 200) {
                                _.forEach(thResult[0].data.list, value => {

                                    if (value.resource_name === 'shopTopBanner_app') {
                                        Object.assign(finalResult, {
                                            brandBg: JSON.parse(value.resource_data).shopSrc
                                        });
                                    }
                                });

                            } else {
                                logger.info('getShopsDecoratorList api code no 200');
                            }

                            return finalResult;
                        });
                    } else {
                        logger.error('getShopInfoData api code no 200');
                    }

                    return finalResult;
                });
            } else {

                /* 不是 BLK 的店铺 */
                Object.assign(finalResult, {
                    isBlkShop: false
                });

                logger.info('brand has no shop');
            }
        } else {
            logger.error('getBrandOriginData api code no 200');
        }

        return finalResult;
    });
};

/**
 * 获取品牌店铺商品数据
 * @param params
 * @returns {*|Promise.<TResult>}
 */
const getBrandShopGoodsData = params => {
    let finalResult = {};

    return api.all([
        shopApi.getBrandInfoByDomain({ domain: params.domain })
    ]).then(result => {
        if (result[0].code === 200) {
            return api.all([
                shopApi.getBrandShopGoodsOriginData(Object.assign(params, {
                    brand: result[0].data.id,
                    shopId: result[0].data.shop_id
                }))
            ]).then(subResult => {
                if (subResult[0].code === 200) {
                    prettyFilter(subResult[0].data.filter);
                    finalResult.data = {
                        productList: processProductList(subResult[0].data.product_list),
                        filter: subResult[0].data.filter
                    };
                } else {
                    logger.error('getBrandShopGoodsOriginData api code no 200');
                }

                return camelCase(finalResult);
            });
        } else {
            logger.error('getBrandInfoByDomain api code no 200');
        }

        return camelCase(finalResult);
    });
};

/**
 * 收藏店铺
 * @param params
 * @returns {*|Promise.<TResult>}
 */
const collectShopData = params => {
    return api.all([shopApi.collectShopOriginData(params)]).then(result => {
        return result[0];
    });
};

module.exports = {
    getShopData,
    getBrandShopGoodsData,
    collectShopData
};