PostController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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\CmsTrait;
  11. use App\Traits\UserTrait;
  12. use App\Transformers\Post\CommentTransformer;
  13. use App\Transformers\Post\DetailTransformer;
  14. use App\Transformers\Post\ListTransformer;
  15. use App\Transformers\Post\MyTransformer;
  16. use App\Transformers\Post\ReplyTransformer;
  17. use App\Transformers\Post\SuggestTransformer;
  18. use App\Transformers\Post\VideoTransformer;
  19. use App\Transformers\Topic\TopicDetailTransformer;
  20. use App\Transformers\Topic\TopicListTransformer;
  21. use App\Transformers\Topic\TopicPostTransformer;
  22. use Illuminate\Http\Request;
  23. use Illuminate\Support\Carbon;
  24. use Illuminate\Support\Facades\Log;
  25. use Illuminate\Support\Facades\Redis;
  26. use Illuminate\Support\Facades\Validator;
  27. use Illuminate\Validation\Rule;
  28. use League\Fractal\Manager;
  29. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  30. use League\Fractal\Resource\Collection;
  31. use League\Fractal\Resource\Item;
  32. class PostController extends Controller
  33. {
  34. use UserTrait;
  35. use CmsTrait;
  36. public function __construct(PostRepositories $postRepositories)
  37. {
  38. $this->postRepositories = $postRepositories;
  39. }
  40. /**
  41. * 发布内容
  42. */
  43. public function create(Request $request)
  44. {
  45. $validator = Validator::make($request->all(), [
  46. 'type' => ['required',Rule::in('image', 'video', 'html')],
  47. 'img' => 'required|url',
  48. 'video' => 'required_if:type,video|string|url',
  49. 'topic_ids' => 'required|string|max:64',
  50. 'title' => 'nullable|string|max:20',
  51. 'content' => 'required|string|max:1000',
  52. 'location' => 'nullable|string|max:20',
  53. 'imgs' => 'required_if:type,image|string',
  54. ]);
  55. if ($validator->fails()) {
  56. return jsonError($validator->errors()->first());
  57. }
  58. return $this->postRepositories->create($request->all());
  59. }
  60. /**
  61. * 评论&回复
  62. */
  63. public function comment(Request $request)
  64. {
  65. $validator = Validator::make($request->all(), [
  66. 'post_id' => 'required|integer',
  67. 'content' => 'required|string|max:150',
  68. ]);
  69. if ($validator->fails()) {
  70. return jsonError($validator->errors()->first());
  71. }
  72. return $this->postRepositories->comment($request->all());
  73. }
  74. /**
  75. * 内容列表
  76. */
  77. public function index(Request $request)
  78. {
  79. $userInfo = $this->getUserInfo();
  80. if(empty($userInfo)){
  81. Log::info('获取用户信息失败');
  82. return jsonError('获取用户信息失败');
  83. }
  84. $list = $this->postRepositories->lists($request->all());
  85. $fractal = new Manager();
  86. $resource = new Collection($list, new ListTransformer($userInfo['uid']));
  87. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  88. $data = $fractal->createData($resource)->toArray();
  89. return jsonSuccess($data);
  90. }
  91. /**
  92. * 视频列表
  93. */
  94. public function video(Request $request)
  95. {
  96. $userInfo = $this->getUserInfo();
  97. if(empty($userInfo)){
  98. Log::info('获取用户信息失败');
  99. return jsonError('获取用户信息失败');
  100. }
  101. $list = $this->postRepositories->video($request->all());
  102. $fractal = new Manager();
  103. $resource = new Collection($list, new VideoTransformer($userInfo['uid']));
  104. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  105. $data = $fractal->createData($resource)->toArray();
  106. return jsonSuccess($data);
  107. }
  108. /**
  109. * 个人中心内容
  110. */
  111. public function myPost(Request $request)
  112. {
  113. $validator = Validator::make($request->all(), [
  114. 'type' => ['required',Rule::in('create', 'collect', 'share')],
  115. ]);
  116. if ($validator->fails()) {
  117. return jsonError($validator->errors()->first());
  118. }
  119. $userInfo = $this->getUserInfo();
  120. if(empty($userInfo)){
  121. Log::info('获取用户信息失败');
  122. return jsonError('获取用户信息失败');
  123. }
  124. $list = $this->postRepositories->myPost($request['type'], $userInfo['uid']);
  125. $fractal = new Manager();
  126. $resource = new Collection($list, new MyTransformer());
  127. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  128. $data = $fractal->createData($resource)->toArray();
  129. return jsonSuccess($data);
  130. }
  131. /**
  132. * 推荐内容列表
  133. */
  134. public function suggestPost(Request $request)
  135. {
  136. $userInfo = $this->getUserInfo();
  137. if(empty($userInfo)){
  138. Log::info('获取用户信息失败');
  139. return jsonError('获取用户信息失败');
  140. }
  141. $param = $request->all();
  142. $list = $this->postRepositories->suggestPost($param);
  143. $fractal = new Manager();
  144. $resource = new Collection($list, new SuggestTransformer($userInfo['uid']));
  145. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  146. $data = $fractal->createData($resource)->toArray();
  147. if(!(isset($param['current_page']) && $param['current_page'] > 1)){
  148. $key = 'suggest_post_floor';
  149. $floor = Redis::get($key);
  150. if(!$floor){
  151. $floor = $this->getFloorInfo();
  152. if($floor){
  153. Redis::set($key, json_encode($floor));
  154. Redis::expire($key, 600);
  155. }
  156. }else{
  157. $floor = json_decode($floor, true);
  158. }
  159. if($floor){
  160. $newData = [];
  161. foreach($data['data'] as $key => $val){
  162. if(isset($floor[$key+1])){
  163. if($floor[$key+1]['show_type'] == 'banner'){
  164. $newData[] = [
  165. 'show_type' => 'banner',
  166. 'data' => $floor[$key+1]['data'],
  167. ];
  168. }elseif($floor[$key+1]['show_type'] == 'user'){
  169. $newData[] = [
  170. 'show_type' => 'user',
  171. 'data' => $floor[$key+1]['data'],
  172. ];
  173. }elseif($floor[$key+1]['show_type'] == 'video'){
  174. $newData[] = [
  175. 'show_type' => 'video',
  176. 'data' => $floor[$key+1]['data'],
  177. ];
  178. }elseif($floor[$key+1]['show_type'] == 'topic'){
  179. $newData[] = [
  180. 'show_type' => 'topic',
  181. 'data' => $floor[$key+1]['data'],
  182. ];
  183. }
  184. }
  185. $newData[] = $val;
  186. }
  187. $data['data'] = $newData;
  188. }
  189. }
  190. return jsonSuccess($data);
  191. }
  192. /**
  193. * 内容详情
  194. */
  195. public function detail(Request $request)
  196. {
  197. $validator = Validator::make($request->all(), [
  198. 'id' => 'required|integer',
  199. ]);
  200. if ($validator->fails()) {
  201. return jsonError($validator->errors()->first());
  202. }
  203. $userInfo = $this->getUserInfo();
  204. if(empty($userInfo)){
  205. Log::info('获取用户信息失败');
  206. return jsonError('获取用户信息失败');
  207. }
  208. $detail = $this->postRepositories->detail($request['id']);
  209. if(!$detail){
  210. return jsonError('获取内容信息失败');
  211. }
  212. $fractal = new Manager();
  213. $res = new Item($detail, new DetailTransformer($userInfo['uid']));
  214. $data = $fractal->createData($res)->toArray();
  215. return jsonSuccess($data);
  216. }
  217. /**
  218. * 评论列表
  219. */
  220. public function commentList(Request $request)
  221. {
  222. $validator = Validator::make($request->all(), [
  223. 'post_id' => 'required|integer',
  224. ]);
  225. if ($validator->fails()) {
  226. return jsonError($validator->errors()->first());
  227. }
  228. $list = $this->postRepositories->commentList($request->all());
  229. $fractal = new Manager();
  230. $resource = new Collection($list, new CommentTransformer());
  231. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  232. $data = $fractal->createData($resource)->toArray();
  233. return jsonSuccess($data);
  234. }
  235. /**
  236. * 回复列表
  237. */
  238. public function replyList(Request $request)
  239. {
  240. $validator = Validator::make($request->all(), [
  241. 'id' => 'required|exists:post_comment',
  242. ]);
  243. if ($validator->fails()) {
  244. return jsonError($validator->errors()->first());
  245. }
  246. $list = $this->postRepositories->replyList($request->all());
  247. $fractal = new Manager();
  248. $resource = new Collection($list, new ReplyTransformer());
  249. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  250. $data = $fractal->createData($resource)->toArray();
  251. return jsonSuccess($data);
  252. }
  253. /**
  254. * 话题列表
  255. */
  256. public function topicList(Request $request)
  257. {
  258. $list = $this->postRepositories->topicList($request->all());
  259. $fractal = new Manager();
  260. $resource = new Collection($list, new TopicListTransformer());
  261. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  262. $data = $fractal->createData($resource)->toArray();
  263. return jsonSuccess($data);
  264. }
  265. /**
  266. * 话题详情
  267. */
  268. public function topicDetail(Request $request)
  269. {
  270. $validator = Validator::make($request->all(), [
  271. 'id' => 'required|integer',
  272. ]);
  273. if ($validator->fails()) {
  274. return jsonError($validator->errors()->first());
  275. }
  276. $userInfo = $this->getUserInfo();
  277. if(empty($userInfo)){
  278. Log::info('获取用户信息失败');
  279. return jsonError('获取用户信息失败');
  280. }
  281. $detail = $this->postRepositories->topicDetail($request['id']);
  282. if(!$detail){
  283. return jsonError('获取话题信息失败');
  284. }
  285. $fractal = new Manager();
  286. $res = new Item($detail, new TopicDetailTransformer($userInfo['uid']));
  287. $data = $fractal->createData($res)->toArray();
  288. return jsonSuccess($data);
  289. }
  290. /**
  291. * 话题内容列表
  292. */
  293. public function topicPost(Request $request)
  294. {
  295. $userInfo = $this->getUserInfo();
  296. if(empty($userInfo)){
  297. Log::info('获取用户信息失败');
  298. return jsonError('获取用户信息失败');
  299. }
  300. $list = $this->postRepositories->topicPost($request->all());
  301. $fractal = new Manager();
  302. $resource = new Collection($list, new TopicPostTransformer($userInfo['uid']));
  303. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  304. $data = $fractal->createData($resource)->toArray();
  305. return jsonSuccess($data);
  306. }
  307. /**
  308. * 获取话题
  309. */
  310. public function getTopic(Request $request)
  311. {
  312. $validator = Validator::make($request->all(), [
  313. 'ids' => 'required|string',
  314. ]);
  315. if ($validator->fails()) {
  316. return jsonError($validator->errors()->first());
  317. }
  318. $data = $this->postRepositories->getTopic($request['ids']);
  319. return jsonSuccess($data);
  320. }
  321. /**
  322. * 获取内容视频组
  323. */
  324. public function getPostVideo(Request $request)
  325. {
  326. $validator = Validator::make($request->all(), [
  327. 'ids' => 'required|string',
  328. ]);
  329. if ($validator->fails()) {
  330. return jsonError($validator->errors()->first());
  331. }
  332. $data = $this->postRepositories->getPostVideo($request['ids']);
  333. return jsonSuccess($data);
  334. }
  335. }