brand-list.js 10.6 KB
'use strict';
const api = global.yoho.API;
const serviceAPI = global.yoho.ServiceAPI;
const _ = require('lodash');
const helpers = global.yoho.helpers;

const yhChannel = {
    1: {
        channel: '301'
    },
    2: {
        channel: '302'
    },
    4: {
        channel: '303'
    }
};

const genderMap = {
    MEN男士: '1,3',
    WOMEN女士: '2,3'
};

const channelNav = {
    boys: 'MEN男士',
    girls: 'WOMEN女士'
};

const handleBrandList = origin => {
    let dest = {
        ListData: [],
        indexList: []
    };

    // 标记是否有数字,有数字先暂存
    let hasNum = false;
    let numTemp = {};

    _.forEach(origin, (value, key) => {
        let brands = [];

        if (_.size(value) <= 0) {
            return false;
        }

        if (key === '0~9') {
            hasNum = true;
            numTemp = origin[key];
        } else {
            _.forEach(value, (subValue) => {
                brands.push({
                    name: subValue.brand_name_en || subValue.brand_name_cn || subValue.brand_name,
                    logo: subValue.brand_ico,
                    domain: subValue.brand_domain,
                    brandUrl: '//m.yohobuy.com/product/index/brand?domain=' + subValue.brand_domain + '&app_type=1'
                });
            });

            dest.ListData.push({
                index: key,
                brands: brands
            });

            dest.indexList.push({
                index: key,
                name: key === '0~9' ? '0' : key
            });
        }

    });

    // 商品列表排序一次
    _.sortBy(dest.ListData, o => {
        return o.index.charCodeAt();
    });

    // 字母列表排序一次
    _.sortBy(dest.indexList, o => {
        return o.index.charCodeAt();
    });

    // 如果有数字,单独处理
    if (hasNum) {
        let brands = [];

        _.forEach(numTemp, (subValue) => {
            brands.push({
                name: subValue.brand_name_en || subValue.brand_name_cn || subValue.brand_name,
                logo: subValue.brand_ico,
                domain: subValue.brand_domain
            });
        });
        dest.ListData.push({
            index: '0~9',
            brands: brands
        });

        dest.indexList.push({
            index: '0_9',
            name: '0'
        });
    }

    return dest;
};

const _getChannelData = (params) => {
    let gender = params - 1 || 0;

    return api.get('', {
        method: 'app.blk.getAllChannels'
    }, {
        cache: true,
        code: 200
    }).then(result => {
        let channel = [];

        if (gender === 3 && result.data.length === 3) {
            // 1:男,2:女,3:童装,4:创意生活
            // 如果为3,说明童装没有配置,只配置了创意生活。所以要减一
            gender = 2;
        }

        _.forEach(result.data, (res, index) => {
            channel.push({
                id: res.channel_id,
                mame: res.channel_name,
                code: res.content_code,
                focus: index === gender ? true : false
            });
        });

        return channel;
    });
};

const _getResourcesData = (contentCode) => {
    if (!contentCode) {
        return [];
    }
    return serviceAPI.get('operations/api/v5/resource/get', {
        content_code: contentCode
    }, {
        cache: true,
        code: 200
    }).then(result => {
        return result && result.data ? result.data : [];
    });
};

const _getBrandListOriginData = (channel) => {
    let finalResult = {};

    return api.get('', {
        method: 'app.brand.newBrandList',
        yh_channel: yhChannel[channel].channel,
        app_type: 1
    }, {
        code: 200,
        cache: true
    }).then(result => {
        if (result.data && result.data.all_list) {
            return Object.assign(finalResult, handleBrandList(result.data.all_list));
        } else {
            return {};
        }
        
    });
};

const _processCateData = (list, channel, appType) => {
    let nav = ['MEN男士', 'WOMEN女士'];

    nav = _.map(nav, function(item) {
        return {
            name: item,
            channel: item,
            focus: item === channelNav[channel]
        };
    });

    _.map(list, function(item, key) {
        item.focus = key === channelNav[channel];

        _.map(item, function(firstItem) {
            // 如果有二级菜单,二级菜单跳转,否则一级菜单跳转
            if (firstItem.sub && firstItem.sub.length) {
                _.map(firstItem.sub, function(secondItem) {
                    secondItem.url = helpers.urlFormat('/', {
                        sort: _.get(secondItem, 'relation_parameter.sort'),
                        sort_name: secondItem.category_name,
                        gender: genderMap[key] || '',
                        app_type: appType
                    }, 'list');
                });

                firstItem.sub.unshift({
                    category_name: `全部${firstItem.category_name}`,
                    url: helpers.urlFormat('/', {
                        sort: _.get(firstItem, 'relation_parameter.sort'),
                        sort_name: firstItem.category_name,
                        gender: genderMap[key] || '',
                        app_type: appType
                    }, 'list')
                });
            } else {
                firstItem.url = helpers.urlFormat('/', {
                    sort: _.get(firstItem, 'relation_parameter.sort'),
                    sort_name: firstItem.category_name,
                    gender: genderMap[key] || '',
                    app_type: appType
                }, 'list');
            }
        });
    });

    return {
        nav,
        list
    };
};

