PostController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/5
  6. * Time: 15:58
  7. */
  8. namespace App\Http\Controllers\Post;
  9. use App\Repositories\Post\PostRepository;
  10. use App\Transformers\Post\CommentTransformer;
  11. use App\Transformers\Post\DetailTransformer;
  12. use App\Transformers\Post\LogTransformer;
  13. use App\Transformers\Post\PostTransformer;
  14. use Illuminate\Http\Request;
  15. use Illuminate\Support\Facades\Validator;
  16. use App\Http\Controllers\Controller;
  17. use Illuminate\Validation\Rule;
  18. use League\Fractal\Manager;
  19. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  20. use League\Fractal\Resource\Collection;
  21. use League\Fractal\Resource\Item;
  22. class PostController extends Controller
  23. {
  24. public function __construct(PostRepository $postRepository)
  25. {
  26. $this->postRepository = $postRepository;
  27. }
  28. /**
  29. * 内容列表
  30. */
  31. public function index(Request $request)
  32. {
  33. $productList = $this->postRepository->lists($request->all());
  34. $fractal = new Manager();
  35. $resource = new Collection($productList, new PostTransformer());
  36. $resource->setPaginator(new IlluminatePaginatorAdapter($productList));
  37. $data = $fractal->createData($resource)->toArray();
  38. $data['extra'] = [
  39. 'filters' => [
  40. 'keyword',
  41. 'content',
  42. 'topic_ids',
  43. 'category_ids',
  44. 'is_suggest',
  45. 'created_at',
  46. 'type',
  47. 'sort'
  48. ],
  49. 'columns' => [
  50. 'id',
  51. 'created_at',
  52. 'uid',
  53. 'topic',
  54. 'content',
  55. 'location',
  56. 'pv',
  57. 'praise_count',
  58. 'share_count',
  59. 'comment_count',
  60. 'collect_count',
  61. 'create_bean',
  62. 'is_suggest',
  63. 'is_hide',
  64. ]
  65. ];
  66. return $data;
  67. }
  68. /**
  69. * 回收站列表
  70. */
  71. public function waste(Request $request)
  72. {
  73. return $request;
  74. $productList = $this->postRepository->waste($request->all());
  75. $fractal = new Manager();
  76. $resource = new Collection($productList, new PostTransformer());
  77. $resource->setPaginator(new IlluminatePaginatorAdapter($productList));
  78. $data = $fractal->createData($resource)->toArray();
  79. $data['extra'] = [
  80. 'filters' => [
  81. 'content',
  82. 'topic_ids',
  83. 'category_ids',
  84. 'created_at',
  85. 'sort'
  86. ],
  87. 'columns' => [
  88. 'id',
  89. 'created_at',
  90. 'uid',
  91. 'topic',
  92. 'content',
  93. 'location',
  94. 'pv',
  95. 'praise_count',
  96. 'share_count',
  97. 'comment_count',
  98. 'collect_count',
  99. 'create_bean',
  100. 'is_suggest',
  101. ]
  102. ];
  103. return $data;
  104. }
  105. /**
  106. * 内容详情
  107. */
  108. public function detail(Request $request)
  109. {
  110. $validator = Validator::make($request->all(), [
  111. 'id' => 'required|exists:post'
  112. ]);
  113. if ($validator->fails()) {
  114. return $this->response->error($validator->errors()->first(), 500);
  115. }
  116. $post = $this->postRepository->detail($request->all());
  117. return $this->response->item($post, new DetailTransformer());
  118. }
  119. /**
  120. * 评论列表
  121. */
  122. public function commentList(Request $request)
  123. {
  124. $validator = Validator::make($request->all(), [
  125. 'post_id' => 'required|exists:post,id'
  126. ]);
  127. if ($validator->fails()) {
  128. return $this->response->error($validator->errors()->first(), 500);
  129. }
  130. $commentList = $this->postRepository->commentList($request->all());
  131. $fractal = new Manager();
  132. $resource = new Collection($commentList, new CommentTransformer());
  133. $resource->setPaginator(new IlluminatePaginatorAdapter($commentList));
  134. $data = $fractal->createData($resource)->toArray();
  135. $data['extra'] = [
  136. 'filters' => [
  137. ],
  138. 'columns' => [
  139. 'id',
  140. 'parent_id',
  141. 'uid',
  142. 'content',
  143. 'created_at',
  144. 'is_delete',
  145. ]
  146. ];
  147. return $data;
  148. }
  149. /**
  150. * 发布内容
  151. */
  152. public function create(Request $request)
  153. {
  154. $validator = Validator::make($request->all(), [
  155. 'uid' => 'required|integer',
  156. 'type' => ['required',Rule::in('image', 'video', 'html')],
  157. 'is_suggest' => ['required',Rule::in(0, 1)],
  158. 'img' => 'required|url',
  159. 'video' => 'required_if:type,video|string|url',
  160. 'topic_ids' => 'required|string|max:64',
  161. 'title' => 'nullable|string|max:20',
  162. 'content' => 'required|string|max:1000',
  163. 'location' => 'required|string',
  164. 'imgs' => 'required_if:type,image|array|max:9',
  165. 'imgs.*' => 'required|url',
  166. ]);
  167. if ($validator->fails()) {
  168. return $this->response->error($validator->errors()->first(), 500);
  169. }
  170. return $this->postRepository->create($request->all());
  171. }
  172. /**
  173. * 评论&回复
  174. */
  175. public function comment(Request $request)
  176. {
  177. $validator = Validator::make($request->all(), [
  178. 'post_id' => 'required|integer',
  179. 'uid' => 'required|integer',
  180. 'content' => 'required|string|max:150',
  181. ]);
  182. if ($validator->fails()) {
  183. return $this->response->error($validator->errors()->first(), 500);
  184. }
  185. return $this->postRepository->comment($request->all());
  186. }
  187. /**
  188. * 增加数据
  189. */
  190. public function addData(Request $request)
  191. {
  192. $validator = Validator::make($request->all(), [
  193. 'post_id' => 'required|exists:post,id',
  194. 'add_pv' => 'required|integer|min:0',
  195. 'add_praise_count' => 'required|integer|min:0',
  196. 'add_collect_count' => 'required|integer|min:0',
  197. 'add_share_count' => 'required|integer|min:0',
  198. ]);
  199. if ($validator->fails()) {
  200. return $this->response->error($validator->errors()->first(), 500);
  201. }
  202. return $this->postRepository->addData($request->all());
  203. }
  204. /**
  205. * 推荐内容
  206. */
  207. public function suggest(Request $request)
  208. {
  209. $validator = Validator::make($request->all(), [
  210. 'id' => 'required|integer',
  211. ]);
  212. if ($validator->fails()) {
  213. return $this->response->error($validator->errors()->first(), 500);
  214. }
  215. return $this->postRepository->suggest($request->all());
  216. }
  217. /**
  218. * 删除内容
  219. */
  220. public function delete(Request $request)
  221. {
  222. $validator = Validator::make($request->all(), [
  223. 'id' => 'required|integer',
  224. ]);
  225. if ($validator->fails()) {
  226. return $this->response->error($validator->errors()->first(), 500);
  227. }
  228. return $this->postRepository->delete($request->all());
  229. }
  230. /**
  231. * 删除评论
  232. */
  233. public function commentDelete(Request $request)
  234. {
  235. $validator = Validator::make($request->all(), [
  236. 'id' => 'required|integer',
  237. ]);
  238. if ($validator->fails()) {
  239. return $this->response->error($validator->errors()->first(), 500);
  240. }
  241. return $this->postRepository->commentDelete($request->all());
  242. }
  243. /**
  244. * 隐藏内容
  245. */
  246. public function hide(Request $request)
  247. {
  248. $validator = Validator::make($request->all(), [
  249. 'id' => 'required|integer',
  250. ]);
  251. if ($validator->fails()) {
  252. return $this->response->error($validator->errors()->first(), 500);
  253. }
  254. return $this->postRepository->hide($request->all());
  255. }
  256. /**
  257. * 日志列表
  258. */
  259. public function log(Request $request)
  260. {
  261. $commentList = $this->postRepository->log($request->all());
  262. $fractal = new Manager();
  263. $resource = new Collection($commentList, new LogTransformer());
  264. $resource->setPaginator(new IlluminatePaginatorAdapter($commentList));
  265. $data = $fractal->createData($resource)->toArray();
  266. $data['extra'] = [
  267. 'filters' => [
  268. 'log_type',
  269. 'created_at',
  270. ],
  271. 'columns' => [
  272. 'id',
  273. 'username',
  274. 'log_type',
  275. 'created_at',
  276. 'content',
  277. ]
  278. ];
  279. return $data;
  280. }
  281. }