InteractionController.php 5.5 KB

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