// +---------------------------------------------------------------------- namespace App\Services; use App\Models\CollectModel; use App\Models\DynamicCommentModel; use App\Models\DynamicModel; use App\Models\FollowModel; use App\Models\MemberModel; /** * 动态管理-服务类 * @author wesmiler * @since 2020/11/11 * Class DynamicService * @package App\Services */ class DynamicService extends BaseService { protected static $instance = null; /** * 构造函数 * @author wesmiler * @since 2020/11/11 * DynamicService constructor. */ public function __construct() { $this->model = new DynamicModel(); } /** * 静态入口 * @return DynamicService|null */ public static function make(){ if(!self::$instance){ self::$instance = new DynamicService(); } return self::$instance; } /** * 获取列表 * @return array * @since 2020/11/11 * @author wesmiler */ public function getList() { $params = request()->all(); $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE; $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE; $dataList = $this->model::from('dynamic as a') ->where(function ($query) use ($params) { $query->where('a.mark', 1); $isRecommand = isset($params['is_recommand']) ? intval($params['is_recommand']) : 0; if ($isRecommand > 0) { $query->where('a.is_recommand', $isRecommand); } $status = isset($params['status']) ? $params['status'] : 0; if ($status > 0) { $query->where('a.status', $status); } else { $query->whereIn('a.status', [1, 2]); } }) ->select(['a.id', 'a.user_id', 'a.comment_close', 'a.source_id', 'a.is_recommand', 'a.content', 'a.albums', 'a.status', 'a.create_time', 'a.update_time']) ->orderBy('a.update_time', 'desc') ->paginate($pageSize); $dataList = $dataList ? $dataList->toArray() : []; if ($dataList) { foreach ($dataList['data'] as &$item) { $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : ''; } unset($item); } return [ 'code' => 0, 'success'=> true, 'msg' => '操作成功', 'count' => isset($dataList['total']) ? $dataList['total'] : 0, 'data' => isset($dataList['data']) ? $dataList['data'] : 0, ]; } /** * 获取列表 * @return array * @since 2020/11/11 * @author wesmiler */ public function getDataList($params) { $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE; $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE; $dataList = $this->model::from('dynamic as a') ->leftJoin('article as ar', 'ar.id', '=', 'a.source_id') ->leftJoin('member as m', 'm.id', '=', 'a.user_id') ->where(['a.mark'=>1,'a.status'=> 1,'m.mark'=> 1,'m.status'=> 1]) ->where('m.user_id','>',0) ->select(['a.id', 'a.user_id', 'ar.title as title','m.nickname','m.avatar', 'a.source_id','ar.thumb', 'a.is_recommand', 'a.comment_close', 'a,albums', 'a.content', 'a.status', 'a.create_time', 'a.update_time']) ->orderBy('a.update_time', 'desc') ->paginate($pageSize); $dataList = $dataList ? $dataList->toArray() : []; if ($dataList) { foreach ($dataList['data'] as &$item) { $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : ''; $item['avatar'] = $item['avatar'] ? get_image_url($item['avatar']) : ''; $item['albums'] = $item['albums'] ? json_decode($item['albums'], true) : []; $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : ''; // 关注数量 $item['collect'] = 0; if($item['id']){ $count = CollectModel::where(['source_id'=> $item['id'],'mark'=> 1,'status'=> 1])->count('id'); $item['collect'] = $count? $count : 0; } // 评论数量 $item['comment'] = 0; if($item['id']){ $count = DynamicCommentModel::where(['source_id'=> $item['id'],'mark'=> 1,'status'=> 1])->count('id'); $item['comment'] = $count? $count : 0; } } unset($item); } return [ 'code' => 0, 'success'=> true, 'msg' => '操作成功', 'count' => isset($dataList['total']) ? $dataList['total'] : 0, 'data' => isset($dataList['data']) ? $dataList['data'] : 0, ]; } /** * 获取详情 * @param $id */ public function getDetail($id, $userId=0){ $info = $this->model::from('dynamic as a') ->leftJoin('article as ar', 'ar.id', '=', 'a.source_id') ->leftJoin('member as m', 'm.id', '=', 'a.user_id') ->where(['a.mark'=> 1,'a.status'=> 1,'a.id'=> $id,'m.mark'=> 1,'m.status'=> 1]) ->where('m.id','>',0) ->select(['a.id', 'a.user_id', 'ar.title as title','m.nickname','m.avatar', 'a.source_id','ar.thumb', 'a.is_recommand', 'a.comment_close', 'a,albums', 'a.content', 'a.status', 'a.create_time', 'a.update_time']) ->first(); $info = $info? $info->toArray() : []; if($info){ $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : ''; $info['avatar'] = $info['avatar']? get_image_url($info['avatar']) : ''; $info['create_time'] = $info['create_time']? datetime( $info['create_time'],'Y-m-d H:i:s') : ''; $info['is_follow'] = 0; $info[''] = 0; if($info['user_id']){ $author = MemberModel::where(['id'=> $info['user_id']])->value('nickname'); $info['author'] = $author? $author : '报恩寺'; } // 是否已收藏 $info['is_collect'] = 0; if($userId){ if(CollectModel::where(['user_id'=> $userId,'source_id'=> $id,'type'=> 1,'mark'=> 1,'status'=> 1])->value('id')){ $info['is_collect'] = 1; } } } return $info; } /** * 添加或编辑 * @return array * @since 2020/11/11 * @author wesmiler */ public function edit() { $data = request()->all(); // 图片处理 $type = isset($data['type'])? intval($data['type']) : 0; $image = $data['thumb']? trim($data['thumb']) : ''; $id = isset($data['id']) ? $data['id'] : 0; if (!$id && !$image && $type == 1) { return message('请上传封面图片', false); } if (strpos($image, "temp")) { $data['thumb'] = save_image($image, 'item'); } else { $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']); } $data['update_time'] = time(); $data['publish_at'] = isset($data['publish_at']) && $data['publish_at']? $data['publish_at'] : date('Y-m-d H:i:s'); return parent::edit($data); // TODO: Change the autogenerated stub } }