PostController.php 16 KB

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