Blame view

apps/channel/models/index.js 25.4 KB
biao authored
1 2 3 4 5 6 7
/**
 * girls model
 * @author: 赵彪<bill.zhao@yoho.cn>
 * @date: 2016/05/17
 */
'use strict';
liangxs authored
8
const _ = require('lodash');
biao authored
9 10

const ServiceAPI = require(`${global.library}/api`).ServiceAPI;
liangxs authored
11
const SearchAPI = require(`${global.library}/api`).SearchAPI;
biao authored
12
const sign = require(`${global.library}/sign`);
biao authored
13
const helpers = require(`${global.library}/helpers`);
biao authored
14
const images = require(`${global.utils}/images`);
biao authored
15
const log = require(`${global.library}/logger`);
biao authored
16
biao authored
17 18

const serviceApi = new ServiceAPI();
liangxs authored
19
const searchApi = new SearchAPI();
biao authored
20
liangxs authored
21
const headerModel = require('../../../doraemon/models/header');
biao authored
22
biao authored
23 24 25 26 27
const getShelveTime = duration => {
    let today = new Date(),
        todayMil = today.getTime().toString().substr(0, 10),
        startDayMil = (today.setMonth(today.getMonth() - duration)).toString().substr(0, 10);
biao authored
28
    return `${startDayMil},${todayMil}`;
biao authored
29
biao authored
30 31
        // `
};
biao authored
32
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
// 获取可用的标题
const getText = data => {
    let text = data.split(' ')[0];
    const regResult = /\w+/.exec(text);

    if (data === 'GIRL KIDS' || data === 'BOY KIDS') {
        return data;
    }

    if (regResult) {
        text = text.replace(regResult[0], '');
    }

    return text;
};
liangxs authored
48 49 50 51 52 53 54 55 56 57 58 59

