Blame view

apps/channel/models/index.js 35.3 KB
biao authored
1
/**
biao authored
2
 * 频道页 model
biao authored
3 4 5 6 7
 * @author: 赵彪<bill.zhao@yoho.cn>
 * @date: 2016/05/17
 */
'use strict';
liangxs authored
8
const _ = require('lodash');
biao authored
9
biao authored
10 11
const dataMap = require('../../../config/data-map');
姜枫 authored
12
const helpers = global.yoho.helpers;
htoooth authored
13
const config = global.yoho.config;
biao authored
14
biao authored
15
const processProduct = require(`${global.utils}/product-process`).processProduct;
周少峰 authored
16
const searchApi = require('../../product/models/search-api');
biao authored
17
biao authored
18
yyq authored
19
const api = global.yoho.API;
姜枫 authored
20
const serviceApi = global.yoho.ServiceAPI;
biao authored
21
liangxs authored
22
const headerModel = require('../../../doraemon/models/header');
biao authored
23
24
// 获取可用的标题
biao authored
25
const _getText = data => {
26 27
    let text = data.split(' ')[0];
    const regResult = /\w+/.exec(text);
biao authored
28
    const titleWithoutChinese = /^(?=.*[A-Za-z0-9])[A-Za-z0-9 _]*$/;
29
biao authored
30 31
    // 对于不含中文的标题,直接返回
    if (titleWithoutChinese.test(data)) {
32 33 34 35
        return data;
    }

    if (regResult) {
biao authored
36 37

        // 去掉标题中的英文部分
38 39 40 41 42
        text = text.replace(regResult[0], '');
    }

    return text;
};
liangxs authored
43
biao authored
44
const channelMap = dataMap.channel;
liangxs authored
45
biao authored
46
const sortMap = dataMap.sort;
biao authored
47
liangxs authored
48
/**
hongweigao authored
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
 * 新人专享
 * @param {[Object]} data 原始数据
 * @return {[Object]} 转换后的数据
 */
const _getNewUserFloorData = data => {
    let floorData = {
        newUserFloor: {
            name: _getText(data.title.name),
            navs: {},
            bannerImage: {
                url: data.banner_image[0].url,
                src: helpers.image(data.banner_image[0].src, 1150, 368),
                title: data.banner_image[0].title
            }
        }
    };

    if (data.title.more_name) {
        floorData.newUserFloor.navs.name = data.title.more_name;
    }
    if (data.title.more_url) {
        floorData.newUserFloor.navs.url = data.title.more_url;
    }
    if (data.list) {
        let proList = [];

        // 大于15就取前15个产品数
        proList = data.list.length > 15 ? data.list.slice(0, 15) : data.list;

        // 5个以内为false
        floorData.newUserFloor.hasSwitch = proList.length > 5 ? true : false;

        _.forEach(proList, value => {
82
            value.thumb = helpers.image(value.goods_list[0].images_url, 185, 247);
hongweigao authored
83
            value.for_newuser = true;
m  
OF1706 authored
84
            value.url = helpers.getUrlBySkc(value.product_skn);
hongweigao authored
85 86 87 88 89 90 91 92
        });
        floorData.newUserFloor.list = proList;
    }

    return floorData;
};

/**
biao authored
93 94 95 96
 * 获取带小图的banner
 * @param {[Object]} data 原始数据
 * @return {[Object]} 转换后的数据
 */
biao authored
97
const _getDebrisSlide = data => {
biao authored
98 99 100 101 102 103 104 105 106 107
    let floorData = {
        debrisSlider: {
            left: [],
            center: [],
            right: []
        }
    };

    _.mapKeys(data, (value, key) => {
        _.forEach(value, slideData => {
biao authored
108
            floorData.debrisSlider[key].push(slideData);
biao authored
109 110 111 112 113 114
        });
    });

    return floorData;
};
biao authored
115 116 117 118 119 120

/**
 * 获取广告位
 * @param {Object} data 原始数据
 * @return {Object} 转换后的数据
 */
biao authored
121
const _getadbannerData = data => {
biao authored
122
biao authored
123 124 125
    return {
        adbanner: data
    };
biao authored
126
};
127
biao authored
128 129 130 131 132
/**
 * 生成banner模板数据
 * @param {Object} srcData 原始数据
 * @return {Object} 转换后的数据
 */
biao authored
133
const _getSlideData = srcData => {
biao authored
134
    const slideData = {
biao authored
135 136 137 138 139
        slide: {
            list: [],
            pagination: []
        }
    };
liangxs authored
140
biao authored
141
    if (srcData.big_image) {
biao authored
142
        slideData.slide.list = srcData.big_image;
biao authored
143 144 145
    }

    if (srcData.list) {
biao authored
146
        slideData.slide.pagination = srcData.list;
biao authored
147 148
    }
biao authored
149
    if (_.isArray(srcData)) {
biao authored
150
        slideData.slide.list = srcData;
biao authored
151
    }
liangxs authored
152
biao authored
153 154
    return slideData;
};
liangxs authored
155
hongweigao authored
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
/**
 * 获取品牌列表数据
 * @param {[Object]} args 参数列表
 * @param {String} type 频道类型
 * @return {Object}
 */
