InteractionController.php 5.9 KB

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