PostController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 Carbon\Carbon;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Facades\Validator;
  17. use App\Http\Controllers\Controller;
  18. use Illuminate\Validation\Rule;
  19. use League\Fractal\Manager;
  20. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  21. use League\Fractal\Resource\Collection;
  22. use League\Fractal\Resource\Item;
  23. class PostController extends Controller
  24. {
  25. public function __construct(PostRepository $postRepository)
  26. {
  27. $this->postRepository = $postRepository;
  28. }
  29. /**
  30. * 内容列表
  31. */
  32. public function index(Request $request)
  33. {
  34. $productList = $this->postRepository->lists($request->all());
  35. $fractal = new Manager();
  36. $resource = new Collection($productList, new PostTransformer());
  37. $resource->setPaginator(new IlluminatePaginatorAdapter($productList));
  38. $data = $fractal->createData($resource)->toArray();
  39. $data['extra'] = [
  40. 'filters' => [
  41. 'keyword',
  42. 'content',
  43. 'topic_ids',
  44. 'category_ids',
  45. 'is_suggest',
  46. 'created_at',
  47. 'type',
  48. 'sort'
  49. ],
  50. 'columns' => [
  51. 'id',
  52. 'created_at',
  53. 'uid',
  54. 'topic',
  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. 'video_id' => 'required_if:type,video|string',
  157. 'topic_ids' => 'required|string|max:64',
  158. 'title' => 'nullable|string|max:20',
  159. 'content' => 'required|string',
  160. 'location' => 'nullable|string|max:20',
  161. 'imgs' => 'required_if:type,image|array|max:9',
  162. 'imgs.*' => 'required|url',
  163. ]);
  164. if ($validator->fails()) {
  165. return $this->response->error($validator->errors()->first(), 500);
  166. }
  167. return $this->postRepository->create($request->all());
  168. }
  169. /**
  170. * 评论&回复
  171. */
  172. public function comment(Request $request)
  173. {
  174. $validator = Validator::make($request->all(), [
  175. 'post_id' => 'required|integer',
  176. 'uid' => 'required|integer',
  177. 'content' => 'required|string|max:150',
  178. ]);
  179. if ($validator->fails()) {
  180. return $this->response->error($validator->errors()->first(), 500);
  181. }
  182. return $this->postRepository->comment($request->all());
  183. }
  184. /**
  185. * 增加数据
  186. */
  187. public function addData(Request $request)
  188. {
  189. $validator = Validator::make($request->all(), [
  190. 'post_id' => 'required|exists:post,id',
  191. 'add_pv' => 'required|integer|min:0',
  192. 'add_praise_count' => 'required|integer|min:0',
  193. 'add_collect_count' => 'required|integer|min:0',
  194. 'add_share_count' => 'required|integer|min:0',
  195. ]);
  196. if ($validator->fails()) {
  197. return $this->response->error($validator->errors()->first(), 500);
  198. }
  199. return $this->postRepository->addData($request->all());
  200. }
  201. /**
  202. * 推荐内容
  203. */
  204. public function suggest(Request $request)
  205. {
  206. $validator = Validator::make($request->all(), [
  207. 'id' => 'required|integer',
  208. ]);
  209. if ($validator->fails()) {
  210. return $this->response->error($validator->errors()->first(), 500);
  211. }
  212. return $this->postRepository->suggest($request->all());
  213. }
  214. /**
  215. * 删除内容
  216. */
  217. public function delete(Request $request)
  218. {
  219. $validator = Validator::make($request->all(), [
  220. 'id' => 'required|integer',
  221. ]);
  222. if ($validator->fails()) {
  223. return $this->response->error($validator->errors()->first(), 500);
  224. }
  225. return $this->postRepository->delete($request->all());
  226. }
  227. /**
  228. * 复原内容
  229. */
  230. public function restore(Request $request)
  231. {
  232. $validator = Validator::make($request->all(), [
  233. 'id' => 'required|integer',
  234. ]);
  235. if ($validator->fails()) {
  236. return $this->response->error($validator->errors()->first(), 500);
  237. }
  238. return $this->postRepository->restore($request->all());
  239. }
  240. /**
  241. * 删除评论
  242. */
  243. public function commentDelete(Request $request)
  244. {
  245. $validator = Validator::make($request->all(), [
  246. 'id' => 'required|integer',
  247. ]);
  248. if ($validator->fails()) {
  249. return $this->response->error($validator->errors()->first(), 500);
  250. }
  251. return $this->postRepository->commentDelete($request->all());
  252. }
  253. /**
  254. * 隐藏内容
  255. */
  256. public function hide(Request $request)
  257. {
  258. $validator = Validator::make($request->all(), [
  259. 'id' => 'required|integer',
  260. ]);
  261. if ($validator->fails()) {
  262. return $this->response->error($validator->errors()->first(), 500);
  263. }
  264. return $this->postRepository->hide($request->all());
  265. }
  266. /**
  267. * 日志列表
  268. */
  269. public function log(Request $request)
  270. {
  271. $commentList = $this->postRepository->log($request->all());
  272. $fractal = new Manager();
  273. $resource = new Collection($commentList, new LogTransformer());
  274. $resource->setPaginator(new IlluminatePaginatorAdapter($commentList));
  275. $data = $fractal->createData($resource)->toArray();
  276. $data['extra'] = [
  277. 'filters' => [
  278. 'log_type',
  279. 'created_at',
  280. ],
  281. 'columns' => [
  282. 'id',
  283. 'username',
  284. 'log_type',
  285. 'created_at',
  286. 'content',
  287. ]
  288. ];
  289. return $data;
  290. }
  291. public function statistics(Request $request){
  292. $request = $request->all();
  293. $start = Carbon::parse(!empty($request['start']) ? Carbon::parse($request['start'])->startOfDay()->toDateTimeString() : Carbon::yesterday())->startOfDay()->toDateTimeString();
  294. $end = Carbon::parse(!empty($request['end']) ? Carbon::parse($request['end'])->endOfDay()->toDateTimeString() : Carbon::yesterday())->endOfDay()->toDateTimeString();
  295. return $this->postRepository->statistics($start,$end);
  296. }
  297. }