ResourceParser.js 5.97 KB
'use strict';

// import {image, urlFormat} from './Helper';
const helpers = require('./Helper');
const _ = require('lodash');

/**
 * 销售类目链接拼接 APP
 * @param origin
 * @param shopId
 * @private
 */
function _modifyAppUrl(origin, shopId) {
    if (parseInt(_.get(origin, 'linkType', 0), 10) === 1) {
        return `http://m.yohobuy.com?openby:yohobuy={"action":"go.poollist","params":{"shop_id":"${shopId}","title":"${origin.categoryName}","productPool":"${origin.categoryId}"}}`; //eslint-disable-line
    } else {
        if (_.has(origin, 'url')) {
            return origin.url;
        } else {
            return '';
        }
    }
};

// 店铺banner
function shopTopBanner_APPParser(resData) {
    return resData[0].shopSrc;
}

// 资源位小图 接口返回两组,取每组第一张
function oneRowTwoColImages_APPParser(resData, shopId, resourceId) {
	let spring = [];
    _.forEach(resData, (item) => {
        if (item.data) {
            spring.push({
                url: _modifyAppUrl(item.data[0], shopId),
                src: item.data[0].src,
                resourceId,
            });
        }
    });

    return spring;
}

// 店铺品牌一览
function brandBrowseParser(resData, shopId, resourceId) {
	let brandNumber = resData.length;
    let brands = [];

    let brandId = '';
    let single = false;


    // 少于2个不展示 单品店:单品店根据品牌id查询
    if (brandNumber < 2) {
        brandId = resData[0].id;
        single = true;
    } else {
        _.forEach(resData, (item) => {
            if (item.brandDomain) {
                brands.push({
                    url: `http://m.yohobuy.com?openby:yohobuy={"action":"go.brand","params":{"shop_id":${shopId},"brand_id":${item.id}}}`, //eslint-disable-line
                    src: helpers.image(item.brandIco, 640, 400),
                    name: item.brandName
                });
                brandId += item.id + ',';
            }
        });
    }

    return {
    	single,
    	brandId,
    	brands,
        resourceId,
    };
}


// 资源位大图幻灯
function largeSlideImg_APPParser(resData, shopId, resourceId) {
	let bannerTop = [];

    _.forEach(resData, (item) => {
        if (item.data[0]) {
            bannerTop.push({
                url: _modifyAppUrl(item.data[0], shopId),
                src: item.data[0].src,
                resourceId,
            });
        }
    });

    return bannerTop;
}

// 热门品类
function recommend_APPParser(resData, shopId, resourceId) {
	let hotCategory = {
        list: [],
        resourceId,
    };

    _.forEach(resData, cate => {
        hotCategory.list.push({
            url: _modifyAppUrl(cate, shopId),
            src: helpers.image(cate.src),
            name: cate.name
        });
    });

    return hotCategory;
}

// 人气单品
function hotProducts_APPParser(resData, shopId, resourceId) {
	let hotProduct = {
        productSkn: '',
        moreUrl: `http://m.yohobuy.com?openby:yohobuy={"action":"go.list","params":{"shop_id":"${shopId}","title":"人气单品"}}`,
        resourceId,
    };
	let productSkn = '';

    _.forEach(resData, value => {
        productSkn += value.productSkn + ',';
    });

    hotProduct.productSkn = productSkn;

    return hotProduct;
}

export function parseShopResources(json, shopId) {
	let shopTopBanner_APP = '';
	let oneRowTwoColImages_APP = [];
	let brandBrowse = {};
	let largeSlideImg_APP = [];
	let recommend_APP = {};
	let hotProducts_APP = {};

    let logData = [];

	_.forEach(json.list, floor => {
        let resData = JSON.parse(floor.resource_data);
        let resource_name = floor.resource_name;
        let resourceId = floor.resource_id;

        let logList = [];

        switch(resource_name) {
        	case 'shopTopBanner_APP':
        		shopTopBanner_APP = shopTopBanner_APPParser(resData);
                logList.push({
                    I_INDEX : '1',
                    ACTION_URL : '',
                });
        		break;
        	case 'oneRowTwoColImages_APP':
        		oneRowTwoColImages_APP = oneRowTwoColImages_APPParser(resData, shopId,resourceId);
                oneRowTwoColImages_APP.map((item, i) => {
                    logList.push({
                        I_INDEX : i + 1,
                        ACTION_URL : item.url,
                    });
                });
        		break;
        	case 'brandBrowse':
        		brandBrowse = brandBrowseParser(resData, shopId, resourceId);
                logList.push({
                    I_INDEX : '1',
                    BRAND_ID: brandBrowse.brandId,
                });
        		break;
        	case 'largeSlideImg_APP':
        		largeSlideImg_APP = largeSlideImg_APPParser(resData, shopId, resourceId);
                largeSlideImg_APP.map((item, i) => {
                    logList.push({
                        I_INDEX : i + 1,
                        ACTION_URL : item.url,
                    });
                });
        		break;
        	case 'recommend_APP':
        		recommend_APP = recommend_APPParser(resData, shopId, resourceId);
                recommend_APP.list.map((item, i) => {
                    logList.push({
                        I_INDEX : i + 1,
                        ACTION_URL : item.url,
                    });
                });
        		break;
        	case 'hotProducts_APP':
        		hotProducts_APP = hotProducts_APPParser(resData, shopId, resourceId);
                logList.push({
                    I_INDEX : '0',
                    PRODUCT_SKN: hotProducts_APP.productSkn,
                    MORE_URL : hotProducts_APP.moreUrl,
                });
        		break;
        }

        if (logList.length > 0) {
            logData.push({
                F_NAME : resource_name,
                F_ID : resourceId,
                LIST: logList,
            });
        }
    });

    let data = {
        shopTopBanner_APP,
        oneRowTwoColImages_APP,
        brandBrowse,
        largeSlideImg_APP,
        recommend_APP,
        hotProducts_APP,
    };
    return {
        data,
        logData,
    };
}