BaseService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace core\base;
  12. use app\validate\sys\Page;
  13. use think\db\exception\DbException;
  14. use think\Model;
  15. /**
  16. * 基础服务层
  17. * Class BaseService
  18. * @package app\service
  19. */
  20. abstract class BaseService
  21. {
  22. /**
  23. * Model 实例
  24. * @var BaseModel
  25. */
  26. protected $model;
  27. protected $request;
  28. public function __construct()
  29. {
  30. $this->request = request();
  31. }
  32. /**
  33. * 分页列表参数(页码和每页多少条)
  34. * @return mixed
  35. */
  36. public function getPageParam()
  37. {
  38. $page = request()->params([
  39. ['page', 1],
  40. ['limit', 15]
  41. ]);
  42. validate(Page::class)
  43. ->check($page);
  44. return $page;
  45. }
  46. /**
  47. * 分页列表
  48. * @param Model $model
  49. * @param array $where
  50. * @param string $field
  51. * @param string $order
  52. * @param array $append
  53. * @return array
  54. * @throws DbException
  55. */
  56. public function getPageList(Model $model, array $where, string $field = '*', string $order = '', array $append = [], $with = null, $each = null)
  57. {
  58. $page_params = $this->getPageParam();
  59. $page = $page_params['page'];
  60. $limit = $page_params['limit'];
  61. $list = $model->where($where)->when($append, function ($query) use ($append) {
  62. $query->append($append);
  63. })->when($with, function ($query) use ($with) {
  64. $query->with($with);
  65. })->field($field)->order($order)->paginate([
  66. 'list_rows' => $limit,
  67. 'page' => $page,
  68. ]);
  69. if (!empty($each)) {
  70. $list = $list->each($each);
  71. }
  72. return $list->toArray();
  73. }
  74. /**
  75. * 分页数据查询,传入model(查询后结果)
  76. * @param $model BaseModel
  77. * @return array
  78. * @throws DbException
  79. */
  80. public function pageQuery($model, $each = null)
  81. {
  82. $page_params = $this->getPageParam();
  83. $page = $page_params['page'];
  84. $limit = $page_params['limit'];
  85. $list = $model->paginate([
  86. 'list_rows' => $limit,
  87. 'page' => $page,
  88. ]);
  89. if (!empty($each)) {
  90. $list = $list->each($each);
  91. }
  92. return $list->toArray();
  93. }
  94. /**
  95. * 分页视图列表查询
  96. * @param Model $model
  97. * @param array $where
  98. * @param string $field
  99. * @param string $order
  100. * @param array $append
  101. * @return array
  102. * @throws DbException
  103. */
  104. public function getPageViewList(Model $model, array $where, string $field = '*', string $order = '', array $append = [], $with = null, $each = null)
  105. {
  106. $page_params = $this->getPageParam();
  107. $page = $page_params['page'];
  108. $limit = $page_params['limit'];
  109. $list = $model->where($where)->when($append, function ($query) use ($append) {
  110. $query->append($append);
  111. })->when($with, function ($query) use ($with) {
  112. $query->withJoin($with);
  113. })->field($field)->order($order)->paginate([
  114. 'list_rows' => $limit,
  115. 'page' => $page,
  116. ]);
  117. if (!empty($each)) {
  118. $list = $list->each($each);
  119. }
  120. return $list->toArray();
  121. }
  122. }