Blame view

apps/product/models/public-handler.js 23.4 KB
郭成尧 authored
1
/*
郭成尧 authored
2 3 4
 * @Author: Targaryen
 * @Date:   2016-06-02 15:50:47
 * @Last Modified by:   Targaryen
郭成尧 authored
5
 * @Last Modified time: 2016-06-22 18:36:26
郭成尧 authored
6
 */
郭成尧 authored
7 8 9

'use strict';
const _ = require('lodash');
姜枫 authored
10
const helpers = global.yoho.helpers;
周少峰 authored
11
const queryString = require('querystring');
郭成尧 authored
12 13

/**
郭成尧 authored
14 15 16 17
 * 处理用于筛选的 URL , 拼接 URL 参数
 * @param originParam 当前 URL 中的参数
 * @param newParam 要拼接的 参数
 * @returns {string}
郭成尧 authored
18
 */
郭成尧 authored
19
const handleFilterUrl = (originParam, newParam) => {
郭成尧 authored
20 21 22
    let dest = '?';
    let tempOriginParam = {};
郭成尧 authored
23
    tempOriginParam = Object.assign(tempOriginParam, originParam, newParam);
郭成尧 authored
24
    delete tempOriginParam.uid;
郭成尧 authored
25
郭成尧 authored
26
    _.forEach(tempOriginParam, function(value, key) {
郭成尧 authored
27 28 29 30 31 32 33
        dest = dest + key + '=' + value + '&';
    });

    return _.trim(dest, '&');
};

/**
34
 * 处理最新降价折扣信息
郭成尧 authored
35 36
 * @param params 当前 URL 的参数
 * @returns {Array|*[]}
郭成尧 authored
37
 */
38
const handleSaleNewSaleSortData = (params) => {
郭成尧 authored
39
    return [{
40
        name: '1~3折',
郭成尧 authored
41
        href: handleFilterUrl(params, {p_d: '0.1,0.3'}),
郭成尧 authored
42
        checked: params.p_d === '0.1,0.3'
43 44
    }, {
        name: '4~6折',
郭成尧 authored
45
        href: handleFilterUrl(params, {p_d: '0.4,0.6'}),
郭成尧 authored
46
        checked: params.p_d === '0.4,0.6'
47 48
    }, {
        name: '7折以上',
郭成尧 authored
49
        href: handleFilterUrl(params, {p_d: '0.7,1'}),
郭成尧 authored
50
        checked: params.p_d === '0.7,1'
51 52 53 54 55 56 57 58
    }];
};

/**
 * 处理断码区分类筛选数据
 * @return {[type]} [description]
 */
const handleSalebreakingYardsSortData = (origin, params) => {
郭成尧 authored
59
    let dest = [];
60 61 62 63

    if (!_.isEmpty(params.sort)) {

        // 是否有分类筛选参数,如果有
徐祁xuqi authored
64 65
        _.forEach(params.sort.split(','), sortParam => {
            _.forEach(origin, value => {
66 67 68

                // 判断是否在这组数据里面
                if (!_.isEmpty(_.find(value.sort_id.split(','), o => {
郭成尧 authored
69 70
                    return o === sortParam;
                }))) {
71 72

                    // 如果在某组数据里面,组合数据退出循环
徐祁xuqi authored
73 74 75 76 77 78 79 80
                    _.forEach(value.sub, subValue => {
                        dest.push({
                            checked: +params.size === +subValue.size_id,
                            href: handleFilterUrl(params, {
                                size: subValue.size_id
                            }),
                            name: subValue.size_name
                        });
81 82 83 84 85 86
                    });
                    return false;
                }
            });
        });
    } else { // 如果没有筛选参数,返回所有分类
徐祁xuqi authored
87 88 89 90 91 92 93 94 95
        _.forEach(origin, value => {
            _.forEach(value.sub, subValue => {
                dest.push({
                    checked: +params.size === +subValue.size_id,
                    href: handleFilterUrl(params, {
                        size: subValue.size_id
                    }),
                    name: subValue.size_name
                });
96 97 98 99 100 101
            });
        });
    }

    return _.uniqWith(dest, _.isEqual);
};
郭成尧 authored
102 103

