PostRepository.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 suggest($request)
  183. {
  184. $post = $this->post->where('id', $request['id'])->first();
  185. if(!$post){
  186. return Response::create([
  187. 'message' => '获取内容信息失败',
  188. 'status_code' => 500
  189. ]);
  190. }
  191. if($post->is_suggest == 1){
  192. $post->is_suggest = 0;
  193. }else{
  194. $post->is_suggest = 1;
  195. }
  196. DB::beginTransaction();
  197. try{
  198. $post->save();
  199. DB::commit();
  200. return Response::create();
  201. }catch (QueryException $exception){
  202. DB::rollBack();
  203. Log::debug('推荐内容:'.$request['id'].$exception->getMessage());
  204. return Response::create([
  205. 'message' => '操作失败,请重试',
  206. 'error' => $exception->getMessage(),
  207. 'status_code' => 500
  208. ]);
  209. }
  210. }
  211. /**
  212. * 删除内容
  213. */
  214. public function delete($request)
  215. {
  216. $post = $this->post->where('id', $request['id'])->first();
  217. if(!$post){
  218. return Response::create([
  219. 'message' => '获取内容信息失败',
  220. 'status_code' => 500
  221. ]);
  222. }
  223. DB::beginTransaction();
  224. try{
  225. $post->delete();
  226. DB::commit();
  227. return Response::create();
  228. }catch (QueryException $exception){
  229. DB::rollBack();
  230. Log::debug('删除内容:'.$request['id'].$exception->getMessage());
  231. return Response::create([
  232. 'message' => '操作失败,请重试',
  233. 'error' => $exception->getMessage(),
  234. 'status_code' => 500
  235. ]);
  236. }
  237. }
  238. /**
  239. * 隐藏内容
  240. */
  241. public function hide($request)
  242. {
  243. $post = $this->post->where('id', $request['id'])->first();
  244. if(!$post){
  245. return Response::create([
  246. 'message' => '获取内容信息失败',
  247. 'status_code' => 500
  248. ]);
  249. }
  250. if($post->is_hide == 1){
  251. $post->is_hide = 0;
  252. }else{
  253. $post->is_hide = 1;
  254. }
  255. DB::beginTransaction();
  256. try{
  257. $post->save();
  258. DB::commit();
  259. return Response::create();
  260. }catch (QueryException $exception){
  261. DB::rollBack();
  262. Log::debug('隐藏内容:'.$request['id'].$exception->getMessage());
  263. return Response::create([
  264. 'message' => '操作失败,请重试',
  265. 'error' => $exception->getMessage(),
  266. 'status_code' => 500
  267. ]);
  268. }
  269. }
  270. }