const _getBrandLogosData = (args, showNum) => {
    let items = [],
        brandLogos = [],
        i = 0,
        fillNum = 0;

    // 品牌数据
    _.forEach(args, (floorData) => {
        items.push({
            href: floorData.url,
            img: helpers.image(floorData.src, 185, 86, 2)
        });
    });

    // 不是每页显示个数的倍数,填充数据
    if (items.length % showNum !== 0) {
        fillNum = showNum - (items.length % showNum);
        for (i = 0; i < fillNum; i++) {
            items.push({
yyq authored
181
                href: 'javascript:;', // eslint-disable-line
hongweigao authored
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
                img: ''
            });
        }
    }

    for (i = 0; i < items.length; i++) {
        let item = items[i];

        let brandLogo = {
            pageNum: Math.floor(i / showNum),
            href: item.href,
            img: item.img
        };

        if (i % showNum === 5 || i === 5) {

            // 插入切换按钮的位置
            brandLogo.isSwitch = true;
        } else if (i !== 0 && i % showNum === 0) {

            // 插入more的位置,more占的是下一页第一个brand的位置,所以page是i/17
            brandLogo.morePageNum = Math.floor(i / (showNum + 1));
            brandLogo.isMore = true;
            brandLogo.hasNext = true;
        } else {
            brandLogo.isBrand = true;
        }

        brandLogos.push(brandLogo);
    }
    brandLogos.push({
        morePageNum: Math.floor(i / (showNum + 1)),
        isMore: true
    });
    return brandLogos;
};
liangxs authored
218
biao authored
219
/**
hongweigao authored
220
 * 获取最新速报模板数据 : 大图+小图+大图+single_image(广告位)/+floor(logo列表)
biao authored
221 222 223
 * @param {Object} srcData 原始数据
 * @return {Object} 转换后的数据
 */
224
const _getNewReportFloorData = (args) => {
biao authored
225
    const title = args[0].data.text;
hongweigao authored
226
    let floorId = args[0].template_id;
biao authored
227 228 229
    let item = args[1].data;
    let secondItem = args[2].data;
    let thirdItem = args[3].data;
230
    let forthItem = args[4] || {};
hongweigao authored
231
    let brandLogoObj = {showNum: 10};
liangxs authored
232
biao authored
233
    let list = [];
liangxs authored
234
biao authored
235
    const data = {
biao authored
236
        newReport: {
biao authored
237
            name: title,
hongweigao authored
238
            floorId: floorId,
biao authored
239
            list: []
liangxs authored
240
        }
biao authored
241 242 243
    };

biao authored
244 245
    let adData;
    let floorDatas = [];
biao authored
246
biao authored
247
    list.push(item[0]);
biao authored
248 249

    _.forEach(secondItem, (floorData) => {
biao authored
250
        list.push(floorData);
liangxs authored
251 252
    });
biao authored
253
    list.push(thirdItem[0]);
biao authored
254 255 256

    data.newReport.list = list;
biao authored
257 258
    floorDatas.push(data);
hongweigao authored
259 260
    // 广告位
    if (forthItem.template_name && forthItem.template_name === 'single_image') {
biao authored
261
        adData = _getadbannerData(forthItem.data[0]);
biao authored
262 263 264
        floorDatas.push(adData);
    }
hongweigao authored
265 266
    // logo列表
    if (forthItem.template_name && forthItem.template_name === 'floor') {
267
        // 品牌数据
hongweigao authored
268
        if (_.get(args[5], 'data[0].url', '')) {
269
            brandLogoObj.moreBrand = args[5].data[0].url || '';
270 271
        }
272
        brandLogoObj.brandLogos = _getBrandLogosData(forthItem.data, brandLogoObj.showNum);
hongweigao authored
273
        data.newReport.brandLogoObj = brandLogoObj;
hongweigao authored
274
    }
biao authored
275
    return floorDatas;
liangxs authored
276 277
};
biao authored
278 279 280 281 282 283 284

/**
 * 给目标对象绑定频道属性
 * @param {Object} obj 需要绑定频道属性的对象
 * @param {String} type 需要设置的频道类型
 * @return undefined
 */
biao authored
285
const _setChannelType = (obj, type) => {
biao authored
286 287 288
    obj[type + 'Channel'] = true;
};
biao authored
289 290 291 292 293 294
/**
 * 获取优选品牌模板数据
 * @param {[Object]} args 参数列表
 * @param {String} type 频道类型
 * @return {Object}
 */
biao authored
295
const _getPreBrandTopData = (args, type) => {
biao authored
296
    const title = args[0].data.text;
297 298
    let item = args[1].data,
        logoItems = args[2] || {},
299 300 301 302
        showNum = type === 'boys' ? 16 : 10;


    let brandLogoObj = {showNum: showNum};
biao authored
303 304 305

    const data = {
        preferenceBrands: {
biao authored
306
            name: title,
307
            imgBrand: []
biao authored
308 309 310
        }
    };
311
    // logo列表
312
    if (logoItems) {
313
        // 品牌数据
hongweigao authored
314
        if (_.get(args[3], 'data[0].url', '')) {
315
            brandLogoObj.moreBrand = args[3].data[0].url;
316
        }
317
        brandLogoObj.brandLogos = _getBrandLogosData(args[2].data, showNum);
hongweigao authored
318
        data.preferenceBrands.brandLogoObj = brandLogoObj;
319
    }
biao authored
320
    data.preferenceBrands.imgBrand = item;
biao authored
321
    if (type === 'kids') {
322
        data.preferenceBrands.sliderColor = '#7AD3F8';
biao authored
323
    }
biao authored
324
    _setChannelType(data.preferenceBrands, type);
biao authored
325
biao authored
326 327 328
    return data;
};
biao authored
329 330 331 332 333 334
/**
 * 获取热门分类模板数据
 * @param {[Object]} args 参数列表
 * @param {String} type 频道类型
 * @return {Object}
 */
