PageHelper.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Library for ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2021 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://gitee.com/zoujingli/ThinkLibrary
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 仓库地址 :https://gitee.com/zoujingli/ThinkLibrary
  12. // | github 仓库地址 :https://github.com/zoujingli/ThinkLibrary
  13. // +----------------------------------------------------------------------
  14. declare (strict_types=1);
  15. namespace think\admin\helper;
  16. use think\admin\Helper;
  17. use think\db\exception\DataNotFoundException;
  18. use think\db\exception\DbException;
  19. use think\db\exception\ModelNotFoundException;
  20. use think\db\Query;
  21. use think\Model;
  22. /**
  23. * 列表处理管理器
  24. * Class PageHelper
  25. * @package think\admin\helper
  26. */
  27. class PageHelper extends Helper
  28. {
  29. /**
  30. * 逻辑器初始化
  31. * @param Model|Query|string $dbQuery
  32. * @param boolean $page 是否启用分页
  33. * @param boolean $display 是否渲染模板
  34. * @param boolean|integer $total 集合分页记录数
  35. * @param integer $limit 集合每页记录数
  36. * @param string $template 模板文件名称
  37. * @return array
  38. * @throws DataNotFoundException
  39. * @throws DbException
  40. * @throws ModelNotFoundException
  41. */
  42. public function init($dbQuery, bool $page = true, bool $display = true, $total = false, int $limit = 0, string $template = ''): array
  43. {
  44. $this->query = $this->buildQuery($dbQuery);
  45. // 数据列表排序自动处理
  46. if ($this->app->request->isPost() && $this->app->request->post('action') === 'sort') {
  47. if (in_array('sort', $this->query->getTableFields())) {
  48. if ($this->app->request->has($pk = $this->query->getPk() ?: 'id', 'post')) {
  49. $map = [$pk => $this->app->request->post($pk, 0)];
  50. $data = ['sort' => intval($this->app->request->post('sort', 0))];
  51. if ($this->app->db->table($this->query->getTable())->where($map)->update($data) !== false) {
  52. $this->class->success(lang('think_library_sort_success'), '');
  53. }
  54. }
  55. }
  56. $this->class->error(lang('think_library_sort_error'));
  57. }
  58. // 列表分页及结果集处理
  59. if ($page) {
  60. if (empty($limit)) {
  61. $limit = $this->app->request->get('limit', $this->app->cookie->get('limit', 20));
  62. if (intval($this->app->request->get('not_cache_limit', 0)) < 1) {
  63. $this->app->cookie->set('limit', ($limit = intval($limit >= 10 ? $limit : 20)) . '');
  64. }
  65. }
  66. [$options, $query] = ['', $this->app->request->get()];
  67. $pager = $this->query->paginate(['list_rows' => $limit, 'query' => $query], $total);
  68. foreach ([10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 300, 400, 500, 600] as $num) {
  69. [$query['limit'], $query['page'], $selects] = [$num, 1, $limit === $num ? 'selected' : ''];
  70. if (stripos($this->app->request->get('spm', '-'), 'm-') === 0) {
  71. $url = sysuri('admin/index/index') . '#' . $this->app->request->baseUrl() . '?' . urldecode(http_build_query($query));
  72. } else {
  73. $url = $this->app->request->baseUrl() . '?' . urldecode(http_build_query($query));
  74. }
  75. $options .= "<option data-num='{$num}' value='{$url}' {$selects}>{$num}</option>";
  76. }
  77. $selects = "<select onchange='location.href=this.options[this.selectedIndex].value' data-auto-none>{$options}</select>";
  78. $pagetext = lang('think_library_page_html', [$pager->total(), $selects, $pager->lastPage(), $pager->currentPage()]);
  79. $pagehtml = "<div class='pagination-container nowrap'><span>{$pagetext}</span>{$pager->render()}</div>";
  80. if (stripos($this->app->request->get('spm', '-'), 'm-') === 0) {
  81. $this->class->assign('pagehtml', preg_replace('|href="(.*?)"|', 'data-open="$1" onclick="return false" href="$1"', $pagehtml));
  82. } else {
  83. $this->class->assign('pagehtml', $pagehtml);
  84. }
  85. $result = ['page' => ['limit' => $limit, 'total' => $pager->total(), 'pages' => $pager->lastPage(), 'current' => $pager->currentPage()], 'list' => $pager->items()];
  86. } else {
  87. $pager = $this->query->select();
  88. $result = ['page' => ['limit' => $pager->count(), 'total' => 1, 'pages' => 1, 'current' => 1], 'list' => $pager->toArray()];
  89. }
  90. if (false !== $this->class->callback('_page_filter', $result['list']) && $display) {
  91. if ($this->app->request->get('output') === 'json') {
  92. $this->class->success('JSON-DATA', $result);
  93. } else {
  94. $this->class->fetch($template, $result);
  95. }
  96. }
  97. return $result;
  98. }
  99. }