CommonLogic.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * CommonLogic.php UTF-8
  4. * 公共逻辑处理
  5. *
  6. * @date : 2020/9/14 15:51
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : H5IOS 1.0
  11. */
  12. namespace huosdk\h5ios\core\logic;
  13. use huolib\constant\CommonConst;
  14. use think\Model;
  15. class CommonLogic extends Model {
  16. protected $model = '';
  17. protected $base_field = [];
  18. /**
  19. * 排序
  20. *
  21. * @param string $order
  22. *
  23. * @return array|string
  24. */
  25. public function orderFilter($order = '') {
  26. if (empty($order)) {
  27. return '';
  28. }
  29. $orderWhere = [];
  30. $_order = $this->strToArr($order);
  31. foreach ($_order as $key => $value) {
  32. $upDwn = substr($value, 0, 1);
  33. $orderType = $upDwn == '-' ? 'desc' : 'asc';
  34. $orderField = substr($value, 1);
  35. $orderWhere[$orderField] = $orderType;
  36. }
  37. return $orderWhere;
  38. }
  39. /**
  40. * 懒人函数
  41. *
  42. * @access public
  43. *
  44. * @param string $string 字符串
  45. *
  46. * @return array
  47. */
  48. public function strToArr($string) {
  49. return is_string($string) ? explode(',', $string) : $string;
  50. }
  51. /**
  52. * 转换查询条件
  53. *
  54. * @param array $param
  55. *
  56. * @return array
  57. */
  58. public function getWhere($param = []) {
  59. $_map = [];
  60. $_field_types = $this->model->getFieldType();
  61. foreach ($_field_types as $_field => $_type) {
  62. if (isset($param[$_field]) && !empty($param[$_field])) {
  63. $_map[$_field] = $param[$_field];
  64. }
  65. }
  66. /* 时间搜索 */
  67. if (!empty($param['end_time'])) {
  68. if (!empty($param['start_time'])) {
  69. $_map['create_time']
  70. = [
  71. 'between',
  72. [
  73. strtotime($param['start_time']),
  74. CommonConst::CONST_DAY_SECONDS + strtotime($param['end_time'])
  75. ]
  76. ];
  77. } else {
  78. $_map['create_time'] = ['elt', CommonConst::CONST_DAY_SECONDS + strtotime($param['end_time'])];
  79. }
  80. } elseif (!empty($param['start_time'])) {
  81. $_map['create_time'] = ['egt', strtotime($param['start_time'])];
  82. }
  83. return $_map;
  84. }
  85. /**
  86. * 获取列表底层函数
  87. *
  88. * @param array $where 搜索条件
  89. * @param string $page 列表个数
  90. * @param string $order 排序
  91. * @param array $field 附加字段
  92. * @param string $group 分类
  93. * @param string $with 关联
  94. *
  95. * @return array ['count'=>0,'list'=>[]]
  96. */
  97. public function getList(
  98. $where = [], $page = '1,10', $order = '-id', $field = [], $group = '', $with = ''
  99. ) {
  100. $_map = $where;
  101. $_field = $field;
  102. $_model = $this->model;
  103. $_count = $_model->where($_map)->count();
  104. if (empty($_count)) {
  105. return [
  106. 'count' => 0,
  107. 'list' => []
  108. ];
  109. }
  110. $_order = $this->orderFilter($order);
  111. if (!empty($group)) {
  112. $_datas = $_model
  113. ->with($with)
  114. ->field($_field)
  115. ->where($_map)
  116. ->order($_order)
  117. ->group($group)
  118. ->page($page)
  119. ->select();
  120. } else {
  121. $_datas = $_model
  122. ->with($with)
  123. ->field($_field)
  124. ->where($_map)
  125. ->order($_order)
  126. ->page($page)
  127. ->select();
  128. }
  129. if (is_object($_datas)) {
  130. $_datas = $_datas->toArray();
  131. }
  132. if (empty($_datas)) {
  133. return [
  134. 'count' => 0,
  135. 'list' => []
  136. ];
  137. }
  138. return [
  139. 'count' => $_count,
  140. 'list' => $_datas
  141. ];
  142. }
  143. /**
  144. * 获取后台列表
  145. *
  146. * @param array $where 搜索条件
  147. * @param string $page 列表个数
  148. * @param string $order 排序
  149. * @param array $field 附加字段
  150. * @param string $group 分类
  151. * @param string $with 附加
  152. *
  153. * @return array ['count'=>0,'list'=>[]]
  154. */
  155. public function getAdminList($where = [], $page = '1,10', $order = '-id', $field = [], $group = '', $with = '') {
  156. $_map = $this->getWhere($where);
  157. $_field = $this->base_field;
  158. if (empty($_field)) {
  159. $_field_types = $this->model->getFieldType();
  160. $_field = array_keys($_field_types);
  161. }
  162. if (!empty($field)) {
  163. $_field = array_merge($_field, $field);/* 获取后台字段 */
  164. }
  165. return $this->getList($_map, $page, $order, $_field, $group, $with);
  166. }
  167. }