Blame view

apps/product/models/search-handler.js 48.9 KB
周少峰 authored
1
/*
周少峰 authored
2
 * @Author: sefon
周少峰 authored
3 4 5 6 7 8 9 10
 * @Date:   2016-06-02 15:50:47
 * @Last Modified by:   Targaryen
 * @Last Modified time: 2016-06-22 18:36:26
 */

'use strict';
const _ = require('lodash');
const helpers = global.yoho.helpers;
yyq authored
11
const crypto = global.yoho.crypto;
周少峰 authored
12
const queryString = require('querystring');
周少峰 authored
13 14 15 16
const indexUrl = {
    boys: helpers.urlFormat('/'),
    girls: helpers.urlFormat('/woman'),
    kids: helpers.urlFormat('/kids'),
周少峰 authored
17 18
    lifestyle: helpers.urlFormat('/lifestyle')
};
周少峰 authored
19
20 21 22 23 24 25 26
// 打折、新品、限量
const checksName = {
    new: '新品',
    specialoffer: '打折',
    limited: '限量'
};
yyq authored
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
const sortFilterParam = (param) => {
    let resData = [];

    _.forEach(param, (value, key) => {
        resData.push({
            key: key,
            value: value
        });
    });

    return _.sortBy(resData, [o => {
        return o.key;
    }]);
};
周少峰 authored
42 43 44 45 46 47
/**
 * 处理用于筛选的 URL , 拼接 URL 参数
 * @param originParam 当前 URL 中的参数
 * @param newParam 要拼接的 参数
 * @returns {string}
 */
yyq authored
48
const handleFilterUrl = (originParam, newParam, delParam) => {
yyq authored
49
    let dest = '?';
周少峰 authored
50 51
    let tempOriginParam = {};
yyq authored
52 53
    delParam = delParam || {};
周少峰 authored
54 55 56
    tempOriginParam = Object.assign(tempOriginParam, originParam, newParam);
    delete tempOriginParam.uid;
yyq authored
57 58
    _.forEach(sortFilterParam(tempOriginParam), info => {
        if (!delParam[info.key] && info.value) {
htoooth authored
59
            // NOTE: 这里会对 query 进行编码,因为 query 有可以能是中文
yyq authored
60 61 62 63
            if (info.key === 'query') {
                info.value = encodeURIComponent(info.value);
            }
            dest += `${info.key}=${info.value}&`;
yyq authored
64
        }
周少峰 authored
65 66
    });
yyq authored
67
    return _.trim(dest, '&');
yyq authored
68
};
周少峰 authored
69 70 71 72 73 74 75 76 77 78 79 80 81 82

/**
 * 处理选中数据
 * @param  {[type]} params [description]
 * @param  {[type]} origin [description]
 * @param  {[type]} param  [description]
 * @return {[type]}        [description]
 */
const handleCheckedData = (params, origin, param) => {
    let dest = [];

    if (!_.isEmpty(origin)) {
        _.forEach(origin, value => {
            if (value.checked) {
周少峰 authored
83
                let tempPatam = _.cloneDeep(params);
周少峰 authored
84
周少峰 authored
85
                // 删除选中
yyq authored
86 87 88 89 90 91
                if (param === 'gender') {
                    // 某些特殊带频道信息页面,清除性别,需将gender设为1,2,3 (2017-3 配合SEO进行URL改造)
                    tempPatam[param] = '1,2,3';
                } else {
                    delete tempPatam[param];
                }
周少峰 authored
92 93 94

                dest.push({
                    name: value.name,
刘传洋 authored
95 96
                    href: handleFilterUrl(tempPatam),
                    itemType: param
周少峰 authored
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
                });
            }
        });
    }
    return dest;
};

/**
 * 处理品牌筛选数据
 * @param params
 * @param origin
 * @returns {{}}
 */
const handleBrandCheckedData = (params, origin) => {
    let dest = [];

    // 分拆品牌参数
    let brands = _.split(params.brand, ',');
    let intBrands = [];
hongweigao authored
116
周少峰 authored
117 118 119 120
    _.forEach(brands, value => {
        intBrands.push(parseInt(value, 10));
    });
hongweigao authored
121 122
    let checkedCount = 0,
        brandsTotalName = '';
hongweigao authored
123
周少峰 authored
124 125
    // 遍历品牌数据,如果在参数中,那么加 checked,将此数据加入到 checked 数组中
    if (!_.isEmpty(origin)) {
hongweigao authored
126 127

        brandsTotalName = _.map(origin, 'name').join('、');
周少峰 authored
128 129
        _.forEach(origin, (value) => {
            if (typeof _.find(intBrands, o => {
周少峰 authored
130
                return _.isEqual(o, +value.id);
周少峰 authored
131
            }) !== 'undefined') {
周少峰 authored
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
                let checked = {
                    name: value.name
                }; // push 到已选择数组

                // 为了不污染原始数据
                let tempIntBrands = _.cloneDeep(intBrands);

                // 去掉当前选项
                _.remove(tempIntBrands, n => {
                    return n === value.id;
                });

                // 拼接参数
                let brand = '';

                // 建议保留,需要品牌可以单个删除时放开注释即可
                // _.forEach(tempIntBrands, subValue => {
                //     brand += subValue + ',';
                // });

                // 清除参数,保留没有去掉的参数
                let tempParam = {
                    brand: brand
                };

                checked.href = handleFilterUrl(params, tempParam);
刘传洋 authored
158
                checked.itemType = 'brand';
周少峰 authored
159 160 161 162 163 164
                if (checkedCount === 0) {
                    dest[0] = checked;
                } else if (checkedCount === 1) {
                    dest[0].name += '、' + checked.name.substring(0, 3) + '...';
                }
                checkedCount++;
周少峰 authored
165
周少峰 authored
166 167
                // 建议保留,需要品牌可以单个删除时放开注释即可
                // dest.push(checked);
hongweigao authored
168
                dest[0].totalName = brandsTotalName;
周少峰 authored
169 170
            }
        });
hongweigao authored
171
周少峰 authored
172 173 174 175 176
    }
    return dest;
};

/**
177 178
 * 新品、折扣、限量
 */
刘传洋 authored
179 180
const hadleChecks = (name, params, filter) => {
    let tempParam = _.cloneDeep(params),
刘传洋 authored
181
        checked = false;
182
刘传洋 authored
183
    if (!params[name] && !filter[name]) {
刘传洋 authored
184 185
        return null;
    }
186 187

    if (params[name] && params[name] === 'Y') {
刘传洋 authored
188
        checked = true;
189
        delete tempParam[name];
刘传洋 authored
190 191
    } else {
        tempParam[name] = 'Y';
192 193
    }
刘传洋 authored
194 195 196 197 198
    return {
        name: checksName[name],
        checked: checked,
        href: handleFilterUrl(tempParam)
    };
199 200
};
刘传洋 authored
201 202 203 204 205 206 207 208 209 210 211
/**
 * 获取standard
 */