/**
郭成尧 authored
104 105 106 107 108 109
 * 处理选中数据
 * @param  {[type]} params [description]
 * @param  {[type]} origin [description]
 * @param  {[type]} param  [description]
 * @return {[type]}        [description]
 */
110
const handleCheckedData = (params, origin, param) => {
郭成尧 authored
111
    let dest = [];
郭成尧 authored
112 113 114

    // 品牌选中数据
    if (!_.isEmpty(origin)) {
徐祁xuqi authored
115
        _.forEach(origin, value => {
郭成尧 authored
116
            if (value.checked) {
徐祁xuqi authored
117
                let tempPatam = {};
郭成尧 authored
118
徐祁xuqi authored
119
                tempPatam[param] = '';
郭成尧 authored
120
徐祁xuqi authored
121 122 123 124
                dest.push({
                    name: value.name,
                    href: handleFilterUrl(params, tempPatam)
                });
郭成尧 authored
125 126 127 128 129 130
            }
        });
    }

    return dest;
};
郭成尧 authored
131 132

/**
郭成尧 authored
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
 * 处理品牌筛选数据
 * @param params
 * @param origin
 * @returns {{}}
 */
const handleBrandCheckedData = (params, origin) => {
    let dest = [];

    // 分拆品牌参数
    let brands = _.split(params.brand, ',');
    let intBrands = [];

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

    let checkedCount = 0;

    // 遍历品牌数据,如果在参数中,那么加 checked,将此数据加入到 checked 数组中
    if (!_.isEmpty(origin)) {
        _.forEach(origin, (value) => {
            if (typeof _.find(intBrands, o => {
郭成尧 authored
155 156
                return _.isEqual(o, value.id);
            }) !== 'undefined') {
郭成尧 authored
157 158 159 160 161 162 163 164 165 166 167 168 169 170 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

                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);
                if (checkedCount === 0) {
                    dest[0] = checked;
                } else if (checkedCount === 1) {
                    dest[0].name += '、' + checked.name.substring(0, 3) + '...';
                }
                checkedCount++;

                // 建议保留,需要品牌可以单个删除时放开注释即可
                // dest.push(checked);
            }
        });
    }
    return dest;
};

/**
郭成尧 authored
200
 * 处理 opts 排序数据
郭成尧 authored
201 202 203 204
 * @param params
 * @param total
 * @param extra 什么都可以传进来,多个参数传Object
 * @returns {{}}
郭成尧 authored
205
 */
