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

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

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

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

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

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

        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
                });
            });

            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 => {
        return Object.assign(finalResult, handleBrandList(result.data.all_list));
    });
};

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

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

            if (result[1]) {
                _.forEach(result[1], function(data) {
                    if (data.focus_type === '1') {
                        brandList.bannerTop = data;
                    } else if (data.focus_type === '2') {
                        brandList.focusData = data.data;
                    }
                });
            }

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

            list.brandList = brandList;
            return list;
        });
        // return favfavBrand(uid, page, limit).then(result=> {
        //     if (result.total === 0) {
        //         result = {nobrandData: 1};
        //     }
        //     return result;
        // });
    } else {
        // return favProduct(uid, page, limit).then(result=> {
        //     if (result.total === 0) {
        //         result = {noproductData: 1};
        //     }
        //     return result;
        // });
    }
};

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 === '1') {
                    brandList.bannerTop = data;
                } else if (data.focus_type === '2') {
                    brandList.focusData = data.data;
                }
            });
        }

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

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

module.exports = {
    indexData,
    brandListData
};