const getStandard = (qStandard, addOne, delOne) => {
    let s = [];

    qStandard = Object.assign({}, qStandard);
    if (addOne && addOne.k) {
        qStandard[addOne.k] = addOne.v;
    }
周少峰 authored
212 213
    if (delOne && delOne.k && qStandard[delOne.k]) {
        delete qStandard[delOne.k];
刘传洋 authored
214 215 216 217 218 219 220 221 222 223 224
    }

    _.each(qStandard, (val, key) => {
        if (!delOne || !delOne.k || delOne.v !== val) {
            s.push([key, val].join('_'));
        }
    });

    return s.join(',');
};
225 226 227 228 229 230 231 232 233 234 235 236
const formatterFilterBrands = (source, paramBrand, params) => {

    let dbrand = {
        default: [],
        brandsShow: [],
        brandIndex: [{
            index: 'all',
            name: '全部'
        }, {
            index: '0-9',
            name: '0~9'
        }],
yyq authored
237
        selectedBrands: []
238 239 240
    };

htoooth authored
241
    // 品牌索引数据处理
242 243 244 245 246 247 248
    for (let i = 65; i < 91; i++) {
        dbrand.brandIndex.push({
            index: String.fromCharCode(i).toLowerCase(),
            name: String.fromCharCode(i)
        });
    }
htoooth authored
249 250
    // 品牌数据处理
    // 分拆品牌参数
251 252 253 254 255 256 257 258 259 260 261 262 263
    let brands = _.split(params.brand, ',');
    let intBrands = [];

    _.forEach(brands, value => {
        intBrands.push(parseInt(value, 10));
    });

    if (source) {
        let count = 0;

        _.forEach(source, function(value) {
            let brand = {
                checked: (typeof _.find(intBrands, o => {
周少峰 authored
264
                    return _.isEqual(o, +value.id);
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
                }) !== 'undefined'),
                href: handleFilterUrl(params, {brand: value.id}),
                name: value.brand_name,
                key: value.brand_domain,
                id: value.id
            };

            if (!_.isEmpty(value.brand_alif)) {
                if (!_.isNaN(parseInt(value.brand_alif, 10))) {
                    brand.index = '0-9';
                } else {
                    brand.index = value.brand_alif.toLowerCase();
                }
            }

            if (count < 10) {
                dbrand.default.push(brand);
            }

            dbrand.brandsShow.push(brand);
            count++;
        });
yyq authored
288 289 290 291 292 293
        if (dbrand.default.length > 9) {
            Object.assign(dbrand, {
                showMore: true,
                showMulti: true
            });
        }
294 295 296 297 298 299 300 301 302 303 304 305 306
        if (paramBrand) {
            _.forEach(paramBrand, value => {
                let brand = {
                    href: handleFilterUrl(params, {brand: value.id}),
                    name: value.brand_name,
                    key: value.brand_domain,
                    id: value.id
                };

                dbrand.selectedBrands.push(brand);
            });
        }
htoooth authored
307
        // 没有品牌的情况下将 brand 设置为 false,前端不显示 品牌
308 309 310
        if (_.size(dbrand.brandsShow) <= 0) {
            return false;
        } else {
htoooth authored
311
            // 品牌排序
312 313 314 315 316 317 318 319 320
            dbrand.brandsShow = _.sortBy(dbrand.brandsShow, o => {
                return o.index;
            });
        }
    }

    return dbrand;
};
周少峰 authored
321
322
/**
周少峰 authored
323 324 325 326 327 328 329 330
 * 处理 opts 排序数据
 * @param params
 * @param total
 * @param extra 什么都可以传进来,多个参数传Object
 * @returns {{}}
 */
exports.handleOptsData = (params, total, extra) => {
    let dest = {
331 332
        sortType: [],
        checks: []
周少峰 authored
333 334 335 336 337 338 339 340 341 342
    };

    // 用来标记是否是折扣专场,折扣专场只需要前三个排序参数
    let count = (extra === 'discont') ? 3 : 4;

    for (let i = 0; i < count; i++) {
        let opt = {};

        switch (i) {
            case 0:
yyq authored
343
                opt.href = handleFilterUrl(params, {}, {page: true, order: true});
周少峰 authored
344 345 346 347 348 349 350 351
                if (extra === 'discont') { // 如果是折扣专场
                    opt.name = '全部';
                    if (_.isEmpty(params.order) || params.order === 's_t_desc') {
                        opt.active = true;
                    } else {
                        opt.active = false;
                    }
                } else {
周少峰 authored
352 353
                    opt.name = '默认';
                    if (_.isEmpty(params.order) || params.order === 's_n_desc') {
周少峰 authored
354 355 356 357 358 359 360
                        opt.active = true;
                    } else {
                        opt.active = false;
                    }
                }
                break;
            case 1:
周少峰 authored
361
                if (params.order !== 's_t_desc' && params.order !== 's_t_asc') {
yyq authored
362
                    opt.href = handleFilterUrl(params, {order: 's_t_desc'}, {page: true});
周少峰 authored
363 364 365 366
                    opt.hasSortOrient = true;
                } else {
                    opt.hasSortOrient = true;
                    opt.active = true;
周少峰 authored
367
                    if (params.order === 's_t_desc') {
yyq authored
368
                        opt.href = handleFilterUrl(params, {order: 's_t_asc'}, {page: true});
周少峰 authored
369 370
                        opt.desc = false;
                    } else {
yyq authored
371
                        opt.href = handleFilterUrl(params, {order: 's_t_desc'}, {page: true});
周少峰 authored
372 373 374 375
                        opt.desc = true;
                    }
                }
周少峰 authored
376
                opt.name = '最新';
周少峰 authored
377 378
                break;
            case 2:
周少峰 authored
379
                if (params.order !== 's_p_desc' && params.order !== 's_p_asc') {
yyq authored
380
                    opt.href = handleFilterUrl(params, {order: 's_p_desc'}, {page: true});
周少峰 authored
381 382 383 384
                    opt.hasSortOrient = true;
                } else {
                    opt.hasSortOrient = true;
                    opt.active = true;
周少峰 authored
385
                    if (params.order === 's_p_desc') {
yyq authored
386
                        opt.href = handleFilterUrl(params, {order: 's_p_asc'}, {page: true});
周少峰 authored
387 388
                        opt.desc = false;
                    } else {
yyq authored
389
                        opt.href = handleFilterUrl(params, {order: 's_p_desc'}, {page: true});
周少峰 authored
390 391 392 393
                        opt.desc = true;
                    }
                }
周少峰 authored
394
                opt.name = '价格';
周少峰 authored
395 396
                break;
            case 3:
周少峰 authored
397
                if (params.order !== 'p_d_desc' && params.order !== 'p_d_asc') {
yyq authored
398
                    opt.href = handleFilterUrl(params, {order: 'p_d_desc'}, {page: true});
周少峰 authored
399 400 401 402
                    opt.hasSortOrient = true;
                } else {
                    opt.hasSortOrient = true;
                    opt.active = true;
周少峰 authored
403
                    if (params.order === 'p_d_desc') {
yyq authored
404
                        opt.href = handleFilterUrl(params, {order: 'p_d_asc'}, {page: true});
周少峰 authored
405 406
                        opt.desc = false;
                    } else {
yyq authored
407
                        opt.href = handleFilterUrl(params, {order: 'p_d_desc'}, {page: true});
周少峰 authored
408 409 410 411
                        opt.desc = true;
                    }
                }
周少峰 authored
412
                opt.name = '折扣';
周少峰 authored
413 414 415 416 417 418 419 420 421 422
                break;
            default:
                break;
        }

        dest.sortType.push(opt);
    }

    // 上下翻页数据处理
    dest.pageCounts = [{
yyq authored
423
        href: handleFilterUrl(params, {limit: 60}, {page: true}),
刘传洋 authored
424
        count: 60
周少峰 authored
425
    }, {
yyq authored
426
        href: handleFilterUrl(params, {limit: 100}, {page: true}),
周少峰 authored
427 428
        count: 100
    }, {
yyq authored
429
        href: handleFilterUrl(params, {limit: 200}, {page: true}),
刘传洋 authored
430
        count: 200
周少峰 authored
431 432 433 434 435 436 437 438
    }];

    dest.curPage = _.isEmpty(params.page) ? 1 : params.page; // 当前页码数

    // 每页商品数量
    dest.countPerPage = _.isEmpty(params.limit) ? 60 : params.limit;

    // 全部页码数量
yyq authored
439
    dest.pageCount = _.ceil(total / (dest.countPerPage - 1));
周少峰 authored
440 441 442 443 444 445 446 447 448

    // 每页多少商品
    let paramsLimit = parseInt((_.isEmpty(params.limit) ? 60 : params.limit), 10);

    // 上一页下一页
    let preHref = (!_.isEmpty(params.page) && parseInt(params.page, 10) > 1) ?
    parseInt(params.page, 10) - 1 : 1;
    let nextHref = (!_.isEmpty(params.page)) ? parseInt(params.page, 10) + 1 : 2;
刘传洋 authored
449 450
    params.page = _.isEmpty(params.page) ? 1 : params.page;
周少峰 authored
451 452 453 454
    if (dest.pageCount > 1 && (parseInt(params.page, 10) !== 1) &&
        (parseInt(params.page, 10) !== dest.pageCount)) {
        dest.preHref = handleFilterUrl(params, {page: preHref});
        dest.nextHref = handleFilterUrl(params, {page: nextHref});
刘传洋 authored
455
    } else if (dest.pageCount > 1 && (parseInt(params.page, 10) === 1 || _.isEmpty(params.page))) {
周少峰 authored
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
        dest.nextHref = handleFilterUrl(params, {page: nextHref});
    } else if (dest.pageCount > 1 && (parseInt(params.page, 10) === dest.pageCount)) {
        dest.preHref = handleFilterUrl(params, {page: preHref});
    }

    // 全部商品数
    dest.totalCount = total;

    // 商品开始结束数
    dest.start = (!_.isEmpty(params.page) ? (params.page - 1) : 0) * paramsLimit + 1;
    if (total < paramsLimit) {
        dest.end = total;
    } else {
        dest.end = (dest.pageCount === parseInt(params.page, 10)) ?
            total : parseInt(dest.start, 10) + paramsLimit - 1;
    }
yyq authored
473 474
    if (extra) {
        // 新品
刘传洋 authored
475
        dest.checks.push(hadleChecks('new', params, extra));
476
yyq authored
477
        // 打折
刘传洋 authored
478
        dest.checks.push(hadleChecks('specialoffer', params, extra));
479
yyq authored
480
        // 限量
刘传洋 authored
481 482 483 484 485
        dest.checks.push(hadleChecks('limited', params, extra));

        dest.checks = _.filter(dest.checks, chk => {
            return chk !== null;
        });
486
    }
周少峰 authored
487 488 489 490 491 492 493 494
    return dest;
};

