PostController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. 'parent_id',
  136. 'uid',
  137. 'content',
  138. 'created_at',
  139. 'is_delete',
  140. ]
  141. ];
  142. return $data;
  143. }
  144. /**
  145. * 发布内容
  146. */
  147. public function create(Request $request)
  148. {
  149. $validator = Validator::make($request->all(), [
  150. 'uid' => 'required|integer',
  151. 'type' => ['required',Rule::in('image', 'video', 'html')],
  152. 'is_suggest' => ['required',Rule::in(0, 1)],
  153. 'img' => 'required|url',
  154. 'video' => 'required_if:type,video|string|url',
  155. 'topic_ids' => 'required|string|max:64',
  156. 'title' => 'nullable|string|max:20',
  157. 'content' => 'required|string|max:1000',
  158. 'location' => 'nullable|string|max:20',
  159. 'imgs' => 'required_if:type,image|array|max:9',
  160. 'imgs.*' => 'required|url',
  161. ]);
  162. if ($validator->fails()) {
  163. return $this->response->error($validator->errors()->first(), 500);
  164. }
  165. return $this->postRepository->create($request->all());
  166. }
  167. /**
  168. * 评论&回复
  169. */
  170. public function comment(Request $request)
  171. {
  172. $validator = Validator::make($request->all(), [
  173. 'post_id' => 'required|integer',
  174. 'uid' => 'required|integer',
  175. 'content' => 'required|string|max:150',
  176. ]);
  177. if ($validator->fails()) {
  178. return $this->response->error($validator->errors()->first(), 500);
  179. }
  180. return $this->postRepository->comment($request->all());
  181. }
  182. /**
  183. * 增加数据
  184. */
  185. public function addData(Request $request)
  186. {
  187. $validator = Validator::make($request->all(), [
  188. 'post_id' => 'required|exists:post,id',
  189. 'add_pv' => 'required|integer|min:0',
  190. 'add_praise_count' => 'required|integer|min:0',
  191. 'add_collect_count' => 'required|integer|min:0',
  192. 'add_share_count' => 'required|integer|min:0',
  193. ]);
  194. if ($validator->fails()) {
  195. return $this->response->error($validator->errors()->first(), 500);
  196. }
  197. return $this->postRepository->addData($request->all());
  198. }
  199. /**
  200. * 推荐内容
  201. */
  202. public function suggest(Request $request)
  203. {
  204. $validator = Validator::make($request->all(), [
  205. 'id' => 'required|integer',
  206. ]);
  207. if ($validator->fails()) {
  208. return $this->response->error($validator->errors()->first(), 500);
  209. }
  210. return $this->postRepository->suggest($request->all());
  211. }
  212. /**
  213. * 删除内容
  214. */
  215. public function delete(Request $request)
  216. {
  217. $validator = Validator::make($request->all(), [
  218. 'id' => 'required|integer',
  219. ]);
  220. if ($validator->fails()) {
  221. return $this->response->error($validator->errors()->first(), 500);
  222. }
  223. return $this->postRepository->delete($request->all());
  224. }
  225. /**
  226. * 复原内容
  227. */
  228. public function restore(Request $request)
  229. {
  230. $validator = Validator::make($request->all(), [
  231. 'id' => 'required|integer',
  232. ]);
  233. if ($validator->fails()) {
  234. return $this->response->error($validator->errors()->first(), 500);
  235. }
  236. return $this->postRepository->restore($request->all());
  237. }
  238. /**
  239. * 删除评论
  240. */
  241. public function commentDelete(Request $request)
  242. {
  243. $validator = Validator::make($request->all(), [
  244. 'id' => 'required|integer',
  245. ]);
  246. if ($validator->fails()) {
  247. return $this->response->error($validator->errors()->first(), 500);
  248. }
  249. return $this->postRepository->commentDelete($request->all());
  250. }
  251. /**
  252. * 隐藏内容
  253. */
  254. public function hide(Request $request)
  255. {
  256. $validator = Validator::make($request->all(), [
  257. 'id' => 'required|integer',
  258. ]);
  259. if ($validator->fails()) {
  260. return $this->response->error($validator->errors()->first(), 500);
  261. }
  262. return $this->postRepository->hide($request->all());
  263. }
  264. /**
  265. * 日志列表
  266. */
  267. public function log(Request $request)
  268. {
  269. $commentList = $this->postRepository->log($request->all());
  270. $fractal = new Manager();
  271. $resource = new Collection($commentList, new LogTransformer());
  272. $resource->setPaginator(new IlluminatePaginatorAdapter($commentList));
  273. $data = $fractal->createData($resource)->toArray();
  274. $data['extra'] = [
  275. 'filters' => [
  276. 'log_type',
  277. 'created_at',
  278. ],
  279. 'columns' => [
  280. 'id',
  281. 'username',
  282. 'log_type',
  283. 'created_at',
  284. 'content',
  285. ]
  286. ];
  287. return $data;
  288. }
  289. }