| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- 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'] : 0;
- $status = isset($params['status'])? $params['status'] : 0;
- $attrType = isset($params['attr_type'])? $params['attr_type'] : 0;
- 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('article 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['post_time'] = $item['post_time']? datetime($item['post_time']):'';
- $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();
- // 图片处理
- if(isset($data['cover'])){
- $cover = isset($data['cover'])? trim($data['cover']) : '';
- if (strpos($cover, "temp")) {
- $data['cover'] = save_image($cover, 'ad');
- } else {
- $data['cover'] = str_replace(IMG_URL, "", $data['cover']);
- }
- }
- // 开始时间
- if (isset($data['post_time'])) {
- $data['post_time'] = $data['post_time']? strtotime($data['post_time']) : time();
- }else{
- $data['post_time'] = time();
- }
- $type = isset($data['type'])? $data['type'] : 0;
- $attrType = isset($data['attr_type'])? $data['attr_type'] : 0;
- $id = isset($data['id'])? $data['id'] : 0;
- if($attrType==2){
- if($id && $this->model->where(['type'=> $type,'mark'=> 1])->whereNotIn('id',[$id])->value('id')){
- return returnJson('已经存在该分类单页文章', false);
- }else if($id<=0 && $this->model->where(['type'=> $type,'attr_type'=>2,'mark'=> 1])->value('id')){
- return returnJson('已经存在该分类单页文章', false);
- }
- }
- return parent::edit($data); // TODO: Change the autogenerated stub
- }
- }
|