/**
 * 处理页面左侧分类筛选数据
 * @param origin 分类原始数据,一般是接口返回数据中的 group_sort 字段
 * @param params 当前 URL 中已有的参数
 * @param extra 可以任意传值用来处理特殊情况
495
 * @param {string} [baseUrl] 需要跳转非当前页面传的相对路径(可不传,默认为空字符串)
周少峰 authored
496 497
 * @returns {{}}
 */
498
exports.handleSortData = (origin, params, originParams, baseUrl) => {
周少峰 authored
499
    let leftContent = {
周少峰 authored
500
        sort: {misort: []},
周少峰 authored
501
        checked: []
周少峰 authored
502
    };
周少峰 authored
503
yyq authored
504
    let list = [],
yyq authored
505
        allCount = 0;
506
yyq authored
507 508
    baseUrl = baseUrl || '';
yyq authored
509
    let all = [{
yyq authored
510
        name: '全部品类',
yyq authored
511
        num: allCount,
yyq authored
512
        href: `${baseUrl}${handleFilterUrl(params, {}, {msort: true, misort: true, sort: true, category_id: true})}`
yyq authored
513 514
    }];
yyq authored
515
    originParams = originParams || {};
yyq authored
516
周少峰 authored
517
    _.forEach(origin, value => {
yyq authored
518
        let equalCategory = `${originParams.category_id}` === `${value.category_id}`;
周少峰 authored
519
        let category = {
yyq authored
520
            categoryId: value.category_id,
yyq authored
521 522
            name: value.category_name,
            num: value.node_count,
周少峰 authored
523 524
            childList: [
                {
yyq authored
525
                    categoryId: value.category_id,
yyq authored
526 527
                    name: `全部${value.category_name}`,
                    num: value.node_count,
yyq authored
528 529 530 531 532 533
                    href: `${baseUrl}${handleFilterUrl(params, {category_id: value.category_id}, {
                        msort: true,
                        misort: true,
                        sort: true
                    })}`,
                    childActive: equalCategory
周少峰 authored
534
                }
yyq authored
535
            ]
周少峰 authored
536 537
        };
yyq authored
538 539
        allCount += parseInt(value.node_count, 10);
yyq authored
540 541 542
        if (equalCategory) {
            category.active = true;
        }
周少峰 authored
543
yyq authored
544 545
        _.forEach(value.sub, subValue => {
            let child = {
yyq authored
546
                categoryId: subValue.category_id,
yyq authored
547 548
                name: subValue.category_name,
                num: subValue.node_count,
yyq authored
549 550 551 552 553 554
                href: `${baseUrl}${handleFilterUrl(params, {category_id: subValue.category_id}, {
                    msort: true,
                    misort: true,
                    sort: true
                })}`,
                childActive: `${originParams.category_id}` === `${subValue.category_id}`
yyq authored
555
            };
周少峰 authored
556
yyq authored
557
            category.childList.push(child);
周少峰 authored
558
yyq authored
559 560
            if (child.childActive) {
                category.active = true;
周少峰 authored
561
            }
周少峰 authored
562 563
        });
yyq authored
564
        list.push(category);
周少峰 authored
565 566
    });
yyq authored
567
    leftContent.allSort = {all: all, list: list};
周少峰 authored
568
周少峰 authored
569 570 571 572
    return leftContent;
};

/**
yyq authored
573 574 575 576 577 578 579 580 581 582 583 584 585
 * 店铺品类聚合
 * @param data 分类数据
 * @returns {{}}
 */
exports.setShopSort = (data, params) => {
    let resData = {};

    if (!_.isEmpty(data)) {
        let list = [];

        _.forEach(data, value => {
            _.forEach(value.sub, subValue => {
                list.push({
yyq authored
586 587 588
                    name: subValue.category_name,
                    href: handleFilterUrl(params, {category_id: subValue.category_id}),
                    curMenu: `${params.category_id}` === `${subValue.category_id}`
yyq authored
589 590 591 592 593
                });
            });
        });

        if (list.length) {
yyq authored
594 595 596 597
            resData.goodsMenu = {
                menuList: list,
                url: `/product/shoplist?navBar=1&shopId=${params.shopId}`
            };
yyq authored
598 599 600 601 602 603 604
        }
    }

    return resData;
};

/**
yyq authored
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
 * 店铺性别筛选
 * @param params qs
 * @returns {{}}
 */
exports.setGenderFilter = (params) => {
    return {genderFilter: [
        {
            name: '全部',
            href: handleFilterUrl(params, {}, {gender: true}),
            checked: !params.gender || params.gender === '1,2,3'
        },
        {
            name: 'BOYS',
            href: handleFilterUrl(params, {gender: '1,3'}),
            checked: params.gender === '1,3'
        }, {
            name: 'GIRLS',
            href: handleFilterUrl(params, {gender: '2,3'}),
            checked: params.gender === '2,3'
        }
    ]};
};

/**
周少峰 authored
629 630 631 632 633
 * 处理一般筛选数据
 * @param origin 要处理的筛选数据 filter
 * @param params 当前 URL 中已有的参数,处理选中状态使用
 * @returns {{}}
 */
