InteractionController.php 5.8 KB

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