InteractionController.php 5.5 KB

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