shop-service.js 3.16 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 _signboard = data => {
    let result = [];
    let resources = data.resource_data;

    resources.forEach(resource => {
        if (_.isEmpty(resource)) {
            return;
        }

        resource.data.forEach(val => {
            result.push({
                img: _imageView2(val.src, 160, 240),
                url: val.url
            });
        });
    });

    return {
        title: '',
        list: result
    };
};

const _shopTopBanner_base = _shopTopBanner;

/**
 * 基础模板
 */
exports.basisTemplateAsync = shopId => {
    return co(function * () {
        let data = { shopTopBanner_base: {}};
        const ResourceHandler = {
            shopTopBanner_base: _shopTopBanner_base
        };
        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;
                    }
            }

        });

        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 = (bid) => {
    return co(function * () {
        let result = yield api.queryShopsByBrandId(bid);

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

        return _.some(_.get(result, 'data', []), i => _.has(i, 'shop_id'));
    })();
};