刘传洋 authored
634
exports.handleFilterData = (origin, params, total) => {
周少峰 authored
635
    let dest = {
周少峰 authored
636
        ageLevel: [],
周少峰 authored
637 638
        price: [],
        gender: [],
639
        color: [],
周少峰 authored
640 641 642 643 644
        checkedConditions: {
            conditions: []
        }
    };
yyq authored
645 646 647
    if (origin.brand) {
        dest.brand = formatterFilterBrands(origin.brand, origin.paramBrand, params);
    }
周少峰 authored
648 649 650 651 652

    // 处理价格筛选数据
    let priceRangechecked = false;

    if (!_.isEmpty(origin.priceRange)) {
刘传洋 authored
653 654
        let customPrice = _.split(params.price, ',');
周少峰 authored
655 656 657 658
        _.forEach(origin.priceRange, (value, key) => {
            if (params.price === key) {
                priceRangechecked = true;
            }
yyq authored
659 660 661
            if (_.isString(value)) {
                value = _.replace(value, '¥', '¥');
            }
周少峰 authored
662 663 664
            let price = {
                checked: params.price === key,
                href: handleFilterUrl(params, {price: key}),
665
                name: value,
周少峰 authored
666
                order: +key.split(',')[0]
周少峰 authored
667 668 669 670
            };

            dest.price.push(price);
        });
刘传洋 authored
671
672 673
        // 价格排序
        dest.price = _.sortBy(dest.price, (item) => {
周少峰 authored
674 675
            return item.order;
        });
676
刘传洋 authored
677 678 679 680
        dest.customPrice = {
            min: customPrice[0],
            max: customPrice[1]
        };
周少峰 authored
681 682 683 684
    }

    // 处理用户自主填写的价格区间
    if (!priceRangechecked && params.price) {
yyq authored
685 686 687
        let customPrice = _.split(params.price, ','),
            min = customPrice[0] || 0,
            name;
周少峰 authored
688
yyq authored
689 690 691 692 693 694
        customPrice[0] = customPrice[0];
        name =  ${min}-${customPrice[1]}`;

        if (!customPrice[1]) {
            customPrice[1] = '';
            name =  ${min}以上`;
周少峰 authored
695
        }
yyq authored
696 697 698 699 700 701 702

        dest.customPrice = {
            min: customPrice[0],
            max: customPrice[1]
        };
        dest.checkedConditions.conditions.push({
            name: name,
703
            itemType: 'price',
yyq authored
704 705
            href: handleFilterUrl(params, {}, {price: true})
        });
周少峰 authored
706 707 708 709 710 711 712 713 714 715 716 717 718 719
    }

    // 处理性别数据
    dest.gender = [{
        name: 'BOYS',
        href: handleFilterUrl(params, {gender: '1,3'}),
        checked: params.gender === '1,3'
    }, {
        name: 'GIRLS',
        href: handleFilterUrl(params, {gender: '2,3'}),
        checked: params.gender === '2,3'
    }];

    // 尺码处理
周少峰 authored
720
    if (!_.isEmpty(origin.size) && ((params.msort && !_.includes(params.msort, ',')) || params.misort || params.sort)) {
周少峰 authored
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747

        dest.size = [];

        if (_.isArray(origin.size)) {
            _.forEach(origin.size, value => {
                dest.size.push({
                    checked: parseInt(params.size, 10) === parseInt(value.size_id, 10),
                    href: handleFilterUrl(params, {
                        size: value.size_id
                    }),
                    name: value.size_name
                });
            });
        } else {
            dest.size.push({
                checked: parseInt(params.size, 10) === parseInt(origin.size.size_id, 10),
                href: handleFilterUrl(params, {
                    size: origin.size.size_id
                }),
                name: origin.size.size_name
            });
        }

        dest.checkedConditions.conditions = _.union(dest.checkedConditions.conditions,
            handleCheckedData(params, dest.size, 'size'));
    }
748 749
    // 处理颜色筛选数据
    if (!_.isEmpty(origin.color)) {
周少峰 authored
750
        _.forEach(origin.color, (value) => {
yyq authored
751 752
            value.color_code = value.color_code.replace(/#/ig, '');
753
            let color = {
周少峰 authored
754
                checked: parseInt(params.color, 10) === parseInt(value.color_id, 10),
755 756
                href: handleFilterUrl(params, {color: value.color_id}),
                name: value.color_name,
周少峰 authored
757
                rgb: value.color_value ? 'url(' + value.color_value.replace('http://', '//') + ')' : '#' + value.color_code
758 759
            };
周少峰 authored
760 761
            // 处理颜色选中数据
            if (color.checked) {
yyq authored
762 763 764
                let href = handleFilterUrl(params, null, {color: value.color_id});

                dest.checkedConditions.conditions.push({
yyq authored
765 766
                    itemType: 'color',
                    name: value.color_name,
yyq authored
767 768 769
                    href: href,
                    color: color.rgb
                });
周少峰 authored
770 771
            }
772 773 774 775
            dest.color.push(color);
        });
    }
周少峰 authored
776
    // 处理年龄段
周少峰 authored
777
    if (!_.isEmpty(origin.ageLevel)) {
周少峰 authored
778
刘传洋 authored
779 780
        // 只有一个默认选中
        let isChecked = origin.ageLevel.length === 1;
yyq authored
781
周少峰 authored
782 783 784
        do { // eslint-disable-line
            if (isChecked && origin.ageLevel[0].name === '成人' && +params.age_level !== 1) {
                break;
周少峰 authored
785 786
            }
周少峰 authored
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
            _.forEach(origin.ageLevel, (value) => {
                let ageLevel = {
                    checked: params.age_level === value.id || isChecked,
                    href: handleFilterUrl(params, {age_level: value.id}),
                    name: value.name
                };

                // 处理颜色年龄段数据
                if (ageLevel.checked || isChecked) {
                    ageLevel.href = handleFilterUrl(params, null, {age_level: value.id});
                    dest.checkedConditions.conditions.push(ageLevel);
                }

                dest.ageLevel.push(ageLevel);
            });
        } while (false);
周少峰 authored
803 804
    }
周少峰 authored
805
    // 清除所有选中数据
yyq authored
806 807
    // 某些特殊带频道信息页面,清除性别,需将gender设为1,2,3 (2017-3 配合SEO进行URL改造)
    let remainParams = {gender: '1,2,3'};
周少峰 authored
808 809 810 811 812 813 814 815 816 817 818

    if (params.id) {
        remainParams.id = params.id;
    }
    dest.checkedConditions.clearUrl = '?' + queryString.stringify(remainParams);

    // 处理频道筛选数据
    dest.checkedConditions.conditions = _.union(dest.checkedConditions.conditions,
        handleCheckedData(params, dest.gender, 'gender'));

    // 处理品牌筛选数据
yyq authored
819
    if (dest.brand && dest.brand.brandsShow) {
周少峰 authored
820 821

        dest.checkedConditions.conditions = _.union(dest.checkedConditions.conditions,
822
            handleBrandCheckedData(params, dest.brand.selectedBrands));
周少峰 authored
823 824 825 826 827 828
    }

    // 处理价格筛选数据
    dest.checkedConditions.conditions = _.union(dest.checkedConditions.conditions,
        handleCheckedData(params, dest.price, 'price'));
刘传洋 authored
829 830 831 832 833
    // 商品记录小于10,不显示价格区间
    if (total && total < 10) {
        dest.price = null;
    }
周少峰 authored
834 835 836 837
    return dest;
};

/**
yyq authored
838 839 840 841 842 843
 * 处理高级选项数据
 * @param origin 要处理的筛选数据 filter
 * @param params 当前 URL 中已有的参数,处理选中状态使用
 * @returns {{}}
 */
