thinkphp5 列表页数据分页查询2-带搜索条件
一、控制器部分
<?php namespace app\user\controller; use app\index\controller\Common; use app\user\model\HelpCenterFeedback as HelpCenterFeedbackModel; use org\Response; use app\index\logic\Upload; use think\Request; use org\Ucenter; use think\Db; /** * 帮助中心意见反馈控制器 * Class Feedback * @package app\user\controller */ class Feedback extends Common { /** * 反馈列表数据 * @return array */ public function index(Request $request) { $page = $request->get('page', 1);//页数 $row = $request->get('row', 2);//每页数据 $keywords = $request->get('keywords', '');//搜索关键词 $source = $request->get('source', '0');//版本 0-用户端 1-企业端 //查询模型取数据 $feedback = new HelpCenterFeedbackModel(); $feedback->getList($page, $row, $keywords, $source); //提取模型查询的数据 $total = $feedback->total; $pages = $feedback->pages; $list = $feedback->list; $data = [ 'total' => $total,//数据总量 'page' => $page,//当前页 'pages' => $pages,//总页数 'trainlist' => $list,//数据详情 ]; return Response::response(0, $data); } public function create() { // } public function edit(Request $request,$id) { // } /** * 反馈列表状态修改 * @param $id * @return array */ public function update(Request $request,$id) { $status = $request->put('status',''); if (empty($status) && $status != '0') { return Response::response(1005, [], '操作状态错误'); } $res= Db::table('qj_help_center_feedback')->where('id',$id)->update(['status'=>$status]); if ($res === false) Response::response(1, '修改失败'); return Response::response(0, '修改成功'); } }
二、模型
<?php namespace app\user\model; use Helper\JobHelper; use Helper\ResumeHelper; use think\Model; use think\Request; use traits\model\SoftDelete; use think\Db; /** * 帮助中心模型 * Class Feedback * @package app\user\model */ class HelpCenterFeedback extends Model { /** * @param $page 页数 * @param $row 每页数据 * @param $keywords 搜索关键词 * @param $source 版本 0-用户端 1-企业端 */ public function getList($page, $row, $keywords, $source) { $offset = ($page - 1) * $row; $where = []; if ($keywords) { $where['a.user_account'] = [ 'like', $keywords . '%']; } //0用户端,1企业端 $where['source'] = $source?$source:0; //统计总数 $total = Db::table('qj_help_center_feedback')->alias('a') ->join('qj_feedback_type t','a.type=t.id','left') ->where($where) ->order('a.id asc') ->count(); //查询列表信息 $list = Db::table('qj_help_center_feedback')->alias('a')->field('a.*,t.type') ->join('qj_feedback_type t','a.type=t.id','left') ->where($where) ->order('a.id asc') ->limit($offset, $row) ->select(); //处理状态和图片路径处理 foreach ($list as $key => &$val) { //处理状态转换 if ($val['status'] == 0) { $val['status'] = '待处理'; } elseif ($val['status'] == 1) { $val['status'] = '已回访'; } else { $val['status'] = '忽略'; } //图片path路径转oss路径 $img_arrs = []; if ($val['img'] !='') { $img_arr = explode(',',$val['img']); foreach($img_arr as $k=>$v){ $img_arrs[$k]=get_image_url($v); } $list[$key]['img']=$img_arrs; } } $this->pages = ceil($total / $row); $this->list = $list; $this->total = $total; } }
路由定义参考如下(这里定义的是资源路由)
return [ '__pattern__' => [ 'name' => '\w+', ], '/error' => 'index/error/index',//错误页面 '__rest__' => [ 'user/feedback'=>'user/feedback',//帮助中心-意见反馈-用户端 ] ];
请求数据
数据如下
Status: 200 OK Time: 683 ms { "code": 0, "message": "success", -"data": { "total": 25, "page": "1", "pages": 13, -"trainlist": [ -{ "id": 51, "user_account": "17076611177", "version": "PC_3.0.0", "type": "优化建议", "content": "这些健议优化一下", "addtime": "2018-05-11 09:22:43", "status": "忽略", "source": 0, -"img": [ "http://zcplant1.oss-cn-shenzhen.aliyuncs.com/user/20180511/ddb5254c9dc2d65ec60cb377039c5a06.jpg", "http://zcplant1.oss-cn-shenzhen.aliyuncs.com/user/20180511/ddb5254c9dc2d65ec60cb377039c5a06.jpg", "http://zcplant1.oss-cn-shenzhen.aliyuncs.com/user/20180511/ddb5254c9dc2d65ec60cb377039c5a06.jpg" ] }, -{ "id": 52, "user_account": "17076611177", "version": "PC_3.0.0", "type": "优化建议", "content": "123", "addtime": "2018-05-11 14:49:17", "status": "待处理", "source": 0, -"img": [ "http://zcplant1.oss-cn-shenzhen.aliyuncs.com/user/20180511/ddb5254c9dc2d65ec60cb377039c5a06.jpg" ] } ] } }