实现代码
public function pageApi(Request $request)
{
$page = $request->post('page', 1);
$pageSize = $request->post('page_size', 10);
$param = $request->post();
$where = [];
$where['is_delete'] = 0;
if (!empty($param['username'])) {
$where['username[~]'] = $param['username'];
}
if (!empty($param['status']) && in_array($param['status'], [1, 2])) {
$where['status'] = $param['status'];
}
if (isset($param['admin']) && in_array($param['admin'], [0, 1])) {
$where['admin'] = $param['admin'];
}
$total = $this->medoo->count('user', 'id', $where);
if ($total == 0) {
return ['total' => 0, 'data' => []];
}
$where['LIMIT'] = [($page - 1) * $pageSize, $pageSize];
$where['ORDER'] = ['id' => 'DESC'];
$data = $this->medoo->select('user', ['id','username', 'password', 'head_url', 'status'], $where);
return ['total' => $total, 'data' => $data];
}
数据库表结构
CREATE TABLE `api_user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`password` varchar(50) NOT NULL,
`head_url` char(150) NOT NULL COMMENT '头像',
`admin` tinyint(4) NOT NULL DEFAULT '0',
`time` int(11) unsigned NOT NULL,
`is_delete` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '0:未删除,1:已删除',
`status` tinyint(3) unsigned NOT NULL DEFAULT '1' COMMENT '状态 1:启用, 2:禁用',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=34 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;