123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- <?php
- namespace App\Http\Controllers\Api\V1;
- use App\Models\BaseSettingsModel;
- use App\Models\CommentInfoModel;
- use App\Models\DreamInfoModel;
- use App\Models\InteractionInfo;
- use App\Models\ReplyCommentsInfo;
- use App\Models\SupportDreamModel;
- use App\Models\SystemInfoModel;
- use App\Models\UserCareDream;
- use App\Models\UserCareUser;
- use App\Models\UserInfoModel;
- use Illuminate\Http\Request;
- use App\Services\Base\ErrorCode;
- use App\Helper\JpushHelper;
- class InteractionController extends Controller
- {
- use JpushHelper;
- // 发布关于梦想的动态
- /**
- * @api {post} /api/interaction/store 新增动态
- * @apiDescription 新增动态
- * @apiGroup Interaction
- * @apiPermission Passport
- * @apiVersion 0.1.0
- * @apiParam {int} id 梦想ID
- * @apiParam {string} title 互动标题
- * @apiParam {string} [video] 视频
- * @apiParam {array} [pics[]] 图片数组
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- *{
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": ""
- *}
- * @apiErrorExample {json} Error-Response:
- *HTTP/1.1 400 Bad Request
- * {
- * "state": false,
- * "code": 1000,
- * "message": "传入参数不正确",
- * "data": null or []
- * }
- *
- */
- public function store(Request $request)
- {
- $user = $this->getUser();
- $validator = \Validator::make($request->all(),
- [
- 'id' => 'required',
- 'title' => 'required',
- ],
- [
- 'id.required' => '梦想ID不能为空',
- 'title.required' => '动态标题不能为空',
- ]
- );
- if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
- $data = [];
- $pics = $request->pics;
- if (empty($pics) || !is_array($pics)) {
- // return $this->error(ErrorCode::ATTACHMENT_NOT_EXIST);
- }else{
- foreach ($pics as $k => $pic) {
- $data['pic'.($k+1)] = $pic;
- }
- }
- $dream_id = $request->id;
- $dream_name = DreamInfoModel::find($dream_id)->name;
- $title = $request->title;
- $data['dream_id'] = $dream_id;
- $data['title'] = $title;
- $video = $request->video;
- if (!empty($video)) $data['video'] =env('APP_URL').'/attachment/'. $request->video;
- $ok = InteractionInfo::create($data);
- if ($ok) {
- // 新的互动应该有消息通知《支持者》
- $support_user = SupportDreamModel::where('dream_id',$dream_id)->get();
- if (!empty(count($support_user))) {
- $user_ids = array_column($support_user->toArray(),'user_id');
- foreach ($user_ids as $user_id) {
- $arr['user_id']=$user->id;
- $arr['to_user_id']=$user_id;
- $arr['message']='您支持的梦想《'.$dream_name.'》又有新的动态啦';
- $arr['interaction_id']=$ok->id;
- $arr['dream_id']=$dream_id;
- SystemInfoModel::create($arr);
- // 长连接
- $this->jPush($arr['message'],'',$user_id);
- }
- }
- // 收藏梦想的最新动态加一
- UserCareDream::where('dream_id',$dream_id)->increment('interaction_number',1);
- $dream_user_id = DreamInfoModel::find($dream_id)->user_id;
- UserCareUser::where('other_user_id',$dream_user_id)->update(['dream_id'=>$dream_id,'dream_number'=>'1']);
- // UserCareUser::where('dream_id',$dream_id)->increment('interaction_number',1);
- return $this->api('');
- }else{
- return $this->error(ErrorCode::SAVE_USER_FAILED);
- }
- }
- // 评论互动
- /**
- * @api {post} /api/interaction/comment 评论动态
- * @apiDescription 评论动态
- * @apiGroup Interaction
- * @apiPermission Passport
- * @apiVersion 0.1.0
- * @apiParam {int} id 动态ID不存在
- * @apiParam {int} u_id @用户id
- * @apiParam {int} to_user_id 发布动态的梦想者id
- * @apiParam {int} [comment_user_id] //已经评论者id
- * @apiParam {string} content 内容不能为空
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- *{
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": ""
- *}
- * @apiErrorExample {json} Error-Response:
- *HTTP/1.1 400 Bad Request
- * {
- * "state": false,
- * "code": 1000,
- * "message": "传入参数不正确",
- * "data": null or []
- * }
- *
- */
- public function comment(Request $request)
- {
- $validator = \Validator::make($request->all(),
- [
- 'id' => 'required',
- 'content' => 'required',
- ],
- [
- 'id.required' => '动态ID不存在',
- 'content.required' => '内容不能为空',
- ]
- );
- if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
- $user = $this->getUser();
- /* $user_id = $user->id;
- $user_avatar = $user->avatar;
- $user_nickname = $user->nickname;
- $interaction_id = $request->id;
- $content = $request->content;
- $is_read = 1;
- $data = compact('user_id','user_avatar','user_nickname','interaction_id','content','is_read');*/
- $toid = $request->input('to_user_id');
- $dream_id = InteractionInfo::find($request->id)->dream_id;
- // $data['to_user_avatar'] = InteractionInfo::find($request->id)->dream->user_avatar;
- // $data['to_user_nickname'] = InteractionInfo::find($request->id)->dream->user_nickname;
- if (!empty($request->input('comment_user_id'))) {
- $to_user = UserInfoModel::find($request->input('comment_user_id'));
- if (!empty($to_user)){
- $data['to_user_id'] = $request->input('to_user_id');
- $data['to_user_avatar'] = $to_user->avatar;
- $data['to_user_nickname'] = $to_user->nickname;
- //点击去看看
- $message = ""."<span style='color: #00c3da'>[$user->nickname]</span> ".'在你的互动上留言啦!点击去看看';
- $info = [
- 'to_user_id' => $data['to_user_id'],
- 'message' => $message,
- 'is_url' => 1,
- 'type_id' => 1,
- 'attr_id' => 3,
- 'dream_id' => $dream_id,
- 'interaction_id' => $request->input('id'),
- ];
- SystemInfoModel::create($info);
- }
- }
- $this->jPush($user->nickname."在你的互动上留言啦!点击去看看",'',$toid);
- if (!empty($request->input('u_id'))) {
- $message = ""."<span style='color: #00c3da'>[$user->nickname]</span> ".'提起了你哦~点击看看!';
- $info = [
- 'to_user_id' => $request->input('u_id'),
- 'message' => $message,
- 'is_url' => 1,
- 'type_id' => 1,
- 'attr_id' => 8,
- 'dream_id' => $dream_id,
- 'interaction_id' => $request->input('id'),
- ];
- SystemInfoModel::create($info);
- }
- $data['user_id'] = $user->id;
- $data['user_avatar'] =$user->avatar;
- $data['user_nickname'] = $user->nickname;
- $data['interaction_id'] = $request->id;
- $data['content'] = $request->content;
- $data['is_read'] = 1;
- $ok = CommentInfoModel::create($data);
- if ($ok) {
- // 评论动态也会出现在首页用户
- UserCareDream::where('dream_id',$dream_id)->increment('interaction_number',1);
- $this->autoCareDream($user->id,$user->nickname,$dream_id);
- return $this->api('');
- // 评论成功自动收藏梦想
- }else{
- return $this->error(ErrorCode::OPERATION_FAILED);
- }
- }
- // 回复评论
- /**
- * @api {post} /api/interaction/reply 我的回复
- * @apiDescription 我的回复
- * @apiGroup Interaction
- * @apiParam {text} content 回复内容
- * @apiParam {int} comment_id 评论ID
- * @apiParam {int} interaction_id 动态ID
- * @apiPermission Passport
- * @apiVersion 0.1.0
- * @apiSuccessExample {json} Success-Response:
- * {
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": ""
- *}
- * HTTP/1.1 200 OK
- * @apiErrorExample {json} Error-Response:
- * {
- * "status": false,
- * "status_code": 1000,
- * "message": "输入不正确",
- * "data": null
- *}
- * HTTP/1.1 400 Bad Request
- */
- public function reply(Request $request)
- {
- $validator = \Validator::make($request->all(),
- [
- 'id' => 'required',
- 'interaction_id' => 'required',
- 'content' => 'required',
- ],
- [
- 'id.required' => '评论ID不存在',
- 'content.required' => '内容不能为空',
- ]
- );
- if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
- $user = $this->getUser();
- /* $data['to_user_id'] = $user->id;
- $data['to_user_avatar'] = $user->avatar;
- $data['to_user_nickname'] = $user->nickname; */
- $data['to_user_id'] = CommentInfoModel::find($request->id)->user_id;
- // $data['to_user_avatar'] = CommentInfoModel::find($request->id)->user_avatar;
- $data['to_user_nickname'] = CommentInfoModel::find($request->id)->user_nickname;
- $data['user_id'] = $user->id;
- $data['user_avatar'] =$user->avatar;
- $data['user_nickname'] = $user->nickname;
- $data['interaction_id'] = $request->id;
- $data['content'] = $request->content;
- $data['is_read'] = 1;
- if (!$request->content)
- return $this->error(ErrorCode::CONNET_NOT_EXIST);
- $ok = CommentInfoModel::create($data);
- if ($ok) {
- // 评论成功自动收藏梦想
- $dream_id = InteractionInfo::find($request->id)->dream_id;
- $this->autoCareDream($user->id,$user->nickname,$dream_id);
- return $this->api('');
- }else{
- return $this->error(ErrorCode::OPERATION_FAILED);
- }
- }
- /**
- * @api {get} /api/comment/delete 删除评论
- * @apiDescription 删除评论
- * @apiGroup Interaction
- * @apiParam {int} id 评论ID
- * @apiPermission Passport
- * @apiVersion 0.1.0
- * @apiSuccessExample {json} Success-Response:
- * {
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": ""
- *}
- * HTTP/1.1 200 OK
- * @apiErrorExample {json} Error-Response:
- * {
- * "status": false,
- * "status_code": 700,
- * "message": "操作失败",
- * "data": null
- *}
- * HTTP/1.1 400 Bad Request
- */
- public function delete(Request $request)
- {
- $validator = \Validator::make($request->all(),
- [
- 'id' => 'required',
- ],
- [
- 'id.required' => '评论ID不存在',
- ]
- );
- if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
- $user = $this->getUser();
- $ok = CommentInfoModel::where('user_id',$user->id)->where('id',$request->id)->delete();
- if ($ok) {
- return $this->api('');
- }else{
- return $this->error(ErrorCode::OPERATION_FAILED);
- }
- }
- /**
- * @api {get} /api/interaction/destroy 删除动态
- * @apiDescription 删除动态
- * @apiGroup Interaction
- * @apiParam {int} id 动态ID
- * @apiPermission Passport
- * @apiVersion 0.1.0
- * @apiSuccessExample {json} Success-Response:
- * {
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": ""
- *}
- * HTTP/1.1 200 OK
- * @apiErrorExample {json} Error-Response:
- * {
- * "status": false,
- * "status_code": 700,
- * "message": "操作失败",
- * "data": null
- *}
- * HTTP/1.1 400 Bad Request
- */
- public function destroy(Request $request)
- {
- $validator = \Validator::make($request->all(),
- [
- 'id' => 'required',
- ],
- [
- 'id.required' => '动态ID不存在',
- ]
- );
- if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
- // $user = $this->getUser();
- $id = $request->input('id');
- CommentInfoModel::where('interaction_id',$id)->delete();
- $ok = InteractionInfo::destroy($id);
- if ($ok) {
- return $this->api('');
- }else{
- return $this->error(ErrorCode::OPERATION_FAILED);
- }
- }
- public function autoCareDream($user_id,$nickname,$dream_id)
- {
- $user_care_dream = UserCareDream::where('user_id',$user_id)->where('dream_id',$dream_id)->first();
- if (empty($user_care_dream)) {
- $dream = DreamInfoModel::find($dream_id);
- $data_info = [
- 'user_id' =>$user_id,
- 'dream_id' =>$dream_id,
- 'dream_user_id' =>$dream->user_id,
- ];
- UserCareDream::create($data_info);
- // 关注成功发送私信
- $message = BaseSettingsModel::where('category','message')->first();
- $message = empty($message) ? ""."<span style='color: #00c3da'>[$nickname]</span> 收藏了你的梦想《".$dream->name.'》' : $message->value;
- $info2 = [
- 'to_user_id' => $dream->user_id,
- 'message' => $message,
- 'dream_id' => $dream_id,
- 'user_id' => $user_id,
- 'is_reply' => 1,
- 'type_id' => 1,
- 'attr_id' => 2,
- ];
- SystemInfoModel::firstOrCreate($info2);
- }
- }
- /**
- * @api {get} /api/interaction/add_comment_blackList 加入黑名单
- * @apiDescription 隐藏某个人的评论
- * @apiGroup Interaction
- * @apiParam {int} dream_id 梦想id
- * @apiParam {int} interaction_id 动态id
- * @apiParam {int} user_id 加入黑名单用户id
- * @apiPermission Passport
- * @apiVersion 0.1.0
- * @apiSuccessExample {json} Success-Response:
- * {
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": ""
- *}
- * HTTP/1.1 200 OK
- * @apiErrorExample {json} Error-Response:
- * {
- * "status": false,
- * "status_code": 700,
- * "message": "操作失败",
- * "data": null
- *}
- * HTTP/1.1 400 Bad Request
- */
- public function addCommentBlackList(Request $request)
- {
- $user = $this->getUser();
- $user_id = $request->input('user_id');
- $interaction_id = $request->input('interaction_id');
- $dream_id = $request->input('dream_id');
- $login_user_id = $user->id;
- $dream = DreamInfoModel::find($dream_id);
- if (empty($dream)) return $this->error(ErrorCode::DREAM_NOT_EXIST);
- if ($dream->user_id != $login_user_id) return $this->error(ErrorCode::NOT_ROOT);
- $interaction = InteractionInfo::find($interaction_id);
- if (empty($interaction)) return $this->error(ErrorCode::INTERACTION_NOT_EXIST);
- $interaction->black_list = $user_id.',';
- $ok = $interaction->save();
- if ($ok) return $this->api();
- return $this->error(ErrorCode::OPERATION_FAILED);
- }
- }
|