InteractionController.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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\SystemInfoModel;
  7. use App\Models\UserCareDream;
  8. use Illuminate\Http\Request;
  9. use App\Services\Base\ErrorCode;
  10. use App\Helper\JpushHelper;
  11. class InteractionController extends Controller
  12. {
  13. use JpushHelper;
  14. // 发布关于梦想的动态
  15. /**
  16. * @api {post} /api/interaction/store 新增动态
  17. * @apiDescription 新增动态
  18. * @apiGroup Interaction
  19. * @apiPermission Passport
  20. * @apiVersion 0.1.0
  21. * @apiParam {int} id 梦想ID
  22. * @apiParam {string} title 互动标题
  23. * @apiParam {string} [video] 视频
  24. * @apiParam {array} pics[] 图片数组
  25. * @apiSuccessExample {json} Success-Response:
  26. * HTTP/1.1 200 OK
  27. *{
  28. * "status": true,
  29. * "status_code": 0,
  30. * "message": "",
  31. * "data": ""
  32. *}
  33. * @apiErrorExample {json} Error-Response:
  34. *HTTP/1.1 400 Bad Request
  35. * {
  36. * "state": false,
  37. * "code": 1000,
  38. * "message": "传入参数不正确",
  39. * "data": null or []
  40. * }
  41. *
  42. */
  43. public function store(Request $request)
  44. {
  45. $validator = \Validator::make($request->all(),
  46. [
  47. 'id' => 'required',
  48. 'title' => 'required',
  49. ],
  50. [
  51. 'id.required' => '梦想ID不能为空',
  52. 'title.required' => '动态标题不能为空',
  53. ]
  54. );
  55. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  56. $data = [];
  57. $pics = $request->pics;
  58. if (empty($pics) || !is_array($pics)) {
  59. // return $this->error(ErrorCode::ATTACHMENT_NOT_EXIST);
  60. }else{
  61. foreach ($pics as $k => $pic) {
  62. $data['pic'.($k+1)] = $pic;
  63. }
  64. }
  65. $dream_id = $request->id;
  66. $title = $request->title;
  67. $data['dream_id'] = $dream_id;
  68. $data['title'] = $title;
  69. $data['video'] = $request->video;
  70. $ok = InteractionInfo::create($data);
  71. if ($ok) {
  72. // 收藏梦想最新动态加一
  73. UserCareDream::where('dream_id',$dream_id)->increment('interaction_number',1);
  74. return $this->api('');
  75. }else{
  76. return $this->error(ErrorCode::SAVE_USER_FAILED);
  77. }
  78. }
  79. // 评论互动
  80. /**
  81. * @api {post} /api/interaction/comment 评论动态
  82. * @apiDescription 评论动态
  83. * @apiGroup Interaction
  84. * @apiPermission Passport
  85. * @apiVersion 0.1.0
  86. * @apiParam {int} id 动态ID不存在
  87. * @apiParam {string} content 内容不能为空
  88. * @apiSuccessExample {json} Success-Response:
  89. * HTTP/1.1 200 OK
  90. *{
  91. * "status": true,
  92. * "status_code": 0,
  93. * "message": "",
  94. * "data": ""
  95. *}
  96. * @apiErrorExample {json} Error-Response:
  97. *HTTP/1.1 400 Bad Request
  98. * {
  99. * "state": false,
  100. * "code": 1000,
  101. * "message": "传入参数不正确",
  102. * "data": null or []
  103. * }
  104. *
  105. */
  106. public function comment(Request $request)
  107. {
  108. $validator = \Validator::make($request->all(),
  109. [
  110. 'id' => 'required',
  111. 'content' => 'required',
  112. ],
  113. [
  114. 'id.required' => '动态ID不存在',
  115. 'content.required' => '内容不能为空',
  116. ]
  117. );
  118. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  119. $user = $this->getUser();
  120. /* $user_id = $user->id;
  121. $user_avatar = $user->avatar;
  122. $user_nickname = $user->nickname;
  123. $interaction_id = $request->id;
  124. $content = $request->content;
  125. $is_read = 1;
  126. $data = compact('user_id','user_avatar','user_nickname','interaction_id','content','is_read');*/
  127. $data['to_user_id'] = InteractionInfo::find($request->id)->dream->user_id;
  128. $data['to_user_avatar'] = InteractionInfo::find($request->id)->dream->user_avatar;
  129. $data['to_user_nickname'] = InteractionInfo::find($request->id)->dream->user_nickname;
  130. $data['user_id'] = $user->id;
  131. $data['user_avatar'] =$user->avatar;
  132. $data['user_nickname'] = $user->nickname;
  133. $data['interaction_id'] = $request->id;
  134. $data['content'] = $request->content;
  135. $data['is_read'] = 1;
  136. $ok = CommentInfoModel::create($data);
  137. $message = $user->nickname.'在你的互动上留言啦!点击去看看!';
  138. $info = [
  139. 'user_id' => $data['to_user_id'],
  140. 'message' => $message,
  141. ];
  142. SystemInfoModel::create($info);
  143. // 长连接
  144. $this->jPush($message,'',$data['to_user_id']);
  145. if ($ok) {
  146. return $this->api('');
  147. }else{
  148. return $this->error(ErrorCode::OPERATION_FAILED);
  149. }
  150. }
  151. // 回复评论
  152. /**
  153. * @api {post} /api/interaction/reply 我的回复
  154. * @apiDescription 我的回复
  155. * @apiGroup Interaction
  156. * @apiParam {text} content 回复内容
  157. * @apiParam {int} comment_id 评论ID
  158. * @apiParam {int} interaction_id 动态ID
  159. * @apiPermission Passport
  160. * @apiVersion 0.1.0
  161. * @apiSuccessExample {json} Success-Response:
  162. * {
  163. * "status": true,
  164. * "status_code": 0,
  165. * "message": "",
  166. * "data": ""
  167. *}
  168. * HTTP/1.1 200 OK
  169. * @apiErrorExample {json} Error-Response:
  170. * {
  171. * "status": false,
  172. * "status_code": 1000,
  173. * "message": "输入不正确",
  174. * "data": null
  175. *}
  176. * HTTP/1.1 400 Bad Request
  177. */
  178. public function reply(Request $request)
  179. {
  180. $validator = \Validator::make($request->all(),
  181. [
  182. 'id' => 'required',
  183. 'interaction_id' => 'required',
  184. 'content' => 'required',
  185. ],
  186. [
  187. 'id.required' => '评论ID不存在',
  188. 'content.required' => '内容不能为空',
  189. ]
  190. );
  191. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  192. $user = $this->getUser();
  193. /* $data['to_user_id'] = $user->id;
  194. $data['to_user_avatar'] = $user->avatar;
  195. $data['to_user_nickname'] = $user->nickname; */
  196. $data['to_user_id'] = CommentInfoModel::find($request->id)->user_id;
  197. // $data['to_user_avatar'] = CommentInfoModel::find($request->id)->user_avatar;
  198. $data['to_user_nickname'] = CommentInfoModel::find($request->id)->user_nickname;
  199. $data['user_id'] = $user->id;
  200. $data['user_avatar'] =$user->avatar;
  201. $data['user_nickname'] = $user->nickname;
  202. $data['interaction_id'] = $request->id;
  203. $data['content'] = $request->content;
  204. $data['is_read'] = 1;
  205. if (!$request->content)
  206. return $this->error(ErrorCode::CONNET_NOT_EXIST);
  207. $ok = CommentInfoModel::create($data);
  208. if ($ok) {
  209. return $this->api('');
  210. }else{
  211. return $this->error(ErrorCode::OPERATION_FAILED);
  212. }
  213. }
  214. /**
  215. * @api {get} /api/comment/delete 删除评论
  216. * @apiDescription 删除评论
  217. * @apiGroup Interaction
  218. * @apiParam {int} id 评论ID
  219. * @apiPermission Passport
  220. * @apiVersion 0.1.0
  221. * @apiSuccessExample {json} Success-Response:
  222. * {
  223. * "status": true,
  224. * "status_code": 0,
  225. * "message": "",
  226. * "data": ""
  227. *}
  228. * HTTP/1.1 200 OK
  229. * @apiErrorExample {json} Error-Response:
  230. * {
  231. * "status": false,
  232. * "status_code": 700,
  233. * "message": "操作失败",
  234. * "data": null
  235. *}
  236. * HTTP/1.1 400 Bad Request
  237. */
  238. public function delete(Request $request)
  239. {
  240. $validator = \Validator::make($request->all(),
  241. [
  242. 'id' => 'required',
  243. ],
  244. [
  245. 'id.required' => '评论ID不存在',
  246. ]
  247. );
  248. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  249. $user = $this->getUser();
  250. $ok = CommentInfoModel::where('user_id',$user->id)->where('id',$request->id)->delete();
  251. if ($ok) {
  252. return $this->api('');
  253. }else{
  254. return $this->error(ErrorCode::OPERATION_FAILED);
  255. }
  256. }
  257. }