query = $this->buildQuery($dbQuery); // 数据列表排序自动处理 if ($this->app->request->isPost()) $this->_sortAction(); // 列表设置默认排序处理 if (!$this->query->getOptions('order')) $this->_orderAction(); // 列表分页及结果集处理 if ($page) { if ($limit > 0) { $limit = intval($limit); } else { $limit = $this->app->request->get('limit', $this->app->cookie->get('limit', 20)); if (intval($this->app->request->get('not_cache_limit', 0)) < 1) { $this->app->cookie->set('limit', ($limit = intval($limit >= 10 ? $limit : 20)) . ''); } } [$options, $query] = ['', $this->app->request->get()]; $pager = $this->query->paginate(['list_rows' => $limit, 'query' => $query], $total); foreach ([10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200] as $num) { [$query['limit'], $query['page'], $selects] = [$num, 1, $limit === $num ? 'selected' : '']; if (stripos($this->app->request->get('spm', '-'), 'm-') === 0) { $url = sysuri('admin/index/index') . '#' . $this->app->request->baseUrl() . '?' . urldecode(http_build_query($query)); } else { $url = $this->app->request->baseUrl() . '?' . urldecode(http_build_query($query)); } $options .= ""; } $selects = ""; $pagetext = lang('think_library_page_html', [$pager->total(), $selects, $pager->lastPage(), $pager->currentPage()]); $pagehtml = "
{$pagetext}{$pager->render()}
"; if (stripos($this->app->request->get('spm', '-'), 'm-') === 0) { $this->class->assign('pagehtml', preg_replace('|href="(.*?)"|', 'data-open="$1" onclick="return false" href="$1"', $pagehtml)); } else { $this->class->assign('pagehtml', $pagehtml); } $result = ['page' => ['limit' => intval($limit), 'total' => intval($pager->total()), 'pages' => intval($pager->lastPage()), 'current' => intval($pager->currentPage())], 'list' => $pager->items()]; } else { $result = ['list' => $this->query->select()->toArray()]; } if (false !== $this->class->callback('_page_filter', $result['list']) && $display) { if ($this->app->request->get('output') === 'json') { $this->class->success('JSON-DATA', $result); } else { $this->class->fetch($template, $result); } } else { return $result; } } /** * 执行列表排序操作 * POST 提交 {action:sort,PK:$PK,SORT:$SORT} * @throws \think\db\exception\DbException */ private function _sortAction() { if ($this->app->request->post('action') === 'sort') { if (method_exists($this->query, 'getTableFields') && in_array('sort', $this->query->getTableFields())) { $pk = $this->query->getPk() ?: 'id'; if ($this->app->request->has($pk, 'post')) { $map = [$pk => $this->app->request->post($pk, 0)]; $data = ['sort' => intval($this->app->request->post('sort', 0))]; if ($this->app->db->table($this->query->getTable())->where($map)->update($data) !== false) { $this->class->success(lang('think_library_sort_success'), ''); } } } $this->class->error(lang('think_library_sort_error')); } } /** * 列表默认排序处理 * 未配置排序规则时自动按SORT排序 */ private function _orderAction() { if (method_exists($this->query, 'getTableFields')) { if (in_array('sort', $this->query->getTableFields())) { $this->query->order('sort desc'); } } } }