const channelMap = {
    boys: {
        code: '79372627eee75d73afe7f9bac91e5ce6',
        gender: '1,3'
    },
    girls: {
        code: '75215008957605c05e8cd375eac4f817',
        gender: '2,3'
    },
    kids: {
        code: 'd71f4b27f2a7229fbb31a4bc490a6f36',
biao authored
60
        gender: '2,3'
liangxs authored
61 62 63
    },
    lifestyle: {
        code: '8a341ca7eacc069ba80f02dec80eaf34',
64
65 66
        // code: '380c38155fd8beee10913a3f5b462da6',
        // code: '665f7c2fb9d037ee820766953ee34bf7',
biao authored
67
        gender: '2,3'
liangxs authored
68 69 70
    }
};
liangxs authored
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
const sortMap = {
    boys: [
        {sort: 147, viewNum: 5}, // 休闲运动鞋
        {sort: 129, viewNum: 5}, // 休闲裤
        {sort: 152, viewNum: 5}, // 双肩包
        {misort: 11, viewNum: 5}, // T恤
        {sort: 115, viewNum: 5}, // 衬衫
        {sort: 130, viewNum: 5}, // 牛仔裤
        {misort: 60, viewNum: 5}, // 帽子
        {sort: 124, viewNum: 5}, // 夹克
        {sort: 119, viewNum: 5}, // 卫衣
        {sort: 162, viewNum: 5}, // 手表
        {sort: 148, viewNum: 5}, // 靴子
        {misort: 65, viewNum: 5}, // 首饰
        {sort: 151, viewNum: 5}, // 时装鞋
        {misort: 61, viewNum: 5}, // 太阳镜
        {misort: 39, viewNum: 5}, // 袜子
        {sort: 346, viewNum: 5}, // 运动裤
        {sort: 131, viewNum: 5}, // 短裤
        {misort: 66, viewNum: 5}, // 配饰
        {misort: 309, viewNum: 5}, // 内裤
        {misort: 30, viewNum: 5}, // 打底裤/紧身裤
        {sort: 342, viewNum: 5} // 邮差包
    ],
    girls: [
        {misort: 16, viewNum: 4}, // 卫衣
        {misort: 12, viewNum: 4}, // 衬衫
        {misort: 44, viewNum: 4}, // 休闲/运动鞋
        {misort: 11, viewNum: 4}, // T恤
        {misort: 21, viewNum: 4}, // 夹克
        {misort: 257, viewNum: 4}, // 毛衣/针织
        {misort: 22, viewNum: 4}, // 大衣/风衣
        {misort: 26, viewNum: 4}, // 休闲裤
        {misort: 27, viewNum: 4}, // 牛仔裤
        {misort: 31, viewNum: 4}, // 连衣裙
        {misort: 32, viewNum: 4}, // 半身裙
        {misort: 48, viewNum: 4}, // 时装鞋
        {misort: 49, viewNum: 4}, // 双肩包
        {misort: 50, viewNum: 4}, // 手拎包/单肩包
        {misort: 60, viewNum: 4}, // 帽子
        {misort: 65, viewNum: 4}, // 首饰
        {misort: 59, viewNum: 4}, // 手表
        {misort: 61, viewNum: 4}, // 太阳镜
        {misort: 66, viewNum: 4} // 配饰
    ],
    kids: [
        {misort: 366, viewNum: 4}, // T恤
        {misort: 367, viewNum: 4}, // 衬衫
        {misort: 396, viewNum: 4}, // 卫衣
        {misort: 400, viewNum: 4}, // // 毛衣/针织
        {misort: 404, viewNum: 4}, // 夹克
        {misort: 369, viewNum: 4}, // 休闲裤
        {misort: 388, viewNum: 4}, // 牛仔裤
        {misort: 371, viewNum: 4}, // 连衣裙
        {misort: 370, viewNum: 4}, // 半身裙
        {misort: 368, viewNum: 4}, // 休闲/运动鞋
        {misort: 392, viewNum: 4}, // 双肩包
        {misort: 414, viewNum: 4}, // 帽子
        {misort: 372, viewNum: 4}, // 短裤
        {misort: 384, viewNum: 4}, // 打底裤/紧身裤
        {misort: 382, viewNum: 4}, // 凉鞋/拖鞋
        {misort: 402, viewNum: 4}, // 马甲
        {misort: 386, viewNum: 4}, // 背心
        {misort: 406, viewNum: 4}, // 大衣/风衣
        {misort: 430, viewNum: 4}, // 羽绒服
        {misort: 423, viewNum: 4}, // 棉衣
        {misort: 417, viewNum: 4} // 套装
    ],
    lifestyle: [
        {sort: 171, viewNum: 5}, // 耳机
        {sort: 398, viewNum: 5}, // 只能装备
        {sort: 185, viewNum: 5}, // 相机
        {misort: 259, viewNum: 5}, // 美妆
        {sort: 267, viewNum: 5}, // 杯子/水壶
        {sort: 313, viewNum: 5}, // 手机/ipad壳套
        {sort: 211, viewNum: 5}, // 数码配件
        {sort: 292, viewNum: 5}, // 玩偶
        {sort: 272, viewNum: 5}, // 储物收纳
        {sort: 183, viewNum: 5}, // 启用家居
        {sort: 273, viewNum: 5}, // 厨具/餐具
        {sort: 271, viewNum: 5} // 靠枕/靠垫/抱枕
    ]
};
biao authored
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
const getNavs = rawNavs => {
    const navs = rawNavs;
    let list = [];

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

        obj.name = it.name;
        obj.href = it.url;

        list.push(obj);
    });

    return list;
};
liangxs authored
171 172 173 174 175 176 177 178 179 180 181 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 218 219 220 221 222 223 224
// 构建url
const httpBuildQuery = data => {
    return searchApi.get('/search.json', data);
};

/**
 * 格式化商品信息
 *
 * @param array $productData 需要格式化的商品数据
 * @param bool $showTags 控制是否显示标签
 * @param bool $showNew 控制是否显示NEW图标
 * @param bool $showSale 控制是否显示SALE图标
 * @param int $width 图片的宽度
 * @param int $height 图片的高度
 * @param bool $isApp 判断是不是APP访问
 * @param bool $showPoint 商品价格是否显示小数位,默认显示
 * @return array | false
 */
