PostRepository.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/5
  6. * Time: 16:03
  7. */
  8. namespace App\Repositories\Post;
  9. use App\Models\CategoryTopic;
  10. use App\Models\Post;
  11. use App\Models\PostComment;
  12. use App\Models\PostData;
  13. use App\Models\PostImgs;
  14. use App\Models\PostLog;
  15. use App\Models\Topic;
  16. use Illuminate\Database\QueryException;
  17. use Dingo\Api\Http\Response;
  18. use Illuminate\Support\Facades\DB;
  19. use Illuminate\Support\Facades\Log;
  20. use Illuminate\Support\Facades\Redis;
  21. use Symfony\Component\HttpKernel\Exception\HttpException;
  22. use Tymon\JWTAuth\Facades\JWTAuth;
  23. class PostRepository
  24. {
  25. public function __construct(Post $post,
  26. PostData $postData,
  27. PostComment $postComment,
  28. PostImgs $postImgs,
  29. PostLog $postLog,
  30. CategoryTopic $categoryTopic,
  31. Topic $topic)
  32. {
  33. $this->post = $post;
  34. $this->postData = $postData;
  35. $this->postComment = $postComment;
  36. $this->postImgs = $postImgs;
  37. $this->postLog = $postLog;
  38. $this->categoryTopic = $categoryTopic;
  39. $this->topic = $topic;
  40. }
  41. /**
  42. * 发布内容
  43. */
  44. public function create($request)
  45. {
  46. //验证小号
  47. //验证话题
  48. $topicIds = $this->topic->whereIn('id', explode(',', $request['topic_ids']))->pluck('id')->toArray();
  49. $topicCount = count($topicIds);
  50. if($topicCount == 0 || $topicCount >= 5){
  51. throw new HttpException(500, '所选话题必须1-5个');
  52. }
  53. $topicIds = implode(',', $topicIds);
  54. $data = [
  55. 'uid' => $request['uid'],
  56. 'username' => '暂无',
  57. 'mobile' => '暂无',
  58. 'avatar' => '暂无',
  59. 'type' => $request['type'],
  60. 'img' => $request['img'],
  61. 'video' => $request['video']??'',
  62. 'topic_ids' => $topicIds,
  63. 'title' => $request['title']??'',
  64. 'content' => $request['content'],
  65. 'location' => $request['location']??'',
  66. 'is_suggest' => $request['is_suggest'],
  67. 'is_hide' => 0
  68. ];
  69. $date = date('Y-m-d H:i:s');
  70. DB::beginTransaction();
  71. try{
  72. $post = $this->post->create($data);
  73. $this->postData->create([
  74. 'post_id' => $post->id,
  75. 'pv' => 0,
  76. 'pv_real' => 0,
  77. 'dislike_count' => 0,
  78. 'praise_count' => 0,
  79. 'praise_real_count' => 0,
  80. 'share_count' => 0,
  81. 'share_real_count' => 0,
  82. 'comment_count' => 0,
  83. 'comment_real_count' => 0,
  84. 'collect_count' => 0,
  85. 'collect_real_count' => 0,
  86. 'available_bean' => 0,
  87. 'will_collect_bean' => 0,
  88. 'collect_bean' => 0,
  89. 'weight' => 0
  90. ]);
  91. if(!empty($request['imgs']) && $request['type'] == 'image'){
  92. $imgData = [];
  93. foreach($request['imgs'] as $img){
  94. $imgData[] = [
  95. 'post_id' => $post->id,
  96. 'img' => $img,
  97. 'created_at' => $date,
  98. 'updated_at' => $date
  99. ];
  100. }
  101. $this->postImgs->insert($imgData);
  102. }
  103. DB::commit();
  104. return Response::create();
  105. }catch (QueryException $exception){
  106. DB::rollBack();
  107. Log::debug('发布内容:'.$exception->getMessage());
  108. return Response::create([
  109. 'message' => '发布失败,请重试',
  110. 'error' => $exception->getMessage(),
  111. 'status_code' => 500
  112. ]);
  113. }
  114. }
  115. /**
  116. * 内容列表
  117. */
  118. public function lists($request)
  119. {
  120. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  121. $where = [];
  122. if(isset($request['content'])){
  123. $where[] = ['content', 'like', "%{$request['content']}%"];
  124. }
  125. if(isset($request['is_suggest'])){
  126. $where[] = ['is_suggest', $request['is_suggest']];
  127. }
  128. if(isset($request['type'])){
  129. $where[] = ['type', $request['type']];
  130. }
  131. $sort = 'post.id';
  132. if(isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count'])){
  133. $sort = $request['sort'];
  134. }
  135. return $this->post
  136. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  137. ->select('post.*')
  138. ->where($where)
  139. ->where(function($query) use ($request){
  140. if(isset($request['keyword'])){
  141. $query->where('uid', '=', $request['keyword'])
  142. ->orWhere('username', 'like', "%{$request['keyword']}%")
  143. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  144. }
  145. })
  146. ->where(function($query) use ($request){
  147. if(isset($request['created_at'])){
  148. $time = explode('_', $request['created_at']);
  149. $query->whereBetween('post.created_at', $time);
  150. }
  151. })
  152. ->where(function ($query) use ($request){
  153. if(isset($request['category_ids']) || isset($request['topic_ids'])){
  154. $ids = [];
  155. if (isset($request['category_ids'])) {
  156. $categoryIds = explode('_', $request['category_ids']);
  157. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  158. }
  159. if (isset($request['topic_ids'])) {
  160. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  161. }
  162. foreach ($ids as $key=>$id) {
  163. if ($key==0) {
  164. $query = $query->whereRaw('FIND_IN_SET('.$id.',topic_ids)');
  165. } else {
  166. $query = $query->orWhereRaw('FIND_IN_SET('.$id.',topic_ids)');
  167. }
  168. }
  169. }
  170. })
  171. ->orderBy($sort,'desc')
  172. ->paginate($perPage);
  173. }
  174. }