brand.js 2.94 KB
/**
 * 品牌一览
 * @author: zxr
 * @date: 2016/07/13
 */

'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;

/**
 * 品牌一览列表数据处理
 * @returns {*}
 */
const _processListData = (list) => {

    let listData = [];

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

    let listKey = [];

    // console.log(list)

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

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

        if (index !== '0~9') {
            listKey.push(index);
        }
    });

    listKey.sort();

    listKey.push('0-9');

    _.forEach(listKey, function(key) {
        let keyName = key;

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

        listData.push({
            key: keyName,
            brands: list.data.allList[key]
        });
    });

    return listData;
};

/**
 * 品牌一览banner数据处理
 * @returns {*}
 */
const _processTabData = (list) => {
    let tabData = [];

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

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

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

    return tabData;

};

/**
 * 获取品牌banner
 * @param channel
 * @returns {*}
 */
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('The data of brand resources return code is not 200');
            return {};
        }
    });
};

/**
 * 获取品牌列表数据
 * @param channel
 * @returns {*}
 */
const _getBreakingSort = (channel, appType) => {
    return api.get('', {
        yh_channel: channel,
        app_type: appType,
        method: 'app.brand.newBrandList'
    }).then((result) => {
        if (result && result.code === 200) {
            return _processListData(result);
        } else {
            logger.error('The data of brand resources return code is not 200');
            return {};
        }
    });
};

/**
 * 获取品牌一览相关数据
 * @returns {*}
 */
const getListData = (contentCode, channel, appType) => {
    return Promise.all([_getResources(contentCode), _getBreakingSort(channel, appType)])
    .then((result) => {

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

module.exports = {
    getListData: getListData
};