PostRepository.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 lists($request)
  124. {
  125. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  126. $where = [];
  127. if(isset($request['content'])){
  128. $where[] = ['content', 'like', "%{$request['content']}%"];
  129. }
  130. if(isset($request['is_suggest'])){
  131. $where[] = ['is_suggest', $request['is_suggest']];
  132. }
  133. if(isset($request['type'])){
  134. $where[] = ['type', $request['type']];
  135. }
  136. $sort = 'post.id';
  137. if(isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])){
  138. $sort = $request['sort'];
  139. }
  140. return $this->post
  141. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  142. ->select('post.*')
  143. ->where($where)
  144. ->where(function($query) use ($request){
  145. if(isset($request['keyword'])){
  146. $query->where('uid', '=', $request['keyword'])
  147. ->orWhere('username', 'like', "%{$request['keyword']}%")
  148. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  149. }
  150. })
  151. ->where(function($query) use ($request){
  152. if(isset($request['created_at'])){
  153. $time = explode('_', $request['created_at']);
  154. $query->whereBetween('post.created_at', $time);
  155. }
  156. })
  157. ->where(function ($query) use ($request){
  158. if(isset($request['category_ids']) || isset($request['topic_ids'])){
  159. $ids = [];
  160. if (isset($request['category_ids'])) {
  161. $categoryIds = explode('_', $request['category_ids']);
  162. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  163. }
  164. if (isset($request['topic_ids'])) {
  165. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  166. }
  167. foreach ($ids as $key=>$id) {
  168. if ($key==0) {
  169. $query = $query->whereRaw('FIND_IN_SET('.$id.',topic_ids)');
  170. } else {
  171. $query = $query->orWhereRaw('FIND_IN_SET('.$id.',topic_ids)');
  172. }
  173. }
  174. }
  175. })
  176. ->orderBy($sort,'desc')
  177. ->paginate($perPage);
  178. }
  179. /**
  180. * 内容详情
  181. */
  182. public function detail($request)
  183. {
  184. return $this->post->withTrashed()->find($request['id']);
  185. }
  186. /**
  187. * 评论列表
  188. */
  189. public function commentList($request)
  190. {
  191. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  192. return $this->postComment
  193. ->where('post_id', $request['id'])
  194. ->orderBy('id','desc')
  195. ->paginate($perPage);
  196. }
  197. /**
  198. * 推荐内容
  199. */
  200. public function suggest($request)
  201. {
  202. $post = $this->post->where('id', $request['id'])->first();
  203. if(!$post){
  204. return Response::create([
  205. 'message' => '获取内容信息失败',
  206. 'status_code' => 500
  207. ]);
  208. }
  209. if($post->is_suggest == 1){
  210. $post->is_suggest = 0;
  211. }else{
  212. $post->is_suggest = 1;
  213. }
  214. DB::beginTransaction();
  215. try{
  216. $post->save();
  217. DB::commit();
  218. return Response::create();
  219. }catch (QueryException $exception){
  220. DB::rollBack();
  221. Log::debug('推荐内容:'.$request['id'].$exception->getMessage());
  222. return Response::create([
  223. 'message' => '操作失败,请重试',
  224. 'error' => $exception->getMessage(),
  225. 'status_code' => 500
  226. ]);
  227. }
  228. }
  229. /**
  230. * 删除内容
  231. */
  232. public function delete($request)
  233. {
  234. $post = $this->post->where('id', $request['id'])->first();
  235. if(!$post){
  236. return Response::create([
  237. 'message' => '获取内容信息失败',
  238. 'status_code' => 500
  239. ]);
  240. }
  241. DB::beginTransaction();
  242. try{
  243. $post->delete();
  244. DB::commit();
  245. return Response::create();
  246. }catch (QueryException $exception){
  247. DB::rollBack();
  248. Log::debug('删除内容:'.$request['id'].$exception->getMessage());
  249. return Response::create([
  250. 'message' => '操作失败,请重试',
  251. 'error' => $exception->getMessage(),
  252. 'status_code' => 500
  253. ]);
  254. }
  255. }
  256. /**
  257. * 隐藏内容
  258. */
  259. public function hide($request)
  260. {
  261. $post = $this->post->where('id', $request['id'])->first();
  262. if(!$post){
  263. return Response::create([
  264. 'message' => '获取内容信息失败',
  265. 'status_code' => 500
  266. ]);
  267. }
  268. if($post->is_hide == 1){
  269. $post->is_hide = 0;
  270. }else{
  271. $post->is_hide = 1;
  272. }
  273. DB::beginTransaction();
  274. try{
  275. $post->save();
  276. DB::commit();
  277. return Response::create();
  278. }catch (QueryException $exception){
  279. DB::rollBack();
  280. Log::debug('隐藏内容:'.$request['id'].$exception->getMessage());
  281. return Response::create([
  282. 'message' => '操作失败,请重试',
  283. 'error' => $exception->getMessage(),
  284. 'status_code' => 500
  285. ]);
  286. }
  287. }
  288. }