1
0

BaseDataLists.php 7.2 KB

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