// +---------------------------------------------------------------------- namespace App\Services; use App\Models\ArticleModel; 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; $userId = isset($params['user_id']) ? intval($params['user_id']) : 0; $where = ['a.mark'=>1,'a.status'=> 1,'m.mark'=> 1,'m.status'=> 1]; if($userId){ $where['a.user_id'] = $userId; } $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($where) ->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']) ->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['create_time_text'] = $item['create_time']? format_time(strtotime($item['create_time'])) : ''; $item['content'] = $item['content']? htmlspecialchars_decode($item['content']) : ''; if($item['albums']){ foreach ($item['albums'] as &$v){ $v = $v? get_image_url($v) : ''; } unset($v); } // 关注收藏数量 $item['collect'] = 0; if($item['id']){ $count = CollectModel::where(['source_id'=> $item['id'],'type'=>2,'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; } // 是否已收藏 $item['is_collect'] = 0; if($userId){ if(CollectModel::where(['user_id'=> $userId,'source_id'=> $item['id'],'type'=> 2,'mark'=> 1,'status'=> 1])->value('id')){ $item['is_collect'] = 1; } } } 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['create_time_text'] = $info['create_time']? format_time(strtotime($info['create_time'])) : ''; $info['content'] = $info['content']? htmlspecialchars_decode($info['content']) : ''; $info['albums'] = $info['albums']? json_decode($info['albums']) : []; if($info['albums']){ foreach ($info['albums'] as &$v){ $v = $v? get_image_url($v) : ''; } unset($v); } $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'=> 2,'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 } /** * 发布动态 * @param $userId * @return array */ public function publish($userId){ $params = request()->all(); $content = isset($params['content'])? htmlspecialchars($params['content']) : ''; $albums = isset($params['fileList'])? $params['fileList'] : []; $sourceId = isset($params['source_id'])? intval($params['source_id']) : 0; if($sourceId && !ArticleModel::where(['id'=> $sourceId, 'mark'=> 1,'status'=> 1])->value('id')){ return message('抱歉,当前文章状态不可分享到动态', false); } // 验证用户 $memberInfo = MemberModel::where(['id'=> $userId, 'mark'=> 1,'status'=> 1]) ->select(['id','openid','nickname']) ->first(); if(!$memberInfo){ return message('账户不可操作或已冻结,请联系客服', false); } $confirm = ConfigService::make()->getConfigByCode('dynamic_confirm'); $confirm = intval($confirm)? intval($confirm) : 2; $data = [ 'user_id'=> $userId, 'source_id'=> $sourceId, 'comment_close'=> isset($params['comment_close']) && $params['comment_close']? 1 : 2, 'content'=> $content, 'albums'=> $albums? json_encode($albums, 256) : '', 'create_time'=> time(), 'update_time'=> time(), 'status'=> $confirm, ]; if($id = $this->model::insertGetId($data)){ return message('发布成功', true, ['id'=> $id]); } return message('发布失败', false); } }