exports.handleSeniorFilterData = (data, params) => {
yyq authored
844
    let resData = {};
yyq authored
845 846 847

    if (data) {
        let chose = [];
yyq authored
848
        let conditions = [];
yyq authored
849 850

        if (!_.isEmpty(data.style)) {
yyq authored
851 852 853
            let sub = [],
                conName = [];
            let styles = _.split(_.get(params, 'style', ''), ',');
yyq authored
854 855

            _.forEach(data.style, value => {
yyq authored
856 857 858 859 860
                let ched = styles.indexOf(`${value.style_id}`) >= 0;

                if (ched && conName.length < 2) {
                    conName.push(value.style_name);
                }
yyq authored
861 862
                sub.push({
                    id: value.style_id,
yyq authored
863
                    checked: ched,
yyq authored
864 865 866 867
                    href: handleFilterUrl(params, {style: value.style_id}),
                    name: value.style_name
                });
            });
yyq authored
868 869 870 871

            if (conName.length) {
                conditions.push({
                    name: _.join(conName, '、'),
hongweigao authored
872
                    itemType: '风格',
yyq authored
873 874 875
                    href: handleFilterUrl(params, {}, {style: true})
                });
            }
刘传洋 authored
876 877 878 879 880 881 882 883 884

            if (sub && sub.length > 1) {
                chose.push({
                    name: '风格',
                    showMulti: true,
                    sub: sub,
                    attr: 'style'
                });
            }
yyq authored
885 886 887
        }

        if (!_.isEmpty(data.standard)) {
刘传洋 authored
888 889
            let qStandardStr = (params && params.standard) || '';
            let qStandard = {};
yyq authored
890
刘传洋 authored
891 892
            _.forEach(qStandardStr.split(','), value => {
                let val = value.split('_');
yyq authored
893
刘传洋 authored
894 895 896 897 898
                if (val && val.length >= 2) {
                    qStandard[parseInt(val[0], 10)] = parseInt(val[1], 10);
                }
            });
yyq authored
899
            _.forEach(data.standard, value => {
yyq authored
900 901 902
                if (!value) {
                    return;
                }
yyq authored
903
yyq authored
904 905
                let sub = [],
                    standardName = value.standard_name;
yyq authored
906 907

                _.forEach(value.sub, subValue => {
刘传洋 authored
908
                    let ched = qStandard[value.standard_id] === parseInt(subValue.standard_id, 10);
yyq authored
909 910 911 912

                    if (ched) {
                        conditions.push({
                            name: subValue.standard_name,
913
                            itemType: standardName,
yyq authored
914 915 916 917 918 919
                            href: handleFilterUrl(params, {
                                standard: getStandard(qStandard, null, {
                                    k: value.standard_id,
                                    v: subValue.standard_id
                                })
                            })
yyq authored
920 921 922
                        });
                    }
yyq authored
923 924
                    sub.push({
                        id: value.standard_id,
yyq authored
925
                        checked: ched,
yyq authored
926 927 928 929 930 931
                        href: handleFilterUrl(params, {
                            standard: getStandard(qStandard, {
                                k: value.standard_id,
                                v: subValue.standard_id
                            })
                        }),
yyq authored
932
                        name: subValue.standard_name
yyq authored
933 934
                    });
                });
刘传洋 authored
935 936 937 938 939 940 941 942

                // 只有一条不显示
                if (sub && sub.length > 1) {
                    chose.push({
                        name: value.standard_name,
                        sub: sub
                    });
                }
yyq authored
943
            });
yyq authored
944 945 946 947 948
        }

        if (!_.isEmpty(chose)) {
            resData.seniorChose = chose;
        }
yyq authored
949 950 951 952

        if (!_.isEmpty(conditions)) {
            _.set(resData, 'checkedConditions.conditions', conditions);
        }
yyq authored
953 954 955
    }

    return resData;
yyq authored
956
};
yyq authored
957
yyq authored
958
exports.handleFilterDataAll = (data, qs) => {
yyq authored
959
    let destFilter = {};
yyq authored
960 961 962
    let params = _.cloneDeep(qs);

    _.unset(params, 'page'); // 去除筛选项page
刘传洋 authored
963
yyq authored
964
    let baseFilter = this.handleFilterData(_.get(data, 'filter', {}), params, data.total);
刘传洋 authored
965 966 967 968 969
    let seniorFilter = this.handleSeniorFilterData({
        style: _.get(data, 'filter.style', []),
        standard: _.get(data, 'standard', [])
    }, params);
yyq authored
970 971
    let conditions = _.union(_.get(baseFilter, 'checkedConditions.conditions'),
        _.get(seniorFilter, 'checkedConditions.conditions'));
刘传洋 authored
972
yyq authored
973
    Object.assign(destFilter, seniorFilter, baseFilter);
刘传洋 authored
974
yyq authored
975
    _.set(destFilter, 'checkedConditions.conditions', conditions);
刘传洋 authored
976
    return destFilter;
刘传洋 authored
977 978
};
yyq authored
979
/**
周少峰 authored
980 981 982
 * 根据页面设置面包屑导航
 * @type {[type]}
 */
刘传洋 authored
983 984 985 986 987 988 989 990
exports.handlePathNavData = (data, params, page, channel) => {

    let rootName = '首页';

    if (channel && _.isString(channel)) {
        rootName = channel.toUpperCase() + rootName;
    }
周少峰 authored
991
    let pathNav = [{
周少峰 authored
992
        href: indexUrl[channel],
刘传洋 authored
993 994
        name: rootName, // '首页', // TODO 从根据cookie获取频道
        pathTitle: rootName // '首页'
周少峰 authored
995 996 997 998
    }];

    switch (page) {
        case 'search':
周少峰 authored
999
            // 搜索结果提示
周少峰 authored
1000
            if (params.query || params.keywords) {
周少峰 authored
1001
                params.query = params.query || params.keywords;
周少峰 authored
1002
                pathNav.push({
周少峰 authored
1003 1004 1005
                    name: '“<span id="nav_keyword">' +
                    params.query + '</span>”&nbsp;&nbsp;共<span id="nav_keyword_count">' +
                    data.total + '</span>个结果'
周少峰 authored
1006 1007 1008 1009 1010 1011 1012 1013
                });
            } else {
                pathNav.push({
                    name: '所有商品'
                });
            }
            break;
        case 'brand':
周少峰 authored
1014
周少峰 authored
1015
            // 品牌
周少峰 authored
1016 1017 1018 1019
            pathNav.push(
                {
                    name: '品牌一览',
                    pathTitle: '品牌一览',
周少峰 authored
1020
                    href: helpers.urlFormat('/brands')
周少峰 authored
1021 1022
                },
                {
周少峰 authored
1023 1024
                    name: data.brandNameEn + data.brandNameCn,
                    pathTitle: data.brandNameEn + data.brandNameCn
周少峰 authored
1025 1026 1027
                }
            );
            break;
周少峰 authored
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
        case 'shop':

            // 店铺
            pathNav.push(
                {
                    name: data.brandName,
                    pathTitle: data.brandName
                }
            );
            break;
周少峰 authored
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
        case 'new':

            // 新品到着
            pathNav.push(
                {
                    name: '新品到着',
                    pathTitle: '新品到着'
                }
            );
            break;
周少峰 authored
1048
        default :
周少峰 authored
1049
            // 分类
yyq authored
1050 1051 1052 1053

            _.forEach(data, sort => {
                let navList = [];
yyq authored
1054
                if (`${params.category_id}` === `${sort.category_id}`) {
yyq authored
1055 1056 1057 1058
                    navList = [{
                        name: sort.category_name,
                        pathTitle: sort.category_name
                    }];
周少峰 authored
1059
                }
周少峰 authored
1060
周少峰 authored
1061 1062
                if (!_.isEmpty(sort.sub)) {
                    _.forEach(sort.sub, misort => {
yyq authored
1063
                        if (`${params.category_id}` === `${misort.category_id}`) {
yyq authored
1064 1065 1066 1067 1068 1069 1070 1071 1072
                            navList = [{
                                name: sort.category_name,
                                href: handleFilterUrl(sort.relation_parameter),
                                pathTitle: sort.category_name
                            },
                            {
                                name: misort.category_name,
                                pathTitle: misort.category_name
                            }];
周少峰 authored
1073 1074 1075
                        }
                    });
                }
yyq authored
1076 1077 1078 1079

                if (!_.isEmpty(navList)) {
                    pathNav = _.concat(pathNav, navList);
                }
周少峰 authored
1080 1081 1082 1083
            });
            break;
    }
    return {
1084 1085
        pathNav: pathNav,
        listType: page
周少峰 authored
1086 1087 1088 1089 1090 1091 1092
    };
};

