InteractionController.php 8.2 KB

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