郭成尧 authored
206
exports.handleSaleOptsData = (params, total, extra) => {
徐祁xuqi authored
207 208 209
    let dest = {
        sortType: []
    };
郭成尧 authored
210
郭成尧 authored
211 212 213 214
    // 用来标记是否是折扣专场,折扣专场只需要前三个排序参数
    let count = (extra === 'discont') ? 3 : 4;

    for (let i = 0; i < count; i++) {
郭成尧 authored
215 216 217 218
        let opt = {};

        switch (i) {
            case 0:
郭成尧 authored
219
                opt.href = handleFilterUrl(params, {order: 's_t_desc'});
郭成尧 authored
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
                if (extra === 'discont') { // 如果是折扣专场
                    opt.name = '全部';
                    if (_.isEmpty(params.order) || params.order === 's_t_desc') {
                        opt.active = true;
                    } else {
                        opt.active = false;
                    }
                } else {
                    opt.name = '最新';
                    opt.hasSortOrient = true;
                    if (_.isEmpty(params.order) || params.order === 's_t_desc') {
                        opt.active = true;
                    } else {
                        opt.active = false;
                    }
                    opt.desc = true;
                }
郭成尧 authored
237 238
                break;
            case 1:
郭成尧 authored
239
                if (params.order !== 's_p_desc' && params.order !== 's_p_asc') {
郭成尧 authored
240
                    opt.href = handleFilterUrl(params, {order: 's_p_desc'});
郭成尧 authored
241
                    opt.hasSortOrient = true;
郭成尧 authored
242
                } else {
郭成尧 authored
243 244
                    opt.hasSortOrient = true;
                    opt.active = true;
郭成尧 authored
245
                    if (params.order === 's_p_desc') {
郭成尧 authored
246
                        opt.href = handleFilterUrl(params, {order: 's_p_asc'});
郭成尧 authored
247 248
                        opt.desc = false;
                    } else {
郭成尧 authored
249
                        opt.href = handleFilterUrl(params, {order: 's_p_desc'});
郭成尧 authored
250 251
                        opt.desc = true;
                    }
郭成尧 authored
252
                }
郭成尧 authored
253
郭成尧 authored
254 255 256
                opt.name = '价格';
                break;
            case 2:
郭成尧 authored
257
                if (params.order !== 'p_d_desc' && params.order !== 'p_d_asc') {
郭成尧 authored
258
                    opt.href = handleFilterUrl(params, {order: 'p_d_desc'});
郭成尧 authored
259
                    opt.hasSortOrient = true;
郭成尧 authored
260
                } else {
郭成尧 authored
261 262
                    opt.hasSortOrient = true;
                    opt.active = true;
郭成尧 authored
263
                    if (params.order === 'p_d_desc') {
郭成尧 authored
264
                        opt.href = handleFilterUrl(params, {order: 'p_d_asc'});
郭成尧 authored
265 266
                        opt.desc = false;
                    } else {
郭成尧 authored
267
                        opt.href = handleFilterUrl(params, {order: 'p_d_desc'});
郭成尧 authored
268 269
                        opt.desc = true;
                    }
郭成尧 authored
270
                }
郭成尧 authored
271
郭成尧 authored
272 273
                opt.name = '折扣';
                break;
郭成尧 authored
274
            case 3:
郭成尧 authored
275
                if (params.order !== 's_n_desc' && params.order !== 's_n_asc') {
郭成尧 authored
276
                    opt.href = handleFilterUrl(params, {order: 's_n_desc'});
郭成尧 authored
277
                    opt.hasSortOrient = true;
郭成尧 authored
278
                } else {
郭成尧 authored
279 280
                    opt.hasSortOrient = true;
                    opt.active = true;
郭成尧 authored
281
                    if (params.order === 's_n_desc') {
郭成尧 authored
282
                        opt.href = handleFilterUrl(params, {order: 's_n_asc'});
郭成尧 authored
283 284
                        opt.desc = false;
                    } else {
郭成尧 authored
285
                        opt.href = handleFilterUrl(params, {order: 's_n_desc'});
郭成尧 authored
286 287
                        opt.desc = true;
                    }
郭成尧 authored
288
                }
郭成尧 authored
289
郭成尧 authored
290 291
                opt.name = '销量';
                break;
郭成尧 authored
292 293 294 295 296 297 298 299 300
            default:
                break;
        }

        dest.sortType.push(opt);
    }

    // 上下翻页数据处理
    dest.pageCounts = [{
郭成尧 authored
301
        href: handleFilterUrl(params, {limit: 200}),
郭成尧 authored
302 303
        count: 200
    }, {
郭成尧 authored
304
        href: handleFilterUrl(params, {limit: 100}),
郭成尧 authored
305 306
        count: 100
    }, {
郭成尧 authored
307
        href: handleFilterUrl(params, {limit: 60}),
郭成尧 authored
308 309 310 311 312 313 314 315 316 317 318 319 320 321
        count: 60
    }];

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

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

    // 全部页码数量
    dest.pageCount = parseInt(total / (_.isEmpty(params.limit) ? 60 : params.limit), 10) + 1;

    // 每页多少商品
    let paramsLimit = parseInt((_.isEmpty(params.limit) ? 60 : params.limit), 10);
郭成尧 authored
322 323
    // 上一页下一页
    let preHref = (!_.isEmpty(params.page) && parseInt(params.page, 10) > 1) ?
郭成尧 authored
324
    parseInt(params.page, 10) - 1 : 1;
郭成尧 authored
325 326 327 328
    let nextHref = (!_.isEmpty(params.page)) ? parseInt(params.page, 10) + 1 : 2;

    if (dest.pageCount > 1 && (parseInt(params.page, 10) !== 1) &&
        (parseInt(params.page, 10) !== dest.pageCount)) {
郭成尧 authored
329 330
        dest.preHref = handleFilterUrl(params, {page: preHref});
        dest.nextHref = handleFilterUrl(params, {page: nextHref});
郭成尧 authored
331
    } else if (dest.pageCount > 1 && (parseInt(params.page, 10) === 1)) {
郭成尧 authored
332
        dest.nextHref = handleFilterUrl(params, {page: nextHref});
郭成尧 authored
333
    } else if (dest.pageCount > 1 && (parseInt(params.page, 10) === dest.pageCount)) {
郭成尧 authored
334
        dest.preHref = handleFilterUrl(params, {page: preHref});
郭成尧 authored
335
    }
郭成尧 authored
336
周少峰 authored
337 338 339
    // 全部商品数
    dest.totalCount = total;
郭成尧 authored
340
    // 商品开始结束数
郭成尧 authored
341
    dest.start = (!_.isEmpty(params.page) ? (params.page - 1) : 0) * paramsLimit + 1;
342 343 344 345 346 347
    if (total < paramsLimit) {
        dest.end = total;
    } else {
        dest.end = (dest.pageCount === parseInt(params.page, 10)) ?
            total : parseInt(dest.start, 10) + paramsLimit - 1;
    }
郭成尧 authored
348 349

    return dest;
郭成尧 authored
350
};
351 352 353

