123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
- /**
- * CommonLogic.php UTF-8
- * 公共逻辑处理
- *
- * @date : 2020/9/14 15:51
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : chenbingling <cbl@huosdk.com>
- * @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);
- }
- }
|