/**
 * 分页
 * @param  {[type]} total 总页数
 * @param  {[type]} params 筛选条件
刘传洋 authored
1093
 * @param  {[noNextBtn]} 列表是否有下一页的按钮,如果有则实际查询数比传递参数的少一个,入60 => 59
周少峰 authored
1094 1095
 * @return {[type]}        [description]
 */
刘传洋 authored
1096
exports.handlePagerData = (total, params, noNextBtn) => {
周少峰 authored
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
    let dest = {
        prePage: {
            url: ''
        },
        nextPage: {
            url: ''
        },
        pages: []
    };
刘传洋 authored
1107
    let currentPage = parseInt(_.get(params, 'page', 1), 10); // 当前页
周少峰 authored
1108
    let perPageCount = parseInt(_.get(params, 'limit', noNextBtn === true ? 60 : 59), 10); // 每页商品数
刘传洋 authored
1109 1110
    let totalPage = Math.ceil(total / perPageCount); // 总页数
周少峰 authored
1111
    if (noNextBtn !== true) {
刘传洋 authored
1112 1113 1114 1115 1116 1117 1118
        switch (perPageCount) {
            case 200:
                perPageCount = 199;
                break;
            case 100:
                perPageCount = 99;
                break;
周少峰 authored
1119
            default :
刘传洋 authored
1120 1121 1122 1123
                perPageCount = 59;
                break;
        }
    }
周少峰 authored
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162

    if (currentPage === 1) {
        // 当前页为 1,一定没有上一页
        delete dest.prePage;
    } else {
        dest.prePage.url = handleFilterUrl(params, {page: currentPage - 1});
    }

    if (currentPage === totalPage) {

        // 当前页为最后一页,一定没有下一页
        delete dest.nextPage;
    } else {
        dest.nextPage.url = handleFilterUrl(params, {page: currentPage + 1});
    }

    // 页码临时数据
    let pages = [];

    if (currentPage > 2 && currentPage <= totalPage - 2) {
        for (let i = currentPage - 2; i <= ((currentPage + 2) > totalPage ? totalPage : (currentPage + 2)); i++) {
            pages.push({
                url: handleFilterUrl(params, {page: i}),
                num: i,
                cur: currentPage === i
            });
        }

        // 处理页码小于等于 2 的情况
    } else if (currentPage <= 2) {
        for (let i = 1; i <= (totalPage < 5 ? totalPage : 5); i++) {
            pages.push({
                url: handleFilterUrl(params, {page: i}),
                num: i,
                cur: currentPage === i
            });
        }
    } else if (currentPage > totalPage - 2) {
        for (let i = totalPage; i >= totalPage - 4; i--) {
刘传洋 authored
1163
刘传洋 authored
1164
            if (i > 0) {
刘传洋 authored
1165 1166 1167 1168 1169 1170
                pages.push({
                    url: handleFilterUrl(params, {page: i}),
                    num: i,
                    cur: currentPage === i
                });
            }
周少峰 authored
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
        }
        pages = _.sortBy(pages, ['num']);
    }

    let prevPages = [];
    let nextPages = [];

    if (_.size(pages) === 5) {
        if (currentPage > 4) {
            prevPages.push({
                url: handleFilterUrl(params, {page: 1}),
                num: 1
            });
            prevPages.push({
                num: '...'
            });
        }
刘传洋 authored
1188
        if (currentPage < totalPage - 2 && totalPage > 5) {
周少峰 authored
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
            nextPages.push({
                num: '...'
            });
            nextPages.push({
                url: handleFilterUrl(params, {page: totalPage}),
                num: totalPage
            });
        }
    }

    // 商品开始结束数
    dest.tip = {
        total: total,
        start: (currentPage ? currentPage - 1 : 0) * perPageCount + 1
    };

    let endPageNum = (totalPage === currentPage) ?
        total : parseInt(dest.tip.start, 10) + perPageCount - 1;

    dest.tip.end = (total < perPageCount) ? total : endPageNum;
    dest.pages = _.concat(prevPages, pages, nextPages);

    return dest;

};

/**
周少峰 authored
1216 1217 1218
 * 处理分类介绍
 * @type {[type]}
 */
yyq authored
1219
exports.handleSortIntro = (data) => {
周少峰 authored
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
    let sortIntro = {
        name: data.title,
        enName: data.subtitle,
        description: data.intro,
        img: data.logo,
        keyEntry: []
    };

    _.forEach(data.keyword, function(value) {
        sortIntro.keyEntry.push({
            name: value.word,
htoooth authored
1231
            url: value.url.replace(/(parameter_)(\d+)=(\d+)/, (match, p1, p2, p3) => `standard=${p2}_${p3}`)
周少峰 authored
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
        });
    });

    return sortIntro;
};

/**
 * 处理分类页左侧广告
 * @type {[type]}
 */
周少峰 authored
1242
exports.handleSortAds = (data) => {
周少峰 authored
1243
    let sortAds = {
周少峰 authored
1244 1245 1246 1247
        picLink: {
            picTitle: '',
            list: []
        }
周少峰 authored
1248 1249
    };
周少峰 authored
1250 1251 1252 1253 1254 1255
    _.forEach(data, function(value) {
        sortAds.picLink.list.push({
            href: value.ads_url,
            src: value.ads_image
        });
    });
周少峰 authored
1256 1257 1258 1259 1260

    return sortAds;
};

/**
周少峰 authored
1261 1262 1263 1264
 * 处理一周新品上架
 * @type {[type]}
 */
exports.handleWeekNew = (data, params) => {
yyq authored
1265
    let list = [];
周少峰 authored
1266 1267 1268 1269 1270 1271 1272 1273
    let dest = {
        newSales: {
            name: '',
            list: []
        }
    };

    _.forEach(data.recent, function(value, key) {
周少峰 authored
1274
        let t = _.split(key, '-'),
周少峰 authored
1275
            dayStart = new Date(t[0], _.parseInt(t[1]) - 1, t[2]).getTime() / 1000,
周少峰 authored
1276
            day = `${dayStart},${dayStart + 86400}`;
yyq authored
1277 1278 1279

        list.push({
            name: `${t[1] || ''}${t[2] || ''}日`,
周少峰 authored
1280 1281
            href: handleFilterUrl(params, {shelveTime: day}),
            active: day === params.shelveTime,
yyq authored
1282
            sort: +t.join('')
周少峰 authored
1283 1284 1285
        });
    });
yyq authored
1286 1287 1288 1289
    _.set(dest, 'newSales.list', _.sortBy(list, function(o) {
        return -o.sort;
    }));
周少峰 authored
1290 1291
    return dest;
};
周少峰 authored
1292
周少峰 authored
1293
/**
周少峰 authored
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
 * 处理搜索提示
 * @type {[type]}
 */
