PostController.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. $params = $request->all();
  74. $params['waste'] = 1;
  75. $productList = $this->postRepository->lists($params);
  76. $fractal = new Manager();
  77. $resource = new Collection($productList, new PostTransformer());
  78. $resource->setPaginator(new IlluminatePaginatorAdapter($productList));
  79. $data = $fractal->createData($resource)->toArray();
  80. $data['extra'] = [
  81. 'filters' => [
  82. 'content',
  83. 'topic_ids',
  84. 'category_ids',
  85. 'created_at',
  86. 'sort'
  87. ],
  88. 'columns' => [
  89. 'id',
  90. 'created_at',
  91. 'uid',
  92. 'topic',
  93. 'content',
  94. 'location',
  95. 'pv',
  96. 'praise_count',
  97. 'share_count',
  98. 'comment_count',
  99. 'collect_count',
  100. 'create_bean',
  101. 'is_suggest',
  102. ]
  103. ];
  104. return $data;
  105. }
  106. /**
  107. * 内容详情
  108. */
  109. public function detail(Request $request)
  110. {
  111. $validator = Validator::make($request->all(), [
  112. 'id' => 'required|exists:post'
  113. ]);
  114. if ($validator->fails()) {
  115. return $this->response->error($validator->errors()->first(), 500);
  116. }
  117. $post = $this->postRepository->detail($request->all());
  118. return $this->response->item($post, new DetailTransformer());
  119. }
  120. /**
  121. * 评论列表
  122. */
  123. public function commentList(Request $request)
  124. {
  125. $commentList = $this->postRepository->commentList($request->all());
  126. $fractal = new Manager();
  127. $resource = new Collection($commentList, new CommentTransformer());
  128. $resource->setPaginator(new IlluminatePaginatorAdapter($commentList));
  129. $data = $fractal->createData($resource)->toArray();
  130. $data['extra'] = [
  131. 'filters' => [
  132. ],
  133. 'columns' => [
  134. 'id',
  135. 'post_id',
  136. 'parent_id',
  137. 'uid',
  138. 'content',
  139. 'created_at',
  140. 'is_delete',
  141. ]
  142. ];
  143. return $data;
  144. }
  145. /**
  146. * 发布内容
  147. */
  148. public function create(Request $request)
  149. {
  150. $validator = Validator::make($request->all(), [
  151. 'uid' => 'required|integer',
  152. 'type' => ['required',Rule::in('image', 'video', 'html')],
  153. 'is_suggest' => ['required',Rule::in(0, 1)],
  154. 'img' => 'required|url',
  155. 'video' => 'required_if:type,video|string|url',
  156. 'topic_ids' => 'required|string|max:64',
  157. 'title' => 'nullable|string|max:20',
  158. 'content' => 'required|string|max:1000',
  159. 'location' => 'nullable|string|max:20',
  160. 'imgs' => 'required_if:type,image|array|max:9',
  161. 'imgs.*' => 'required|url',
  162. ]);
  163. if ($validator->fails()) {
  164. return $this->response->error($validator->errors()->first(), 500);
  165. }
  166. return $this->postRepository->create($request->all());
  167. }
  168. /**
  169. * 评论&回复
  170. */
  171. public function comment(Request $request)
  172. {
  173. $validator = Validator::make($request->all(), [
  174. 'post_id' => 'required|integer',
  175. 'uid' => 'required|integer',
  176. 'content' => 'required|string|max:150',
  177. ]);
  178. if ($validator->fails()) {
  179. return $this->response->error($validator->errors()->first(), 500);
  180. }
  181. return $this->postRepository->comment($request->all());
  182. }
  183. /**
  184. * 增加数据
  185. */
  186. public function addData(Request $request)
  187. {
  188. $validator = Validator::make($request->all(), [
  189. 'post_id' => 'required|exists:post,id',
  190. 'add_pv' => 'required|integer|min:0',
  191. 'add_praise_count' => 'required|integer|min:0',
  192. 'add_collect_count' => 'required|integer|min:0',
  193. 'add_share_count' => 'required|integer|min:0',
  194. ]);
  195. if ($validator->fails()) {
  196. return $this->response->error($validator->errors()->first(), 500);
  197. }
  198. return $this->postRepository->addData($request->all());
  199. }
  200. /**
  201. * 推荐内容
  202. */
  203. public function suggest(Request $request)
  204. {
  205. $validator = Validator::make($request->all(), [
  206. 'id' => 'required|integer',
  207. ]);
  208. if ($validator->fails()) {
  209. return $this->response->error($validator->errors()->first(), 500);
  210. }
  211. return $this->postRepository->suggest($request->all());
  212. }
  213. /**
  214. * 删除内容
  215. */
  216. public function delete(Request $request)
  217. {
  218. $validator = Validator::make($request->all(), [
  219. 'id' => 'required|integer',
  220. ]);
  221. if ($validator->fails()) {
  222. return $this->response->error($validator->errors()->first(), 500);
  223. }
  224. return $this->postRepository->delete($request->all());
  225. }
  226. /**
  227. * 复原内容
  228. */
  229. public function restore(Request $request)
  230. {
  231. $validator = Validator::make($request->all(), [
  232. 'id' => 'required|integer',
  233. ]);
  234. if ($validator->fails()) {
  235. return $this->response->error($validator->errors()->first(), 500);
  236. }
  237. return $this->postRepository->restore($request->all());
  238. }
  239. /**
  240. * 删除评论
  241. */
  242. public function commentDelete(Request $request)
  243. {
  244. $validator = Validator::make($request->all(), [
  245. 'id' => 'required|integer',
  246. ]);
  247. if ($validator->fails()) {
  248. return $this->response->error($validator->errors()->first(), 500);
  249. }
  250. return $this->postRepository->commentDelete($request->all());
  251. }
  252. /**
  253. * 隐藏内容
  254. */
  255. public function hide(Request $request)
  256. {
  257. $validator = Validator::make($request->all(), [
  258. 'id' => 'required|integer',
  259. ]);
  260. if ($validator->fails()) {
  261. return $this->response->error($validator->errors()->first(), 500);
  262. }
  263. return $this->postRepository->hide($request->all());
  264. }
  265. /**
  266. * 日志列表
  267. */
  268. public function log(Request $request)
  269. {
  270. $commentList = $this->postRepository->log($request->all());
  271. $fractal = new Manager();
  272. $resource = new Collection($commentList, new LogTransformer());
  273. $resource->setPaginator(new IlluminatePaginatorAdapter($commentList));
  274. $data = $fractal->createData($resource)->toArray();
  275. $data['extra'] = [
  276. 'filters' => [
  277. 'log_type',
  278. 'created_at',
  279. ],
  280. 'columns' => [
  281. 'id',
  282. 'username',
  283. 'log_type',
  284. 'created_at',
  285. 'content',
  286. ]
  287. ];
  288. return $data;
  289. }
  290. }