biao authored
335
const _getHotGoodsFloorData = (args, type) => {
biao authored
336 337 338
    let item = args[0];
    let nextItem = args[1];
liangxs authored
339 340
    let list = [];
biao authored
341 342 343 344 345 346 347
    let object = {},
        keyword = [],
        category = [],
        brands = [],
        types = [],
        products = [];
biao authored
348
    const data = {
biao authored
349 350 351 352 353
        recommend: {
            tplrecommend: []
        }
    };
biao authored
354
    category = item.data.menuNav.list;
biao authored
355
biao authored
356
    keyword = item.data.menuNav.blocks;
biao authored
357 358 359

    _.forEach(item.data.imgs, (it, idx) => {
biao authored
360
        if (idx === 0 || (idx === 4 && type === 'boys')) {
biao authored
361
            brands.push(it);
biao authored
362
        } else {
biao authored
363
            types.push(it);
liangxs authored
364
        }
biao authored
365 366
    });
biao authored
367
    products = nextItem.data;
biao authored
368 369 370 371 372 373

    object.name = item.data.name;
    object.keyword = keyword;
    object.category = category;
    object.brands = brands;
    object.types = types;
biao authored
374
    object.navs = item.data.navs.list;
biao authored
375 376 377 378
    object.products = products;
    list.push(object);
    data.recommend.tplrecommend = list;
biao authored
379
    _setChannelType(data.recommend, type);
biao authored
380 381

biao authored
382
    return data;
liangxs authored
383 384
};
hongweigao authored
385 386 387 388 389 390 391
/**
 * 获取japanKorean潮流上装Jacket,title+左大图+右6小图模板数据
 * @param {[Object]} args 参数列表
 * @param {String} type 频道类型
 * @return {Object}
 */
const _getJacketData = (arg) => {
hongweigao authored
392
    let resData = {
hongweigao authored
393 394 395 396 397
        tpltopic: {
            bigPic: arg.big_image[0],
            types: arg.list
        }
    };
hongweigao authored
398 399

    if (arg.title.title !== '') {
hongweigao authored
400 401 402 403
        resData.tpltopic.title = arg.title;
    }

    return resData;
hongweigao authored
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
};

/**
 * 获取7个品类图
 * @param {[Object]} args 参数列表
 * @param {String} type 频道类型
 * @return {Object}
 */
const _getClothesCategory = (arg) => {
    return {
        sevenCategory: {
            categorys: arg.list
        }
    };
};
biao authored
419
biao authored
420 421 422 423 424 425
/**
 * 获取boys人气单品模版数据
 * @param {[Object]} args 参数列表
 * @param {String} type 频道类型
 * @return {Object}
 */
biao authored
426
const _getBoysSingleHot = (args, type) => {
biao authored
427
    const len = 10;
biao authored
428
    const data = {
biao authored
429
        singlehot: {
biao authored
430
            name: args[0].data.text,
biao authored
431 432 433 434
            imgHot: []
        }
    };
biao authored
435
    let list = [];
biao authored
436 437 438 439 440 441 442 443
    let adData;
    let floorDatas = [];

    for (let i = 0; i < len; i++) {
        let pos = i;
        let val = {};

        if (i === 1) {
biao authored
444
            val = args[1].data[0]; // 第二个是大图
biao authored
445
        } else if (i === len - 1) {
biao authored
446
            val = args[1].data[1]; // 最后一个是大图
biao authored
447 448 449
        } else {
            if (pos > 1) { // 小图
                pos = pos - 1;
cailing authored
450
            }
biao authored
451
            val = args[2].data[pos];
cailing authored
452
        }
biao authored
453
        list.push(val);
biao authored
454 455
    }
biao authored
456 457 458 459 460 461
    _.forEach(list, (item, index) => {
        if (index === 1 || index === list.length - 1) {
            item.big = true;
        }
    });
biao authored
462
    data.singlehot.imgHot = list;
biao authored
463
    _setChannelType(data.singlehot, type);
biao authored
464 465
    floorDatas.push(data);
biao authored
466
    if (args[3].template_name === 'single_image') {
biao authored
467
        adData = _getadbannerData(args[3].data[0]);
biao authored
468 469 470 471
        floorDatas.push(adData);
    }

    return floorDatas;
cailing authored
472 473
};
biao authored
474 475 476 477 478 479
/**
 * 获取girls人气单品模版数据
 * @param {[Object]} args 参数列表
 * @param {String} type 频道类型
 * @return {Object}
 */
480
const _getGirlsSingleHot = (args, type) => {
biao authored
481
    let goods = args[2] && args[2].data;
biao authored
482
    let skns = '';
biao authored
483
biao authored
484 485 486 487 488 489 490
    if (goods) {
        _.forEach(goods, good => {
            skns += good.id + ' ';
        });
    } else {
        return;
    }
biao authored
491
周少峰 authored
492
    return searchApi.getProductList({
493 494 495 496 497 498 499 500
        client_type: 'web',
        query: skns,
        order: 'shelve_time:desc',
        status: 1,
        sales: 'Y',
        attribute_not: '2',
        stocknumber: 1,
        page: 1,
501
        gender: channelMap[type].gender,
yyq authored
502
        limit: 60
htoooth authored
503
    }, config.apiCache);
biao authored
504 505
};
506
biao authored
507 508 509 510 511 512
/**
 * 人气单品入口
 * @param {[Object]} args 参数列表
 * @param {String} type 频道类型
 * @return {Object}
 */
biao authored
513
const _getSingleHotFloorData = (args, type) => {
biao authored
514
    if (type === 'boys') {
biao authored
515
        return _getBoysSingleHot(args, type);
516
    } else {
biao authored
517
        return _getGirlsSingleHot(args, type);
biao authored
518 519 520
    }
};
biao authored
521
/**
hongweigao authored
522 523 524 525 526 527 528 529 530 531
 * 组装最新上架楼层数据
 *
 * @param string data
 * return obj
 */
const _getNewGoodsFloorData = args => {
    const data = {};

    if (!_.isEmpty(args)) {
        data.newArrivls = {
hongweigao authored
532
            floorId: args[0].template_id,
yyq authored
533
            name: _.replace(args[0].data.text, '最新', '新品'),
hongweigao authored
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
            navs: []
        };

        data.newArrivls.navs = args[1] ? args[1].data : {};
    }
    return data;
};

/**
 * 文本标题
 * @param {[Object]} args 参数列表
 * @param {String} type 频道类型
 * @return {Object}
 */