/**
 * 处理页面左侧分类筛选数据
郭成尧 authored
354 355 356 357
 * @param origin 分类原始数据,一般是接口返回数据中的 group_sort 字段
 * @param params 当前 URL 中已有的参数
 * @param extra 可以任意传值用来处理特殊情况
 * @returns {{}}
358
 */
359
exports.handleSaleSortData = (origin, params, extra) => {
郭成尧 authored
360
    let leftContent = {};
361 362 363 364

    leftContent.allSort = {};
    leftContent.allSort.list = [];
郭成尧 authored
365
    // 删除 msort misort
郭成尧 authored
366 367
    // delete params.msort;
    // delete params.misort;
郭成尧 authored
368
郭成尧 authored
369 370
    if (extra === 'discount') {
        let tempParams = _.cloneDeep(params);
郭成尧 authored
371
郭成尧 authored
372
        delete tempParams.sort;
郭成尧 authored
373
郭成尧 authored
374 375
        leftContent.allSort.all = {
            name: '全部品类',
376
            href: handleFilterUrl(tempParams, {sort: ''}),
郭成尧 authored
377
            active: _.isEmpty(params.sort)
郭成尧 authored
378 379 380 381
        };
    } else {
        leftContent.allSort.all = {
            name: '全部品类',
382
            href: handleFilterUrl(params, {sort: ''})
郭成尧 authored
383 384
        };
    }
385
徐祁xuqi authored
386
    _.forEach(origin, value => {
387
徐祁xuqi authored
388 389 390 391
        let category = {
            name: value.category_name,
            num: value.node_count,
            childList: [],
郭成尧 authored
392
            href: handleFilterUrl(params, {sort: value.relation_parameter.sort}),
徐祁xuqi authored
393 394
            active: params.sort === value.relation_parameter.sort
        };
395
徐祁xuqi authored
396
        _.forEach(value.sub, subValue => {
397 398 399
            category.childList.push({
                name: subValue.category_name,
                num: subValue.node_count,
郭成尧 authored
400
                href: handleFilterUrl(params, {sort: subValue.relation_parameter.sort}),
郭成尧 authored
401
                childActive: params.sort === subValue.relation_parameter.sort
402
            });
403 404 405 406
            if (params.sort === subValue.relation_parameter.sort) {
                category.active = true;
            }
407 408 409 410 411 412 413 414 415
        });

        leftContent.allSort.list.push(category);
    });

    return leftContent;
};

/**
郭成尧 authored
416 417 418 419
 * 处理一般筛选数据
 * @param origin 要处理的筛选数据 filter
 * @param params 当前 URL 中已有的参数,处理选中状态使用
 * @returns {{}}
420 421
 */
