InteractionController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. $user_avatar = $user->avatar;
  119. $user_nickname = $user->nickname;
  120. $interaction_id = $request->id;
  121. $content = $request->content;
  122. $data = compact('user_id','user_avatar','user_nickname','interaction_id','content');
  123. $ok = CommentInfoModel::create($data);
  124. if ($ok) {
  125. return $this->api('');
  126. }else{
  127. return $this->error(ErrorCode::OPERATION_FAILED);
  128. }
  129. }
  130. // 回复评论
  131. /**
  132. * @api {post} /api/interaction/reply 我的回复
  133. * @apiDescription 我的回复
  134. * @apiGroup Interaction
  135. * @apiParam {text} content 回复内容
  136. * @apiParam {int} comment_id 评论ID
  137. * @apiParam {int} interaction_id 动态ID
  138. * @apiPermission Passport
  139. * @apiVersion 0.1.0
  140. * @apiSuccessExample {json} Success-Response:
  141. * {
  142. * "status": true,
  143. * "status_code": 0,
  144. * "message": "",
  145. * "data": ""
  146. *}
  147. * HTTP/1.1 200 OK
  148. * @apiErrorExample {json} Error-Response:
  149. * {
  150. * "status": false,
  151. * "status_code": 1000,
  152. * "message": "输入不正确",
  153. * "data": null
  154. *}
  155. * HTTP/1.1 400 Bad Request
  156. */
  157. public function reply(Request $request)
  158. {
  159. $validator = \Validator::make($request->all(),
  160. [
  161. 'id' => 'required',
  162. 'interaction_id' => 'required',
  163. 'content' => 'required',
  164. ],
  165. [
  166. 'id.required' => '评论ID不存在',
  167. 'content.required' => '内容不能为空',
  168. ]
  169. );
  170. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  171. $user = $this->getUser();
  172. /* $data['to_user_id'] = $user->id;
  173. $data['to_user_avatar'] = $user->avatar;
  174. $data['to_user_nickname'] = $user->nickname; */
  175. $data['to_user_id'] = CommentInfoModel::find($request->id)->user_id;
  176. $data['to_user_avatar'] = CommentInfoModel::find($request->id)->user_avatar;
  177. $data['to_user_nickname'] = CommentInfoModel::find($request->id)->user_nickname;
  178. $data['user_id'] = $user->id;
  179. $data['user_avatar'] =$user->avatar;
  180. $data['user_nickname'] = $user->nickname;
  181. $data['interaction_id'] = $request->interaction_id;
  182. $data['content'] = $request->content;
  183. if (!$request->content)
  184. return $this->error(ErrorCode::CONNET_NOT_EXIST);
  185. $ok = CommentInfoModel::create($data);
  186. if ($ok) {
  187. return $this->api('');
  188. }else{
  189. return $this->error(ErrorCode::OPERATION_FAILED);
  190. }
  191. }
  192. /**
  193. * @api {get} /api/comment/delete 删除评论
  194. * @apiDescription 删除评论
  195. * @apiGroup Interaction
  196. * @apiParam {int} id 评论ID
  197. * @apiPermission Passport
  198. * @apiVersion 0.1.0
  199. * @apiSuccessExample {json} Success-Response:
  200. * {
  201. * "status": true,
  202. * "status_code": 0,
  203. * "message": "",
  204. * "data": ""
  205. *}
  206. * HTTP/1.1 200 OK
  207. * @apiErrorExample {json} Error-Response:
  208. * {
  209. * "status": false,
  210. * "status_code": 700,
  211. * "message": "操作失败",
  212. * "data": null
  213. *}
  214. * HTTP/1.1 400 Bad Request
  215. */
  216. public function delete(Request $request)
  217. {
  218. $validator = \Validator::make($request->all(),
  219. [
  220. 'id' => 'required',
  221. ],
  222. [
  223. 'id.required' => '评论ID不存在',
  224. ]
  225. );
  226. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  227. $user = $this->getUser();
  228. $ok = CommentInfoModel::where('user_id',$user->id)->where('id',$request->id)->delete();
  229. if ($ok) {
  230. return $this->api('');
  231. }else{
  232. return $this->error(ErrorCode::OPERATION_FAILED);
  233. }
  234. }
  235. }