const formatProduct = (productData, showTags, showNew, showSale, width, height, isApp, showPoint) => {
    let result = {};

    // 默认值
    if (!showTags) {
        showTags = true;
    }
    if (!showNew) {
        showNew = true;
    }
    if (!showSale) {
        showSale = true;
    }
    if (!width) {
        width = 290;
    }
    if (!height) {
        height = 388;
    }
    if (!isApp) {
        isApp = false;
    }
    if (!showPoint) {
        showPoint = true;
    }

    // 商品信息有问题,则不显示
    if (!productData.product_skn || !productData.goods_list[0]) {
        return false;
    }

    // 市场价和售价一样,则不显示市场价
    if (parseInt(productData.market_price, 0) === parseInt(productData.sales_price, 0)) {
        productData.market_price = false;
    }
liangxs authored
225 226 227 228 229 230 231 232 233 234 235
    // 设置默认图片
    _.forEach(productData.goods_list, item => {
        if (item.is_default === 'Y') {
            productData.default_images = item.images_url;
        }
    });

    if (!productData.default_images) {
        productData.default_images = productData.goods_list[0].images_url;
    }
liangxs authored
236 237
    result.id = productData.product_skn;
    result.product_id = productData.product_id;
liangxs authored
238
    result.thumb = images.getImageUrl(productData.default_images, width, height);
liangxs authored
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    result.name = productData.product_name;
    result.price = !productData.market_price ? false : productData.market_price;
    result.salePrice = productData.sales_price;
    if (showPoint) {
        result.price += '.00';
        result.salePrice += '.00';
    }

    result.is_soon_sold_out = (productData.is_soon_sold_out === 'Y');
    result.url = 'http://item.yohobuy.com/product/pro_' +
        productData.product_id + '_' +
        productData.goods_list[0].goods_id + '/' +
        productData.cn_alphabet + '.html';

    // APP访问需要加附加的参数
    // 备注:如果以后APP的接口太多,可以把这边参数提取出来,变成一个公共的方法来生成,便于以后管理维护
    if (isApp) {
        result.url += '?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":' +
            productData.product_skn + '}}';
    }

    if (showTags) {
        result.tags = {};
        result.tags.is_new = showNew && productData.is_new && productData.is_new === 'Y'; // 新品
        result.tags.is_discount = showSale && productData.is_discount && productData.is_discount === 'Y'; // 在售
        result.tags.is_limited = productData.is_limited && productData.is_limited === 'Y'; // 限量
        result.tags.is_yohood = productData.is_yohood && productData.is_yohood === 'Y'; // YOHOOD
        result.tags.midYear = productData['mid-year'] && productData['mid-year'] === 'Y'; // 年中
        result.tags.yearEnd = productData['year-end'] && productData['year-end'] === 'Y'; // 年末
        result.tags.is_advance = productData.is_advance && productData.is_advance === 'Y'; // 再到着

        if (result.is_soon_sold_out && result.tags.is_discount) {
            result.tags.is_new = false;// 打折与即将售完组合显示打折
        } else if (result.tags.is_discount &&
            (result.tags.is_new || result.tags.is_limited || result.tags.is_yohood || result.tags.is_advance)) {
            result.tags.is_discount = false;// 打折与其它组合则隐藏打折
        } else if (result.tags.is_yohood && result.tags.is_new) {
            result.tags.is_new = false;// YOHOOD和新品组合显示YOHOOD
        }
    }
    return result;
};
liangxs authored
282
const getBannerList = data => {
biao authored
283
    let list = [];
liangxs authored
284 285 286 287 288 289 290 291

    _.forEach(data, (bannerData) => {
        let obj = {};

        obj.href = bannerData.url;
        obj.img = bannerData.src;
        obj.name = bannerData.title;
biao authored
292
        list.push(obj);
liangxs authored
293 294
    });
biao authored
295
    return list;
liangxs authored
296 297
};
biao authored
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321

