pager.js 3.24 KB
/**
 * 分页处理
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2017/6/1
 */

'use strict';

const _ = require('lodash');

/**
 * 拼接url
 * @function joinUrl
 * @param params 要拼接的 参数
 * @returns {string}
 */
const joinUrl = (params) => {
    let dest = '?';

    _.forEach(params, function(value, key) {
        dest += `${key}=${value}&`;
    });

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

/**
 * 设置分页
 * @setPager setPager
 * @param total 总页数
 * @param params 参数
 * @returns {object}
 */
module.exports  = (total, params)=>{
    let resData = {};
    let defParams = _.cloneDeep(params);
    let cutStatus, // 切割状态 1:去头 2:去尾 3:双切
        i;
    let pages = [];
    let currentPage = parseInt(defParams.page || 1, 10); // 当前页

    // 小于两页直接退出
    if (total < 2) {
        return resData;
    }

    for (i = currentPage - 2; i <= currentPage + 2; i++) {
        if (i < 1) {
            cutStatus = 1;
            continue;
        }

        if (i > total) {
            cutStatus = cutStatus ? 3 : 2;
            continue;
        }

        pages.push({
            url: joinUrl(Object.assign(defParams, {page: i})),
            num: i,
            cur: currentPage === i
        });
    }

    // 分页中部补全
    let len = 5 - pages.length;
    let list = [];

    if (cutStatus === 1) {
        for (i = 1; i <= len; i++) {
            let p = currentPage + i + 2;

            if (p > total) {
                break;
            }

            list.push({
                url: joinUrl(Object.assign(defParams, {page: p})),
                num: p
            });
        }
        pages = _.concat(pages, list);
    } else if (cutStatus === 2) {
        for (i = 1; i <= len; i++) {
            let p = currentPage - i - 2;

            if (p < 1) {
                break;
            }

            list.push({
                url: joinUrl(Object.assign(defParams, {page: p})),
                num: p
            });
        }
        pages = _.concat(list, pages);
    }

    // 分页头尾补全
    let fnum = _.get(_.head(pages), 'num', 1),
        lnum = _.get(_.last(pages), 'num', 1);

    if (fnum > 1) {
        if (fnum > 2) {
            pages = _.concat({
                url: joinUrl(Object.assign(defParams, {page: 1})),
                num: 1
            }, {num: '...'}, pages);
        } else {
            pages = _.concat({
                url: joinUrl(Object.assign(defParams, {page: 1})),
                num: 1
            }, pages);
        }
    }

    if (lnum < total) {
        if (lnum < total - 1) {
            pages = _.concat(pages, {num: '...'}, {
                url: joinUrl(Object.assign(defParams, {page: total})),
                num: total
            });
        } else {
            pages = _.concat(pages, {
                url: joinUrl(Object.assign(defParams, {page: total})),
                num: total
            });
        }
    }

    resData.pages = pages;

    // 上一页
    if (currentPage > 1) {
        resData.prePage = {url: joinUrl(Object.assign(defParams, {page: currentPage - 1}))};
    }

    // 下一页
    if (currentPage < total) {
        resData.nextPage = {url: joinUrl(Object.assign(defParams, {page: currentPage + 1}))};
    }

    return resData;
};