123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/6/5
- * Time: 16:03
- */
- namespace App\Repositories\Post;
- use App\Models\CategoryTopic;
- use App\Models\Post;
- use App\Models\PostComment;
- use App\Models\PostData;
- use App\Models\PostImgs;
- use App\Models\PostLog;
- use App\Models\Topic;
- use App\Traits\PostTrait;
- use Illuminate\Database\QueryException;
- use Dingo\Api\Http\Response;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Redis;
- use Symfony\Component\HttpKernel\Exception\HttpException;
- use Tymon\JWTAuth\Facades\JWTAuth;
- class PostRepository
- {
- use PostTrait;
- public function __construct(Post $post,
- PostData $postData,
- PostComment $postComment,
- PostImgs $postImgs,
- PostLog $postLog,
- CategoryTopic $categoryTopic,
- Topic $topic)
- {
- $this->post = $post;
- $this->postData = $postData;
- $this->postComment = $postComment;
- $this->postImgs = $postImgs;
- $this->postLog = $postLog;
- $this->categoryTopic = $categoryTopic;
- $this->topic = $topic;
- }
- /**
- * 发布内容
- */
- public function create($request)
- {
- //验证小号
- //验证话题
- $topicIds = $this->topic->whereIn('id', explode(',', $request['topic_ids']))->pluck('id')->toArray();
- $topicCount = count($topicIds);
- if($topicCount == 0 || $topicCount >= 5){
- return Response::create([
- 'message' => '所选话题必须1-5个',
- 'status_code' => 500
- ]);
- }
- $topicIds = implode(',', $topicIds);
- $data = [
- 'uid' => $request['uid'],
- 'username' => '暂无',
- 'mobile' => '暂无',
- 'avatar' => '暂无',
- 'type' => $request['type'],
- 'img' => $request['img'],
- 'video' => $request['video']??'',
- 'topic_ids' => $topicIds,
- 'title' => $request['title']??'',
- 'content' => $request['content'],
- 'location' => $request['location']??'',
- 'is_suggest' => $request['is_suggest'],
- 'is_hide' => 0
- ];
- $date = date('Y-m-d H:i:s');
- DB::beginTransaction();
- try{
- $post = $this->post->create($data);
- $this->postData->create([
- 'post_id' => $post->id,
- 'pv' => 0,
- 'pv_real' => 0,
- 'dislike_count' => 0,
- 'praise_count' => 0,
- 'praise_real_count' => 0,
- 'share_count' => 0,
- 'share_real_count' => 0,
- 'comment_count' => 0,
- 'comment_real_count' => 0,
- 'collect_count' => 0,
- 'collect_real_count' => 0,
- 'available_bean' => $this->availableBean(),
- 'will_collect_bean' => rand(100, 200),
- 'collect_bean' => 0,
- 'weight' => 0
- ]);
- if(!empty($request['imgs']) && $request['type'] == 'image'){
- $imgData = [];
- foreach($request['imgs'] as $img){
- $imgData[] = [
- 'post_id' => $post->id,
- 'img' => $img,
- 'created_at' => $date,
- 'updated_at' => $date
- ];
- }
- $this->postImgs->insert($imgData);
- }
- DB::commit();
- return Response::create();
- }catch (QueryException $exception){
- DB::rollBack();
- Log::debug('发布内容:'.$exception->getMessage());
- return Response::create([
- 'message' => '发布失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- /**
- * 内容列表
- */
- public function lists($request)
- {
- $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
- $where = [];
- if(isset($request['content'])){
- $where[] = ['content', 'like', "%{$request['content']}%"];
- }
- if(isset($request['is_suggest'])){
- $where[] = ['is_suggest', $request['is_suggest']];
- }
- if(isset($request['type'])){
- $where[] = ['type', $request['type']];
- }
- $sort = 'post.id';
- if(isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])){
- $sort = $request['sort'];
- }
- return $this->post
- ->join('post_data', 'post_data.post_id', '=', 'post.id')
- ->select('post.*')
- ->where($where)
- ->where(function($query) use ($request){
- if(isset($request['keyword'])){
- $query->where('uid', '=', $request['keyword'])
- ->orWhere('username', 'like', "%{$request['keyword']}%")
- ->orWhere('mobile', 'like', "%{$request['keyword']}%");
- }
- })
- ->where(function($query) use ($request){
- if(isset($request['created_at'])){
- $time = explode('_', $request['created_at']);
- $query->whereBetween('post.created_at', $time);
- }
- })
- ->where(function ($query) use ($request){
- if(isset($request['category_ids']) || isset($request['topic_ids'])){
- $ids = [];
- if (isset($request['category_ids'])) {
- $categoryIds = explode('_', $request['category_ids']);
- $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
- }
- if (isset($request['topic_ids'])) {
- $ids = array_merge($ids, explode('_', $request['topic_ids']));
- }
- foreach ($ids as $key=>$id) {
- if ($key==0) {
- $query = $query->whereRaw('FIND_IN_SET('.$id.',topic_ids)');
- } else {
- $query = $query->orWhereRaw('FIND_IN_SET('.$id.',topic_ids)');
- }
- }
- }
- })
- ->orderBy($sort,'desc')
- ->paginate($perPage);
- }
- /**
- * 内容详情
- */
- public function detail($request)
- {
- return $this->post->withTrashed()->find($request['id']);
- }
- /**
- * 评论列表
- */
- public function commentList($request)
- {
- $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
- return $this->postComment
- ->where('post_id', $request['id'])
- ->orderBy('id','desc')
- ->paginate($perPage);
- }
- /**
- * 推荐内容
- */
- public function suggest($request)
- {
- $post = $this->post->where('id', $request['id'])->first();
- if(!$post){
- return Response::create([
- 'message' => '获取内容信息失败',
- 'status_code' => 500
- ]);
- }
- if($post->is_suggest == 1){
- $post->is_suggest = 0;
- }else{
- $post->is_suggest = 1;
- }
- DB::beginTransaction();
- try{
- $post->save();
- DB::commit();
- return Response::create();
- }catch (QueryException $exception){
- DB::rollBack();
- Log::debug('推荐内容:'.$request['id'].$exception->getMessage());
- return Response::create([
- 'message' => '操作失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- /**
- * 删除内容
- */
- public function delete($request)
- {
- $post = $this->post->where('id', $request['id'])->first();
- if(!$post){
- return Response::create([
- 'message' => '获取内容信息失败',
- 'status_code' => 500
- ]);
- }
- DB::beginTransaction();
- try{
- $post->delete();
- DB::commit();
- return Response::create();
- }catch (QueryException $exception){
- DB::rollBack();
- Log::debug('删除内容:'.$request['id'].$exception->getMessage());
- return Response::create([
- 'message' => '操作失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- /**
- * 隐藏内容
- */
- public function hide($request)
- {
- $post = $this->post->where('id', $request['id'])->first();
- if(!$post){
- return Response::create([
- 'message' => '获取内容信息失败',
- 'status_code' => 500
- ]);
- }
- if($post->is_hide == 1){
- $post->is_hide = 0;
- }else{
- $post->is_hide = 1;
- }
- DB::beginTransaction();
- try{
- $post->save();
- DB::commit();
- return Response::create();
- }catch (QueryException $exception){
- DB::rollBack();
- Log::debug('隐藏内容:'.$request['id'].$exception->getMessage());
- return Response::create([
- 'message' => '操作失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- }
|