* @version : H5IOS 1.0 */ namespace huosdk\h5ios\core\logic; use huolib\constant\CommonConst; use think\Model; class CommonLogic extends Model { protected $model = ''; protected $base_field = []; /** * 排序 * * @param string $order * * @return array|string */ public function orderFilter($order = '') { if (empty($order)) { return ''; } $orderWhere = []; $_order = $this->strToArr($order); foreach ($_order as $key => $value) { $upDwn = substr($value, 0, 1); $orderType = $upDwn == '-' ? 'desc' : 'asc'; $orderField = substr($value, 1); $orderWhere[$orderField] = $orderType; } return $orderWhere; } /** * 懒人函数 * * @access public * * @param string $string 字符串 * * @return array */ public function strToArr($string) { return is_string($string) ? explode(',', $string) : $string; } /** * 转换查询条件 * * @param array $param * * @return array */ public function getWhere($param = []) { $_map = []; $_field_types = $this->model->getFieldType(); foreach ($_field_types as $_field => $_type) { if (isset($param[$_field]) && !empty($param[$_field])) { $_map[$_field] = $param[$_field]; } } /* 时间搜索 */ if (!empty($param['end_time'])) { if (!empty($param['start_time'])) { $_map['create_time'] = [ 'between', [ strtotime($param['start_time']), CommonConst::CONST_DAY_SECONDS + strtotime($param['end_time']) ] ]; } else { $_map['create_time'] = ['elt', CommonConst::CONST_DAY_SECONDS + strtotime($param['end_time'])]; } } elseif (!empty($param['start_time'])) { $_map['create_time'] = ['egt', strtotime($param['start_time'])]; } return $_map; } /** * 获取列表底层函数 * * @param array $where 搜索条件 * @param string $page 列表个数 * @param string $order 排序 * @param array $field 附加字段 * @param string $group 分类 * @param string $with 关联 * * @return array ['count'=>0,'list'=>[]] */ public function getList( $where = [], $page = '1,10', $order = '-id', $field = [], $group = '', $with = '' ) { $_map = $where; $_field = $field; $_model = $this->model; $_count = $_model->where($_map)->count(); if (empty($_count)) { return [ 'count' => 0, 'list' => [] ]; } $_order = $this->orderFilter($order); if (!empty($group)) { $_datas = $_model ->with($with) ->field($_field) ->where($_map) ->order($_order) ->group($group) ->page($page) ->select(); } else { $_datas = $_model ->with($with) ->field($_field) ->where($_map) ->order($_order) ->page($page) ->select(); } if (is_object($_datas)) { $_datas = $_datas->toArray(); } if (empty($_datas)) { return [ 'count' => 0, 'list' => [] ]; } return [ 'count' => $_count, 'list' => $_datas ]; } /** * 获取后台列表 * * @param array $where 搜索条件 * @param string $page 列表个数 * @param string $order 排序 * @param array $field 附加字段 * @param string $group 分类 * @param string $with 附加 * * @return array ['count'=>0,'list'=>[]] */ public function getAdminList($where = [], $page = '1,10', $order = '-id', $field = [], $group = '', $with = '') { $_map = $this->getWhere($where); $_field = $this->base_field; if (empty($_field)) { $_field_types = $this->model->getFieldType(); $_field = array_keys($_field_types); } if (!empty($field)) { $_field = array_merge($_field, $field);/* 获取后台字段 */ } return $this->getList($_map, $page, $order, $_field, $group, $with); } }