// +---------------------------------------------------------------------- namespace App\Services\Common; use App\Models\ArticleModel; use App\Services\BaseService; /** * 文章管理-服务类 * Class ArticleService * @package App\Services\Common */ class ArticleService extends BaseService { protected static $instance = null; /** * 构造函数 * ArticleService constructor. */ public function __construct() { $this->model = new ArticleModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * 获取挂单广告列表 * @param $params 参数 * @param int $pageSize 分页大小:默认 15 * @return array */ public function getDataList($params, $pageSize = 15, $field=[]) { $where = ['a.mark' => 1]; $type = isset($params['type'])? $params['type'] : 1; $status = isset($params['status'])? $params['status'] : 0; $attrType = isset($params['attr_type'])? $params['attr_type'] : 1; if($type>0){ $where['a.type'] = $type; } if($status>0){ $where['a.status'] = $status; } if($attrType>0){ $where['a.attr_type'] = $attrType; } $list = $this->model->from('advert as a') ->where($where) ->where(function($query) use($params){ $title = isset($params['title'])? trim($params['title']) : ''; if($title){ $query->where('a.title','like',"%{$title}%"); } $timeType = isset($params['time_type'])? $params['time_type'] : 0; if($timeType == 1){ $query->where('a.post_time','>=', time()); } }) ->select($field? $field : ['a.*']) ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list? $list->toArray() :[]; if($list){ $siteName = \App\Services\ConfigService::make()->getConfigByCode('site_name'); foreach($list['data'] as &$item){ $item['create_time_text'] = $item['create_time']? datetime($item['create_time']):''; $item['post_time_text'] = $item['post_time']? datetime($item['post_time'], 'm-d H:i'):''; $item['cover'] = $item['cover']? get_image_url($item['cover']):''; $item['author'] = $item['author']? $item['author'] : $siteName; } } return [ 'pageSize'=> $pageSize, 'total'=>isset($list['total'])? $list['total'] : 0, 'list'=> isset($list['data'])? $list['data'] : [] ]; } /** * 获取资料详情 * @param $where * @param array $field */ public function getInfo($where, array $field = []) { $field = $field ? $field : ['*']; if (is_array($where)) { $info = $this->model->where($where)->select($field)->first(); } else { $info = $this->model->where(['id' => (int)$where])->select($field)->first(); } $info = $info ? $info->toArray() : []; if($info){ $info['cover'] = $info['cover']? get_image_url($info['cover']):''; $info['post_time_text'] = $info['post_time']? datetime($info['post_time']):''; } return $info; } /** * 更新浏览 * @param $id * @return mixed */ public function updateViews($id){ return $this->model->where(['id'=> $id])->increment('view_num', 1); } /** * 添加或编辑 * @return array * @since 2020/11/11 * @author laravel开发员 */ public function edit() { $data = request()->all(); // 图片处理 $cover = trim($data['cover']); if (strpos($cover, "temp")) { $data['cover'] = save_image($cover, 'ad'); } else { $data['cover'] = str_replace(IMG_URL, "", $data['cover']); } // 开始时间 if ($data['post_time']) { $data['post_time'] = strtotime($data['post_time']); } return parent::edit($data); // TODO: Change the autogenerated stub } }