shop-handler.js 5.97 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 shopRecommentName = '经典推荐';
const shopListUrl = '/shoplist';

/**
 * 新品上架
 */
const newProducts = (data) => {
    let 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.getProductUrl(value.productId, value.productSkn, value.productName)
        });
    });
    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.getProductUrl(value.productId, value.productSkn, value.productName),
            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) => {
    return {
        shopTopBanner: {
            banner: data[0].shopSrc || '',
            detailSrc: data[0].detailSrc || '',
            isShowShopName: 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 = [];

    console.log(data);
    _.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) => {
    let dest = [];

    _.forEach(data, (value) => {
        dest.push({
            enName: value.enName,
            name: value.name,
            img: value.src,
            title: value.title,
            url: helpers.urlFormat(value.url)
        });
    });

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

/**
 * 品牌一览
 * @param type $data
 * @return type []
 */
// const brandBrowse = (params) => {

// };

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

    _.forEach(data.list, (value) => {
        let tabBar;

        switch (value.resource_name) {
            case 'signboard':
                break;
            case 'newProducts':
                dest.newArrivel = newProducts(JSON.parse(value.resource_data));
                break;
            case 'hotProducts':
                dest.hotSingle = hotProducts(JSON.parse(value.resource_data));
                break;
            case 'goodsTabBar':
                tabBar = goodsTabBar(JSON.parse(value.resource_data));
                Object.assign(dest.newArrivel, {navs: tabBar.new});
                Object.assign(dest.hotSingle, {navs: tabBar.hot});
                break;
            case 'shopTopBanner':
                Object.assign(dest, shopTopBanner(JSON.parse(value.resource_data)));
                break;
            case 'navigationBar':
                Object.assign(dest, navigationBar(JSON.parse(value.resource_data), shopId));
                break;
            case 'largeSlideImg':
                Object.assign(dest, largeSlideImg(JSON.parse(value.resource_data)));
                break;
            case 'oneRowTwoColImages':
                Object.assign(dest, oneRowTwoColImages(JSON.parse(value.resource_data), shopId));
                break;
            case 'recommend':
                Object.assign(dest, recommend(JSON.parse(value.resource_data), shopId));
                break;
            default:
                break;
        }
    });

    // console.log(dest);
    return dest;
};