PostController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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\Models\Post;
  10. use App\Repositories\PostRepositories;
  11. use App\Traits\CmsTrait;
  12. use App\Traits\UserTrait;
  13. use App\Transformers\Post\CommentTransformer;
  14. use App\Transformers\Post\DetailTransformer;
  15. use App\Transformers\Post\ListTransformer;
  16. use App\Transformers\Post\MyTransformer;
  17. use App\Transformers\Post\PostTransformer;
  18. use App\Transformers\Post\ReplyTransformer;
  19. use App\Transformers\Post\SuggestTransformer;
  20. use App\Transformers\Post\VideoTransformer;
  21. use App\Transformers\Topic\TopicDetailTransformer;
  22. use App\Transformers\Topic\TopicListTransformer;
  23. use App\Transformers\Topic\TopicPostTransformer;
  24. use Illuminate\Http\Request;
  25. use Illuminate\Support\Facades\Log;
  26. use Illuminate\Support\Facades\Redis;
  27. use Illuminate\Support\Facades\Validator;
  28. use Illuminate\Validation\Rule;
  29. use League\Fractal\Manager;
  30. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  31. use League\Fractal\Resource\Collection;
  32. use League\Fractal\Resource\Item;
  33. class PostController extends Controller
  34. {
  35. use UserTrait;
  36. use CmsTrait;
  37. public function __construct(PostRepositories $postRepositories)
  38. {
  39. $this->postRepositories = $postRepositories;
  40. }
  41. /**
  42. * 发布内容
  43. */
  44. public function create(Request $request)
  45. {
  46. $validator = Validator::make($request->all(), [
  47. 'type' => ['required', Rule::in('image', 'video', 'html')],
  48. 'img' => 'required|url',
  49. 'video' => 'required_if:type,video|string',
  50. 'topic_ids' => 'required|string|max:64',
  51. 'title' => 'nullable|string|max:20',
  52. 'content' => 'required|string|max:1000',
  53. 'location' => 'nullable|string|max:32',
  54. 'imgs' => 'required_if:type,image|string',
  55. ]);
  56. if ($validator->fails()) {
  57. return jsonError($validator->errors()->first());
  58. }
  59. return $this->postRepositories->create($request->all());
  60. }
  61. /**
  62. * 评论&回复
  63. */
  64. public function comment(Request $request)
  65. {
  66. $validator = Validator::make($request->all(), [
  67. 'post_id' => 'required|integer',
  68. 'content' => 'required|string|max:150',
  69. ]);
  70. if ($validator->fails()) {
  71. return jsonError($validator->errors()->first());
  72. }
  73. return $this->postRepositories->comment($request->all());
  74. }
  75. /**
  76. * 内容列表
  77. */
  78. public function index(Request $request)
  79. {
  80. Log::debug('内容搜索' . json_encode($request));
  81. $userInfo = $this->getUserInfo();
  82. if (empty($userInfo)) {
  83. Log::debug('获取用户信息失败index'.json_encode($request));
  84. return jsonError('获取用户信息失败');
  85. }
  86. $list = $this->postRepositories->lists($request->all());
  87. $fractal = new Manager();
  88. $resource = new Collection($list, new ListTransformer($userInfo['uid'], $userInfo['invite_code']));
  89. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  90. $data = $fractal->createData($resource)->toArray();
  91. return jsonSuccess($data);
  92. }
  93. /**
  94. * 视频列表
  95. */
  96. public function video(Request $request)
  97. {
  98. $userInfo = $this->getUserInfo();
  99. if (empty($userInfo)) {
  100. Log::debug('获取用户信息失败video'.json_encode($request));
  101. return jsonError('获取用户信息失败');
  102. }
  103. $list = $this->postRepositories->video($request->all());
  104. $fractal = new Manager();
  105. $resource = new Collection($list, new VideoTransformer($userInfo['uid'], $userInfo['invite_code']));
  106. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  107. $data = $fractal->createData($resource)->toArray();
  108. return jsonSuccess($data);
  109. }
  110. /**
  111. * 个人中心内容
  112. */
  113. public function myPost(Request $request)
  114. {
  115. $validator = Validator::make($request->all(), [
  116. 'type' => ['required', Rule::in('create', 'collect', 'share')],
  117. ]);
  118. if ($validator->fails()) {
  119. return jsonError($validator->errors()->first());
  120. }
  121. $userInfo = $this->getUserInfo();
  122. if (empty($userInfo)) {
  123. Log::debug('获取用户信息失败myPost'.json_encode($request));
  124. return jsonError('获取用户信息失败');
  125. }
  126. $param = $request->all();
  127. if (isset($param['uid'])) {
  128. $uid = $param['uid'];
  129. } else {
  130. $uid = $userInfo['uid'];
  131. }
  132. $list = $this->postRepositories->myPost($param, $uid);
  133. $fractal = new Manager();
  134. $resource = new Collection($list, new MyTransformer());
  135. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  136. $data = $fractal->createData($resource)->toArray();
  137. return jsonSuccess($data);
  138. }
  139. /**
  140. * 推荐内容列表
  141. */
  142. public function suggestPost(Request $request)
  143. {
  144. $userInfo = $this->getUserInfo();
  145. if (empty($userInfo)) {
  146. Log::debug('获取用户信息失败suggestPost'.json_encode($request));
  147. return jsonError('获取用户信息失败');
  148. }
  149. $param = $request->all();
  150. $list = $this->postRepositories->suggestPost($param);
  151. $fractal = new Manager();
  152. $resource = new Collection($list, new SuggestTransformer($userInfo['uid'], $userInfo['invite_code']));
  153. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  154. $data = $fractal->createData($resource)->toArray();
  155. if (!(isset($param['page']) && $param['page'] > 1)) {
  156. $key = 'suggest_post_floor';
  157. $floor = Redis::get($key);
  158. if (!$floor) {
  159. $floor = $this->getFloorInfo();
  160. if ($floor) {
  161. Redis::set($key, json_encode($floor));
  162. Redis::expire($key, 600);
  163. }
  164. } else {
  165. $floor = json_decode($floor, true);
  166. }
  167. if ($floor) {
  168. $newData = [];
  169. foreach ($data['data'] as $key => $val) {
  170. if (isset($floor[$key + 1])) {
  171. if ($floor[$key + 1]['show_type'] == 'banner') {
  172. $bannerData = [];
  173. foreach ($floor[$key + 1]['data'] as $item) {
  174. if ($item['type'] == 1) {
  175. $postType = Post::where('id', $item['link_content_id'])->value('type');
  176. if (!$postType) {
  177. Log::info('banner类型为内容,未找到内容,被丢弃' . json_encode($item));
  178. continue;
  179. }
  180. $bannerData[] = array_merge($item, ['post_type' => $postType]);
  181. } else {
  182. $bannerData[] = $item;
  183. }
  184. }
  185. $newData[] = [
  186. 'show_type' => 'banner',
  187. 'data' => $bannerData,
  188. ];
  189. } elseif ($floor[$key + 1]['show_type'] == 'user') {
  190. $userData = [];
  191. foreach ($floor[$key + 1]['data'] as $item) {
  192. $userData[] = array_merge($item, ['follow_status' => $this->getFollowStatus($userInfo['uid'], $item['uid'])]);
  193. }
  194. if ($userData) {
  195. $newData[] = [
  196. 'show_type' => 'user',
  197. 'data' => $userData,
  198. ];
  199. }
  200. } elseif ($floor[$key + 1]['show_type'] == 'video') {
  201. $newData[] = [
  202. 'show_type' => 'video',
  203. 'data' => $floor[$key + 1]['data'],
  204. ];
  205. } elseif ($floor[$key + 1]['show_type'] == 'topic') {
  206. $newData[] = [
  207. 'show_type' => 'topic',
  208. 'data' => $floor[$key + 1]['data'],
  209. ];
  210. }
  211. }
  212. $newData[] = $val;
  213. }
  214. $data['data'] = $newData;
  215. }
  216. }
  217. return jsonSuccess($data);
  218. }
  219. /**
  220. * 内容详情
  221. */
  222. public function detail(Request $request)
  223. {
  224. Log::debug("内容详情-参数".json_encode($request));
  225. $validator = Validator::make($request->all(), [
  226. 'id' => 'required|integer',
  227. ]);
  228. if ($validator->fails()) {
  229. return jsonError($validator->errors()->first());
  230. }
  231. $userInfo = $this->getUserInfo();
  232. if (empty($userInfo)) {
  233. Log::debug('获取用户信息失败detail'.json_encode($request));
  234. return jsonError('获取用户信息失败');
  235. }
  236. $detail = $this->postRepositories->detail($request['id']);
  237. if (!$detail) {
  238. return jsonError('内容飞走了');
  239. }
  240. $fractal = new Manager();
  241. $res = new Item($detail, new DetailTransformer($userInfo['uid'], $userInfo['invite_code']));
  242. $data = $fractal->createData($res)->toArray();
  243. return jsonSuccess($data);
  244. }
  245. /**
  246. * 评论列表
  247. */
  248. public function commentList(Request $request)
  249. {
  250. $validator = Validator::make($request->all(), [
  251. 'post_id' => 'required|integer',
  252. ]);
  253. if ($validator->fails()) {
  254. return jsonError($validator->errors()->first());
  255. }
  256. $exists = $this->postRepositories->detailExists($request['post_id']);
  257. if (!$exists) {
  258. return jsonError('内容飞走了');
  259. }
  260. $list = $this->postRepositories->commentList($request->all());
  261. $fractal = new Manager();
  262. $resource = new Collection($list, new CommentTransformer());
  263. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  264. $data = $fractal->createData($resource)->toArray();
  265. $commentCount = $this->postRepositories->getCommentCount($request['post_id']);
  266. return jsonSuccess($data, '成功', ['comment_count' => $commentCount]);
  267. }
  268. /**
  269. * 回复列表
  270. */
  271. public function replyList(Request $request)
  272. {
  273. $validator = Validator::make($request->all(), [
  274. 'id' => 'required|exists:post_comment',
  275. ]);
  276. if ($validator->fails()) {
  277. return jsonError($validator->errors()->first());
  278. }
  279. $list = $this->postRepositories->replyList($request->all());
  280. $fractal = new Manager();
  281. $resource = new Collection($list, new ReplyTransformer());
  282. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  283. $data = $fractal->createData($resource)->toArray();
  284. return jsonSuccess($data);
  285. }
  286. /**
  287. * 话题列表
  288. */
  289. public function topicList(Request $request)
  290. {
  291. $list = $this->postRepositories->topicList($request->all());
  292. $fractal = new Manager();
  293. $resource = new Collection($list, new TopicListTransformer());
  294. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  295. $data = $fractal->createData($resource)->toArray();
  296. return jsonSuccess($data);
  297. }
  298. /**
  299. * 话题详情
  300. */
  301. public function topicDetail(Request $request)
  302. {
  303. $validator = Validator::make($request->all(), [
  304. 'id' => 'required|integer',
  305. ]);
  306. if ($validator->fails()) {
  307. return jsonError($validator->errors()->first());
  308. }
  309. $userInfo = $this->getUserInfo();
  310. if (empty($userInfo)) {
  311. Log::debug('获取用户信息失败topicDetail'.json_encode($request));
  312. return jsonError('获取用户信息失败');
  313. }
  314. $detail = $this->postRepositories->topicDetail($request['id']);
  315. if (!$detail) {
  316. return jsonError('获取话题信息失败');
  317. }
  318. $fractal = new Manager();
  319. $res = new Item($detail, new TopicDetailTransformer($userInfo['uid']));
  320. $data = $fractal->createData($res)->toArray();
  321. return jsonSuccess($data);
  322. }
  323. /**
  324. * 话题内容列表
  325. */
  326. public function topicPost(Request $request)
  327. {
  328. $userInfo = $this->getUserInfo();
  329. if (empty($userInfo)) {
  330. Log::debug('获取用户信息失败topicPost'.json_encode($request));
  331. return jsonError('获取用户信息失败');
  332. }
  333. $list = $this->postRepositories->topicPost($request->all());
  334. $fractal = new Manager();
  335. $resource = new Collection($list, new TopicPostTransformer($userInfo['uid'], $userInfo['invite_code']));
  336. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  337. $data = $fractal->createData($resource)->toArray();
  338. return jsonSuccess($data);
  339. }
  340. /**
  341. * 获取话题
  342. */
  343. public function getTopic(Request $request)
  344. {
  345. $validator = Validator::make($request->all(), [
  346. 'ids' => 'required|string',
  347. ]);
  348. if ($validator->fails()) {
  349. return jsonError($validator->errors()->first());
  350. }
  351. $data = $this->postRepositories->getTopic($request['ids']);
  352. return jsonSuccess($data);
  353. }
  354. /**
  355. * 获取内容视频组
  356. */
  357. public function getPostVideo(Request $request)
  358. {
  359. $validator = Validator::make($request->all(), [
  360. 'ids' => 'required|string',
  361. ]);
  362. if ($validator->fails()) {
  363. return jsonError($validator->errors()->first());
  364. }
  365. $data = $this->postRepositories->getPostVideo($request['ids']);
  366. return jsonSuccess($data);
  367. }
  368. //用户内容数统计
  369. public function memberPostStatistics(Request $request)
  370. {
  371. $validator = Validator::make($request->all(), [
  372. 'uid' => 'required|int',
  373. ]);
  374. if ($validator->fails()) {
  375. return jsonError($validator->errors()->first());
  376. }
  377. return $this->postRepositories->memberPostStatistics($request['uid']);
  378. }
  379. /**
  380. * 删除内容
  381. */
  382. public function delete(Request $request)
  383. {
  384. $validator = Validator::make($request->all(), [
  385. 'id' => 'required|integer',
  386. ]);
  387. if ($validator->fails()) {
  388. return jsonError($validator->errors()->first());
  389. }
  390. return $this->postRepositories->delete($request->all());
  391. }
  392. /**
  393. * 查询帖子内容详情(内部接口使用)
  394. */
  395. public function find(Request $request)
  396. {
  397. $validator = Validator::make($request->all(), [
  398. 'id' => 'required|integer',
  399. ]);
  400. if ($validator->fails()) {
  401. return jsonError($validator->errors()->first());
  402. }
  403. $detail = $this->postRepositories->detail($request['id']);
  404. if (!$detail) {
  405. return jsonError('获取内容信息失败');
  406. }
  407. $fractal = new Manager();
  408. $res = new Item($detail, new PostTransformer());
  409. $data = $fractal->createData($res)->toArray();
  410. return jsonSuccess($data);
  411. }
  412. }