InteractionController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Models\CommentInfoModel;
  4. use App\Models\DreamInfoModel;
  5. use App\Models\InteractionInfo;
  6. use App\Models\ReplyCommentsInfo;
  7. use App\Models\SystemInfoModel;
  8. use App\Models\UserCareDream;
  9. use App\Models\UserCareUser;
  10. use App\Models\UserInfoModel;
  11. use Illuminate\Http\Request;
  12. use App\Services\Base\ErrorCode;
  13. use App\Helper\JpushHelper;
  14. class InteractionController extends Controller
  15. {
  16. use JpushHelper;
  17. // 发布关于梦想的动态
  18. /**
  19. * @api {post} /api/interaction/store 新增动态
  20. * @apiDescription 新增动态
  21. * @apiGroup Interaction
  22. * @apiPermission Passport
  23. * @apiVersion 0.1.0
  24. * @apiParam {int} id 梦想ID
  25. * @apiParam {string} title 互动标题
  26. * @apiParam {string} [video] 视频
  27. * @apiParam {array} [pics[]] 图片数组
  28. * @apiSuccessExample {json} Success-Response:
  29. * HTTP/1.1 200 OK
  30. *{
  31. * "status": true,
  32. * "status_code": 0,
  33. * "message": "",
  34. * "data": ""
  35. *}
  36. * @apiErrorExample {json} Error-Response:
  37. *HTTP/1.1 400 Bad Request
  38. * {
  39. * "state": false,
  40. * "code": 1000,
  41. * "message": "传入参数不正确",
  42. * "data": null or []
  43. * }
  44. *
  45. */
  46. public function store(Request $request)
  47. {
  48. $validator = \Validator::make($request->all(),
  49. [
  50. 'id' => 'required',
  51. 'title' => 'required',
  52. ],
  53. [
  54. 'id.required' => '梦想ID不能为空',
  55. 'title.required' => '动态标题不能为空',
  56. ]
  57. );
  58. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  59. $data = [];
  60. $pics = $request->pics;
  61. if (empty($pics) || !is_array($pics)) {
  62. // return $this->error(ErrorCode::ATTACHMENT_NOT_EXIST);
  63. }else{
  64. foreach ($pics as $k => $pic) {
  65. $data['pic'.($k+1)] = $pic;
  66. }
  67. }
  68. $dream_id = $request->id;
  69. $title = $request->title;
  70. $data['dream_id'] = $dream_id;
  71. $data['title'] = $title;
  72. $data['video'] = $request->video;
  73. $ok = InteractionInfo::create($data);
  74. if ($ok) {
  75. // 收藏梦想最新动态加一
  76. UserCareDream::where('dream_id',$dream_id)->increment('interaction_number',1);
  77. $dream_user_id = DreamInfoModel::find($dream_id)->user_id;
  78. UserCareUser::where('other_user_id',$dream_user_id)->update(['dream_id'=>$dream_id,'dream_number'=>'1']);
  79. // UserCareUser::where('dream_id',$dream_id)->increment('interaction_number',1);
  80. return $this->api('');
  81. }else{
  82. return $this->error(ErrorCode::SAVE_USER_FAILED);
  83. }
  84. }
  85. // 评论互动
  86. /**
  87. * @api {post} /api/interaction/comment 评论动态
  88. * @apiDescription 评论动态
  89. * @apiGroup Interaction
  90. * @apiPermission Passport
  91. * @apiVersion 0.1.0
  92. * @apiParam {int} id 动态ID不存在
  93. * @apiParam {int} [comment_user_id] //已经评论者id
  94. * @apiParam {string} content 内容不能为空
  95. * @apiSuccessExample {json} Success-Response:
  96. * HTTP/1.1 200 OK
  97. *{
  98. * "status": true,
  99. * "status_code": 0,
  100. * "message": "",
  101. * "data": ""
  102. *}
  103. * @apiErrorExample {json} Error-Response:
  104. *HTTP/1.1 400 Bad Request
  105. * {
  106. * "state": false,
  107. * "code": 1000,
  108. * "message": "传入参数不正确",
  109. * "data": null or []
  110. * }
  111. *
  112. */
  113. public function comment(Request $request)
  114. {
  115. $validator = \Validator::make($request->all(),
  116. [
  117. 'id' => 'required',
  118. 'content' => 'required',
  119. ],
  120. [
  121. 'id.required' => '动态ID不存在',
  122. 'content.required' => '内容不能为空',
  123. ]
  124. );
  125. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  126. $user = $this->getUser();
  127. /* $user_id = $user->id;
  128. $user_avatar = $user->avatar;
  129. $user_nickname = $user->nickname;
  130. $interaction_id = $request->id;
  131. $content = $request->content;
  132. $is_read = 1;
  133. $data = compact('user_id','user_avatar','user_nickname','interaction_id','content','is_read');*/
  134. // $data['to_user_id'] = InteractionInfo::find($request->id)->dream->user_id;
  135. // $data['to_user_avatar'] = InteractionInfo::find($request->id)->dream->user_avatar;
  136. // $data['to_user_nickname'] = InteractionInfo::find($request->id)->dream->user_nickname;
  137. if (!empty($request->input('comment_user_id'))) {
  138. $to_user = UserInfoModel::find($request->input('comment_user_id'));
  139. if (!empty($to_user)){
  140. $data['to_user_id'] = $request->input('comment_user_id');
  141. $data['to_user_avatar'] = $to_user->avatar;
  142. $data['to_user_nickname'] = $to_user->nickname;
  143. $message = $user->nickname.'在你的互动上留言啦!点击去看看!';
  144. $info = [
  145. 'user_id' => $data['to_user_id'],
  146. 'message' => $message,
  147. ];
  148. SystemInfoModel::create($info);
  149. // 长连接
  150. $this->jPush($message,'',$data['to_user_id']);
  151. }
  152. }
  153. $data['user_id'] = $user->id;
  154. $data['user_avatar'] =$user->avatar;
  155. $data['user_nickname'] = $user->nickname;
  156. $data['interaction_id'] = $request->id;
  157. $data['content'] = $request->content;
  158. $data['is_read'] = 1;
  159. $ok = CommentInfoModel::create($data);
  160. if ($ok) {
  161. return $this->api('');
  162. }else{
  163. return $this->error(ErrorCode::OPERATION_FAILED);
  164. }
  165. }
  166. // 回复评论
  167. /**
  168. * @api {post} /api/interaction/reply 我的回复
  169. * @apiDescription 我的回复
  170. * @apiGroup Interaction
  171. * @apiParam {text} content 回复内容
  172. * @apiParam {int} comment_id 评论ID
  173. * @apiParam {int} interaction_id 动态ID
  174. * @apiPermission Passport
  175. * @apiVersion 0.1.0
  176. * @apiSuccessExample {json} Success-Response:
  177. * {
  178. * "status": true,
  179. * "status_code": 0,
  180. * "message": "",
  181. * "data": ""
  182. *}
  183. * HTTP/1.1 200 OK
  184. * @apiErrorExample {json} Error-Response:
  185. * {
  186. * "status": false,
  187. * "status_code": 1000,
  188. * "message": "输入不正确",
  189. * "data": null
  190. *}
  191. * HTTP/1.1 400 Bad Request
  192. */
  193. public function reply(Request $request)
  194. {
  195. $validator = \Validator::make($request->all(),
  196. [
  197. 'id' => 'required',
  198. 'interaction_id' => 'required',
  199. 'content' => 'required',
  200. ],
  201. [
  202. 'id.required' => '评论ID不存在',
  203. 'content.required' => '内容不能为空',
  204. ]
  205. );
  206. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  207. $user = $this->getUser();
  208. /* $data['to_user_id'] = $user->id;
  209. $data['to_user_avatar'] = $user->avatar;
  210. $data['to_user_nickname'] = $user->nickname; */
  211. $data['to_user_id'] = CommentInfoModel::find($request->id)->user_id;
  212. // $data['to_user_avatar'] = CommentInfoModel::find($request->id)->user_avatar;
  213. $data['to_user_nickname'] = CommentInfoModel::find($request->id)->user_nickname;
  214. $data['user_id'] = $user->id;
  215. $data['user_avatar'] =$user->avatar;
  216. $data['user_nickname'] = $user->nickname;
  217. $data['interaction_id'] = $request->id;
  218. $data['content'] = $request->content;
  219. $data['is_read'] = 1;
  220. if (!$request->content)
  221. return $this->error(ErrorCode::CONNET_NOT_EXIST);
  222. $ok = CommentInfoModel::create($data);
  223. if ($ok) {
  224. return $this->api('');
  225. }else{
  226. return $this->error(ErrorCode::OPERATION_FAILED);
  227. }
  228. }
  229. /**
  230. * @api {get} /api/comment/delete 删除评论
  231. * @apiDescription 删除评论
  232. * @apiGroup Interaction
  233. * @apiParam {int} id 评论ID
  234. * @apiPermission Passport
  235. * @apiVersion 0.1.0
  236. * @apiSuccessExample {json} Success-Response:
  237. * {
  238. * "status": true,
  239. * "status_code": 0,
  240. * "message": "",
  241. * "data": ""
  242. *}
  243. * HTTP/1.1 200 OK
  244. * @apiErrorExample {json} Error-Response:
  245. * {
  246. * "status": false,
  247. * "status_code": 700,
  248. * "message": "操作失败",
  249. * "data": null
  250. *}
  251. * HTTP/1.1 400 Bad Request
  252. */
  253. public function delete(Request $request)
  254. {
  255. $validator = \Validator::make($request->all(),
  256. [
  257. 'id' => 'required',
  258. ],
  259. [
  260. 'id.required' => '评论ID不存在',
  261. ]
  262. );
  263. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  264. $user = $this->getUser();
  265. $ok = CommentInfoModel::where('user_id',$user->id)->where('id',$request->id)->delete();
  266. if ($ok) {
  267. return $this->api('');
  268. }else{
  269. return $this->error(ErrorCode::OPERATION_FAILED);
  270. }
  271. }
  272. /**
  273. * @api {get} /api/interaction/destroy 删除动态
  274. * @apiDescription 删除动态
  275. * @apiGroup Interaction
  276. * @apiParam {int} id 动态ID
  277. * @apiPermission Passport
  278. * @apiVersion 0.1.0
  279. * @apiSuccessExample {json} Success-Response:
  280. * {
  281. * "status": true,
  282. * "status_code": 0,
  283. * "message": "",
  284. * "data": ""
  285. *}
  286. * HTTP/1.1 200 OK
  287. * @apiErrorExample {json} Error-Response:
  288. * {
  289. * "status": false,
  290. * "status_code": 700,
  291. * "message": "操作失败",
  292. * "data": null
  293. *}
  294. * HTTP/1.1 400 Bad Request
  295. */
  296. public function destroy(Request $request)
  297. {
  298. $validator = \Validator::make($request->all(),
  299. [
  300. 'id' => 'required',
  301. ],
  302. [
  303. 'id.required' => '动态ID不存在',
  304. ]
  305. );
  306. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  307. // $user = $this->getUser();
  308. $id = $request->input('id');
  309. CommentInfoModel::where('interaction_id',$id)->delete();
  310. $ok = InteractionInfo::destroy($id);
  311. if ($ok) {
  312. return $this->api('');
  313. }else{
  314. return $this->error(ErrorCode::OPERATION_FAILED);
  315. }
  316. }
  317. }