ResourceParser.js 4.29 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) {
	let spring = [];
    _.forEach(resData, (item) => {
        if (item.data) {
            spring.push({
                url: _modifyAppUrl(item.data[0], shopId),
                src: item.data[0].src,
            });
        }
    });

    return spring;
}

// 店铺品牌一览
function brandBrowseParser(resData, shopId) {
	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,
    };
}


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

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

    return bannerTop;
}

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

    _.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) {
	let hotProduct = {
        productSkn: '',
        moreUrl: `http://m.yohobuy.com?openby:yohobuy={"action":"go.list","params":{"shop_id":"${shopId}","title":"人气单品"}}`,
    };
	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 = {};

	_.forEach(json.list, floor => {
        let resData = JSON.parse(floor.resource_data);
        let resource_name = floor.resource_name;
        switch(resource_name) {
        	case 'shopTopBanner_APP':
        		shopTopBanner_APP = shopTopBanner_APPParser(resData);
        		break;
        	case 'oneRowTwoColImages_APP':
        		oneRowTwoColImages_APP = oneRowTwoColImages_APPParser(resData, shopId);
        		break;
        	case 'brandBrowse':
        		brandBrowse = brandBrowseParser(resData, shopId);
        		break;
        	case 'largeSlideImg_APP':
        		largeSlideImg_APP = largeSlideImg_APPParser(resData, shopId);
        		break;
        	case 'recommend_APP':
        		recommend_APP = recommend_APPParser(resData, shopId);
        		break;
        	case 'hotProducts_APP':
        		hotProducts_APP = hotProducts_APPParser(resData, shopId);
        		break;
        }
    });

    return {
    	shopTopBanner_APP,
    	oneRowTwoColImages_APP,
    	brandBrowse,
    	largeSlideImg_APP,
    	recommend_APP,
    	hotProducts_APP,
    };
}