shop-service.js 2.74 KB
/**
 * Created by TaoHuang on 2016/6/28.
 */

'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const _ = require('lodash');

const api = require('./shop-api');
const DEFAULT_IMG = '01091c21f2317a64f123f1649fbbccf7ba';

/**
 * 生成缩略图
 */
const _imageView2 = (url, width, height) => {
    url += url.includes('?') ? '&' : '?';
    return `${url}imageView2/1/w${width}/h/${height}`;
};

/**
 * 解析 resource_data 参数
 */
const _getResourceData = list => {
    list.resource_data = JSON.parse(list.resource_data || '[]');
    return list;
};

/**
 * 店铺Banner 资源位
 */
const _shopTopBanner = data => {
    let result = {
        banner: '',
        isShowShopName: false,
        bannerHeight: 150
    };
    let resource = _.head(data.resource_data);

    if (resource.shopSrc) {
        result.banner = _imageView2(resource.shopSrc, 1150, 150);
    }

    if (resource.detailSrc) {
        result.detailSrc = _imageView2(resource.detailSrc, 1150, 150);
    }

    if (resource.isShowShopName) {
        result.isShowShopName = resource.isShowShopName === 'Y';
    }

    return result;
};

const _shopTopBannerBase = _shopTopBanner;

/**
 * 基础模板
 */
exports.basisTemplateAsync = shopId => {
    return co(function * () {
        let data = { shopTopBanner_base: {}};
        const ResourceHandler = {
            shopTopBanner_base: _shopTopBannerBase
        };
        let shops = yield api.shopsDecoratorListAsync(shopId);

        if (!shops.code || shops.code !== 200 || _.isEmpty(shops.data.list)) {
            return data;
        }

        shops.data.list.forEach(shop => {
            let resourceHandlerName = shop.resource_name;

            if (!ResourceHandler[resourceHandlerName]) {
                return;
            }

            let resourceData = ResourceHandler[resourceHandlerName](_getResourceData(shop));

            switch (resourceHandlerName) {
                case 'shopTopBanner_base':
                    {
                        data[resourceHandlerName] = resourceData;
                        break;
                    }
                default:
                    break;
            }

        });

        return data;
    })();
};

exports.getShopBannerAsync = (shopId) => {
    return api.shopBannerAsync(shopId).then((result) => {
        if (result.code === 200) {
            return _.includes(result.data.banner, DEFAULT_IMG) ? '' : result.data.banner;
        } else {
            return null;
        }
    });
};

exports.queryShopByBrandIdAsync = (sid, bid) => {
    return co(function * () {
        let result = yield api.queryShopsByBrandId(sid, bid);

        if (_.get(result, 'code') !== 200) {
            return false;
        }

        return _.get(result, 'data[0]', {});
    })();
};