const getDebrisSlide = data => {
    let floorData = {
        debrisSlider: {
            left: [],
            center: [],
            right: []
        }
    };

    _.mapKeys(data, (value, key) => {
        _.forEach(value, slideData => {
            let obj = {};

            obj.href = slideData.url;
            obj.img = slideData.img;

            floorData.debrisSlider[key].push(obj);
        });
    });

    return floorData;
};
biao authored
322
const getadbannerData = data => {
biao authored
323
    const obj = {
biao authored
324 325 326 327 328 329 330 331 332 333 334 335 336
        adbanner: {
            href: '',
            img: '',
            name: ''
        }
    };

    obj.adbanner.href = data.url;
    obj.adbanner.img = data.src;
    obj.adbanner.name = data.title;

    return obj;
};
337
biao authored
338
const getSlideData = srcData => {
biao authored
339
    const slideData = {
biao authored
340 341 342 343 344
        slide: {
            list: [],
            pagination: []
        }
    };
liangxs authored
345
biao authored
346 347 348 349 350 351 352 353
    if (srcData.big_image) {
        slideData.slide.list = getBannerList(srcData.big_image);
    }

    if (srcData.list) {
        slideData.slide.pagination = getBannerList(srcData.list);
    }
biao authored
354
    if (_.isArray(srcData)) {
biao authored
355 356
        slideData.slide.list = getBannerList(srcData);
    }
liangxs authored
357
biao authored
358 359
    return slideData;
};
liangxs authored
360 361

biao authored
362
const getNewReportFloorData = args => {
biao authored
363 364 365 366 367
    const title = args[0].data.text;
    let item = args[1].data;
    let secondItem = args[2].data;
    let thirdItem = args[3].data;
    let forthItem = args[4];
liangxs authored
368
biao authored
369 370
    let list = [];
    let obj = {};
liangxs authored
371
biao authored
372
    const data = {
biao authored
373
        newReport: {
biao authored
374
            name: title,
biao authored
375
            list: []
liangxs authored
376
        }
biao authored
377 378 379
    };

biao authored
380 381
    let adData;
    let floorDatas = [];
biao authored
382 383

biao authored
384 385 386 387 388 389 390 391 392 393 394 395
    obj.href = item[0].url;
    obj.img = item[0].src;

    list.push(obj);

    _.forEach(secondItem, (floorData) => {
        let o = {};

        o.href = floorData.url;
        o.img = floorData.src;

        list.push(o);
liangxs authored
396 397
    });
biao authored
398 399 400 401 402 403 404
    obj.href = thirdItem[0].url;
    obj.img = thirdItem[0].src;

    list.push(obj);

    data.newReport.list = list;
biao authored
405 406 407 408 409 410 411 412
    floorDatas.push(data);

    if (forthItem.template_name === 'single_image') {
        adData = getadbannerData(forthItem.data[0]);
        floorDatas.push(adData);
    }

    return floorDatas;
liangxs authored
413 414
};
biao authored
415 416 417 418
const setChannelType = (obj, type) => {
    obj[type + 'Channel'] = true;
};
biao authored
419
// 优选品牌
biao authored
420 421 422
const getPreBrandTopData = (args, type) => {
    const title = args[0].data.text;
    let item = args[1].data;
biao authored
423 424 425

    const data = {
        preferenceBrands: {
biao authored
426
            name: title,
biao authored
427
            imgBrand: [],
毕凯 authored
428
            brandUrl: '/getbrandFloorDataAjax?channelType=' + type
biao authored
429 430 431 432 433 434 435 436 437 438 439 440
        }
    };

    _.forEach(item, (topData) => {
        let o = {};

        o.href = topData.url;
        o.img = topData.src;

        data.preferenceBrands.imgBrand.push(o);
    });
biao authored
441 442
    setChannelType(data.preferenceBrands, type);
biao authored
443 444 445
    return data;
};
liangxs authored
446
// 热门品类
biao authored
447
const getHotGoodsFloorData = (args, type) => {
biao authored
448 449 450
    let item = args[0];
    let nextItem = args[1];
liangxs authored
451 452
    let list = [];
biao authored
453 454 455 456 457 458 459
    let object = {},
        keyword = [],
        category = [],
        brands = [],
        types = [],
        products = [];
biao authored
460
    const data = {
biao authored
461 462 463 464 465
        recommend: {
            tplrecommend: []
        }
    };
biao authored
466
    _.forEach(item.data.menuNav.list, it => {
biao authored
467 468
        let obj = {};
biao authored
469
biao authored
470 471 472 473 474
        obj.name = it.name;
        obj.href = it.url;
        category.push(obj);
    });
biao authored
475
    _.forEach(item.data.menuNav.blocks, it => {
biao authored
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
        let obj = {};

        obj.name = it.title;
        obj.href = it.url;
        obj.img = it.img;
        keyword.push(obj);
    });

    _.forEach(item.data.imgs, (it, idx) => {
        let obj = {};

        obj.name = it.title;
        obj.href = it.url;
        obj.img = it.img;
biao authored
491
        if (idx === 0 || (idx === 4 && type === 'boys')) {
biao authored
492 493 494
            brands.push(obj);
        } else {
            types.push(obj);
liangxs authored
495
        }
biao authored
496 497
    });
biao authored
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
    _.forEach(nextItem.data, (it) => {
        let obj = {};

        obj.name = it.title;
        obj.href = it.url;
        obj.img = it.src;

        products.push(obj);
    });

    object.name = item.data.name;
    object.keyword = keyword;
    object.category = category;
    object.brands = brands;
    object.types = types;
biao authored
513
    object.navs = getNavs(item.data.navs.list);
biao authored
514 515 516 517 518
    object.products = products;
    list.push(object);

    data.recommend.tplrecommend = list;
biao authored
519 520 521
    setChannelType(data.recommend, type);

biao authored
522
    return data;
liangxs authored
523 524
};
biao authored
525 526

