123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/6/14
- * Time: 16:23
- */
- namespace App\Http\Controllers\V1;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Redis;
- use Illuminate\Support\Facades\Validator;
- use Illuminate\Validation\Rule;
- use League\Fractal\Manager;
- use League\Fractal\Pagination\IlluminatePaginatorAdapter;
- use League\Fractal\Resource\Collection;
- use League\Fractal\Resource\Item;
- class CircleController extends Controller
- {
- public function __construct()
- {
- }
- /**
- * 发布内容
- */
- public function create(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'type' => ['required', Rule::in('image', 'video', 'html')],
- 'img' => 'required|url',
- 'video' => 'required_if:type,video|string',
- 'topic_ids' => 'required|string|max:64',
- 'title' => 'nullable|string|max:20',
- 'content' => 'required|string|max:1000',
- 'location' => 'nullable|string|max:32',
- 'imgs' => 'required_if:type,image|string',
- ]);
- if ($validator->fails()) {
- return jsonError($validator->errors()->first());
- }
- return $this->postRepositories->create($request->all());
- }
- /**
- * 评论&回复
- */
- public function comment(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'post_id' => 'required|integer',
- 'content' => 'required|string|max:150',
- ]);
- if ($validator->fails()) {
- return jsonError($validator->errors()->first());
- }
- return $this->postRepositories->comment($request->all());
- }
- /**
- * 删除评论
- */
- public function commentDelete(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'id' => 'required|integer',
- ]);
- if ($validator->fails()) {
- return jsonError($validator->errors()->first());
- }
- return $this->postRepositories->commentDelete($request->all());
- }
- /**
- * 内容列表
- */
- public function index(Request $request)
- {
- Log::debug('内容搜索' . json_encode($request));
- $userInfo = $this->getUserInfo();
- if ($userInfo) {
- $uid = $userInfo['uid'];
- $inviteCode = $userInfo['invite_code'];
- }else{
- $uid = 0;
- $inviteCode = '';
- }
- $list = $this->postRepositories->lists($request->all());
- $fractal = new Manager();
- $resource = new Collection($list, new ListTransformer($uid, $inviteCode));
- $resource->setPaginator(new IlluminatePaginatorAdapter($list));
- $data = $fractal->createData($resource)->toArray();
- return jsonSuccess($data);
- }
- /**
- * 视频列表
- */
- public function video(Request $request)
- {
- $userInfo = $this->getUserInfo();
- if ($userInfo) {
- $uid = $userInfo['uid'];
- $inviteCode = $userInfo['invite_code'];
- }else{
- $uid = 0;
- $inviteCode = '';
- }
- $list = $this->postRepositories->video($request->all());
- $fractal = new Manager();
- $resource = new Collection($list, new VideoTransformer($uid, $inviteCode));
- $resource->setPaginator(new IlluminatePaginatorAdapter($list));
- $data = $fractal->createData($resource)->toArray();
- return jsonSuccess($data);
- }
- /**
- * 个人中心内容
- */
- public function myPost(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'type' => ['required', Rule::in('create', 'collect', 'share')],
- ]);
- if ($validator->fails()) {
- return jsonError($validator->errors()->first());
- }
- $userInfo = $this->getUserInfo();
- if (empty($userInfo)) {
- Log::debug('获取用户信息失败myPost'.json_encode($request));
- return jsonError('获取用户信息失败');
- }
- $param = $request->all();
- if (isset($param['uid'])) {
- $uid = $param['uid'];
- } else {
- $uid = $userInfo['uid'];
- }
- $list = $this->postRepositories->myPost($param, $uid);
- $fractal = new Manager();
- $resource = new Collection($list, new MyTransformer());
- $resource->setPaginator(new IlluminatePaginatorAdapter($list));
- $data = $fractal->createData($resource)->toArray();
- return jsonSuccess($data);
- }
- /**
- * 推荐内容列表
- */
- public function suggestPost(Request $request)
- {
- $userInfo = $this->getUserInfo();
- if ($userInfo) {
- $uid = $userInfo['uid'];
- $inviteCode = $userInfo['invite_code'];
- }else{
- $uid = 0;
- $inviteCode = '';
- }
- $param = $request->all();
- $list = $this->postRepositories->suggestPost($param,$uid);
- $fractal = new Manager();
- $resource = new Collection($list, new SuggestTransformer($uid, $inviteCode));
- $resource->setPaginator(new IlluminatePaginatorAdapter($list));
- $data = $fractal->createData($resource)->toArray();
- if (!(isset($param['page']) && $param['page'] > 1) && !(isset($param['category_id']) && $param['category_id'])) {
- $key = 'suggest_post_floor';
- $floor = Redis::get($key);
- if (!$floor) {
- $floor = $this->getFloorInfo();
- if ($floor) {
- Redis::set($key, json_encode($floor));
- Redis::expire($key, 600);
- }
- } else {
- $floor = json_decode($floor, true);
- }
- if ($floor) {
- $newData = [];
- foreach ($data['data'] as $key => $val) {
- if (isset($floor[$key + 1])) {
- if ($floor[$key + 1]['show_type'] == 'banner') {
- $bannerData = [];
- foreach ($floor[$key + 1]['data'] as $item) {
- if ($item['type'] == 1) {
- $postInfo = $this->getPostInfo($item['link_content_id'], 1);
- if (!$postInfo || !$postInfo['type']) {
- Log::info('banner类型为内容,未找到内容,被丢弃' . json_encode($item));
- continue;
- }
- $bannerData[] = array_merge($item, ['post_type' => $postInfo['type']]);
- } else {
- $bannerData[] = $item;
- }
- }
- $newData[] = [
- 'show_type' => 'banner',
- 'data' => $bannerData,
- ];
- } elseif ($floor[$key + 1]['show_type'] == 'user') {
- $userData = [];
- foreach ($floor[$key + 1]['data'] as $item) {
- $followStatus = $uid ? $this->getFollowStatus($userInfo['uid'], $item['uid']) : 0;
- $userData[] = array_merge($item, ['follow_status' => $followStatus]);
- }
- if ($userData) {
- $newData[] = [
- 'show_type' => 'user',
- 'data' => $userData,
- ];
- }
- } elseif ($floor[$key + 1]['show_type'] == 'video') {
- $newData[] = [
- 'show_type' => 'video',
- 'data' => $floor[$key + 1]['data'],
- ];
- } elseif ($floor[$key + 1]['show_type'] == 'topic') {
- $newData[] = [
- 'show_type' => 'topic',
- 'data' => $floor[$key + 1]['data'],
- ];
- }
- }
- $newData[] = $val;
- }
- $data['data'] = $newData;
- }
- }
- return jsonSuccess($data);
- }
- /**
- * 内容详情
- */
- public function detail(Request $request)
- {
- Log::debug("内容详情-参数".json_encode($request));
- $validator = Validator::make($request->all(), [
- 'id' => 'required|integer',
- ]);
- if ($validator->fails()) {
- return jsonError($validator->errors()->first());
- }
- $userInfo = $this->getUserInfo();
- if ($userInfo) {
- $uid = $userInfo['uid'];
- $inviteCode = $userInfo['invite_code'];
- }else{
- $uid = 0;
- $inviteCode = '';
- }
- $detail = $this->postRepositories->detail($request['id']);
- if (!$detail) {
- return jsonError('内容飞走了');
- }
- $fractal = new Manager();
- $res = new Item($detail, new DetailTransformer($uid, $inviteCode));
- $data = $fractal->createData($res)->toArray();
- return jsonSuccess($data);
- }
- /**
- * 评论列表
- */
- public function commentList(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'post_id' => 'required|integer',
- ]);
- if ($validator->fails()) {
- return jsonError($validator->errors()->first());
- }
- $exists = $this->postRepositories->detailExists($request['post_id']);
- if (!$exists) {
- return jsonError('内容飞走了');
- }
- $userInfo = $this->getUserInfo();
- if ($userInfo) {
- $uid = $userInfo['uid'];
- }else{
- $uid = 0;
- }
- $list = $this->postRepositories->commentList($request->all());
- $fractal = new Manager();
- $resource = new Collection($list, new CommentTransformer($request['post_id'], $uid));
- $resource->setPaginator(new IlluminatePaginatorAdapter($list));
- $data = $fractal->createData($resource)->toArray();
- $commentCount = $this->postRepositories->getCommentCount($request['post_id']);
- return jsonSuccess($data, '成功', ['comment_count' => $commentCount]);
- }
- /**
- * 回复列表
- */
- public function replyList(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'id' => 'required|exists:post_comment',
- ]);
- if ($validator->fails()) {
- return jsonError($validator->errors()->first());
- }
- $userInfo = $this->getUserInfo();
- if ($userInfo) {
- $uid = $userInfo['uid'];
- }else{
- $uid = 0;
- }
- $postId = $this->postRepositories->getPostId($request['id']);
- $list = $this->postRepositories->replyList($request->all());
- $fractal = new Manager();
- $resource = new Collection($list, new ReplyTransformer($postId, $uid));
- $resource->setPaginator(new IlluminatePaginatorAdapter($list));
- $data = $fractal->createData($resource)->toArray();
- return jsonSuccess($data);
- }
- /**
- * 话题列表
- */
- public function topicList(Request $request)
- {
- $fractal = new Manager();
- $param = $request->all();
- if(isset($param['category_id']) && $param['category_id'] == -1){
- $list = $this->postRepositories->myTopicList($request->all());
- $resource = new Collection($list, new MyTopicListTransformer());
- $resource->setPaginator(new IlluminatePaginatorAdapter($list));
- }else{
- $list = $this->postRepositories->topicList($request->all());
- $resource = new Collection($list, new TopicListTransformer());
- $resource->setPaginator(new IlluminatePaginatorAdapter($list));
- }
- $data = $fractal->createData($resource)->toArray();
- return jsonSuccess($data);
- }
- /**
- * 话题详情
- */
- public function topicDetail(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'id' => 'required|integer',
- ]);
- if ($validator->fails()) {
- return jsonError($validator->errors()->first());
- }
- $userInfo = $this->getUserInfo();
- if ($userInfo) {
- $uid = $userInfo['uid'];
- }else{
- $uid = 0;
- }
- $detail = $this->postRepositories->topicDetail($request['id']);
- if (!$detail) {
- return jsonError('获取话题信息失败');
- }
- $fractal = new Manager();
- $res = new Item($detail, new TopicDetailTransformer($uid));
- $data = $fractal->createData($res)->toArray();
- return jsonSuccess($data);
- }
- /**
- * 话题内容列表
- */
- public function topicPost(Request $request)
- {
- $userInfo = $this->getUserInfo();
- if ($userInfo) {
- $uid = $userInfo['uid'];
- $inviteCode = $userInfo['invite_code'];
- }else{
- $uid = 0;
- $inviteCode = '';
- }
- $list = $this->postRepositories->topicPost($request->all());
- $fractal = new Manager();
- $resource = new Collection($list, new TopicPostTransformer($uid, $inviteCode));
- $resource->setPaginator(new IlluminatePaginatorAdapter($list));
- $data = $fractal->createData($resource)->toArray();
- return jsonSuccess($data);
- }
- /**
- * 获取话题
- */
- public function getTopic(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'ids' => 'required|string',
- ]);
- if ($validator->fails()) {
- return jsonError($validator->errors()->first());
- }
- $data = $this->postRepositories->getTopic($request['ids']);
- return jsonSuccess($data);
- }
- /**
- * 获取内容视频组
- */
- public function getPostVideo(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'ids' => 'required|string',
- ]);
- if ($validator->fails()) {
- return jsonError($validator->errors()->first());
- }
- $data = $this->postRepositories->getPostVideo($request['ids']);
- return jsonSuccess($data);
- }
- //用户内容数统计
- public function memberPostStatistics(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'uid' => 'required|int',
- ]);
- if ($validator->fails()) {
- return jsonError($validator->errors()->first());
- }
- return $this->postRepositories->memberPostStatistics($request['uid']);
- }
- /**
- * 删除内容
- */
- public function delete(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'id' => 'required|integer',
- ]);
- if ($validator->fails()) {
- return jsonError($validator->errors()->first());
- }
- return $this->postRepositories->delete($request->all());
- }
- /**
- * 查询帖子内容详情(内部接口使用)
- */
- public function find(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'id' => 'required|integer',
- ]);
- if ($validator->fails()) {
- return jsonError($validator->errors()->first());
- }
- $detail = $this->postRepositories->detail($request['id']);
- if (!$detail) {
- return jsonError('获取内容信息失败');
- }
- $fractal = new Manager();
- $res = new Item($detail, new PostTransformer());
- $data = $fractal->createData($res)->toArray();
- return jsonSuccess($data);
- }
- /**
- * 图片验证
- */
- public function checkImage(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'image' => 'required|url',
- ]);
- if ($validator->fails()) {
- return jsonError($validator->errors()->first());
- }
- $res = $this->postRepositories->checkImage($request['image']);
-
- return jsonSuccess($res);
- }
- /**
- * 下载量
- */
- public function downloadCount()
- {
- Redis::INCRBY('app_h5_download_count', 1);
- return jsonSuccess();
- }
- }
|