brand-list.js 8.48 KB
'use strict';
const _ = require('lodash');
const helpers = global.yoho.helpers;

const yhChannel = {
    1: {
        channel: '1'
    },
    2: {
        channel: '2'
    },
    4: {
        channel: '3'
    }
};

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

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

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    handleBrandList(origin) {
        let dest = {
            ListData: [],
            indexList: []
        };

        let keyList = [];

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

        _.forEach(origin, (value, key) => {

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

            _.forEach(value, function(subValue) {
                subValue.brandUrl = helpers.urlFormat('/', {
                    query: subValue.brand_domain,
                    app_type: 1
                }, 'search');
                subValue.name = subValue.brand_name_en || subValue.brand_name_cn || subValue.brand_name;
                subValue.domain = subValue.brand_domain;
            });

            if (key === '0~9') {
                hasNum = true;
                numTemp = origin[key];
            } else {

                keyList.push(key);
            }

        });

        keyList.sort();

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

            dest.ListData.push({
                index: keyName,
                brands: origin[key]
            });

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

        // 如果有数字,单独处理
        if (hasNum) {

            dest.ListData.push({
                index: '0~9',
                brands: numTemp
            });

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

        return dest;
    }

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

        return this.get({
            data: {method: 'app.blk.getAllChannels'},
            param: {
                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;
        });
    }

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

    _getBrandListOriginData(channel) {
        let finalResult = {};

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

    // 全部分类数据
    _processCateData(list, channel, appType) {
        let nav = ['MEN男士', 'WOMEN女士'];

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

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

            _.forEach(item, function(firstItem) {
                // 如果有二级菜单,二级菜单跳转,否则一级菜单跳转
                if (firstItem.sub && firstItem.sub.length) {
                    _.forEach(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
        };
    }

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

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

    indexData(gender, brandCode, channel, appType) {
        return Promise.all([
            this._getChannelData(gender),
            this._getResourcesData(brandCode),
            this._getBrandListOriginData(gender),
            this._getCateData(channel, appType)
        ]).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 === '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;
            }

            if (result[3]) {

                list.categoryData = result[3];
            }

            list.brandList = brandList;

            return list;
        });
    }

    brandListData(code, gender) {
        return Promise.all([this._getResourcesData(code), this._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;
        });
    }
};