shop-service.js 5.13 KB
/**
 * 店铺相关数据处理
 *
 * @author: jiangfeng<jeff.jiang@yoho.cn>
 * @date: 2016/07/14
 **/

'use strict';

const Promise = require('bluebird');
const co = Promise.coroutine;
const BrandService = require('./brand-service');
const ShopApi = require('./shop-api');
const Search = require('../models/search');
const _ = require('lodash');

/**
 * 处理店铺默认菜单
 * @param domain
 * @returns {*[]}
 */
function shopMenu(domain, customMenu) {
    let menus = [
        {id: 'index', name: '店铺首页', url: `/product/shop/${domain}`},
        {id: 'all', name: '全部商品', url: `/product/shop/${domain}/list`},
        {id: 'hot', name: '人气单品', url: `/product/shop/${domain}/list?order=s_n_desc`},
        {id: 'new', name: '新品上架', url: `/product/shop/${domain}/list?order=s_t_desc`}
    ];

    if (customMenu && customMenu.length > 0) {
        menus = _.concat(menus, customMenu);
    }

    return menus;
}

/**
 * 处理店铺首页资源位数据
 * @param data
 * @returns {{}}
 */
function resourceDataHandle(data) {
    let resource = {};

    if (data && _.isArray(data)) {
        data.forEach(d => {
            resource[d.resource_name] = JSON.parse(d.resource_data);
        });
    }

    return resource;
}

const ShopService = {

    /**
     * 获取店铺信息
     * @param shopId
     * @param uid
     */
    getShopIntro(shopId, uid) {
        return co(function*() {
            let shopIntro = yield ShopApi.getShopIntro(shopId, uid);

            if (shopIntro && shopIntro.code === 200) {
                return shopIntro.data;
            } else {
                return {};
            }
        })();
    },

    /**
     * 获取店铺装修
     * @param shopId
     */
    getShopDecorator(shopId) {
        return co(function*() {
            let data = yield ShopApi.getShopDecorator(shopId);

            if (data && data.code === 200) {
                return data.data;
            } else {
                return {};
            }
        })();
    },

    /**
     * 获取店铺二级分类
     * @param shopId
     */
    getShopSecondSorts(brandId, shopId) {
        return co(function*() {
            let data = yield Search.queryAllSort({
                brand: brandId,
                shop: shopId,
                small_sort: 0
            });
            let sortArray = [];

            if (data && data.data) {
                let sorts = data.data.sort;

                _.each(sorts, s => {
                    sortArray = sortArray.concat(s.sub);
                });

                sortArray = sortArray.sort((s1, s2) => {
                    return s2.count - s1.count;
                });
            }
            return sortArray;
        })();
    },

    /**
     * 获取店铺首页数据(头部、装修)
     * @param domain
     * @param uid
     */
    getShopHeadData(domain, shopId, uid) {
        return co(function*() {
            let info = {};
            let favType = 'brand';

            let domainInfo = yield BrandService.getDomainInfo(domain);

            if (shopId) {
                info.shopId = shopId;
            } else {
                info.brandId = domainInfo.id;
                info.shopId = domainInfo.shop_id;
                info.brandBanner = domainInfo.brand_banner;
                info.info = domainInfo.brand_intro;
            }

            if (info.shopId) {
                let shopIntro = yield ShopService.getShopIntro(info.shopId, uid);

                info.name = shopIntro.shop_name;
                info.info = shopIntro.shop_intro;
                info.btnName = '品牌介绍';
                info.isFavorite = shopIntro.is_favorite === 'Y';
                info.showShopName = shopIntro.is_show_shop_name === 'Y';

                let shopData = yield Promise.all([ShopService.getShopDecorator(info.shopId),
                    ShopService.getShopSecondSorts(info.brandId, shopId)]);
                let shopList = shopData[0];
                let sorts = shopData[1];
                let resources = resourceDataHandle(shopList.list);

                info.sorts = sorts;

                if (resources.shopTopBanner) {  // eslint-disable-line
                    info.banner = resources.shopTopBanner.shopSrc;  // eslint-disable-line
                }
                info.resources = resources;
                info.menus = shopMenu(domain, resources.navigationBar);
                favType = 'shop';
            }
            if (info.brandId) {
                let brandInfo = yield BrandService.getBrandInfo(info.brandId, uid);

                info.name = info.name || brandInfo.brand_name;

                // info.info = brandInfo.brandIntro;
                info.btnName = '品牌介绍';
                if (!info.isFavorite && brandInfo.is_favorite === 'Y') {
                    favType = 'brand';
                }
                info.isFavorite = info.isFavorite || (brandInfo.is_favorite === 'Y');
                info.banner = info.banner || info.brandBanner;
            }

            info.brandId = domainInfo.id; // 如果是店铺最后赋值brandId
            info.favType = favType;
            return info;
        })();
    }
};

module.exports = ShopService;