const _getFloorTitleData = (args) => {
    let resData = {
        floorHeader: {}
    };

    if (args instanceof Array) {
        resData.floorHeader.name = args[0].data.text;
        resData.floorHeader.navs = args[1].data;
hongweigao authored
556
        resData.floorHeader.floorId = args[0].template_id;
hongweigao authored
557 558
    } else {
        resData.floorHeader.name = args.data.text;
hongweigao authored
559
        resData.floorHeader.floorId = args.template_id;
hongweigao authored
560 561 562 563 564 565 566 567 568
    }

    if (resData.floorHeader.name.indexOf('最新上架') >= 0) {
        resData = _getNewGoodsFloorData(args);
    }
    return resData;
};

/**
biao authored
569 570
 * 处理异步获取的人气单品数据
 * @param {[Object]} args 参数列表
biao authored
571
 * @param {String} originQuery 原始skns
biao authored
572 573 574 575
 * @param {Object} queryResult 异步获取的数据
 * @param {String} type 频道类型
 * @return {Object}
 */
biao authored
576 577
const _getSingehotViaResult = (args, originQuery, queryResult, type) => {
    let data = {
578 579 580 581 582 583 584 585
        singlehot: {
            name: args[0].data.text,
            navs: [],
            imgHot: [],
            brands: []
        }
    };
biao authored
586 587 588 589 590 591 592
    let pos = 0;


    const getIndex = skn => {
        let index;

biao authored
593 594
        if (originQuery) {
            const originQueryArr = originQuery.split(' ');
biao authored
595
            let setFlag = false;
biao authored
596 597

            _.forEach(originQueryArr, (query, idx) => {
biao authored
598
                if (!setFlag && query.toString() === skn.toString()) {
biao authored
599
                    index = idx;
biao authored
600
                    setFlag = true;
biao authored
601 602 603
                }
            });
        }
biao authored
604 605 606 607

        return index;
    };
608
    if (args[3].template_name === 'app_icon_list') {
biao authored
609
        data.singlehot.brands = args[3].data;
610 611
    }
biao authored
612
    if (queryResult.data) {
biao authored
613
        _.forEach(queryResult.data.product_list || [], it => {
biao authored
614 615
            const formatData = processProduct(it, {
                width: 280,
biao authored
616 617
                height: 373,
                gender: channelMap[type].gender
biao authored
618
            });
619
biao authored
620
            if (pos > 11) {
biao authored
621 622
                return;
            }
623
biao authored
624 625
            if (it.product_id) {
                formatData.idx = getIndex(it.product_skn);
biao authored
626 627 628 629
                if (formatData.idx === 0 || formatData.idx) {
                    data.singlehot.imgHot.push(formatData);
                    pos += 1;
                }
biao authored
630
            }
biao authored
631 632
        });
    }
biao authored
633 634 635 636 637 638 639 640 641 642 643 644

    data.singlehot.imgHot = _.sortBy(data.singlehot.imgHot, item => {
        return item.idx;
    });

    pos = 1;
    _.forEach(data.singlehot.imgHot, item => {
        if (pos < 4 && item) {
            item.tip = 'TOP' + pos;
            pos += 1;
        }
    });
biao authored
645
    data.singlehot.navs = args[1].data;
biao authored
646
    _setChannelType(data.singlehot, type);
647 648 649
    return data;
};
biao authored
650 651 652 653
/**
 * 异步获取人气单品
 * @param {[Object]} rawData 接口返回的原始数据
 * @param {[Object]} floorData 已经经过处理的楼层数据
biao authored
654
 * @param {String} originQuery 原始的query数据
biao authored
655 656 657 658 659
 * @param {Object} queryResult 接口中用于请求人气单品的数据
 * @param {String} title 人气单品楼层的标题
 * @param {Type} type 人气单品楼层的类型
 * @return {Object}
 */
biao authored
660
const _processFloorDataWithQueryReusult = (rawData, originQuery, queryResult, title, type) => {
661 662 663
    let data = {};

    _.forEach(rawData, (subData, index) => {
hongweigao authored
664 665

        if (subData && subData.data && subData.data.text) {
hongweigao authored
666
            const text = subData.data.text && _getText(subData.data.text);
667
biao authored
668
hongweigao authored
669 670 671
            if (text === title) {
                data = _getSingehotViaResult(rawData.slice(index, index + 4), originQuery, queryResult, type);
            }
672 673 674 675
        }
    });

    return data;
676
};
677
678
liangxs authored
679
biao authored
680
const _getCategoryFloorData = args => {
biao authored
681 682 683
    const data = {
        category: {
            name: args[0].data.text,
biao authored
684
            navs: args[1].data,
biao authored
685 686 687 688 689 690 691 692
            list: []
        }
    };

    _.forEach(args[2].data, (it, index) => {

        // 设置图片大小
        if (index === 1) {
biao authored
693 694
            it.w = '377;';
            it.h = '504;';
biao authored
695
        } else {
biao authored
696 697
            it.w = '185';
            it.h = '510';
biao authored
698 699
        }
biao authored
700
        data.category.list.push(it);
biao authored
701 702 703 704 705
    });

    return data;
};
biao authored
706
const _getAccordionFloorData = args => {
biao authored
707 708 709
    let data = {
        accordion: {
            name: args[0].data.text,
biao authored
710
            navs: args[1].data,
biao authored
711 712 713 714
            slide: []
        }
    };
biao authored
715
    data.accordion.slide = args[2].data;
biao authored
716 717 718 719

    return data;
};
hongweigao authored
720
const _requestContent = (type, params, code) => {
毕凯 authored
721 722
    let data = {
        client_type: 'web',
hongweigao authored
723
        content_code: code || channelMap[type || 'boys'].code,
liangxs authored
724 725 726
        gender: channelMap[type || 'boys'].gender,
        page: 1,
        limit: 1000
毕凯 authored
727
    };
liangxs authored
728
729
    Object.assign(data, params);
730
    return serviceApi.get('operations/api/v5/resource/home', data, {
biao authored
731 732 733 734
        cache: true,
        code: 200
    }).then(result => {
        return result;
biao authored
735
    });
liangxs authored
736 737
};
biao authored
738
const floorMap = {
biao authored
739 740 741 742 743 744
    slide: _getSlideData,
    hot: _getHotGoodsFloorData,
    最新速报: _getNewReportFloorData,
    人气单品: _getSingleHotFloorData,
    'GIRL KIDS': _getSingleHotFloorData,
    'BOY KIDS': _getSingleHotFloorData,
biao authored
745
    ACCESSORIES: _getSingleHotFloorData,
biao authored
746 747 748 749 750
    优选品牌: _getPreBrandTopData,
    最新上架: _getNewGoodsFloorData,
    ad: _getadbannerData,
    category: _getCategoryFloorData,
    accordion: _getAccordionFloorData,
hongweigao authored
751
    debrisSlide: _getDebrisSlide,
hongweigao authored
752 753 754
    jacket: _getJacketData,
    sevenCategory: _getClothesCategory,
    floorTitle: _getFloorTitleData,
hongweigao authored
755
    newuser: _getNewUserFloorData
biao authored
756 757
};
758
biao authored
759 760 761 762 763
const needQuery = {
    'GIRL KIDS': true,
    'BOY KIDS': true,
    人气单品: true,
    ACCESSORIES: true
biao authored
764
};
biao authored
765 766 767 768 769 770 771 772 773 774 775 776