exports.handleSuggest = (data) => {
    let suggest = [];

    _.forEach(data, function(value) {
        suggest.push({
            href: helpers.urlFormat('', {query: value.keyword}, 'search'),
            keyword: value.keyword,
            count: value.count
        });
    });

    return suggest;
};
周少峰 authored
1311 1312 1313 1314
/**
 * 处理品牌页banner
 * @type {[type]}
 */
周少峰 authored
1315 1316 1317
exports.handleBrandBanner = (data) => {
    return {
        bannerHeight: 150,
yyq authored
1318
        banner: helpers.getForceSourceUrl(data.brandBanner || ''),
周少峰 authored
1319 1320 1321 1322 1323 1324
        brandHome: data.url,
        brandIntro: helpers.urlFormat('/about', '', data.brandDomain),
        dataId: data.brandId
    };
};
周少峰 authored
1325
/**
周少峰 authored
1326
 * 处理品牌系列folder_id
周少峰 authored
1327 1328
 * @type {[type]}
 */
yyq authored
1329 1330 1331 1332
exports.handleFolderData = (data, params) => {
    params = params || {};

    let baseQs = params.shopId ? `&shopId=${params.shopId}` : '';
周少峰 authored
1333
    let dest = [];
周少峰 authored
1334
周少峰 authored
1335 1336
    _.forEach(data, (value) => {
        dest.push({
yyq authored
1337
            href: `?folder_id=${value.id}${baseQs}`,
周少峰 authored
1338
            src: value.brand_sort_ico
周少峰 authored
1339 1340
        });
    });
周少峰 authored
1341 1342

    return dest;
周少峰 authored
1343
};
周少峰 authored
1344
周少峰 authored
1345 1346 1347 1348
/**
 * 处理品牌系列series
 * @type {[type]}
 */
yyq authored
1349 1350 1351 1352
exports.handleSeriesData = (data, params) => {
    params = params || {};

    let baseQs = params.shopId ? `&shopId=${params.shopId}` : '';
周少峰 authored
1353 1354 1355 1356
    let dest = [];

    _.forEach(data, (value) => {
        dest.push({
yyq authored
1357
            href: `?series=${value.id}${baseQs}`,
周少峰 authored
1358 1359
            src: value.series_banner
        });
周少峰 authored
1360
    });
周少峰 authored
1361
周少峰 authored
1362
    return dest;
周少峰 authored
1363
};
周少峰 authored
1364 1365

/**
周少峰 authored
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
 * 筛选类链接处理的对外接口
 * @type {[type]}
 */
exports.handleFilterUrl = handleFilterUrl;

/**
 * 处理选中数据的对外接口
 * @type {[type]}
 */
exports.handleCheckedData = handleCheckedData;
刘传洋 authored
1376 1377 1378

exports.handleNextPage = (params, total) => {
    let href;
yyq authored
1379 1380
    let currentPage = parseInt((params.page ? params.page : 1), 10); // 当前页
    let perPageCount = parseInt((params.limit ? params.limit : 60) - 1, 10); // 每页商品数
yyq authored
1381
    let totalPage = _.ceil(total / perPageCount); // 总页数
刘传洋 authored
1382
yyq authored
1383
    if (currentPage >= totalPage) {
刘传洋 authored
1384 1385 1386 1387 1388 1389
        return null;
    } else {
        href = handleFilterUrl(params, {page: currentPage + 1});
    }

    return {
刘传洋 authored
1390 1391
        href: href,
        src: '//img10.static.yhbimg.com/product/2014/01/15/11/01fa01614784f6239760f1b749663016f1.jpg?' +
htoooth authored
1392
        'imageMogr2/thumbnail/235x314/extent/235x314/background/d2hpdGU=/position/center/quality/90'
刘传洋 authored
1393 1394
    };
};
刘传洋 authored
1395 1396 1397 1398 1399

const getChannelName = channel => {

    let channelName = '';
刘传洋 authored
1400
    switch (channel) {
刘传洋 authored
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
        case 'boys':
            channelName = '男生';
            break;
        case 'girls':
            channelName = '女生';
            break;
        case 'kids':
            channelName = '潮童';
            break;
        case 'lifestyle':
            channelName = '创意生活';
            break;
        default:
            channelName = '男生';
    }

    return channelName;
};

/**
 * 列表页seo
 * @param channel
 * @param sorts
 * @param checked
 * @returns {{title: string, keywords: string, description: string}}
 */
exports.getListSeo = (channel, sorts, checked) => {
yyq authored
1428 1429
    let keyArr = ['sort', 'brand', 'color', 'size', 'price', 'channel', 'senior'];
    let kd = {};
刘传洋 authored
1430 1431

    _.forEach(sorts, val => {
刘传洋 authored
1432
        if (val.active) {
yyq authored
1433
            kd.sort = val && val.name;
刘传洋 authored
1434
            _.forEach(val.childList, sub => {
yyq authored
1435
                if (sub.childActive && sub.categoryId !== val.categoryId) {
yyq authored
1436
                    kd.sort = sub && sub.name;
刘传洋 authored
1437 1438 1439 1440 1441
                }
            });
        }
    });
yyq authored
1442
    checked = checked || [];
刘传洋 authored
1443
    _.forEach(checked, ck => {
1444 1445 1446 1447 1448
        if (!(ck && ck.itemType)) {
            return false;
        }

        switch (ck && ck.itemType) {
yyq authored
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
            case 'brand':
            case 'color':
            case 'size':
            case 'price':
                kd[ck.itemType] = ck.name;
                break;
            case 'gender':
                kd.channel = ck.name === 'GIRLS' ? '女生' : '男生';
                break;
            default:
                if (ck.itemType) {
                    if (!kd.senior) {
                        kd.senior = '';
                    } else {
                        kd.senior += '、';
                    }
                    kd.senior += ck.name;
                }
                break;
刘传洋 authored
1468 1469 1470
        }
    });
yyq authored
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
    // 无筛选参数时默认tdk
    if (_.isEmpty(kd)) {
        return {
            title: '潮流服装配饰,创意生活用品_男生|女生|潮童服装,鞋履,配饰品牌正品-YOHO!BUY有货',
            keywords: '潮流服装配饰,创意生活用品,男生服装配饰,女生服装配饰,潮童服装配饰',
            description: '潮流服装配饰及创意生活正品网购!YOHO!BUY有货提供男生、女生、潮童服装配饰。100%品牌正品保证,支持货到付款。'
        };
    }

    _.forEach(keyArr, val => {
        kd[val] = kd[val] || '';
    });
刘传洋 authored
1483
yyq authored
1484 1485 1486 1487
    let baseInfo = `${kd.brand}${kd.sort}`;
    let title = `新款${kd.brand}${kd.channel}${kd.color}${kd.size}${kd.sort}${kd.price},${kd.brand}${kd.senior}${kd.sort}品牌正品|YOHO!BUY有货`, // eslint-disable-line
        keywords = `新款${kd.brand}${kd.channel}${kd.color}${kd.size}${kd.sort}${kd.price},${kd.brand}${kd.senior}${kd.sort}品牌正品`, // eslint-disable-line
        description = `正品网购!YOHO!BUY有货提供新款${kd.brand}${kd.channel}${kd.color}${kd.size}${kd.sort}${kd.price},${kd.brand}${kd.senior}${kd.sort}100%品牌正品保证,支持货到付款。`; // eslint-disable-line
刘传洋 authored
1488 1489

    return {
yyq authored
1490 1491 1492
        title: `${baseInfo ? baseInfo + '|' : ''}${title}`,
        keywords: `${baseInfo ? baseInfo + ',' : ''}${keywords}`,
        description: `${baseInfo}${description}`
刘传洋 authored
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
    };
};

