BaseDataLists.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. $this->initController();
  54. }
  55. public function initController()
  56. {
  57. //初始化租户
  58. $this->initTenant();
  59. //分页初始化
  60. $this->initPage();
  61. //搜索初始化
  62. $this->initSearch();
  63. //排序初始化
  64. $this->initSort();
  65. //导出初始化
  66. $this->initExport();
  67. }
  68. public function initTenant()
  69. {
  70. if(!empty($this->request->adminInfo['tenant_id'])){
  71. $this->params['tenant_id'] = $this->request->adminInfo['tenant_id'];
  72. }
  73. }
  74. /**
  75. * @notes 分页参数初始化
  76. * @author 令狐冲
  77. * @date 2021/7/30 23:55
  78. */
  79. private function initPage()
  80. {
  81. $this->pageSizeMax = Config::get('project.lists.page_size_max');
  82. $this->pageSize = Config::get('project.lists.page_size');
  83. $this->pageType = $this->request->get('page_type', 1);
  84. if ($this->pageType == 1) {
  85. //分页
  86. $this->pageNo = $this->request->get('page_no', 1) ?: 1;
  87. $this->pageSize = $this->request->get('page_size', $this->pageSize) ?: $this->pageSize;
  88. } else {
  89. //不分页
  90. $this->pageNo = 1;//强制到第一页
  91. $this->pageSize = $this->pageSizeMax;// 直接取最大记录数
  92. }
  93. //limit查询参数设置
  94. $this->limitOffset = ($this->pageNo - 1) * $this->pageSize;
  95. $this->limitLength = $this->pageSize;
  96. }
  97. /**
  98. * @notes 初始化搜索
  99. * @return array
  100. * @author 令狐冲
  101. * @date 2021/7/31 00:00
  102. */
  103. private function initSearch()
  104. {
  105. if (!($this instanceof ListsSearchInterface)) {
  106. return [];
  107. }
  108. $startTime = $this->request->get('start_time');
  109. if ($startTime) {
  110. $this->startTime = strtotime($startTime);
  111. }
  112. $endTime = $this->request->get('end_time');
  113. if ($endTime) {
  114. $this->endTime = strtotime($endTime);
  115. }
  116. $this->start = $this->request->get('start');
  117. $this->end = $this->request->get('end');
  118. return $this->searchWhere = $this->createWhere($this->setSearch());
  119. }
  120. /**
  121. * @notes 初始化排序
  122. * @return array|string[]
  123. * @author 令狐冲
  124. * @date 2021/7/31 00:03
  125. */
  126. private function initSort()
  127. {
  128. if (!($this instanceof ListsSortInterface)) {
  129. return [];
  130. }
  131. $this->field = $this->request->get('field', '');
  132. $this->orderBy = $this->request->get('order_by', '');
  133. return $this->sortOrder = $this->createOrder($this->setSortFields(), $this->setDefaultOrder());
  134. }
  135. /**
  136. * @notes 导出初始化
  137. * @return false|\think\response\Json
  138. * @author 令狐冲
  139. * @date 2021/7/31 01:15
  140. */
  141. private function initExport()
  142. {
  143. $this->export = $this->request->get('export', '');
  144. //不做导出操作
  145. if ($this->export != ExportEnum::INFO && $this->export != ExportEnum::EXPORT) {
  146. return false;
  147. }
  148. //导出操作,但是没有实现导出接口
  149. if (!($this instanceof ListsExcelInterface)) {
  150. return JsonService::throw('该列表不支持导出');
  151. }
  152. $this->fileName = $this->request->get('file_name', '') ?: $this->setFileName();
  153. //不导出文件,不初始化一下参数
  154. if ($this->export != ExportEnum::EXPORT) {
  155. return false;
  156. }
  157. //导出文件名设置
  158. $this->fileName .= '-' . date('Y-m-d-His') . '.xlsx';
  159. //导出文件准备
  160. if ($this->export == ExportEnum::EXPORT) {
  161. //指定导出范围(例:第2页到,第5页的数据)
  162. if ($this->pageType == 1) {
  163. $this->pageStart = $this->request->get('page_start', $this->pageStart);
  164. $this->pageEnd = $this->request->get('page_end', $this->pageEnd);
  165. //改变查询数量参数(例:第2页到,第5页的数据,查询->page(2,(5-2+1)*25)
  166. $this->limitOffset = ($this->pageStart - 1) * $this->pageSize;
  167. $this->limitLength = ($this->pageEnd - $this->pageStart + 1) * $this->pageSize;
  168. }
  169. $count = $this->count();
  170. //判断导出范围是否有数据
  171. if ($count == 0 || ceil($count / $this->pageSize) < $this->pageStart) {
  172. $msg = $this->pageType ? '第' . $this->pageStart . '页到第' . $this->pageEnd . '页没有数据,无法导出' : '没有数据,无法导出';
  173. return JsonService::throw($msg);
  174. }
  175. }
  176. }
  177. /**
  178. * @notes 不需要分页,可以调用此方法,无需查询第二次
  179. * @return int
  180. * @author 令狐冲
  181. * @date 2021/7/6 00:34
  182. */
  183. public function defaultCount(): int
  184. {
  185. return count($this->lists());
  186. }
  187. public function setParams($params): self
  188. {
  189. $this->params = $params;
  190. $this->initController();
  191. return $this;
  192. }
  193. public function getParams(): array
  194. {
  195. return $this->params;
  196. }
  197. public function setExcelComplexFields(): array
  198. {
  199. return [];
  200. }
  201. public function excelExportList($params = []): array
  202. {
  203. $this->setParams($params?:[])->initSearch();
  204. $this->limitLength = config('export.max_limit_length');
  205. return $this->lists();
  206. }
  207. public function setFileName():string
  208. {
  209. return '';
  210. }
  211. }