shop-handler.js 7.66 KB
/*
 * @Author: sefon
 * @Date: 2016-07-31 22:13:24
 */

'use strict';
const _ = require('lodash');

const helpers = global.yoho.helpers;

// const queryString = require('querystring');

const newProductsName = '新品上架 NEW';
const hotProductsName = '人气单品 HOT';
const shopListUrl = '/product/shoplist';

/**
 * 新品上架
 */
const newProducts = (data) => {
    let dest = {};

    if (!_.isEmpty(data)) {
        Object.assign(dest, {
            name: newProductsName,
            list: []
        });
        _.forEach(data, (value) => {
            dest.list.push({
                productId: value.productId,
                title: value.productName,
                productSkn: value.productSkn,
                price: ${value.salesPrice}`,
                img: value.src,
                url: helpers.getUrlBySkc(value.productId, value.goodsId, value.cnAlphabet)
            });
        });
    }

    return dest;
};

/**
 * 人气单品
 */
const hotProducts = (data) => {
    let dest = {
        name: hotProductsName,
        list: []
    };

    _.forEach(data, (value, key) => {
        dest.list.push({
            productId: value.productId,
            title: value.productName,
            productSkn: value.productSkn,
            price: ${value.salesPrice}`,
            img: value.src,
            url: helpers.getUrlBySkc(value.productId, value.goodsId, value.cnAlphabet),
            index: key + 1
        });
    });
    return dest;
};

/**
 * tabBar
 */
const goodsTabBar = (data, shopId) => {
    let dest = {
            hot: [],
            new: []
        },
        more = {name: 'MORE', href: shopListUrl + '?shopId=' + shopId};


    _.forEach(data.hot, (value) => {
        if (value.url) {
            dest.hot.push({
                name: value.name,
                href: value.url
            });
        }

    });

    _.forEach(data.new, (value) => {
        if (value.url) {
            dest.new.push({
                name: value.name,
                href: value.url
            });
        }
    });
    dest.hot.push(more);
    dest.new.push(more);
    return dest;
};

/**
 * 店铺经典模板banner
 * @param data 装修数据
 * @returns {{}}
 */
const shopTopBanner = (data) => {
    let src = data[0].shopSrc;
    let str = _.split(src, '?').length ? '?' : '&';

    return {
        shopTopBanner: {
            bannerHeight: 200,
            banner: `${src}${str}imageView2/1/w/{width}/h/{height}`,
            detailSrc: data[0].detailSrc || '',
            isShowShopName: data[0].isShowShopName === 'Y'
        }
    };

};

const shopTopBannerBase = (data) => {
    return {
        shopTopBannerBase: {
            bannerHeight: 200,
            banner: helpers.getForceSourceUrl(data[0].shopSrc),
            showShopName: data[0].isShowShopName === 'Y'
        }
    };
};

/**
 * 导航
 * @param data 装修数据
 * @returns {{}}
 */
const navigationBar = (data, shopId) => {
    let shopNav = [
        {
            name: '店铺首页',
            url: `/?navBar=0&shopId=${shopId}`
        },
        {
            name: '全部商品',
            url: `${shopListUrl}/?navBar=1&shopId=${shopId}`
        },
        {
            name: '人气单品',
            url: `${shopListUrl}/?navBar=2&shopId=${shopId}`
        },
        {
            name: '新品上架',
            url: `${shopListUrl}/?navBar=3&shopId=${shopId}`
        }
    ];

    return {navigationBar: _.union(shopNav, _.filter(data, (value) => {
        return value.url;
    }))};
};

/**
 * 资源位大图
 * @param type $data
 * @return type []
 */
const largeSlideImg = (data) => {
    let dest = [];

    _.forEach(data, (value) => {
        dest.push({
            img: value.data[0].src,
            url: helpers.urlFormat(value.data[0].url)
        });
    });

    return {largeSlideImg: dest};
};

/**
 * 资源位小图
 * @param data 装修数据
 * @returns {{}}
 */
const oneRowTwoColImages = (data) => {
    let dest = [];

    _.forEach(data, (value) => {
        dest.push({
            img: value.data[0].src,
            url: helpers.urlFormat(value.data[0].url)
        });
    });
    return {oneRowTwoColImages: dest};
};

/**
 * 经典推荐
 * @param type $data
 * @return type []
 */
const recommend = (data, shopId) => {
    let dest = [];

    _.forEach(data, (value) => {
        dest.push({
            enName: value.enName,
            name: value.name,
            img: value.src,
            title: value.title,
            url: `${shopListUrl}?shopId=${shopId}&filter_poolId=${value.categoryId}`
        });
    });

    return {
        recommend: {
            title: dest[0].title,
            list: dest
        }
    };
};

/**
 * 品牌集合
 * @param type $data
 * @return type []
 */
const brandBrowse = (data, params) => {
    let brand = _.get(params, 'brand', '');
    let resData = {};

    if (!_.isEmpty(data) && data.length > 1) {
        let list = [];

        _.forEach(data, value => {
            list.push({
                url: `${shopListUrl}?shopId=${params.shopId || ''}&brand=${value.id}&title=${value.title}$navBar=1`,
                brandName: value.brandName,
                title: value.title,
                cur: (brand === +value.id)
            });
        });

        resData.brandBrowse = {
            title: list[0].title,
            list: list
        };
    }

    return resData;
};

/**
 * 热门推荐
 * @param type $data
 * @return type []
 */
const hotRecommend = (data) => {
    let resData = {};

    if (!_.isEmpty(data)) {
        let list = [];

        _.forEach(data, value => {
            list.push({
                title: value.title,
                img: value.src,
                url: value.url
            });
        });

        resData.hotRecommend = {
            title: list[0].title,
            list: list
        };
    }

    return resData;
};

/**
 * 店铺装修楼层数据
 * @param data 装修数据
 * @returns {{}}
 */
exports.getShopDecorator = (data, params, shopId) => {
    let dest = {
        newArrivel: {},
        hotSingle: {}
    };

    _.forEach(data.list, (value) => {
        let info = JSON.parse(value.resource_data);
        let tabBar;

        switch (value.resource_name) {
            case 'signboard':
                break;
            case 'newProducts':
                Object.assign(dest.newArrivel, newProducts(info));
                break;
            case 'hotProducts':
                Object.assign(dest.hotSingle, hotProducts(info));
                break;
            case 'goodsTabBar':
                tabBar = goodsTabBar(info);
                Object.assign(dest.newArrivel, {navs: tabBar.new});
                Object.assign(dest.hotSingle, {navs: tabBar.hot});
                break;
            case 'shopTopBanner':
                Object.assign(dest, shopTopBanner(info));
                break;
            case 'shopTopBanner_base':
                Object.assign(dest, shopTopBannerBase(info));
                break;
            case 'navigationBar':
                Object.assign(dest, navigationBar(info, shopId));
                break;
            case 'largeSlideImg':
                Object.assign(dest, largeSlideImg(info));
                break;
            case 'oneRowTwoColImages':
                Object.assign(dest, oneRowTwoColImages(info, shopId));
                break;
            case 'recommend':
                Object.assign(dest, recommend(info, shopId));
                break;
            case 'brandBrowse':
                Object.assign(dest, brandBrowse(info, params));
                break;
            case 'hotRecommend':
                Object.assign(dest, hotRecommend(info));
                break;
            default:
                break;
        }
    });

    return dest;
};