const getBoysSingleHot = (args, type) => {
biao authored
527
    const len = 10;
biao authored
528
    const data = {
biao authored
529
        singlehot: {
biao authored
530
            name: args[0].data.text,
biao authored
531 532 533 534
            imgHot: []
        }
    };
biao authored
535
    let list = [];
biao authored
536 537 538 539 540 541 542 543 544
    let adData;
    let floorDatas = [];

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

        if (i === 1) {
biao authored
545
            val = args[1].data[0]; // 第二个是大图
biao authored
546
        } else if (i === len - 1) {
biao authored
547
            val = args[1].data[1]; // 最后一个是大图
biao authored
548 549 550
        } else {
            if (pos > 1) { // 小图
                pos = pos - 1;
cailing authored
551
            }
biao authored
552
            val = args[2].data[pos];
cailing authored
553
        }
biao authored
554 555 556 557 558 559
        obj.href = val.url;
        obj.img = val.src;
        list.push(obj);
    }

    data.singlehot.imgHot = list;
biao authored
560
    setChannelType(data.singlehot, type);
biao authored
561 562
    floorDatas.push(data);
biao authored
563 564
    if (args[3].template_name === 'single_image') {
        adData = getadbannerData(args[3].data[0]);
biao authored
565 566 567 568
        floorDatas.push(adData);
    }

    return floorDatas;
