brand.js 2.28 KB
'use strict';

const api = global.yoho.API;
const camelCase = global.yoho.camelCase;
const _ = require('lodash');
const logger = global.yoho.logger;
const serviceAPI = global.yoho.ServiceAPI;
const config = global.yoho.config;

const _processListData = (list) => {

    let listData = [];

    list = list || [];
    list = camelCase(list);


    _.forEach(list.data.allList, function(value, index) {

        _.forEach(value, function(data) {
            data.brandDomain = `${config.siteUrl}/product/shop/${data.brandDomain}`;
        });

        if (index === '0~9') {
            index = '0-9';
        }

        listData.push(
            {
                key: index,
                brands: value
            }
        );
    });

    return listData;
};

const _processTabData = (list) => {
    let tabData = [];

    list = list || [];
    list = camelCase(list);

    _.forEach(list, function(value, index) {

        if (index === 0) {
            _.forEach(value.data, function(data) {
                tabData.push(
                    {
                        url: data.url,
                        name: data.title,
                        src: data.src
                    }
                );
            });
        }
    });

    return tabData;

};

const _getResources = (contentCode) => {
    return serviceAPI.get('operations/api/v5/resource/get', {content_code: contentCode}).then((result) => {
        if (result && result.code === 200) {
            return _processTabData(result.data);
        } else {
            logger.error('品牌资源数据返回 code 不是 200');
            return {};
        }
    });
};

const _getBreakingSort = (channel) => {
    return api.get('', {
        yo_channel: channel,
        method: 'app.brand.newBrandList'
    }).then((result) => {
        if (result && result.code === 200) {
            return _processListData(result);
        } else {
            logger.error('品牌页数据返回 code 不是 200');
            return {};
        }
    });
};

const getListData = (contentCode, channel) => {
    return Promise.all([_getResources(contentCode), _getBreakingSort(channel)])
    .then((result) => {

        return {
            tabs: result[0],
            category: result[1]
        };
    });
};

module.exports = {
    getListData: getListData
};