Blame view

doraemon/models/header.js 5.62 KB
biao authored
1 2 3 4 5
/**
 * header model
 * @author: 赵彪<bill.zhao@yoho.cn>
 * @date: 2016/05/03
 */
biao authored
6
biao authored
7
'use strict';
biao authored
8
biao authored
9
const _ = require('lodash');
biao authored
10
yyq authored
11
const api = global.yoho.API;
姜枫 authored
12
const serviceApi = global.yoho.ServiceAPI;
OF1706 authored
13
const helpers = global.yoho.helpers;
biao authored
14
biao authored
15 16 17 18 19
/**
 * 获取菜单
 * @param undefined
 * @return {array} 菜单数组
 */
biao authored
20
const getMenuData = () => (
郭成尧 authored
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    [{
        link: 'http://www.yoho.cn',
        cn: '集团官网',
        en: 'YOHO!'
    }, {
        link: 'http://www.yohoboys.com',
        cn: '男生潮流',
        en: 'YOHO!BOYS'
    }, {
        link: 'http://www.yohogirls.com',
        cn: '女生潮流',
        en: 'YOHO!GIRLS'
    }, {
        link: 'http://www.yohoshow.com',
        cn: '物趣分享',
        en: 'YOHO!SHOW'
    }, {
        link: 'http://www.yohood.cn',
        cn: '潮流嘉年华',
        en: 'YO\'HOOD'
    }]
biao authored
42
);
biao authored
43
biao authored
44 45
/**
 * 获取导航
biao authored
46 47
 * @param {Object} data 要处理的数据
 * @param {String} type 频道类型
biao authored
48 49
 * @return {array} 导航数组
 */
biao authored
50
const getNavBar = (data, type) => {
biao authored
51
    let navBars = [];
biao authored
52
biao authored
53
    _.forEach(data, item => {
htoooth authored
54 55
        let obj = {};
        let lowEn = _.camelCase(item.sort_name_en).toLowerCase();
biao authored
56
57 58 59 60 61 62 63
        Object.assign(obj, {
            type: lowEn,
            link: item.sort_url,
            cn: item.sort_name,
            en: item.sort_name_en,
            isNewPage: item.is_new_page === 'Y'
        });
biao authored
64
yyq authored
65
        if (type === lowEn) {
biao authored
66 67 68
            obj.active = true;
        }
yyq authored
69 70 71 72 73
        // 奥莱频道显示图片,特殊处理
        if (lowEn === 'outlets') {
            obj.ico = item.sort_ico;
        }
biao authored
74 75 76 77
        navBars.push(obj);
    });

    return navBars;
biao authored
78
};
biao authored
79
biao authored
80 81 82

/**
 * 获取品牌名字
biao authored
83
 * @param {Object} data 要处理数据
biao authored
84 85
 * @return {array} 品牌数组
 */
biao authored
86 87
const getBrandItems = (data) => {
    let brandItems = [];
biao authored
88
biao authored
89
    _.forEach(data, item => {
90 91 92 93 94
        let obj = {
            link: '',
            hot: false,
            brandName: ''
        };
biao authored
95 96

        obj.link = item.sort_url;
biao authored
97
        obj.hot = item.is_hot === 'Y' ? true : false;
biao authored
98 99 100 101 102 103 104 105 106
        obj.brandName = item.sort_name;

        brandItems.push(obj);
    });


    return brandItems;
};
biao authored
107 108
/**
 * 获取三级菜单
biao authored
109
 * @param {Object} data 要处理数据
biao authored
110 111
 * @return {array} 三级菜单数组
 */
biao authored
112 113 114
const getThirdNav = (data) => {
    let thirdNav = [];
biao authored
115
    _.forEach(data, item => {
116 117 118 119 120
        let obj = {
            link: '',
            title: '',
            brandItems: false
        };
biao authored
121 122 123

        obj.link = item.sort_url;
        obj.title = item.sort_name;
124
biao authored
125 126 127 128 129 130 131 132 133 134 135 136

        if (item.sub) {
            obj.brandItems = getBrandItems(item.sub);
        }

        thirdNav.push(obj);
    });


    return thirdNav;
};
biao authored
137 138
/**
 * 获取子菜单
biao authored
139 140
 * @param {Object} data 要处理数据
 * @param {String} type 频道类型
biao authored
141 142
 * @return {array} 子菜单数组
 */
