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

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);

        this.shopApi = new ShopApi(ctx);
    }

    /**
     * 获取店铺数据
     * @param params
     * @returns {*|Promise.<TResult>}
     */
    _getShopInfoData(finalResult, params) {
        return this.shopApi.getShopInfoData({
            shopId: params.shopId,
            uid: params.uid
        }).then(subResult => {

            if (subResult.data) {

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

                /* 取资源位店铺背景图 */
                return this.shopApi.getShopsDecoratorList({
                    shopId: params.shopId
                }).then(thResult => {
                    if (thResult.data) {
                        _.forEach(thResult.data.list, value => {

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

                    return finalResult;
                });
            }
            return finalResult;
        });
    }

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

        return this.shopApi.getBrandShopGoodsOriginData(Object.assign(params, {
            shopId: params.shopId
        })).then(subResult => {
            if (subResult.data) {
                // java端不愿意将boys改为wen,所以没办法,只能在这边改了。
                if (subResult.data.filter) {
                    const gender = subResult.data.filter.gender || {};

                    for (let k in gender) {
                        if (k.indexOf('1') > -1) {
                            gender[k] = 'MEN';
                        } else if (k.indexOf('2') > -1) {
                            gender[k] = 'WOMEN';
                        }
                    }
                }

                prettyFilter(subResult.data.filter);
                finalResult = {
                    data: {
                        productList: processProductList(subResult.data.product_list, {
                            gender: params.gender
                        }),
                        filter: subResult.data.filter,
                        page: subResult.data.page,
                        pageTotal: subResult.data.page_total
                    },
                    code: 200
                };
            }
            return finalResult;
        });
    }

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

        // 如果有店铺 ID,直接取店铺信息
        if (params.shopId) {

            /* 是 BLK 的店铺 */
            Object.assign(finalResult, {
                isBlkShop: true,
                shopId: params.shopId
            });

            return this._getShopInfoData(finalResult, params);
        } else {
            return this.shopApi.getBrandInfoByDomain({
                domain: params.domain
            }).then(result => {

                if (result.data) {

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

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

                        /* 是 BLK 的店铺 */
                        Object.assign(finalResult, {
                            isBlkShop: true,
                            shopId: result.data.shop_id
                        });
                        return this._getShopInfoData(finalResult, Object.assign(params, {
                            shopId: result.data.shop_id
                        }));
                    } else {

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

                        logger.info('brand has no shop');
                    }
                    return finalResult;
                }

                return finalResult;
            });
        }
    }

    /**
     * 获取品牌店铺商品
     * @param params
     * @returns {*|Promise.<TResult>}
     */
    getBrandShopGoodsData(params) {
        if (params.shopId) {
            return this._getGoodsList(params);
        } else {
            return this.shopApi.getBrandInfoByDomain({
                domain: params.domain
            }).then(result => {
                if (result.data) {
                    if (result.data.shop_id) {
                        Object.assign(params, {
                            shopId: result.data.shop_id
                        });
                    }
                    if (result.data.id) {
                        Object.assign(params, {
                            brand: result.data.id
                        });
                    }
                }
                return this._getGoodsList(params);
            });
        }
    }

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