PostController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/14
  6. * Time: 16:23
  7. */
  8. namespace App\Http\Controllers\V1;
  9. use App\Repositories\PostRepositories;
  10. use App\Traits\UserTrait;
  11. use App\Transformers\Post\CommentTransformer;
  12. use App\Transformers\Post\DetailTransformer;
  13. use App\Transformers\Post\ListTransformer;
  14. use App\Transformers\Post\ReplyTransformer;
  15. use App\Transformers\Post\SuggestTransformer;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Support\Carbon;
  18. use Illuminate\Support\Facades\Log;
  19. use Illuminate\Support\Facades\Validator;
  20. use Illuminate\Validation\Rule;
  21. use League\Fractal\Manager;
  22. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  23. use League\Fractal\Resource\Collection;
  24. use League\Fractal\Resource\Item;
  25. class PostController extends Controller
  26. {
  27. use UserTrait;
  28. public function __construct(PostRepositories $postRepositories)
  29. {
  30. $this->postRepositories = $postRepositories;
  31. }
  32. /**
  33. * 发布内容
  34. */
  35. public function create(Request $request)
  36. {
  37. $validator = Validator::make($request->all(), [
  38. 'type' => ['required',Rule::in('image', 'video', 'html')],
  39. 'img' => 'required|url',
  40. 'video' => 'required_if:type,video|string|url',
  41. 'topic_ids' => 'required|string|max:64',
  42. 'title' => 'nullable|string|max:20',
  43. 'content' => 'required|string|max:1000',
  44. 'location' => 'nullable|string|max:20',
  45. 'imgs' => 'required_if:type,image|string',
  46. ]);
  47. if ($validator->fails()) {
  48. return jsonError($validator->errors()->first());
  49. }
  50. return $this->postRepositories->create($request->all());
  51. }
  52. /**
  53. * 评论&回复
  54. */
  55. public function comment(Request $request)
  56. {
  57. $validator = Validator::make($request->all(), [
  58. 'post_id' => 'required|integer',
  59. 'content' => 'required|string|max:150',
  60. ]);
  61. if ($validator->fails()) {
  62. return jsonError($validator->errors()->first());
  63. }
  64. return $this->postRepositories->comment($request->all());
  65. }
  66. /**
  67. * 内容列表
  68. */
  69. public function index(Request $request)
  70. {
  71. $userInfo = $this->getUserInfo();
  72. if(empty($userInfo)){
  73. Log::info('获取用户信息失败');
  74. return jsonError('获取用户信息失败');
  75. }
  76. $list = $this->postRepositories->lists($request->all());
  77. $fractal = new Manager();
  78. $resource = new Collection($list, new ListTransformer($userInfo['uid']));
  79. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  80. $data = $fractal->createData($resource)->toArray();
  81. return jsonSuccess($data);
  82. }
  83. /**
  84. * 推荐内容列表
  85. */
  86. public function suggestPost(Request $request)
  87. {
  88. $userInfo = $this->getUserInfo();
  89. if(empty($userInfo)){
  90. Log::info('获取用户信息失败');
  91. return jsonError('获取用户信息失败');
  92. }
  93. $param = $request->all();
  94. $list = $this->postRepositories->suggestPost($param);
  95. $fractal = new Manager();
  96. $resource = new Collection($list, new SuggestTransformer($userInfo['uid']));
  97. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  98. $data = $fractal->createData($resource)->toArray();
  99. if(!(isset($param['current_page']) && $param['current_page'] > 1)){
  100. $newData = [];
  101. foreach($data['data'] as $key => $val){
  102. if($key == 1){
  103. $newData[] = ['show_type' => 1];
  104. }
  105. $newData[] = $val;
  106. }
  107. $data['data'] = $newData;
  108. }
  109. return jsonSuccess($data);
  110. }
  111. /**
  112. * 内容详情
  113. */
  114. public function detail(Request $request)
  115. {
  116. $validator = Validator::make($request->all(), [
  117. 'post_id' => 'required|integer',
  118. ]);
  119. if ($validator->fails()) {
  120. return jsonError($validator->errors()->first());
  121. }
  122. $userInfo = $this->getUserInfo();
  123. if(empty($userInfo)){
  124. Log::info('获取用户信息失败');
  125. return jsonError('获取用户信息失败');
  126. }
  127. $detail = $this->postRepositories->detail($request['post_id']);
  128. if(!$detail){
  129. return jsonError('获取内容信息失败');
  130. }
  131. $fractal = new Manager();
  132. $res = new Item($detail, new DetailTransformer($userInfo['uid']));
  133. return $array = $fractal->createData($res)->toArray();
  134. $param = $request->all();
  135. $list = $this->postRepositories->suggestPost($param);
  136. $resource = new Collection($list, new SuggestTransformer($userInfo['uid']));
  137. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  138. $data = $fractal->createData($resource)->toArray();
  139. if(!(isset($param['current_page']) && $param['current_page'] > 1)){
  140. $newData = [];
  141. foreach($data['data'] as $key => $val){
  142. if($key == 1){
  143. $newData[] = ['show_type' => 1];
  144. }
  145. $newData[] = $val;
  146. }
  147. $data['data'] = $newData;
  148. }
  149. return jsonSuccess($data);
  150. }
  151. /**
  152. * 评论列表
  153. */
  154. public function commentList(Request $request)
  155. {
  156. $validator = Validator::make($request->all(), [
  157. 'post_id' => 'required|integer',
  158. ]);
  159. if ($validator->fails()) {
  160. return jsonError($validator->errors()->first());
  161. }
  162. $list = $this->postRepositories->commentList($request->all());
  163. $fractal = new Manager();
  164. $resource = new Collection($list, new CommentTransformer());
  165. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  166. $data = $fractal->createData($resource)->toArray();
  167. return jsonSuccess($data);
  168. }
  169. /**
  170. * 回复列表
  171. */
  172. public function replyList(Request $request)
  173. {
  174. $validator = Validator::make($request->all(), [
  175. 'id' => 'required|integer',
  176. ]);
  177. if ($validator->fails()) {
  178. return jsonError($validator->errors()->first());
  179. }
  180. $detail = $this->postRepositories->commentDetail($request->all());
  181. if(!$detail){
  182. return jsonError('获取评论信息失败');
  183. }
  184. $list = $this->postRepositories->replyList($request->all());
  185. $fractal = new Manager();
  186. $resource = new Collection($list, new ReplyTransformer());
  187. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  188. $data = $fractal->createData($resource)->toArray();
  189. $data['data']['extra'] = [
  190. 'id' => $detail['id'],
  191. 'uid' => $detail['uid'],
  192. 'username' => $detail['username'],
  193. 'reply_count' => $detail->reply->count(),
  194. 'avatar' => $detail['avatar'],
  195. 'content' => $detail['is_delete']?'该评论已被删除':$detail['content'],
  196. 'created_at' => Carbon::parse($detail['created_at'])->diffForHumans(),
  197. ];
  198. return jsonSuccess($data);
  199. }
  200. }