PostRepository.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. return Response::create([
  52. 'message' => '所选话题必须1-5个',
  53. 'status_code' => 500
  54. ]);
  55. }
  56. $topicIds = implode(',', $topicIds);
  57. $data = [
  58. 'uid' => $request['uid'],
  59. 'username' => '暂无',
  60. 'mobile' => '暂无',
  61. 'avatar' => '暂无',
  62. 'type' => $request['type'],
  63. 'img' => $request['img'],
  64. 'video' => $request['video']??'',
  65. 'topic_ids' => $topicIds,
  66. 'title' => $request['title']??'',
  67. 'content' => $request['content'],
  68. 'location' => $request['location']??'',
  69. 'is_suggest' => $request['is_suggest'],
  70. 'is_hide' => 0
  71. ];
  72. $date = date('Y-m-d H:i:s');
  73. DB::beginTransaction();
  74. try{
  75. $post = $this->post->create($data);
  76. $this->postData->create([
  77. 'post_id' => $post->id,
  78. 'pv' => 0,
  79. 'pv_real' => 0,
  80. 'dislike_count' => 0,
  81. 'praise_count' => 0,
  82. 'praise_real_count' => 0,
  83. 'share_count' => 0,
  84. 'share_real_count' => 0,
  85. 'comment_count' => 0,
  86. 'comment_real_count' => 0,
  87. 'collect_count' => 0,
  88. 'collect_real_count' => 0,
  89. 'available_bean' => 0,
  90. 'will_collect_bean' => 0,
  91. 'collect_bean' => 0,
  92. 'weight' => 0
  93. ]);
  94. if(!empty($request['imgs']) && $request['type'] == 'image'){
  95. $imgData = [];
  96. foreach($request['imgs'] as $img){
  97. $imgData[] = [
  98. 'post_id' => $post->id,
  99. 'img' => $img,
  100. 'created_at' => $date,
  101. 'updated_at' => $date
  102. ];
  103. }
  104. $this->postImgs->insert($imgData);
  105. }
  106. DB::commit();
  107. return Response::create();
  108. }catch (QueryException $exception){
  109. DB::rollBack();
  110. Log::debug('发布内容:'.$exception->getMessage());
  111. return Response::create([
  112. 'message' => '发布失败,请重试',
  113. 'error' => $exception->getMessage(),
  114. 'status_code' => 500
  115. ]);
  116. }
  117. }
  118. /**
  119. * 内容列表
  120. */
  121. public function lists($request)
  122. {
  123. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  124. $where = [];
  125. if(isset($request['content'])){
  126. $where[] = ['content', 'like', "%{$request['content']}%"];
  127. }
  128. if(isset($request['is_suggest'])){
  129. $where[] = ['is_suggest', $request['is_suggest']];
  130. }
  131. if(isset($request['type'])){
  132. $where[] = ['type', $request['type']];
  133. }
  134. $sort = 'post.id';
  135. if(isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])){
  136. $sort = $request['sort'];
  137. }
  138. return $this->post
  139. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  140. ->select('post.*')
  141. ->where($where)
  142. ->where(function($query) use ($request){
  143. if(isset($request['keyword'])){
  144. $query->where('uid', '=', $request['keyword'])
  145. ->orWhere('username', 'like', "%{$request['keyword']}%")
  146. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  147. }
  148. })
  149. ->where(function($query) use ($request){
  150. if(isset($request['created_at'])){
  151. $time = explode('_', $request['created_at']);
  152. $query->whereBetween('post.created_at', $time);
  153. }
  154. })
  155. ->where(function ($query) use ($request){
  156. if(isset($request['category_ids']) || isset($request['topic_ids'])){
  157. $ids = [];
  158. if (isset($request['category_ids'])) {
  159. $categoryIds = explode('_', $request['category_ids']);
  160. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  161. }
  162. if (isset($request['topic_ids'])) {
  163. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  164. }
  165. foreach ($ids as $key=>$id) {
  166. if ($key==0) {
  167. $query = $query->whereRaw('FIND_IN_SET('.$id.',topic_ids)');
  168. } else {
  169. $query = $query->orWhereRaw('FIND_IN_SET('.$id.',topic_ids)');
  170. }
  171. }
  172. }
  173. })
  174. ->orderBy($sort,'desc')
  175. ->paginate($perPage);
  176. }
  177. /**
  178. * 推荐内容
  179. */
  180. public function suggest($request)
  181. {
  182. $post = $this->post->where('id', $request['id'])->first();
  183. if(!$post){
  184. return Response::create([
  185. 'message' => '获取内容信息失败',
  186. 'status_code' => 500
  187. ]);
  188. }
  189. if($post->is_suggest == 1){
  190. $post->is_suggest = 0;
  191. }else{
  192. $post->is_suggest = 1;
  193. }
  194. DB::beginTransaction();
  195. try{
  196. $post->save();
  197. DB::commit();
  198. return Response::create();
  199. }catch (QueryException $exception){
  200. DB::rollBack();
  201. Log::debug('推荐内容:'.$request['id'].$exception->getMessage());
  202. return Response::create([
  203. 'message' => '操作失败,请重试',
  204. 'error' => $exception->getMessage(),
  205. 'status_code' => 500
  206. ]);
  207. }
  208. }
  209. /**
  210. * 删除内容
  211. */
  212. public function delete($request)
  213. {
  214. $post = $this->post->where('id', $request['id'])->first();
  215. if(!$post){
  216. return Response::create([
  217. 'message' => '获取内容信息失败',
  218. 'status_code' => 500
  219. ]);
  220. }
  221. DB::beginTransaction();
  222. try{
  223. $post->delete();
  224. DB::commit();
  225. return Response::create();
  226. }catch (QueryException $exception){
  227. DB::rollBack();
  228. Log::debug('删除内容:'.$request['id'].$exception->getMessage());
  229. return Response::create([
  230. 'message' => '操作失败,请重试',
  231. 'error' => $exception->getMessage(),
  232. 'status_code' => 500
  233. ]);
  234. }
  235. }
  236. /**
  237. * 隐藏内容
  238. */
  239. public function hide($request)
  240. {
  241. $post = $this->post->where('id', $request['id'])->first();
  242. if(!$post){
  243. return Response::create([
  244. 'message' => '获取内容信息失败',
  245. 'status_code' => 500
  246. ]);
  247. }
  248. if($post->is_hide == 1){
  249. $post->is_hide = 0;
  250. }else{
  251. $post->is_hide = 1;
  252. }
  253. DB::beginTransaction();
  254. try{
  255. $post->save();
  256. DB::commit();
  257. return Response::create();
  258. }catch (QueryException $exception){
  259. DB::rollBack();
  260. Log::debug('隐藏内容:'.$request['id'].$exception->getMessage());
  261. return Response::create([
  262. 'message' => '操作失败,请重试',
  263. 'error' => $exception->getMessage(),
  264. 'status_code' => 500
  265. ]);
  266. }
  267. }
  268. }