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

'use strict';

const api = global.yoho.API;
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)
    if (list.data) {
        _.forEach(list.data.all_list, function(value, index) {

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

            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.all_list[key]
            });
        });
    }

    return listData;
};

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

    list = list || [];

    // list = camelCase(list);

    _.forEach(list, function(value) {

        _.forEach(value.data, function(data) {
            tabData.push(
                {
                    url: data.url,
                    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) {
            // console.log(result)
            return _processTabData(result.data);
        } else {
            logger.error('The data of brand resources return code is not 200');
            return {};
        }
    });
};

/**
 * 获取品牌列表数据
 * @param channel
 * @returns {*}
 */
const _getBreakingSort = (channel, gender, appType) => {
    return api.get('', {
        gender: gender,
        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, gender, appType) => {
    return Promise.all([_getResources(contentCode), _getBreakingSort(channel, gender, appType)])
    .then((result) => {

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

module.exports = {
    getListData: getListData
};