PostController.php 17 KB

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