/**
 * 新品到页 seo
 * @param channel
 * @param dlist
 * @returns {{title: string, keywords: string, description: string}}
 */
exports.getNewSeo = (channel, dlist) => {

    let channelName = getChannelName(channel),
        nlabel = '';

    _.forEach(dlist, d => {
刘传洋 authored
1508
        if (d && d.active) {
刘传洋 authored
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
            nlabel = d.name || '';
            return false;
        }
    });

    let newTitle = channelName + nlabel;

    return {
        title: newTitle + '新品上架-YOHO!BUY 有货 100%正品保证',
        keywords: newTitle + '新品上架',
        description: newTitle + '新品上架,正品网购,官方授权!YOHO! 有货中国最大的潮流商品购物网站。100%品牌正品保证,支持货到付款。'
刘传洋 authored
1520
    };
刘传洋 authored
1521 1522
};
1523
/**
yyq authored
1524
 * 品牌店铺页 seo
1525
 * @param channel
yyq authored
1526 1527
 * @param info
 * @param qs
1528 1529
 * @returns {{title: (string|*|string), keywords: string, description: string}}
 */
yyq authored
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
exports.getBrandShopSeo = (channel, info, qs) => {
    let resData = {},
        ctype = {
            boys: {fashionType: '男装', brandType: '男生品牌'},
            girls: {fashionType: '女装', brandType: '女生品牌'},
            kids: {fashionType: '童装', brandType: '潮童品牌'},
            lifestyle: {fashionType: '创意生活', brandType: '创意生活品牌'}
        };
    let params = {
        nameEn: '',
        name: ''
刘传洋 authored
1541
    };
yyq authored
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578

    if (info) {
        Object.assign(params, {
            nameEn: _.get(info, 'brandNameEn', ''),
            nameCn: _.get(info, 'brandNameCn', '')
        });

        params.name = `${params.nameEn} ${params.nameCn}`;

        if (info.shopName) {
            Object.assign(params, {
                nameEn: info.shopName,
                name: info.shopName
            });
        }
    }

    if (qs && qs.gender) {
        if (qs.gender === '1,3') {
            channel = 'boys';
        } else if (qs.gender === '2,3') {
            channel = 'girls';
        }
        Object.assign(params, ctype[channel] || ctype.boys);
        Object.assign(resData, {
            title: `${params.nameEn} | ${params.name} ${params.brandType}`,
            keywords: `${params.nameEn}, ${params.name} ${params.fashionType},${params.nameEn} ${params.brandType}`,
            description: `${params.nameEn}正品网购。${params.name}官方授权!`
        });
    } else {
        Object.assign(resData, {
            title: `${params.nameEn} | ${params.name}官网`,
            keywords: `${params.nameEn}, ${params.name}官网`,
            description: `${params.nameEn}正品网购。${params.name}官方授权!`
        });
    }
yyq authored
1579 1580
    resData.title += resData.title ? ' | YOHO!BUY 有货 100%正品保证' : '';
    resData.description += resData.description ? ' YOHO!BUY 有货中国最大的潮流商品购物网站。100%品牌正品保证,支持货到付款。' : ''; // eslint-disable-line
yyq authored
1581 1582

    return resData;
刘传洋 authored
1583
};
1584
yyq authored
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
exports.getAboutSeo = (name, intro, shop) => {
    let type = shop ? '店铺' : '品牌';

    intro = intro || '';

    intro = _.truncate(_.replace(intro, /<\/?[^>]*>|\s*|(\n)|(\t)|(\r)/g, ''), {
        length: 156,
        omission: '...'
    });

    return {
        title: `${name}品牌 | ${name}${type}介绍 | YOHO!BUY有货 100%正品保证`,
        keywords: `${name}品牌, ${name}${type}介绍`,
        description: `${name}${type}介绍 ${intro}`
    };
};
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
/**
 * 获取商品的前三个skn 提供给第三方推广使用
 * @param glist
 * @returns {Array}
 */
exports.getCriteo = (glist) => {

    let sknArr = [];

    if (glist) {
        _.forEach(glist, (val) => {
刘传洋 authored
1614 1615
            if (sknArr.length < 3) {
                if (val && val.product_skn) {
刘传洋 authored
1616 1617
                    sknArr.push(val.product_skn);
                }
1618 1619 1620 1621 1622 1623 1624 1625
            } else {
                return false;
            }
        });
    }

    return sknArr;
};
刘传洋 authored
1626
刘传洋 authored
1627
// handlePagerData
刘传洋 authored
1628 1629 1630 1631
exports.getSearchParams = params => {

    let mlimit = 59;
刘传洋 authored
1632
    if (params && params.limit) {
刘传洋 authored
1633 1634 1635

        let olimit = parseInt(params.limit, 10);
刘传洋 authored
1636
        switch (olimit) {
刘传洋 authored
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
            case 100:
                mlimit = 99;
                break;
            case 200:
                mlimit = 199;
                break;
            default:
                mlimit = 59;
        }
    }

    /** 处理页面数据 **/
    let nparams = {
        limit: mlimit
    };

    if (params && params.price) {
        let mp = params.price.split(',');
        let nmp = [];

        nmp[0] = (mp && mp[0]) || 0;
        nmp[1] = (mp && mp[1]) || 99999;
        nparams.price = nmp.join(',');
    }
1662 1663 1664 1665 1666 1667 1668 1669 1670
    // 未登录状态uid传个性化推荐id(即上次登录cookie中存储的_PRID)
    if (_.has(params, 'prid')) {
        if (!params.uid && params.prid) {
            nparams.uid = params.prid;
        }

        _.unset(params, 'prid');
    }
htoooth authored
1671 1672 1673 1674 1675 1676 1677
    // 商品详情页促销跳转
    if (params && params.psp_id) {
        Object.assign(nparams, {
            promotion_id: params.psp_id,
            method: 'app.search.promotion'
        });
    }
htoooth authored
1678
hongweigao authored
1679
    // 优惠券商品列表
OF1706 authored
1680
    if (params && params.cpc_id && params.coupon_code) {
hongweigao authored
1681 1682
        Object.assign(nparams, {
            coupon_id: params.cpc_id,
OF1706 authored
1683
            coupon_code: params.coupon_code,
hongweigao authored
1684 1685 1686 1687 1688 1689
            method: 'app.search.coupon'
        });
    }


htoooth authored
1690
    /** 查询参数再处理 **/
htoooth authored
1691 1692 1693 1694 1695
    // 对可能有中文的情况进行处理
    if (params.query) {
        params.query = decodeURIComponent(params.query);
    }
刘传洋 authored
1696
    return Object.assign({}, params, nparams);
刘传洋 authored
1697
};
1698 1699 1700

exports.handleFilterBrands = (brands, params) => {
    return formatterFilterBrands(brands, null, params);
周少峰 authored
1701
};
yyq authored
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727

exports.handleBrandShopCoupons = (data, params) => {
    let resData = {};
    let length;

    data = data || [];
    params = params || {};
    length = data.length || 0;

    if (length) {
        _.forEach(data, value => {
            Object.assign(value, {
                coupon_id: crypto.encryption('', `${value.coupon_id}`),
                money: +value.money
            });
        });

        if (length === 1) {
            data[0].showUnit = true;
        }

        Object.assign(resData, {
            list: data,
            length: length,
            showNextBtn: length > 3
        }, params);
yyq authored
1728 1729

        return resData;
yyq authored
1730 1731
    }
yyq authored
1732
    return false;
yyq authored
1733 1734
};
OF1706 authored
1735 1736