PostController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. return jsonError('获取用户信息失败');
  84. }
  85. $list = $this->postRepositories->lists($request->all());
  86. $fractal = new Manager();
  87. $resource = new Collection($list, new ListTransformer($userInfo['uid'], $userInfo['invite_code']));
  88. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  89. $data = $fractal->createData($resource)->toArray();
  90. return jsonSuccess($data);
  91. }
  92. /**
  93. * 视频列表
  94. */
  95. public function video(Request $request)
  96. {
  97. $userInfo = $this->getUserInfo();
  98. if (empty($userInfo)) {
  99. return jsonError('获取用户信息失败');
  100. }
  101. $list = $this->postRepositories->video($request->all());
  102. $fractal = new Manager();
  103. $resource = new Collection($list, new VideoTransformer($userInfo['uid'], $userInfo['invite_code']));
  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. return jsonError('获取用户信息失败');
  122. }
  123. $param = $request->all();
  124. if (isset($param['uid'])) {
  125. $uid = $param['uid'];
  126. } else {
  127. $uid = $userInfo['uid'];
  128. }
  129. $list = $this->postRepositories->myPost($param, $uid);
  130. $fractal = new Manager();
  131. $resource = new Collection($list, new MyTransformer());
  132. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  133. $data = $fractal->createData($resource)->toArray();
  134. return jsonSuccess($data);
  135. }
  136. /**
  137. * 推荐内容列表
  138. */
  139. public function suggestPost(Request $request)
  140. {
  141. $userInfo = $this->getUserInfo();
  142. if (empty($userInfo)) {
  143. return jsonError('获取用户信息失败');
  144. }
  145. $param = $request->all();
  146. $list = $this->postRepositories->suggestPost($param);
  147. $fractal = new Manager();
  148. $resource = new Collection($list, new SuggestTransformer($userInfo['uid'], $userInfo['invite_code']));
  149. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  150. $data = $fractal->createData($resource)->toArray();
  151. if (!(isset($param['page']) && $param['page'] > 1)) {
  152. $key = 'suggest_post_floor';
  153. $floor = Redis::get($key);
  154. if (!$floor) {
  155. $floor = $this->getFloorInfo();
  156. if ($floor) {
  157. Redis::set($key, json_encode($floor));
  158. Redis::expire($key, 600);
  159. }
  160. } else {
  161. $floor = json_decode($floor, true);
  162. }
  163. if ($floor) {
  164. $newData = [];
  165. foreach ($data['data'] as $key => $val) {
  166. if (isset($floor[$key + 1])) {
  167. if ($floor[$key + 1]['show_type'] == 'banner') {
  168. $bannerData = [];
  169. foreach ($floor[$key + 1]['data'] as $item) {
  170. if ($item['type'] == 1) {
  171. $postType = Post::where('id', $item['link_content_id'])->value('type');
  172. if (!$postType) {
  173. Log::info('banner类型为内容,未找到内容,被丢弃' . json_encode($item));
  174. continue;
  175. }
  176. $bannerData[] = array_merge($item, ['post_type' => $postType]);
  177. } else {
  178. $bannerData[] = $item;
  179. }
  180. }
  181. $newData[] = [
  182. 'show_type' => 'banner',
  183. 'data' => $bannerData,
  184. ];
  185. } elseif ($floor[$key + 1]['show_type'] == 'user') {
  186. $userData = [];
  187. foreach ($floor[$key + 1]['data'] as $item) {
  188. $userData[] = array_merge($item, ['follow_status' => $this->getFollowStatus($userInfo['uid'], $item['uid'])]);
  189. }
  190. if ($userData) {
  191. $newData[] = [
  192. 'show_type' => 'user',
  193. 'data' => $userData,
  194. ];
  195. }
  196. } elseif ($floor[$key + 1]['show_type'] == 'video') {
  197. $newData[] = [
  198. 'show_type' => 'video',
  199. 'data' => $floor[$key + 1]['data'],
  200. ];
  201. } elseif ($floor[$key + 1]['show_type'] == 'topic') {
  202. $newData[] = [
  203. 'show_type' => 'topic',
  204. 'data' => $floor[$key + 1]['data'],
  205. ];
  206. }
  207. }
  208. $newData[] = $val;
  209. }
  210. $data['data'] = $newData;
  211. }
  212. }
  213. return jsonSuccess($data);
  214. }
  215. /**
  216. * 内容详情
  217. */
  218. public function detail(Request $request)
  219. {
  220. Log::debug("内容详情-参数".json_encode($request));
  221. $validator = Validator::make($request->all(), [
  222. 'id' => 'required|integer',
  223. ]);
  224. if ($validator->fails()) {
  225. return jsonError($validator->errors()->first());
  226. }
  227. $userInfo = $this->getUserInfo();
  228. if (empty($userInfo)) {
  229. return jsonError('获取用户信息失败');
  230. }
  231. $detail = $this->postRepositories->detail($request['id']);
  232. if (!$detail) {
  233. return jsonError('内容飞走了');
  234. }
  235. $fractal = new Manager();
  236. $res = new Item($detail, new DetailTransformer($userInfo['uid'], $userInfo['invite_code']));
  237. $data = $fractal->createData($res)->toArray();
  238. return jsonSuccess($data);
  239. }
  240. /**
  241. * 评论列表
  242. */
  243. public function commentList(Request $request)
  244. {
  245. $validator = Validator::make($request->all(), [
  246. 'post_id' => 'required|integer',
  247. ]);
  248. if ($validator->fails()) {
  249. return jsonError($validator->errors()->first());
  250. }
  251. $exists = $this->postRepositories->detailExists($request['post_id']);
  252. if (!$exists) {
  253. return jsonError('内容飞走了');
  254. }
  255. $list = $this->postRepositories->commentList($request->all());
  256. $fractal = new Manager();
  257. $resource = new Collection($list, new CommentTransformer());
  258. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  259. $data = $fractal->createData($resource)->toArray();
  260. $commentCount = $this->postRepositories->getCommentCount($request['post_id']);
  261. return jsonSuccess($data, '成功', ['comment_count' => $commentCount]);
  262. }
  263. /**
  264. * 回复列表
  265. */
  266. public function replyList(Request $request)
  267. {
  268. $validator = Validator::make($request->all(), [
  269. 'id' => 'required|exists:post_comment',
  270. ]);
  271. if ($validator->fails()) {
  272. return jsonError($validator->errors()->first());
  273. }
  274. $list = $this->postRepositories->replyList($request->all());
  275. $fractal = new Manager();
  276. $resource = new Collection($list, new ReplyTransformer());
  277. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  278. $data = $fractal->createData($resource)->toArray();
  279. return jsonSuccess($data);
  280. }
  281. /**
  282. * 话题列表
  283. */
  284. public function topicList(Request $request)
  285. {
  286. $list = $this->postRepositories->topicList($request->all());
  287. $fractal = new Manager();
  288. $resource = new Collection($list, new TopicListTransformer());
  289. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  290. $data = $fractal->createData($resource)->toArray();
  291. return jsonSuccess($data);
  292. }
  293. /**
  294. * 话题详情
  295. */
  296. public function topicDetail(Request $request)
  297. {
  298. $validator = Validator::make($request->all(), [
  299. 'id' => 'required|integer',
  300. ]);
  301. if ($validator->fails()) {
  302. return jsonError($validator->errors()->first());
  303. }
  304. $userInfo = $this->getUserInfo();
  305. if (empty($userInfo)) {
  306. return jsonError('获取用户信息失败');
  307. }
  308. $detail = $this->postRepositories->topicDetail($request['id']);
  309. if (!$detail) {
  310. return jsonError('获取话题信息失败');
  311. }
  312. $fractal = new Manager();
  313. $res = new Item($detail, new TopicDetailTransformer($userInfo['uid']));
  314. $data = $fractal->createData($res)->toArray();
  315. return jsonSuccess($data);
  316. }
  317. /**
  318. * 话题内容列表
  319. */
  320. public function topicPost(Request $request)
  321. {
  322. $userInfo = $this->getUserInfo();
  323. if (empty($userInfo)) {
  324. return jsonError('获取用户信息失败');
  325. }
  326. $list = $this->postRepositories->topicPost($request->all());
  327. $fractal = new Manager();
  328. $resource = new Collection($list, new TopicPostTransformer($userInfo['uid'], $userInfo['invite_code']));
  329. $resource->setPaginator(new IlluminatePaginatorAdapter($list));
  330. $data = $fractal->createData($resource)->toArray();
  331. return jsonSuccess($data);
  332. }
  333. /**
  334. * 获取话题
  335. */
  336. public function getTopic(Request $request)
  337. {
  338. $validator = Validator::make($request->all(), [
  339. 'ids' => 'required|string',
  340. ]);
  341. if ($validator->fails()) {
  342. return jsonError($validator->errors()->first());
  343. }
  344. $data = $this->postRepositories->getTopic($request['ids']);
  345. return jsonSuccess($data);
  346. }
  347. /**
  348. * 获取内容视频组
  349. */
  350. public function getPostVideo(Request $request)
  351. {
  352. $validator = Validator::make($request->all(), [
  353. 'ids' => 'required|string',
  354. ]);
  355. if ($validator->fails()) {
  356. return jsonError($validator->errors()->first());
  357. }
  358. $data = $this->postRepositories->getPostVideo($request['ids']);
  359. return jsonSuccess($data);
  360. }
  361. //用户内容数统计
  362. public function memberPostStatistics(Request $request)
  363. {
  364. $validator = Validator::make($request->all(), [
  365. 'uid' => 'required|int',
  366. ]);
  367. if ($validator->fails()) {
  368. return jsonError($validator->errors()->first());
  369. }
  370. return $this->postRepositories->memberPostStatistics($request['uid']);
  371. }
  372. /**
  373. * 删除内容
  374. */
  375. public function delete(Request $request)
  376. {
  377. $validator = Validator::make($request->all(), [
  378. 'id' => 'required|integer',
  379. ]);
  380. if ($validator->fails()) {
  381. return jsonError($validator->errors()->first());
  382. }
  383. return $this->postRepositories->delete($request->all());
  384. }
  385. /**
  386. * 查询帖子内容详情(内部接口使用)
  387. */
  388. public function find(Request $request)
  389. {
  390. $validator = Validator::make($request->all(), [
  391. 'id' => 'required|integer',
  392. ]);
  393. if ($validator->fails()) {
  394. return jsonError($validator->errors()->first());
  395. }
  396. $detail = $this->postRepositories->detail($request['id']);
  397. if (!$detail) {
  398. return jsonError('获取内容信息失败');
  399. }
  400. $fractal = new Manager();
  401. $res = new Item($detail, new PostTransformer());
  402. $data = $fractal->createData($res)->toArray();
  403. return jsonSuccess($data);
  404. }
  405. }