cailing authored
569 570
};
571
const getGirlsSingleHot = args => {
biao authored
572
    let goods = args[2].data;
biao authored
573
    let skns = '';
biao authored
574 575

biao authored
576 577
    _.forEach(goods, good => {
        skns += good.id + ' ';
biao authored
578 579
    });
580
    return searchApi.get('/search.json', {
581 582 583 584 585 586 587 588 589 590
        client_type: 'web',
        query: skns,
        order: 'shelve_time:desc',
        status: 1,
        sales: 'Y',
        attribute_not: '2',
        stocknumber: 1,
        page: 1,
        viewNum: 60
    });
biao authored
591 592
};
593
biao authored
594 595 596 597
// 人气单品
const getSingleHotFloorData = (args, type) => {
    if (type === 'boys') {
        return getBoysSingleHot(args, type);
598
    } else {
biao authored
599 600 601 602
        return getGirlsSingleHot(args, type);
    }
};
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
const getAsyncSingleHot = (args, queryResult, type) => {
    const data = {
        singlehot: {
            name: args[0].data.text,
            navs: [],
            imgHot: [],
            brands: []
        }
    };

    if (args[3].template_name === 'app_icon_list') {
        _.forEach(args[3].data, it => {
            let obj = {};

            obj.href = it.url;
            obj.name = it.title;
            obj.img = it.src;

            data.singlehot.brands.push(obj);
        });
    }

    _.forEach(queryResult.data.product_list, (it, index) => {
        let obj = {};
        const formatData = formatProduct(it, true, true, true, 280, 373);

        if (index > 12) {
            return;
        }


        obj.price = formatData.salePrice;
        obj.href = formatData.url;
        obj.img = formatData.thumb;
        obj.name = formatData.name;

        if (index < 3) {
            obj.tip = 'TOP' + (index + 1);
        }

        data.singlehot.imgHot.push(obj);
    });

    data.singlehot.navs = getNavs(args[1].data);
    setChannelType(data.singlehot, type);
    return data;
};

const processFloorDataWithQueryReusult = (rawData, floorData, queryResult, title, type) => {
    let data = {};

    _.forEach(rawData, (subData, index) => {
        const text = subData.data.text && getText(subData.data.text);

        if (text === title) {
            data = getAsyncSingleHot(rawData.slice(index, index + 4), queryResult, type);
        }
    });

    return data;
663
};
664
liangxs authored
665 666 667 668 669 670
/**
 * 组装最新上架楼层数据
 *
 * @param string data
 * return obj
 */
671 672 673
const getNewGoodsFloorData = args => {
    const data = {
        newArrivls: {
biao authored
674 675
            name: args[0].data.text,
            navs: []
liangxs authored
676
        }
677 678
    };
biao authored
679
    data.newArrivls.navs = getNavs(args[1].data);
cailing authored
680
681
    return data;
liangxs authored
682 683
};
biao authored
684 685 686


liangxs authored
687 688 689 690 691 692 693
/**
 * 获取最新上架商品数据
 *
 * @param string $channel
 * @return array
 */
exports.getNewArrival = channel => {
liangxs authored
694
    let rel = [],
liangxs authored
695
        sortList = sortMap[channel],
liangxs authored
696 697 698 699 700 701
        params = {
            order: 'shelve_time:desc',
            status: 1,
            sales: 'Y',
            attribute_not: 2,
            stocknumber: 3,
biao authored
702
            shelve_time: getShelveTime(20)
liangxs authored
703
        };
liangxs authored
704
biao authored
705
    params.gender = channelMap[channel].gender;
liangxs authored
706
biao authored
707
liangxs authored
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
    _.forEach(sortList, (item) => {
        let data = Object.assign(item, params);

        rel.push(httpBuildQuery(data));
    });

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

        _.forEach(sortList, (it, index) => {
            if (res[index].data.product_list.length === sortList[index].viewNum) {
                data = data.concat(res[index].data.product_list);
            }
        });

        _.forEach(data, (item) => {
            result.push(formatProduct(item, true, true, true, 280, 373));
        });

        return result;
    });
};
cailing authored
731
biao authored
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784

const getCategoryFloorData = args => {
    const data = {
        category: {
            name: args[0].data.text,
            navs: getNavs(args[1].data),
            list: []
        }
    };

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

        obj.href = it.url;
        obj.img = it.src;

        // 设置图片大小
        if (index === 1) {
            obj.w = '377;';
            obj.h = '504;';
        } else {
            obj.w = '185';
            obj.h = '510';
        }

        data.category.list.push(obj);
    });

    return data;
};

