BaseDataLists.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\lists;
  20. use app\common\enum\ExportEnum;
  21. use app\common\service\JsonService;
  22. use app\common\validate\ListsValidate;
  23. use app\Request;
  24. use think\facade\Config;
  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. $this->startTime = strtotime($startTime);
  98. $endTime = $this->request->get('end_time', '');
  99. $this->endTime = strtotime($endTime);
  100. $this->start = $this->request->get('start', '');
  101. $this->end = $this->request->get('end', '');
  102. return $this->searchWhere = $this->createWhere($this->setSearch());
  103. }
  104. /**
  105. * @notes 初始化排序
  106. * @return array|string[]
  107. * @author 令狐冲
  108. * @date 2021/7/31 00:03
  109. */
  110. private function initSort()
  111. {
  112. if (!($this instanceof ListsSortInterface)) {
  113. return [];
  114. }
  115. $this->field = $this->request->get('field', '');
  116. $this->orderBy = $this->request->get('order_by', '');
  117. return $this->sortOrder = $this->createOrder($this->setSortFields(), $this->setDefaultOrder());
  118. }
  119. /**
  120. * @notes 导出初始化
  121. * @return false|\think\response\Json
  122. * @author 令狐冲
  123. * @date 2021/7/31 01:15
  124. */
  125. private function initExport()
  126. {
  127. $this->export = $this->request->get('export', '');
  128. //不做导出操作
  129. if ($this->export != ExportEnum::INFO && $this->export != ExportEnum::EXPORT) {
  130. return false;
  131. }
  132. //导出操作,但是没有实现导出接口
  133. if (!($this instanceof ListsExcelInterface)) {
  134. return JsonService::throw('该列表不支持导出');
  135. }
  136. $this->fileName = $this->request->get('file_name', '') ?: $this->setFileName();
  137. //不导出文件,不初始化一下参数
  138. if ($this->export != ExportEnum::EXPORT) {
  139. return false;
  140. }
  141. //导出文件名设置
  142. $this->fileName .= '-' . date('Y-m-d-His') . '.xlsx';
  143. //导出文件准备
  144. if ($this->export == ExportEnum::EXPORT) {
  145. //指定导出范围(例:第2页到,第5页的数据)
  146. if ($this->pageType == 1) {
  147. $this->pageStart = $this->request->get('page_start', $this->pageStart);
  148. $this->pageEnd = $this->request->get('page_end', $this->pageEnd);
  149. //改变查询数量参数(例:第2页到,第5页的数据,查询->page(2,(5-2+1)*25)
  150. $this->limitOffset = ($this->pageStart - 1) * $this->pageSize;
  151. $this->limitLength = ($this->pageEnd - $this->pageStart + 1) * $this->pageSize;
  152. }
  153. $count = $this->count();
  154. //判断导出范围是否有数据
  155. if ($count == 0 || ceil($count / $this->pageSize) < $this->pageStart) {
  156. $msg = $this->pageType ? '第' . $this->pageStart . '页到第' . $this->pageEnd . '页没有数据,无法导出' : '没有数据,无法导出';
  157. return JsonService::throw($msg);
  158. }
  159. }
  160. }
  161. /**
  162. * @notes 不需要分页,可以调用此方法,无需查询第二次
  163. * @return int
  164. * @author 令狐冲
  165. * @date 2021/7/6 00:34
  166. */
  167. public function defaultCount(): int
  168. {
  169. return count($this->lists());
  170. }
  171. }