exports.handleSaleFilterData = (origin, params) => {
徐祁xuqi authored
422 423 424 425
    let dest = {
        brand: {
            default: [],
            brandsShow: [],
426 427 428 429 430 431 432
            brandIndex: [{
                index: 'all',
                name: '全部'
            }, {
                index: '0-9',
                name: '0~9'
            }],
徐祁xuqi authored
433 434 435 436 437 438 439 440
            showMore: true,
            showMulti: true
        },
        price: [],
        gender: [],
        checkedConditions: {
            conditions: []
        }
441 442
    };
郭成尧 authored
443
    // 品牌索引数据处理
444
    for (let i = 65; i < 91; i++) {
郭成尧 authored
445
        dest.brand.brandIndex.push({
446 447 448 449 450 451
            index: String.fromCharCode(i).toLowerCase(),
            name: String.fromCharCode(i)
        });
    }

    // 品牌数据处理
郭成尧 authored
452 453 454 455 456 457 458
    // 分拆品牌参数
    let brands = _.split(params.brand, ',');
    let intBrands = [];

    _.forEach(brands, value => {
        intBrands.push(parseInt(value, 10));
    });
郭成尧 authored
459 460

    if (origin.brand) {
461 462
        let count = 0;
郭成尧 authored
463
        _.forEach(origin.brand, function(value) {
徐祁xuqi authored
464
            let brand = {
郭成尧 authored
465 466 467
                checked: (typeof _.find(intBrands, o => {
                    return _.isEqual(o, value.id);
                }) !== 'undefined'),
郭成尧 authored
468
                href: handleFilterUrl(params, {brand: value.id}),
徐祁xuqi authored
469
                name: value.brand_name,
郭成尧 authored
470 471
                key: value.brand_domain,
                id: value.id
徐祁xuqi authored
472
            };
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489

            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) {
                dest.brand.default.push(brand);
            }

            dest.brand.brandsShow.push(brand);
            count++;
        });
郭成尧 authored
490 491 492 493 494 495 496 497 498
        // 没有品牌的情况下将 brand 设置为 false,前端不显示 品牌
        if (_.size(dest.brand.brandsShow) <= 0) {
            dest.brand = false;
        } else {
            // 品牌排序
            dest.brand.brandsShow = _.sortBy(dest.brand.brandsShow, o => {
                return o.index;
            });
        }
499
    }
500
501 502 503
    // 处理价格筛选数据
    let priceRangechecked = false;
504
    if (!_.isEmpty(origin.priceRange)) {
徐祁xuqi authored
505
        _.forEach(origin.priceRange, (value, key) => {
郭成尧 authored
506 507 508
            if (params.price === key) {
                priceRangechecked = true;
            }
509
            let price = {
郭成尧 authored
510
                checked: params.price === key,
郭成尧 authored
511
                href: handleFilterUrl(params, {price: key}),
512 513 514 515 516 517 518
                name: value
            };

            dest.price.push(price);
        });
    }
519 520 521
    // 处理用户自主填写的价格区间
    if (!priceRangechecked && params.price) {
        let customPrice = _.split(params.price, ',');
522
523 524 525 526 527 528 529
        if (customPrice[1] !== '99999') {
            dest.customPrice = {
                min: customPrice[0],
                max: customPrice[1]
            };
            dest.checkedConditions.conditions.push({
                name: '¥ ' + customPrice[0] + '-' + customPrice[1],
郭成尧 authored
530
                href: handleFilterUrl(params, {price: ''})
531 532
            });
        }
533 534
    }
郭成尧 authored
535
    // 处理性别数据
536 537
    dest.gender = [{
        name: 'BOYS',
郭成尧 authored
538
        href: handleFilterUrl(params, {gender: '1,3'}),
539 540 541
        checked: params.gender === '1,3'
    }, {
        name: 'GIRLS',
郭成尧 authored
542
        href: handleFilterUrl(params, {gender: '2,3'}),
543 544
        checked: params.gender === '2,3'
    }];
545 546 547 548 549 550 551 552 553 554

    // 最新降价数据处理
    if (parseInt(params.saleType, 10) === 3) {
        dest.discount = handleSaleNewSaleSortData(params);

        // 最新降价筛选数据处理
        dest.checkedConditions.conditions = _.union(dest.checkedConditions.conditions,
            handleCheckedData(params, dest.discount, 'p_d'));
    }