const getAccordionFloorData = args => {
    let data = {
        accordion: {
            name: args[0].data.text,
            navs: getNavs(args[1].data),
            slide: []
        }
    };

    _.forEach(args[2].data, it => {
        let obj = {};

        obj.name = it.title;
        obj.img = it.src;
        obj.href = it.url;

        data.accordion.slide.push(obj);
    });

    return data;
};
biao authored
785
const requestContent = type => {
biao authored
786
liangxs authored
787
    let data = sign.apiSign({
liangxs authored
788 789
            /* eslint-disable */
            client_type: 'web',
biao authored
790
            /* eslint-enable */
liangxs authored
791 792 793 794 795 796
        content_code: channelMap[type || 'boys'].code,
        gender: channelMap[type || 'boys'].gender,
        page: 1,
        limit: 1000
    });
毕凯 authored
797
    return serviceApi.get('operations/api/v5/resource/home', data).then(res => {
biao authored
798 799 800 801 802 803 804
        if (res.code === 200) {
            return res;
        } else {
            log.error('获取资源位接口返回状态码 不是 200');
            return {};
        }
    });
liangxs authored
805 806
};
biao authored
807 808 809
const floorMap = {
    slide: getSlideData,
    hot: getHotGoodsFloorData,
biao authored
810 811
    最新速报: getNewReportFloorData,
    人气单品: getSingleHotFloorData,
812 813
    'GIRL KIDS': getSingleHotFloorData,
    'BOY KIDS': getSingleHotFloorData,
biao authored
814
    优选品牌: getPreBrandTopData,
815
    最新上架: getNewGoodsFloorData,
biao authored
816
    ad: getadbannerData,
biao authored
817 818
    category: getCategoryFloorData,
    accordion: getAccordionFloorData,
biao authored
819
    debrisSlide: getDebrisSlide
biao authored
820 821
};
822 823

biao authored
824
const processFloorData = (rawData, type) => {
biao authored
825
    let floorList = [];
826 827 828
    let searchPromise = [];
    let singlehotFloorIndex = [];
    let singlehotFloorTitile = [];
biao authored
829
biao authored
830 831 832 833 834
    const bigFloorLength = 5;
    const normalFloorLength = 3;
    const hotCategoryLength = 2;

biao authored
835
    _.forEach(rawData, (data, index) => {
biao authored
836
        let floorData = null;
biao authored
837
biao authored
838
        if (data.template_name === 'recommend_content_three' ||
biao authored
839
           (data.template_intro === '焦点图' && index === 0)) { // 处理banner
biao authored
840
            floorData = floorMap.slide.call(null, data.data);
biao authored
841
        } else if (data.template_intro === '热门品类') { // 处理热门品类
biao authored
842
            floorData = floorMap.hot.call(null, rawData.slice(index, index + hotCategoryLength), type);
biao authored
843
        } else if (data.data.text) { // 处理一般楼层
844
            let text = getText(data.data.text);
biao authored
845 846
            let lastIndex = index + bigFloorLength < rawData.length ?
                            index + bigFloorLength : index + (rawData.length - index - 1);
biao authored
847
biao authored
848 849

            floorData = floorMap[text] &&
biao authored
850 851
                        floorMap[text].call(null, rawData.slice(index, lastIndex), type);
biao authored
852
        } else if (data.template_name === 'debrisSlider') { // 处理girls的banner
biao authored
853
            floorData = floorMap.debrisSlide.call(null, data.data);
biao authored
854 855
        }
biao authored
856
        // 处理lifestyle分类楼层
biao authored
857 858
        if (!floorData && index + normalFloorLength < rawData.length &&
            rawData[index + 1].template_name === 'textNav' &&
biao authored
859 860
            rawData[index + 2].template_name === 'floor') {
            floorData = floorMap.category.call(null, rawData.slice(index, index + normalFloorLength));
biao authored
861 862 863
        }

        // 处理手风琴楼层
biao authored
864 865
        if (!floorData && index + normalFloorLength < rawData.length &&
            rawData[index + 1].template_name === 'textNav' &&
biao authored
866 867
            rawData[index + 2].template_name === 'focus') {
            floorData = floorMap.accordion.call(null, rawData.slice(index, index + normalFloorLength));
biao authored
868
        }
biao authored
869
870
biao authored
871
        // 处理promise
872 873 874 875 876 877 878 879 880
        if (floorData && floorData.then && typeof floorData.then === 'function') {
            searchPromise.push(floorData);

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

            // 记住楼层标题, 以便后面promise获取数据后插入楼层数据
            singlehotFloorTitile.push(getText(data.data.text));
        } else if (!_.isNil(floorData)) {
biao authored
881 882 883 884
            _.isArray(floorData) ?
                floorList = floorList.concat(floorData) :
                floorList.push(floorData);
        }
biao authored
885 886
    });
887 888 889 890 891
    return {
        floors: floorList,
        promise: _.reverse(searchPromise),
        singlehotFloorTitile: _.reverse(singlehotFloorTitile),
        singlehotFloorIndex: _.reverse(singlehotFloorIndex)
892
    };
biao authored
893 894
};
biao authored
895
biao authored
896 897 898 899 900
/**
 * 获取频道页数据
 * @param  {string} type 传入频道页类型,值可以是: boys, girls, kids, lifestyle
 * @return {object}
 */