const getQuery = args => {
    let goods = args[2] && args[2].data;
    let skns = '';

    if (goods) {
        _.forEach(goods, good => {
            skns += good.id + ' ';
        });
    }

    return skns;
biao authored
777
};
biao authored
778
779
hongweigao authored
780
const _processFloorData = (rawData, type) => {
biao authored
781
    let floorList = [];
782 783
    let searchPromise = [];
    let singlehotFloorIndex = [];
biao authored
784
    let singlehotFloorTitle = [];
biao authored
785
    let params = [];
hongweigao authored
786
    let hasNewUser = false;
biao authored
787
biao authored
788
    // 定义各种楼层需要用到的接口返回的数组中元素的个数
biao authored
789 790 791 792
    const bigFloorLength = 5;
    const normalFloorLength = 3;
    const hotCategoryLength = 2;
biao authored
793
    _.forEach(rawData, (data, index) => {
biao authored
794
        let floorData = null;
biao authored
795
        let queryParam = '';
biao authored
796
hongweigao authored
797
        if (data && data.template_name === 'recommend_content_three' ||
htoooth authored
798
            (data.template_intro === '焦点图' && index === 0)) { // 处理banner
biao authored
799
            floorData = floorMap.slide.call(null, data.data);
hongweigao authored
800
        } else if (data && data.template_intro === '热门品类') { // 处理热门品类
biao authored
801
            floorData = floorMap.hot.call(null, rawData.slice(index, index + hotCategoryLength), type);
hongweigao authored
802
        } else if (data && data.data && data.data.text) { // 处理一般楼层
biao authored
803
            let text = _getText(data.data.text);
biao authored
804
            let lastIndex = index + bigFloorLength < rawData.length ?
htoooth authored
805
            index + bigFloorLength : index + (rawData.length - index - 1);
biao authored
806
hongweigao authored
807 808
            floorData = floorMap[text] &&
                floorMap[text].call(null, rawData.slice(index, lastIndex), type);
biao authored
809
biao authored
810 811
            if (needQuery[text]) {
                queryParam = getQuery(rawData.slice(index, lastIndex));
biao authored
812 813 814
                if (queryParam.length > 0) {
                    params.push(queryParam);
                }
biao authored
815 816
            }
hongweigao authored
817
        } else if (data && data.template_name === 'debrisSlider') { // 处理girls的banner
biao authored
818
            floorData = floorMap.debrisSlide.call(null, data.data);
hongweigao authored
819
        } else if (data.template_name === 'new_user_floor' || data.template_intro === '新人专享') {
hongweigao authored
820 821 822 823 824
            if (!hasNewUser) {
                floorData = floorMap.newuser.call(null, data.data);
                hasNewUser = true;
            }
biao authored
825 826
        }
biao authored
827
        // 处理lifestyle分类楼层
biao authored
828 829
        if (!floorData && index + normalFloorLength < rawData.length &&
            rawData[index + 1].template_name === 'textNav' &&
biao authored
830 831
            rawData[index + 2].template_name === 'floor') {
            floorData = floorMap.category.call(null, rawData.slice(index, index + normalFloorLength));
biao authored
832 833 834
        }

        // 处理手风琴楼层
biao authored
835 836
        if (!floorData && index + normalFloorLength < rawData.length &&
            rawData[index + 1].template_name === 'textNav' &&
biao authored
837 838
            rawData[index + 2].template_name === 'focus') {
            floorData = floorMap.accordion.call(null, rawData.slice(index, index + normalFloorLength));
biao authored
839
        }
biao authored
840
841
biao authored
842
        // 处理promise
843 844 845 846 847 848 849
        if (floorData && floorData.then && typeof floorData.then === 'function') {
            searchPromise.push(floorData);

            // 记住楼层位置, 以便后面promise获取数据后插入楼层数据
            singlehotFloorIndex.push(floorList.length);

            // 记住楼层标题, 以便后面promise获取数据后插入楼层数据
biao authored
850
            singlehotFloorTitle.push(_getText(data.data.text));
851
        } else if (!_.isNil(floorData)) {
biao authored
852 853 854 855
            _.isArray(floorData) ?
                floorList = floorList.concat(floorData) :
                floorList.push(floorData);
        }
biao authored
856 857
    });
858 859 860
    return {
        floors: floorList,
        promise: _.reverse(searchPromise),
biao authored
861
        queryParams: _.reverse(params),
biao authored
862
        singlehotFloorTitle: _.reverse(singlehotFloorTitle),
863
        singlehotFloorIndex: _.reverse(singlehotFloorIndex)
864
    };
biao authored
865 866
};
hongweigao authored
867
const _processJKFloorData = (rawData) => {
hongweigao authored
868 869 870 871 872 873 874 875 876 877 878 879 880
    let floorList = [];
    let searchPromise = [];
    let singlehotFloorIndex = [];
    let singlehotFloorTitle = [];
    let params = [];


    _.forEach(rawData, (data, index) => {
        let floorData = null;

        if (data && data.template_name === 'recommend_content_three' ||
            (data.template_intro === '焦点图' && index === 0)) { // 处理banner
            floorData = floorMap.slide.call(null, data.data);
hongweigao authored
881
            floorData.slide.floorId = data.template_id;
hongweigao authored
882 883 884 885 886
        } else if (data && data.data && data.data.text) { // 处理一般楼层
            let text = _getText(data.data.text);

            // 最新速报
            if (text.indexOf('最新速报') >= 0) {
887
                floorData = _getNewReportFloorData(rawData.slice(index, index + 6));
hongweigao authored
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907

                // 单个图作为最新速报模块用过的,标识一下,不作为广告位了
                _.filter(rawData.slice(index, index + 5), function(o) {
                    if (o.template_name === 'single_image') {
                        o.hasUser = true;
                    }
                });
            } else { // 文本

                let href = rawData[index + 1].template_name === 'textNav' ?
                    rawData.slice(index, index + 2) : data;

                floorData = floorMap.floorTitle.call(null, href);
            }

        } else if (data && data.template_name === 'jk_floor') { // 左1大图,右6小图,title
            floorData = floorMap.jacket.call(null, data.data);
        } else if (data && data.template_name === 'recommend_content_five') {
            // 1标题12图,在日韩馆频道页,作为7个品类显示
            floorData = floorMap.sevenCategory.call(null, data.data);
hongweigao authored
908
            floorData.sevenCategory.floorId = data.template_id;
hongweigao authored
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
        } else if (data && data.template_name === 'single_image') {
            if (!data.hasUser) {
                // 一张图,在日韩馆频道页,做广告位
                floorData = floorMap.ad.call(null, data.data);
            }
        }

        // 处理promise
        if (floorData && floorData.then && typeof floorData.then === 'function') {
            searchPromise.push(floorData);

            // 记住楼层位置, 以便后面promise获取数据后插入楼层数据
            singlehotFloorIndex.push(floorList.length);

            // 记住楼层标题, 以便后面promise获取数据后插入楼层数据
            singlehotFloorTitle.push(_getText(data.data.text));
        } else if (!_.isNil(floorData)) {
            _.isArray(floorData) ?
                floorList = floorList.concat(floorData) :
                floorList.push(floorData);
        }


    });

    return {
        floors: floorList,
        promise: _.reverse(searchPromise),
        queryParams: _.reverse(params),
        singlehotFloorTitle: _.reverse(singlehotFloorTitle),
        singlehotFloorIndex: _.reverse(singlehotFloorIndex)
    };
};
943
const _formatResourceParams = (channel, code) => {
htoooth authored
944
    return serviceApi.get('operations/api/v5/resource/get', {content_code: code}, config.apiCache).then(data => {
biao authored
945
        let result = data && data.data[0] && data.data[0].data[0];
biao authored
946 947 948 949 950 951

        if (result) {
            result.channel = channel;
        }

        return result || {};
runner authored
952 953 954 955 956 957 958 959 960 961 962 963 964
    });
};