郭成尧 authored
555
    // 尺码处理
郭成尧 authored
556
    if (!_.isEmpty(origin.size)) {
郭成尧 authored
557
郭成尧 authored
558
        if (params.saleType === '5') {
郭成尧 authored
559 560 561 562 563 564 565 566 567 568 569 570 571 572

            // 断码区尺码处理
            dest.size = handleSalebreakingYardsSortData(origin.size, params);
        } else {
            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
                    });
郭成尧 authored
573
                });
郭成尧 authored
574 575 576 577 578 579 580 581 582 583
            } 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
                });
            }
        }
584 585 586 587 588 589

        dest.checkedConditions.conditions = _.union(dest.checkedConditions.conditions,
            handleCheckedData(params, dest.size, 'size'));
    }

    // 清除所有选中数据
周少峰 authored
590 591 592 593 594 595
    let remainParams = {};

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

    // 处理频道筛选数据
    dest.checkedConditions.conditions = _.union(dest.checkedConditions.conditions,
郭成尧 authored
599
        handleCheckedData(params, dest.gender, 'gender'));
600 601

    // 处理品牌筛选数据
郭成尧 authored
602 603 604 605
    if (dest.brand.brandsShow) {
        dest.checkedConditions.conditions = _.union(dest.checkedConditions.conditions,
            handleBrandCheckedData(params, dest.brand.brandsShow));
    }
606 607 608 609 610 611 612 613 614

    // 处理价格筛选数据
    dest.checkedConditions.conditions = _.union(dest.checkedConditions.conditions,
        handleCheckedData(params, dest.price, 'price'));

    return dest;
};

/**
郭成尧 authored
615 616 617 618 619
 * 处理首页 banner 数据
 * @param  {[type]} origin [description]
 * @return {[type]}        [description]
 */
exports.handleBannerData = (origin) => {
徐祁xuqi authored
620
    let dest = {};
郭成尧 authored
621 622

    dest.list = [];
郭成尧 authored
623
    _.forEach(origin, function(value) {
郭成尧 authored
624
        if (value.template_name === 'focus') {
徐祁xuqi authored
625
            _.forEach(value.data, subValue => {
郭成尧 authored
626
徐祁xuqi authored
627 628 629 630 631
                dest.list.push({
                    bannerHeight: 450,
                    href: subValue.url,
                    img: subValue.src
                });
郭成尧 authored
632 633 634 635 636 637 638 639 640
            });
        }

    });

    return dest;
};

/**
周少峰 authored
641 642 643 644
 * 面包屑导航 TODO
 * @type {[type]}
 */
exports.handlePathNavData = (data, params) => {
645 646 647 648 649
    let pathNav = [{
        href: '/product/outlets', // TODO
        name: 'OUTLET',
        pathTitle: 'OUTLET'
    }];
周少峰 authored
650 651

    _.forEach(data, (sort) => {
周少峰 authored
652 653 654 655 656 657
        if (sort.relation_parameter.sort === params.sort) {
            pathNav.push({
                name: sort.category_name,
                pathTitle: sort.category_name
            });
        }
周少峰 authored
658
        if (!_.isEmpty(sort.sub)) {
徐祁xuqi authored
659
            _.forEach(sort.sub, misort => {
周少峰 authored
660
                if (misort.relation_parameter.sort === params.sort) {
661
                    pathNav.push({
郭成尧 authored
662
                        href: helpers.urlFormat('/product/outlets/list', {sort: sort.relation_parameter.sort}),
663 664 665 666 667 668
                        name: sort.category_name,
                        pathTitle: sort.category_name
                    }, {
                        name: misort.category_name,
                        pathTitle: misort.category_name
                    });
周少峰 authored
669
                }
周少峰 authored
670
            });
周少峰 authored
671
        }
周少峰 authored
672
    });
周少峰 authored
673
徐祁xuqi authored
674 675 676
    return {
        pathNav: pathNav
    };
周少峰 authored
677
};
郭成尧 authored
678 679

/**
郭成尧 authored
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
 * 分页
 * @param  {[type]} total 总页数
 * @param  {[type]} params 筛选条件
 * @return {[type]}        [description]
 */
