thinkphp5框架前后端分离项目实现分页功能的方法

本文实例讲述了thinkphp5框架前后端分离项目实现分页功能的方法。分享给大家供大家参考,具体如下:

方法一

利用tp5提供的paginate方法实现自动分页

参数

page第几页,paginate分页方法会自动获取

size  每页数量

代码

/**
* Notes:消费记录
* Date: 2019/6/25
* Time: 15:43
* @param Request $request
* @return \think\response\Json
*/
public function getMyConsumeLog(Request $request)
{
    global $_W;
    $size = $request->param('size', 6);
    $list = $this->model->getListByMid($_W['user']['id'],$size);
    return json(['data' => $list, 'error' => 0, 'message' => 'success']);
}
public function getListByMid($mid,$size = 10){
    $res = $this
      ->alias('c')
      ->field('c.*,b.book_name,b.book_flash,s.section_title')
      ->leftJoin('booksection s','c.chapter_id = s.id')
      ->leftJoin('book b','s.book_id = b.id')
      ->where('c.mid',$mid)
      ->order('c.id desc')
      ->paginate($size);
    return $res;
}

返回数据

{
    "data": {
        "total": 1,
        "per_page": 1,
        "current_page": 1,
        "last_page": 1,
        "data": [
            {
                "id": 105,
                "mid": 55,
                "book_id": 31,
                "chapter_id": 46046,
                "score": 27,
                "create_time": 1561447448,
                "book_name": "桃运村支书",
                "book_flash": "https://cdnxiaoshuo.t.com/FiO6TM0N4kpzKB7tqrDko64ZS4H4",
                "section_title": "第29章 康庄大道"
            }
        ]
    },
    "error": 0,
    "message": "success"
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/5628.html