/**
 * 频道页首次登陆导航并行调接口数据
 * @param  {Object} data
 * @return {Object} formatData
 */
const _formatParams = (channel, data) => {
    let params = {};

    // 排序数据映射表
    let orderMaps = {
biao authored
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
        s_t_desc: 'shelve_time:desc',
        s_t_asc: 'shelve_time:asc',
        s_p_asc: 'sales_price:asc',
        s_p_desc: 'sales_price:desc',
        p_d_desc: 'discount:desc',
        p_d_asc: 'discount:asc',
        skn_desc: 'product_skn:desc',
        skn_asc: 'product_skn:asc',
        activities_desc: 'activities.order_by:desc',
        activities_asc: 'activities.order_by:asc',
        s_n_asc: 'sales_num:asc',
        s_n_desc: 'sales_num:desc',
        activities_id_desc: 'activities.activity_id:desc',
        activities_id_asc: 'activities.activity_id:asc',
        brand_desc: 'brand_weight:desc'
runner authored
980 981 982 983 984 985
    };

    params.status = 1; // 是否上架,1表示在架,2表示不在
    params.sales = 'Y'; // 只搜索销售的产品
    params.outlets = 2; // 非奥莱商品
    params.stocknumber = 1; // 过滤掉已售罄的商品
biao authored
986
    params.attribute_not = 2; // 过滤掉赠品
runner authored
987 988 989 990 991 992 993 994 995
    if (!data.order) {
        params.order = orderMaps.s_t_desc;
    } else {
        params.order = orderMaps[data.order] ? orderMaps[data.order] : '';
    }
    if (!data.page) {
        params.page = 1;
    }
biao authored
996
    if (data.viewNum) {
runner authored
997
        params.viewNum = data.viewNum;
biao authored
998
    } else if (!data.limit) {
runner authored
999 1000 1001 1002 1003 1004 1005 1006
        params.viewNum = 60;
    } else {
        params.viewNum = data.limit;
        delete data.limit;
    }
    if (data) {
        params = Object.assign(data);
    }
yyq authored
1007
周少峰 authored
1008
    return searchApi.getProductList(params).then(result => {
biao authored
1009
        let ret = result.data;
runner authored
1010
1011 1012 1013
        if (ret) {
            ret.channel = channel;
        }
biao authored
1014 1015

        return ret;
runner authored
1016
    });
biao authored
1017
};
runner authored
1018 1019 1020 1021 1022 1023

/**
 * 格式化频道页首次登陆导航数据
 * @param  {Object} data
 * @return {Object} formatData
 */
