PostRepository.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 App\Traits\PostTrait;
  17. use Illuminate\Database\QueryException;
  18. use Dingo\Api\Http\Response;
  19. use Illuminate\Support\Facades\DB;
  20. use Illuminate\Support\Facades\Log;
  21. use Illuminate\Support\Facades\Redis;
  22. use Symfony\Component\HttpKernel\Exception\HttpException;
  23. use Tymon\JWTAuth\Facades\JWTAuth;
  24. class PostRepository
  25. {
  26. use PostTrait;
  27. public function __construct(Post $post,
  28. PostData $postData,
  29. PostComment $postComment,
  30. PostImgs $postImgs,
  31. PostLog $postLog,
  32. CategoryTopic $categoryTopic,
  33. Topic $topic)
  34. {
  35. $this->post = $post;
  36. $this->postData = $postData;
  37. $this->postComment = $postComment;
  38. $this->postImgs = $postImgs;
  39. $this->postLog = $postLog;
  40. $this->categoryTopic = $categoryTopic;
  41. $this->topic = $topic;
  42. }
  43. /**
  44. * 发布内容
  45. */
  46. public function create($request)
  47. {
  48. //验证小号
  49. //验证话题
  50. $topicIds = $this->topic->whereIn('id', explode(',', $request['topic_ids']))->pluck('id')->toArray();
  51. $topicCount = count($topicIds);
  52. if($topicCount == 0 || $topicCount >= 5){
  53. return Response::create([
  54. 'message' => '所选话题必须1-5个',
  55. 'status_code' => 500
  56. ]);
  57. }
  58. $topicIds = implode(',', $topicIds);
  59. $data = [
  60. 'uid' => $request['uid'],
  61. 'username' => '暂无',
  62. 'mobile' => '暂无',
  63. 'avatar' => '暂无',
  64. 'type' => $request['type'],
  65. 'img' => $request['img'],
  66. 'video' => $request['video']??'',
  67. 'topic_ids' => $topicIds,
  68. 'title' => $request['title']??'',
  69. 'content' => $request['content'],
  70. 'location' => $request['location']??'',
  71. 'is_suggest' => $request['is_suggest'],
  72. 'is_hide' => 0
  73. ];
  74. $date = date('Y-m-d H:i:s');
  75. DB::beginTransaction();
  76. try{
  77. $post = $this->post->create($data);
  78. $this->postData->create([
  79. 'post_id' => $post->id,
  80. 'pv' => 0,
  81. 'pv_real' => 0,
  82. 'dislike_count' => 0,
  83. 'praise_count' => 0,
  84. 'praise_real_count' => 0,
  85. 'share_count' => 0,
  86. 'share_real_count' => 0,
  87. 'comment_count' => 0,
  88. 'comment_real_count' => 0,
  89. 'collect_count' => 0,
  90. 'collect_real_count' => 0,
  91. 'available_bean' => $this->availableBean(),
  92. 'will_collect_bean' => rand(100, 200),
  93. 'collect_bean' => 0,
  94. 'weight' => 0
  95. ]);
  96. if(!empty($request['imgs']) && $request['type'] == 'image'){
  97. $imgData = [];
  98. foreach($request['imgs'] as $img){
  99. $imgData[] = [
  100. 'post_id' => $post->id,
  101. 'img' => $img,
  102. 'created_at' => $date,
  103. 'updated_at' => $date
  104. ];
  105. }
  106. $this->postImgs->insert($imgData);
  107. }
  108. DB::commit();
  109. return Response::create();
  110. }catch (QueryException $exception){
  111. DB::rollBack();
  112. Log::debug('发布内容:'.$exception->getMessage());
  113. return Response::create([
  114. 'message' => '发布失败,请重试',
  115. 'error' => $exception->getMessage(),
  116. 'status_code' => 500
  117. ]);
  118. }
  119. }
  120. /**
  121. * 评论&回复
  122. */
  123. public function comment($request)
  124. {
  125. //验证小号
  126. $post = $this->post->find($request['post_id']);
  127. if(!$post){
  128. return Response::create([
  129. 'message' => '获取内容失败',
  130. 'status_code' => 500
  131. ]);
  132. }
  133. $data = [
  134. 'uid' => $request['uid'],
  135. 'post_id' => $request['post_id'],
  136. 'parent_id' => 0,
  137. 'username' => '暂无',
  138. 'avatar' => '暂无',
  139. 'content' => $request['content'],
  140. 'is_delete' => 0,
  141. ];
  142. if(isset($request['parent_id']) && $request['parent_id'] != 0){
  143. $comment = $this->postComment->find($request['parent_id']);
  144. if(!$comment){
  145. return Response::create([
  146. 'message' => '获取评论信息失败',
  147. 'status_code' => 500
  148. ]);
  149. }
  150. if($comment->parent_id){
  151. return Response::create([
  152. 'message' => '只能回复评论',
  153. 'status_code' => 500
  154. ]);
  155. }
  156. $data['parent_id'] = $request['parent_id'];
  157. }
  158. DB::beginTransaction();
  159. try{
  160. $this->postComment->create($data);
  161. $post->data->comment_count += 1;
  162. $post->data->comment_real_count += 1;
  163. $post->data->save();
  164. DB::commit();
  165. return Response::create();
  166. }catch (QueryException $exception){
  167. DB::rollBack();
  168. Log::debug('评论内容:'.$request['post_id'].$exception->getMessage());
  169. return Response::create([
  170. 'message' => '评论失败,请重试',
  171. 'error' => $exception->getMessage(),
  172. 'status_code' => 500
  173. ]);
  174. }
  175. }
  176. /**
  177. * 内容列表
  178. */
  179. public function lists($request)
  180. {
  181. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  182. $where = [];
  183. if(isset($request['content'])){
  184. $where[] = ['content', 'like', "%{$request['content']}%"];
  185. }
  186. if(isset($request['is_suggest'])){
  187. $where[] = ['is_suggest', $request['is_suggest']];
  188. }
  189. if(isset($request['type'])){
  190. $where[] = ['type', $request['type']];
  191. }
  192. $sort = 'post.id';
  193. if(isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])){
  194. $sort = $request['sort'];
  195. }
  196. return $this->post
  197. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  198. ->select('post.*')
  199. ->where($where)
  200. ->where(function($query) use ($request){
  201. if(isset($request['keyword'])){
  202. $query->where('uid', '=', $request['keyword'])
  203. ->orWhere('username', 'like', "%{$request['keyword']}%")
  204. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  205. }
  206. })
  207. ->where(function($query) use ($request){
  208. if(isset($request['created_at'])){
  209. $time = explode('_', $request['created_at']);
  210. $query->whereBetween('post.created_at', $time);
  211. }
  212. })
  213. ->where(function ($query) use ($request){
  214. if(isset($request['category_ids']) || isset($request['topic_ids'])){
  215. $ids = [];
  216. if (isset($request['category_ids'])) {
  217. $categoryIds = explode('_', $request['category_ids']);
  218. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  219. }
  220. if (isset($request['topic_ids'])) {
  221. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  222. }
  223. foreach ($ids as $key=>$id) {
  224. if ($key==0) {
  225. $query = $query->whereRaw('FIND_IN_SET('.$id.',topic_ids)');
  226. } else {
  227. $query = $query->orWhereRaw('FIND_IN_SET('.$id.',topic_ids)');
  228. }
  229. }
  230. }
  231. })
  232. ->orderBy($sort,'desc')
  233. ->paginate($perPage);
  234. }
  235. /**
  236. * 内容详情
  237. */
  238. public function detail($request)
  239. {
  240. return $this->post->withTrashed()->find($request['id']);
  241. }
  242. /**
  243. * 评论列表
  244. */
  245. public function commentList($request)
  246. {
  247. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  248. return $this->postComment
  249. ->where('post_id', $request['id'])
  250. ->orderBy('id','desc')
  251. ->paginate($perPage);
  252. }
  253. /**
  254. * 推荐内容
  255. */
  256. public function suggest($request)
  257. {
  258. $post = $this->post->where('id', $request['id'])->first();
  259. if(!$post){
  260. return Response::create([
  261. 'message' => '获取内容信息失败',
  262. 'status_code' => 500
  263. ]);
  264. }
  265. if($post->is_suggest == 1){
  266. $post->is_suggest = 0;
  267. }else{
  268. $post->is_suggest = 1;
  269. }
  270. DB::beginTransaction();
  271. try{
  272. $post->save();
  273. DB::commit();
  274. return Response::create();
  275. }catch (QueryException $exception){
  276. DB::rollBack();
  277. Log::debug('推荐内容:'.$request['id'].$exception->getMessage());
  278. return Response::create([
  279. 'message' => '操作失败,请重试',
  280. 'error' => $exception->getMessage(),
  281. 'status_code' => 500
  282. ]);
  283. }
  284. }
  285. /**
  286. * 删除内容
  287. */
  288. public function delete($request)
  289. {
  290. $post = $this->post->where('id', $request['id'])->first();
  291. if(!$post){
  292. return Response::create([
  293. 'message' => '获取内容信息失败',
  294. 'status_code' => 500
  295. ]);
  296. }
  297. DB::beginTransaction();
  298. try{
  299. $post->delete();
  300. DB::commit();
  301. return Response::create();
  302. }catch (QueryException $exception){
  303. DB::rollBack();
  304. Log::debug('删除内容:'.$request['id'].$exception->getMessage());
  305. return Response::create([
  306. 'message' => '操作失败,请重试',
  307. 'error' => $exception->getMessage(),
  308. 'status_code' => 500
  309. ]);
  310. }
  311. }
  312. /**
  313. * 隐藏内容
  314. */
  315. public function hide($request)
  316. {
  317. $post = $this->post->where('id', $request['id'])->first();
  318. if(!$post){
  319. return Response::create([
  320. 'message' => '获取内容信息失败',
  321. 'status_code' => 500
  322. ]);
  323. }
  324. if($post->is_hide == 1){
  325. $post->is_hide = 0;
  326. }else{
  327. $post->is_hide = 1;
  328. }
  329. DB::beginTransaction();
  330. try{
  331. $post->save();
  332. DB::commit();
  333. return Response::create();
  334. }catch (QueryException $exception){
  335. DB::rollBack();
  336. Log::debug('隐藏内容:'.$request['id'].$exception->getMessage());
  337. return Response::create([
  338. 'message' => '操作失败,请重试',
  339. 'error' => $exception->getMessage(),
  340. 'status_code' => 500
  341. ]);
  342. }
  343. }
  344. }