// const _dealBrandUrl = (list) => {
//     var domain;

//     _.forEach(list.data, function(val) {
//         domain = val.url.substring(val.url.indexOf('shop/') + 5, val.url.indexOf('?'));

//         val.url = '//m.yohobuy.com/product/index/brand?domain=' + domain;
//     });

//     return list;
// };

const indexData = (gender, brandCode, cateCode) => {
    return Promise.all([_getChannelData(gender), _getResourcesData(brandCode), _getBrandListOriginData(gender), _getResourcesData(cateCode)]).then(result => {
        let brandList = {};
        let list = {};
        let categoryData = {};

        if (result[0]) {
            categoryData.channel = result[0];
            list.channel = result[0];
        }

        if (result[1]) {
            _.forEach(result[1], function(data) {
                if (data.focus_type && data.focus_type === '1') {
                    //brandList.bannerTop = _dealBrandUrl(data);
                    brandList.bannerTop = data;
                } else if (data.focus_type && data.focus_type === '2') {
                    //brandList.focusData = _dealBrandUrl(data).data;
                    brandList.focusData = data.data;
                } else if (data.template_intro === '一张图片') {
                    brandList.bannerTop = data;
                }
            });
        }

        if (result[2]) {

            brandList.listData = result[2].ListData;
            brandList.indexList = result[2].indexList;
        }

        if (result[3]) {
            _.forEach(result[3], function(data) {
                if (data.template_name === 'recommend_content_five') {
                    // _.forEach(data.data.list, function(val) {
                    //     var param = val.url.substring(val.url.indexOf('list?') + 5);

                    //     val.url = '//list.m.yohobuy.com/?' + param;
                    // });
                    categoryData.list = data.data.list;
                } else if (data.template_name === 'focus') {
                    // _.forEach(data.data, function(val) {

                    //     val.url = '//m.yohobuy.com/guang/cate-all';
                    // });
                    categoryData.focusData = data;
                }
            });
        }

        list.brandList = brandList;
        list.categoryData = categoryData;
        return list;
    });
};

const brandListData = (code, gender) => {
    return Promise.all([_getResourcesData(code), _getBrandListOriginData(gender)]).then(result => {
        let brandList = {};
        let list = {};

        if (result[0]) {
            _.forEach(result[0], function(data) {
                if (data.focus_type && data.focus_type === '1') {
                    //brandList.bannerTop = _dealBrandUrl(data);
                    brandList.bannerTop = data;
                } else if (data.focus_type && data.focus_type === '2') {
                    //brandList.focusData = _dealBrandUrl(data).data;
                    brandList.focusData = data.data;
                } else if (data.template_intro === '一张图片') {
                    brandList.bannerTop = data;
                }
            });
        }

        if (result[1]) {
            brandList.listData = result[1].ListData;
            brandList.indexList = result[1].indexList;
        }

        list.brandList = brandList;
        return list;
    });
};

const categoryData = (code) => {

    return _getResourcesData(code).then(result => {
        let category = {};
        let list = {};

        if (result) {
            _.forEach(result, function(data) {
                if (data.template_name === 'recommend_content_five') {
                    // _.forEach(data.data.list, function(val) {
                    //     var param = val.url.substring(val.url.indexOf('list?') + 5);

                    //     val.url = '//list.m.yohobuy.com/?' + param;
                    // });
                    category.list = data.data.list;
                } else if (data.template_name === 'focus') {
                    // _.forEach(data.data, function(val) {

                    //     val.url = '//m.yohobuy.com/guang/cate-all';
                    // });

                    category.focusData = data;
                }
            });
        }

        list.categoryData = category;
        return list;
    });
};

const getCateData = (channel, appType) => {
    return api.get('', {
        method: 'app.sort.get',
        app_type: 1
    }, {
        cache: true
    }).then((result) => {
        if (!result.code || result.code !== 200 || !result.data) {
            return [];
        }

        // 统一频道名称
        // result.data.boys = result.data.boy;
        // result.data.girls = result.data.girl;
        // _.unset(result.data, 'boy');
        // _.unset(result.data, 'girl');


        return result.data;
    }).then((list) => {
        return _processCateData(list, channel, appType);
    });
};

module.exports = {
    indexData,
    brandListData,
    categoryData,
    getCateData
};