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

'use strict';

const Promise = require('bluebird');
const co = Promise.coroutine;
const camelCase = global.yoho.camelCase;
const BrandService = require('./brand-service');
const ShopApi = require('./shop-api');
const _ = require('lodash');

/**
 * 处理店铺默认菜单
 * @param domain
 * @returns {*[]}
 */
function shopMenu(domain) {
    let menus = [
        {name: '店铺首页', href: `/product/shop/${domain}`},
        {name: '全部商品', href: `/product/shop/${domain}/list`, icon: '&#xe60a;'},
        {name: '人气单品', href: `/product/shop/${domain}/list?order=xxx`},
        {name: '新品上架', href: `/product/shop/${domain}/list?order=s_t_desc`}
    ];

    return menus;
}

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

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

    return resource;
}

const ShopService = {
    getShopIntro(shopId, uid) {
        return co(function*() {
            let shopIntro = yield ShopApi.getShopIntro(shopId, uid);

            if (shopIntro && shopIntro.code === 200) {
                return camelCase(shopIntro.data);
            } else {
                return {};
            }
        })();
    },
    getShopDecorator(shopId) {
        return co(function*() {
            let data = yield ShopApi.getShopDecorator(shopId);

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

    getShopSecondSorts(shopId) {
        return co(function*() {
            let data = yield ShopApi.getShopSorts(shopId);

            if (data && data.code === 200) {
                let sorts = camelCase(data.data);

                return sorts.sort((a, b) => {
                    return a.sub.length >= b.sub.length;
                });
            } else {
                return [];
            }
        })();
    },

    getShopHeadData(domain, uid) {
        return co(function*() {
            let domainInfo = yield BrandService.getDomainInfo(domain);
            let info = {
                brandId: domainInfo.id,
                shopId: ''
            };

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

                info.shopId = shopId;
                info.name = shopIntro.shopName;
                info.info = shopIntro.shopIntro;
                info.btnName = '店铺介绍';
                info.isFavorite = shopIntro.isFavorite === 'Y';

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

                info.sorts = sorts;

                if (resources.shopTopBanner_base && resources.shopTopBanner_base.length > 0) {  // eslint-disable-line
                    info.banner = resources.shopTopBanner_base[0].shopSrc;  // eslint-disable-line
                }
                info.resources = resources;
                info.menus = shopMenu(domain);
            } else {
                let brandId = domainInfo.id;
                let brandInfo = yield BrandService.getBrandInfo(brandId, uid);

                info.name = brandInfo.brandName;
                info.info = brandInfo.brandIntro;
                info.btnName = '品牌介绍';
                info.isFavorite = brandInfo.isFavorite === 'Y';
                info.banner = domainInfo.brandBanner;
            }
            return info;
        })();
    }
};

module.exports = ShopService;