143 144
const getSubNavGroup = (data, type) => {
    let subNavGroup = [];
biao authored
145
biao authored
146
    _.forEach(data, it => {
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
        let subNav = [];

        _.forEach(it.sub, item => {
            let obj = {};

            obj.link = item.sort_url;
            obj.name = item.sort_name;
            obj.isHot = item.is_hot === 'Y' ? true : false;
            obj.isNew = item.is_new === 'Y' ? true : false;

            if (item.sub) {
                obj.thirdNav = getThirdNav(item.sub);
                obj.imgCode = item.content_code;
            }

            subNav.push(obj);
        });

        let lowEn = _.camelCase(it.sort_name_en).toLowerCase();

        subNavGroup.push({
            subType: lowEn,
            subNav: subNav,
            active: lowEn === type
        });
biao authored
172
    });
biao authored
173
174
    return subNavGroup;
biao authored
175 176
};
biao authored
177
biao authored
178 179 180
/**
 * 处理接口返回的数据
 * @param {object} 接口返回的对象
biao authored
181
 * @param {String} 指定页面类型为boys,girls,kids,lifestyle
biao authored
182 183
 * @return {object} 头部数据
 */
184 185
const setHeaderData = (resData, type) => (
    {
yyq authored
186 187 188 189
        header: true,
        headType: type,
        yohoGroup: getMenuData(),
        navbars: resData ? getNavBar(resData, type) : [],
190
        subNavGroup: resData ? getSubNavGroup(resData, type) : []
191 192
    }
);
biao authored
193
biao authored
194
OF1706 authored
195 196
const getHotSearchAsync = (channel) => {
OF1706 authored
197
    return api.get('', {method: 'app.search.getTerms', yh_channel: channel}, {
yyq authored
198 199 200
        cache: 600,
        code: 200
    });
OF1706 authored
201 202

yyq authored
203 204 205 206 207 208 209 210 211
};

const getHeaderNavAsync = () => {
    return serviceApi.get('operations/api/v6/category/getCategory', {}, {
        cache: true,
        code: 200
    });
};
biao authored
212 213
/**
 * 请求头部数据
biao authored
214
 * @param {String} 频道类型
biao authored
215 216
 * @return {promise}
 */
yyq authored
217 218
exports.requestHeaderData = (type) => {
    let resData = {};
biao authored
219
220 221
    type = type || 'boys';
OF1706 authored
222 223 224 225 226 227 228 229
    let channelNum = (function() {
        switch (type) {
            case 'boys':
                return 1;
            case 'girls':
                return 2;
            case 'kids':
                return 3;
OF1706 authored
230 231
            case 'lifestyle':
                return 4;
OF1706 authored
232 233 234 235 236
            default:
                return 1;
        }
    }());
yyq authored
237 238
    return Promise.all([
        getHeaderNavAsync(),
OF1706 authored
239 240
        getHotSearchAsync(channelNum)
yyq authored
241 242 243 244 245 246 247 248
    ]).then(res => {
        resData.headerData = {};

        if (res[0] && res[0].data) {
            Object.assign(resData.headerData, setHeaderData(res[0].data, type));
        }

        if (res[1] && res[1].data) {
周少峰 authored
249
            resData.headerData.defaultSearch = _.get(res[1], 'data.defaultTerms[0].content', '');
OF1706 authored
250 251 252 253 254 255 256 257 258
            resData.headerData.hotTerms = _.map(_.get(res[1], 'data.hotTerms', []), (value) => {
                return {
                    href: helpers.urlFormat('', {query: value.content}, 'search'),
                    content: value.content,
                    sort: value.sort,
                    status: value.status,
                    type: value.type
                };
            });
yyq authored
259 260 261
        }

        return resData;
biao authored
262
    });
biao authored
263
};