BaseDataLists.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\common\lists;
  15. use app\common\enum\ExportEnum;
  16. use app\common\service\JsonService;
  17. use app\common\validate\ListsValidate;
  18. use app\Request;
  19. use think\facade\Config;
  20. /**
  21. * 数据列表基类
  22. * Class BaseDataLists
  23. * @package app\common\lists
  24. */
  25. abstract class BaseDataLists implements ListsInterface
  26. {
  27. use ListsSearchTrait;
  28. use ListsSortTrait;
  29. use ListsExcelTrait;
  30. public Request $request; //请求对象
  31. public int $pageNo; //页码
  32. public int $pageSize; //每页数量
  33. public int $limitOffset; //limit查询offset值
  34. public int $limitLength; //limit查询数量
  35. public int $pageSizeMax;
  36. public int $pageType = 0; //默认类型:0-一般分页;1-不分页,获取最大所有数据
  37. protected string $orderBy;
  38. protected string $field;
  39. protected $startTime;
  40. protected $endTime;
  41. protected $start;
  42. protected $end;
  43. protected array $params;
  44. protected $sortOrder = [];
  45. public string $export;
  46. public function __construct()
  47. {
  48. //参数验证
  49. (new ListsValidate())->get()->goCheck();
  50. //请求参数设置
  51. $this->request = request();
  52. $this->params = $this->request->param();
  53. //分页初始化
  54. $this->initPage();
  55. //搜索初始化
  56. $this->initSearch();
  57. //排序初始化
  58. $this->initSort();
  59. //导出初始化
  60. $this->initExport();
  61. }
  62. /**
  63. * @notes 分页参数初始化
  64. * @author 令狐冲
  65. * @date 2021/7/30 23:55
  66. */
  67. private function initPage()
  68. {
  69. $this->pageSizeMax = Config::get('project.lists.page_size_max');
  70. $this->pageSize = Config::get('project.lists.page_size');
  71. $this->pageType = $this->request->get('page_type', 1);
  72. if ($this->pageType == 1) {
  73. //分页
  74. $this->pageNo = $this->request->get('page_no', 1) ?: 1;
  75. $this->pageSize = $this->request->get('page_size', $this->pageSize) ?: $this->pageSize;
  76. } else {
  77. //不分页
  78. $this->pageNo = 1;//强制到第一页
  79. $this->pageSize = $this->pageSizeMax;// 直接取最大记录数
  80. }
  81. //limit查询参数设置
  82. $this->limitOffset = ($this->pageNo - 1) * $this->pageSize;
  83. $this->limitLength = $this->pageSize;
  84. }
  85. /**
  86. * @notes 初始化搜索
  87. * @return array
  88. * @author 令狐冲
  89. * @date 2021/7/31 00:00
  90. */
  91. private function initSearch()
  92. {
  93. if (!($this instanceof ListsSearchInterface)) {
  94. return [];
  95. }
  96. $startTime = $this->request->get('start_time');
  97. if ($startTime) {
  98. $this->startTime = strtotime($startTime);
  99. }
  100. $endTime = $this->request->get('end_time');
  101. if ($endTime) {
  102. $this->endTime = strtotime($endTime);
  103. }
  104. $this->start = $this->request->get('start');
  105. $this->end = $this->request->get('end');
  106. return $this->searchWhere = $this->createWhere($this->setSearch());
  107. }
  108. /**
  109. * @notes 初始化排序
  110. * @return array|string[]
  111. * @author 令狐冲
  112. * @date 2021/7/31 00:03
  113. */
  114. private function initSort()
  115. {
  116. if (!($this instanceof ListsSortInterface)) {
  117. return [];
  118. }
  119. $this->field = $this->request->get('field', '');
  120. $this->orderBy = $this->request->get('order_by', '');
  121. return $this->sortOrder = $this->createOrder($this->setSortFields(), $this->setDefaultOrder());
  122. }
  123. /**
  124. * @notes 导出初始化
  125. * @return false|\think\response\Json
  126. * @author 令狐冲
  127. * @date 2021/7/31 01:15
  128. */
  129. private function initExport()
  130. {
  131. $this->export = $this->request->get('export', '');
  132. //不做导出操作
  133. if ($this->export != ExportEnum::INFO && $this->export != ExportEnum::EXPORT) {
  134. return false;
  135. }
  136. //导出操作,但是没有实现导出接口
  137. if (!($this instanceof ListsExcelInterface)) {
  138. return JsonService::throw('该列表不支持导出');
  139. }
  140. $this->fileName = $this->request->get('file_name', '') ?: $this->setFileName();
  141. //不导出文件,不初始化一下参数
  142. if ($this->export != ExportEnum::EXPORT) {
  143. return false;
  144. }
  145. //导出文件名设置
  146. $this->fileName .= '-' . date('Y-m-d-His') . '.xlsx';
  147. //导出文件准备
  148. //指定导出范围(例:第2页到,第5页的数据)
  149. if ($this->pageType == 1) {
  150. $this->pageStart = $this->request->get('page_start', $this->pageStart);
  151. $this->pageEnd = $this->request->get('page_end', $this->pageEnd);
  152. //改变查询数量参数(例:第2页到,第5页的数据,查询->page(2,(5-2+1)*25)
  153. $this->limitOffset = ($this->pageStart - 1) * $this->pageSize;
  154. $this->limitLength = ($this->pageEnd - $this->pageStart + 1) * $this->pageSize;
  155. }
  156. $count = $this->count();
  157. //判断导出范围是否有数据
  158. if ($count == 0 || ceil($count / $this->pageSize) < $this->pageStart) {
  159. $msg = $this->pageType ? '第' . $this->pageStart . '页到第' . $this->pageEnd . '页没有数据,无法导出' : '没有数据,无法导出';
  160. return JsonService::throw($msg);
  161. }
  162. }
  163. /**
  164. * @notes 不需要分页,可以调用此方法,无需查询第二次
  165. * @return int
  166. * @author 令狐冲
  167. * @date 2021/7/6 00:34
  168. */
  169. public function defaultCount(): int
  170. {
  171. return count($this->lists());
  172. }
  173. }