runner authored
1024
const formatIndexGuideData = data => {
runner authored
1025
    let channels = {
biao authored
1026 1027 1028 1029
        boys: {gender: '1,3', limit: 1},
        girls: {gender: '2,3', limit: 1},
        lifestyle: {msort: 10, misort: '266,280,101,103,259', limit: 1},
        kids: {msort: 365, limit: 1}
runner authored
1030 1031 1032 1033 1034 1035 1036 1037
    };
    let formatData = [];
    let promiseArr = [];

    _.forEach(data, item => {
        let channel = item.sort_name_en.toLowerCase().replace(' ', '');

        formatData.push({
biao authored
1038 1039 1040 1041 1042 1043 1044
            channel: channel,
            url: item.sort_url,
            sort_name: item.sort_name,
            sort_name_en: item.sort_name_en,
            content_code: item.content_code,
            src: '',
            num: 0
runner authored
1045 1046 1047
        });

        if (channels[channel]) {
biao authored
1048
            promiseArr.push(_formatParams(channel, channels[channel]));
runner authored
1049
        }
biao authored
1050
    });
runner authored
1051
biao authored
1052
    return Promise.all(promiseArr).then(result => {
徐祁xuqi authored
1053 1054 1055 1056
        _.forEach(formatData, item => {
            _.forEach(result, it => {
                if (item && it && item.channel === it.channel) {
                    item.num = it.total;
runner authored
1057
                }
biao authored
1058 1059
            });
        });
runner authored
1060 1061 1062

        return formatData;
    });
biao authored
1063 1064
};
biao authored
1065 1066 1067 1068 1069 1070
/**
 * 获取最新上架商品数据
 *
 * @param string $channel
 * @return array
 */
hongweigao authored
1071
const getNewArrival = (channel, isJKChannel, poolId)=> {
biao authored
1072 1073
    let rel = [],
        sortList = sortMap[channel],
hongweigao authored
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
        params = {},
        method = '';



    if (isJKChannel) {
        method = 'app.search.sales';
        params = {
            productPool: poolId
        };
hongweigao authored
1084
hongweigao authored
1085 1086 1087 1088 1089 1090
        // 没有商品池id,不查数据
        if (!poolId) {
            return Promise.all(rel).then();
        }
    } else {
        method = 'web.search.newshelve';
biao authored
1091 1092 1093 1094 1095 1096
        params = {
            order: 'shelve_time:desc',
            status: 1,
            sales: 'Y',
            attribute_not: 2,
            stocknumber: 3,
周少峰 authored
1097
            dayLimit: 4
biao authored
1098
        };
hongweigao authored
1099
    }
biao authored
1100 1101

hongweigao authored
1102
    params.gender = channelMap[channel].gender;
biao authored
1103 1104

    _.forEach(sortList, (item) => {
yyq authored
1105
        let data = Object.assign(item, params, {limit: item.viewNum});
biao authored
1106
周少峰 authored
1107
        rel.push(api.get('', Object.assign({
hongweigao authored
1108
            method: method
周少峰 authored
1109
        }, data), {cache: true}));
biao authored
1110 1111 1112 1113 1114 1115 1116
    });

    return Promise.all(rel).then(res => {
        let data = [],
            result = [];

        _.forEach(sortList, (it, index) => {
yyq authored
1117
            let list = _.get(res[index], 'data.product_list', []);
biao authored
1118
yyq authored
1119 1120
            if (list.length === sortList[index].viewNum) {
                data = data.concat(list);
biao authored
1121 1122 1123 1124 1125 1126
            }
        });

        _.forEach(data, (item) => {
            result.push(processProduct(item, {
                width: 280,
runner authored
1127 1128
                height: 373,
                gender: params.gender
biao authored
1129 1130 1131 1132 1133 1134 1135
            }));
        });
        return result;
    });
};

biao authored
1136 1137
/**
 * 获取频道页数据
biao authored
1138 1139
 * @param  {String} type 传入频道页类型,值可以是: boys, girls, kids, lifestyle
 * @return {Object}
biao authored
1140
 */
郝肖肖 authored
1141
const getContent = (type, isJKChannel, poolId) => {
hongweigao authored
1142
hongweigao authored
1143
    let params = {new_device: 'Y'};
1144
郝肖肖 authored
1145 1146 1147 1148 1149
    return Promise.all([
        headerModel.requestHeaderData(type),
        _requestContent(type, params),
        getNewArrival(type, isJKChannel, poolId)
    ]).then(res => {
liangxs authored
1150
biao authored
1151
        let headerData = res[0].data || res[0],
1152
            contentData = res[1].data ? res[1].data.list : res[1];
liangxs authored
1153
biao authored
1154
        let data = {};
liangxs authored
1155
hongweigao authored
1156
        const processResult = _processFloorData(contentData, type);
1157
biao authored
1158 1159 1160 1161 1162
        data = headerData;
        data.module = 'channel';
        data.page = 'channel';
        data.pageType = type;
        data.footerTop = true;
郝肖肖 authored
1163 1164 1165 1166 1167
        data.channel = processResult.floors.map(function(elem) {

            if (elem.newArrivls) {
                elem.newArrivls.goods = res[2];
            }
biao authored
1168
郝肖肖 authored
1169 1170
            return elem;
        });
1171
biao authored
1172 1173 1174 1175 1176
        return {
            rawData: contentData,
            floorData: data,
            searchPromise: processResult.promise,
            singlehotFloorIndex: processResult.singlehotFloorIndex,
biao authored
1177
            singlehotFloorTitle: processResult.singlehotFloorTitle,
biao authored
1178
            queryParams: processResult.queryParams,
biao authored
1179 1180
            channelType: type
        };
1181 1182 1183 1184 1185 1186 1187 1188 1189

    }).then(result => {

        // 如果有promise则做相应处理
        if (result.searchPromise.length) {
            return Promise.all(result.searchPromise).then(res => {
                _.forEach(res, (data, index) => {
                    result.floorData.channel
                        .splice(result.singlehotFloorIndex[index], 0,
biao authored
1190
                            _processFloorDataWithQueryReusult(
biao authored
1191
                                result.rawData,
biao authored
1192
                                result.queryParams[index],
biao authored
1193 1194 1195 1196
                                data,
                                result.singlehotFloorTitle[index],
                                result.channelType
                            ));
1197 1198 1199
                });

                return result.floorData;
1200
            });
biao authored
1201
        }
郝肖肖 authored
1202 1203

        return result.floorData || result;
biao authored
1204 1205
    });
};
biao authored
1206
hongweigao authored
1207 1208 1209 1210 1211 1212
/**
 * 获取日韩馆频道页数据
 * @param  {String} type 传入频道页类型,值可以是: boys, girls, kids, lifestyle
 * @return {Object}
 */