biao authored
901
exports.getContent = type => {
biao authored
902
    return Promise.all([headerModel.requestHeaderData(type), requestContent(type)]).then(res => {
liangxs authored
903
biao authored
904 905
        let headerData = res[0].data || res[0],
            contentData = res[1].data ? res[1].data.list : res[1];
liangxs authored
906
biao authored
907
        let data = {};
liangxs authored
908
biao authored
909
        const processResult = processFloorData(contentData, type);
910
biao authored
911 912 913 914 915 916
        data = headerData;
        data.module = 'channel';
        data.page = 'channel';
        data.pageType = type;
        data.footerTop = true;
        data.channel = processResult.floors;
biao authored
917
918
biao authored
919 920 921 922 923 924 925 926
        return {
            rawData: contentData,
            floorData: data,
            searchPromise: processResult.promise,
            singlehotFloorIndex: processResult.singlehotFloorIndex,
            singlehotFloorTitile: processResult.singlehotFloorTitile,
            channelType: type
        };
927 928 929 930 931 932 933 934 935

    }).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,
936 937
                            processFloorDataWithQueryReusult(result.rawData,
                                result.floorData, data, result.singlehotFloorTitile[index], result.channelType));
938 939 940
                });

                return result.floorData;
941
            });
942
        } else {
biao authored
943
            return result.floorData || result;
biao authored
944
        }
biao authored
945 946
    });
};
biao authored
947 948 949 950 951


// 优选品牌楼层floorData-ajax
exports.getbrandFloorDataAjax = type => {
    return requestContent(type).then(res => {
biao authored
952
        let contentData = res.data ? res.data.list : [];
biao authored
953
biao authored
954 955 956 957
        let data = {
            logoBrand: [],
            moreBrand: ''
        };
biao authored
958
biao authored
959 960 961 962
        _.forEach(contentData, (d, index) => {
            if (d.data.text && d.data.text.indexOf('优选品牌') >= 0) {
                _.forEach(contentData[index + 2].data, (floorData) => {
                    let o = {};
biao authored
963
biao authored
964 965
                    o.href = floorData.url;
                    o.img = helpers.image(floorData.src, 185, 86, 2);
biao authored
966
biao authored
967 968
                    data.logoBrand.push(o);
                });
biao authored
969
biao authored
970 971 972
                data.moreBrand = contentData[index + 3].data[0].url;
            }
        });
biao authored
973
biao authored
974
        return data;
biao authored
975 976
    });
};