exports.handlePagerData = (total, params) => {
    let dest = {
        prePage: {
            url: ''
        },
        nextPage: {
            url: ''
        },
        pages: []
    };
陈轩 authored
696 697
    let currentPage = parseInt(params.page || 1, 10); // 当前页
    let perPageCount = parseInt(params.limit || 60, 10); // 每页商品数
郭成尧 authored
698 699 700 701 702 703
    let totalPage = parseInt(total / perPageCount, 10) + 1; // 总页数

    if (currentPage === 1) {
        // 当前页为 1,一定没有上一页
        delete dest.prePage;
    } else {
郭成尧 authored
704
        dest.prePage.url = handleFilterUrl(params, {page: currentPage - 1});
郭成尧 authored
705 706 707 708 709 710 711
    }

    if (currentPage === totalPage) {

        // 当前页为最后一页,一定没有下一页
        delete dest.nextPage;
    } else {
郭成尧 authored
712
        dest.nextPage.url = handleFilterUrl(params, {page: currentPage + 1});
郭成尧 authored
713 714 715 716 717 718 719 720
    }

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

    if (currentPage > 2 && currentPage <= totalPage - 2) {
        for (let i = currentPage - 2; i <= ((currentPage + 2) > totalPage ? totalPage : (currentPage + 2)); i++) {
            pages.push({
郭成尧 authored
721
                url: handleFilterUrl(params, {page: i}),
郭成尧 authored
722 723 724 725 726 727 728 729 730
                num: i,
                cur: currentPage === i
            });
        }

        // 处理页码小于等于 2 的情况
    } else if (currentPage <= 2) {
        for (let i = 1; i <= (totalPage < 5 ? totalPage : 5); i++) {
            pages.push({
郭成尧 authored
731
                url: handleFilterUrl(params, {page: i}),
郭成尧 authored
732 733 734 735 736 737 738
                num: i,
                cur: currentPage === i
            });
        }
    } else if (currentPage > totalPage - 2) {
        for (let i = totalPage; i >= totalPage - 4; i--) {
            pages.push({
郭成尧 authored
739
                url: handleFilterUrl(params, {page: i}),
郭成尧 authored
740 741 742 743 744 745 746 747 748 749 750 751 752
                num: i,
                cur: currentPage === i
            });
        }
        pages = _.sortBy(pages, ['num']);
    }

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

    if (_.size(pages) === 5) {
        if (currentPage > 4) {
            prevPages.push({
郭成尧 authored
753
                url: handleFilterUrl(params, {page: 1}),
郭成尧 authored
754 755 756 757 758 759 760 761 762 763 764
                num: 1
            });
            prevPages.push({
                num: '...'
            });
        }
        if (currentPage < totalPage - 2) {
            nextPages.push({
                num: '...'
            });
            nextPages.push({
郭成尧 authored
765
                url: handleFilterUrl(params, {page: totalPage}),
郭成尧 authored
766 767 768 769
                num: totalPage
            });
        }
    }
郭成尧 authored
770
郭成尧 authored
771 772 773
    // 商品开始结束数
    dest.tip = {
        total: total,
郭成尧 authored
774
        start: (currentPage ? currentPage - 1 : 0) * perPageCount + 1
郭成尧 authored
775
    };
郭成尧 authored
776
郭成尧 authored
777 778
    let endPageNum = (totalPage === currentPage) ?
        total : parseInt(dest.tip.start, 10) + perPageCount - 1;
郭成尧 authored
779
郭成尧 authored
780
    dest.tip.end = (total < perPageCount) ? total : endPageNum;
郭成尧 authored
781 782 783 784 785 786 787
    dest.pages = _.concat(prevPages, pages, nextPages);

    return dest;

};

/**
郭成尧 authored
788 789 790 791 792 793 794 795 796
 * 筛选类链接处理的对外接口
 * @type {[type]}
 */
exports.handleFilterUrl = handleFilterUrl;

/**
 * 处理选中数据的对外接口
 * @type {[type]}
 */
郭成尧 authored
797
exports.handleCheckedData = handleCheckedData;