CircleController.php 16 KB

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