const getJKContent = (req) => {
郝肖肖 authored
1213
    let channel = req.query.channel || req.yoho.channel || 'boys',
hongweigao authored
1214 1215
        contentCode = req.query.content_code;
郝肖肖 authored
1216 1217 1218 1219 1220
    return Promise.all([
        headerModel.requestHeaderData(channel),
        _requestContent(channel, {}, contentCode),
        getNewArrival(channel, true, req.query.template_id)
    ]).then(res => {
hongweigao authored
1221
郝肖肖 authored
1222 1223
        let headerData = res[0].data || res[0],
            contentData = res[1].data ? res[1].data.list : res[1];
hongweigao authored
1224
郝肖肖 authored
1225
        let data = {};
hongweigao authored
1226
郝肖肖 authored
1227
        const processResult = _processJKFloorData(contentData);
hongweigao authored
1228
郝肖肖 authored
1229 1230 1231 1232 1233 1234
        data = headerData;
        data.module = 'channel';
        data.page = 'channel';
        data.pageType = channel;
        data.mchannel = 'japanKorean';// 子频道,如日韩馆
        data.footerTop = true;
1235 1236 1237 1238 1239 1240 1241 1242
        data.channel = processResult.floors.map(function(elem) {

            if (elem.newArrivls) {
                elem.newArrivls.goods = res[2];
            }

            return elem;
        });
biao authored
1243
郝肖肖 authored
1244 1245 1246 1247 1248 1249 1250 1251 1252
        return {
            rawData: contentData,
            floorData: data,
            searchPromise: processResult.promise,
            singlehotFloorIndex: processResult.singlehotFloorIndex,
            singlehotFloorTitle: processResult.singlehotFloorTitle,
            queryParams: processResult.queryParams,
            channelType: channel
        };
hongweigao authored
1253
郝肖肖 authored
1254
    }).then(result => {
hongweigao authored
1255 1256

        // 如果有promise则做相应处理
郝肖肖 authored
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
        if (result.searchPromise.length) {
            return Promise.all(result.searchPromise).then(res => {
                _.forEach(res, (data, index) => {
                    result.floorData.channel
                    .splice(result.singlehotFloorIndex[index], 0,
                        _processFloorDataWithQueryReusult(
                            result.rawData,
                            result.queryParams[index],
                            data,
                            result.singlehotFloorTitle[index],
                            result.channelType
                        ));
                });
hongweigao authored
1270
郝肖肖 authored
1271
                return result.floorData;
hongweigao authored
1272
            });
郝肖肖 authored
1273 1274 1275 1276
        }

        return result.floorData || result;
    });
biao authored
1277
};
runner authored
1278
1279
const getResourceData = (formatData) => {
runner authored
1280 1281
    let promiseArr = [];
1282
    _.forEach(formatData, item => {
runner authored
1283
        if (item.content_code) {
biao authored
1284
            promiseArr.push(_formatResourceParams(item.channel, item.content_code));
runner authored
1285
        }
biao authored
1286
    });
1287 1288
    return Promise.all(promiseArr).then(data => {
        _.forEach(formatData, (item, index) => {
biao authored
1289 1290 1291
            _.forEach(data, (it, idx) => {
                if (formatData[index].channel === data[idx].channel) {
                    formatData[index].src = data[idx].src;
1292
                }
biao authored
1293 1294 1295
            });
        });
1296
        return formatData;
biao authored
1297 1298
    });
};
runner authored
1299 1300 1301 1302 1303 1304

/**
 * 获取频道页首次登陆导航数据
 * @param  {String} type 传入频道页类型,值可以是: boys, girls, kids, lifestyle
 * @return {Object}
 */
runner authored
1305
const getIndexGuideData = () => {
runner authored
1306 1307 1308 1309 1310
    let params = {
        client_type: 'web',
        private_key: '0ed29744ed318fd28d2c07985d3ba633'
    };
htoooth authored
1311
    return serviceApi.get('operations/api/v6/category/getCategory', params, config.apiCache);
biao authored
1312
};
runner authored
1313
hongweigao authored
1314 1315
const hasNewUserFloor = (channelType, uid) => {
    let params = {uid: uid};
hongweigao authored
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334

    return _requestContent(channelType, params).then(res => {
        let isNewUser = false,
            contentData = res.data ? res.data.list : res;

        _.forEach(contentData, (data) => {
            if (data.template_name === 'new_user_floor' || data.template_intro === '新人专享') {
                isNewUser = true;
                return false;
            }
        });

        return {
            isNewUser: isNewUser
        };
    });
};

biao authored
1335 1336 1337
module.exports = {
    getNewArrival: getNewArrival,
    getContent: getContent,
hongweigao authored
1338
    getJKContent: getJKContent,
runner authored
1339
    getIndexGuideData: getIndexGuideData,
1340
    formatIndexGuideData: formatIndexGuideData,
hongweigao authored
1341 1342
    getResourceData: getResourceData,
    hasNewUserFloor: hasNewUserFloor
runner authored
1343
biao authored
1344
};