InteractionController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Models\CommentInfoModel;
  4. use Illuminate\Http\Request;
  5. use App\Services\Base\ErrorCode;
  6. class InteractionController extends Controller
  7. {
  8. // 发布关于梦想的动态
  9. /**
  10. * @api {post} /api/interaction/store 新增动态
  11. * @apiDescription 新增动态
  12. * @apiGroup Interaction
  13. * @apiPermission Passport
  14. * @apiVersion 0.1.0
  15. * @apiParam {int} dream_id 梦想ID
  16. * @apiParam {string} title 动态标题
  17. * @apiParam {array} pics[] 图片数组
  18. * @apiSuccessExample {json} Success-Response:
  19. * HTTP/1.1 200 OK
  20. *{
  21. * "status": true,
  22. * "status_code": 0,
  23. * "message": "",
  24. * "data": ""
  25. *}
  26. * @apiErrorExample {json} Error-Response:
  27. *HTTP/1.1 400 Bad Request
  28. * {
  29. * "state": false,
  30. * "code": 1000,
  31. * "message": "传入参数不正确",
  32. * "data": null or []
  33. * }
  34. *
  35. */
  36. public function store(Request $request)
  37. {
  38. $validator = \Validator::make($request->all(),
  39. [
  40. 'dream_id' => 'required',
  41. 'title' => 'required',
  42. ],
  43. [
  44. 'dream_id.required' => '梦想ID不能为空',
  45. 'title.required' => '动态标题不能为空',
  46. ]
  47. );
  48. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  49. $pics = $request->pics;
  50. if (empty($pics) || !is_array($pics)) {
  51. return $this->error(ErrorCode::ATTACHMENT_NOT_EXIST);
  52. }
  53. $data = [];
  54. foreach ($pics as $k => $pic) {
  55. $data['pic'.($k+1)] = $pic;
  56. }
  57. $dream_id = $request->dream_id;
  58. $title = $request->title;
  59. $data['dream_id'] = $dream_id;
  60. $data['title'] = $title;
  61. $ok = InteractionInfo::create($data);
  62. if ($ok) {
  63. // 收藏梦想最新动态加一
  64. UserCareDream::where('dream_id',$dream_id)->increment('interaction_number',1);
  65. return $this->api('');
  66. }else{
  67. return $this->error(ErrorCode::SAVE_USER_FAILED);
  68. }
  69. }
  70. // 评论互动
  71. /**
  72. * @api {post} /api/interaction/comments 评论动态
  73. * @apiDescription 评论动态
  74. * @apiGroup Interaction
  75. * @apiPermission Passport
  76. * @apiVersion 0.1.0
  77. * @apiParam {int} id 动态ID不存在
  78. * @apiParam {string} content 内容不能为空
  79. * @apiSuccessExample {json} Success-Response:
  80. * HTTP/1.1 200 OK
  81. *{
  82. * "status": true,
  83. * "status_code": 0,
  84. * "message": "",
  85. * "data": ""
  86. *}
  87. * @apiErrorExample {json} Error-Response:
  88. *HTTP/1.1 400 Bad Request
  89. * {
  90. * "state": false,
  91. * "code": 1000,
  92. * "message": "传入参数不正确",
  93. * "data": null or []
  94. * }
  95. *
  96. */
  97. public function comments(Request $request)
  98. {
  99. $validator = \Validator::make($request->all(),
  100. [
  101. 'id' => 'required',
  102. 'content' => 'required',
  103. ],
  104. [
  105. 'id.required' => '动态ID不存在',
  106. 'content.required' => '内容不能为空',
  107. ]
  108. );
  109. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  110. $user = $this->getUser();
  111. $user_id = $user->id;
  112. $interaction_id = $request->id;
  113. $content = $request->content;
  114. $data = compact('user_id','interaction_id','content');
  115. $ok = CommentInfoModel::create($data);
  116. if ($ok) {
  117. return $this->api('');
  118. }else{
  119. return $this->error(ErrorCode::OPERATION_FAILED);
  120. }
  121. }
  122. // 回复评论
  123. /**
  124. * @api {post} /api/interaction/reply 我的回复
  125. * @apiDescription 我的回复
  126. * @apiGroup Interaction
  127. * @apiParam {text} content 回复内容
  128. * @apiParam {int} comment_id 评论ID
  129. * @apiPermission Passport
  130. * @apiVersion 0.1.0
  131. * @apiSuccessExample {json} Success-Response:
  132. * {
  133. * "status": true,
  134. * "status_code": 0,
  135. * "message": "",
  136. * "data": ""
  137. *}
  138. * HTTP/1.1 200 OK
  139. * @apiErrorExample {json} Error-Response:
  140. * {
  141. * "status": false,
  142. * "status_code": 1000,
  143. * "message": "输入不正确",
  144. * "data": null
  145. *}
  146. * HTTP/1.1 400 Bad Request
  147. */
  148. public function reply(Request $request)
  149. {
  150. $validator = \Validator::make($request->all(),
  151. [
  152. 'id' => 'required',
  153. 'content' => 'required',
  154. ],
  155. [
  156. 'id.required' => '评论ID不存在',
  157. 'content.required' => '内容不能为空',
  158. ]
  159. );
  160. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  161. $user = $this->getUser();
  162. $data['user_id'] = $user->id;
  163. $data['comment_id'] = $request->id;
  164. $data['content'] = $request->content;
  165. if (!$request->content)
  166. return $this->error(ErrorCode::CONNET_NOT_EXIST);
  167. $ok = ReplyCommentsInfo::create($data);
  168. if ($ok) {
  169. return $this->api('');
  170. }else{
  171. return $this->error(ErrorCode::OPERATION_FAILED);
  172. }
  173. }
  174. }