InteractionController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Models\CommentInfoModel;
  4. use App\Models\DreamInfoModel;
  5. use App\Models\InteractionInfo;
  6. use App\Models\ReplyCommentsInfo;
  7. use App\Models\SupportDreamModel;
  8. use App\Models\SystemInfoModel;
  9. use App\Models\UserCareDream;
  10. use App\Models\UserCareUser;
  11. use App\Models\UserInfoModel;
  12. use Illuminate\Http\Request;
  13. use App\Services\Base\ErrorCode;
  14. use App\Helper\JpushHelper;
  15. class InteractionController extends Controller
  16. {
  17. use JpushHelper;
  18. // 发布关于梦想的动态
  19. /**
  20. * @api {post} /api/interaction/store 新增动态
  21. * @apiDescription 新增动态
  22. * @apiGroup Interaction
  23. * @apiPermission Passport
  24. * @apiVersion 0.1.0
  25. * @apiParam {int} id 梦想ID
  26. * @apiParam {string} title 互动标题
  27. * @apiParam {string} [video] 视频
  28. * @apiParam {array} [pics[]] 图片数组
  29. * @apiSuccessExample {json} Success-Response:
  30. * HTTP/1.1 200 OK
  31. *{
  32. * "status": true,
  33. * "status_code": 0,
  34. * "message": "",
  35. * "data": ""
  36. *}
  37. * @apiErrorExample {json} Error-Response:
  38. *HTTP/1.1 400 Bad Request
  39. * {
  40. * "state": false,
  41. * "code": 1000,
  42. * "message": "传入参数不正确",
  43. * "data": null or []
  44. * }
  45. *
  46. */
  47. public function store(Request $request)
  48. {
  49. $user = $this->getUser();
  50. $validator = \Validator::make($request->all(),
  51. [
  52. 'id' => 'required',
  53. 'title' => 'required',
  54. ],
  55. [
  56. 'id.required' => '梦想ID不能为空',
  57. 'title.required' => '动态标题不能为空',
  58. ]
  59. );
  60. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  61. $data = [];
  62. $pics = $request->pics;
  63. if (empty($pics) || !is_array($pics)) {
  64. // return $this->error(ErrorCode::ATTACHMENT_NOT_EXIST);
  65. }else{
  66. foreach ($pics as $k => $pic) {
  67. $data['pic'.($k+1)] = $pic;
  68. }
  69. }
  70. $dream_id = $request->id;
  71. $title = $request->title;
  72. $data['dream_id'] = $dream_id;
  73. $data['title'] = $title;
  74. $video = $request->video;
  75. if (!empty($video)) $data['video'] =env('APP_URL').'/attachment/'. $request->video;
  76. $ok = InteractionInfo::create($data);
  77. if ($ok) {
  78. // 新的互动应该有消息通知《支持者》
  79. $support_user = SupportDreamModel::where('dream_id',$dream_id)->get();
  80. if (!empty(count($support_user))) {
  81. $user_ids = array_column($support_user->toArray(),'user_id');
  82. foreach ($user_ids as $user_id) {
  83. $arr['user_id']=$user->id;
  84. $arr['to_user_id']=$user_id;
  85. $arr['message']='您支持的梦想又有新的动态啦';
  86. $arr['interaction_id']=$ok->id;
  87. $arr['dream_id']=$dream_id;
  88. SystemInfoModel::create($arr);
  89. // 长连接
  90. $this->jPush($arr['message'],'',$user_id);
  91. }
  92. }
  93. // 收藏梦想的最新动态加一
  94. UserCareDream::where('dream_id',$dream_id)->increment('interaction_number',1);
  95. $dream_user_id = DreamInfoModel::find($dream_id)->user_id;
  96. UserCareUser::where('other_user_id',$dream_user_id)->update(['dream_id'=>$dream_id,'dream_number'=>'1']);
  97. // UserCareUser::where('dream_id',$dream_id)->increment('interaction_number',1);
  98. return $this->api('');
  99. }else{
  100. return $this->error(ErrorCode::SAVE_USER_FAILED);
  101. }
  102. }
  103. // 评论互动
  104. /**
  105. * @api {post} /api/interaction/comment 评论动态
  106. * @apiDescription 评论动态
  107. * @apiGroup Interaction
  108. * @apiPermission Passport
  109. * @apiVersion 0.1.0
  110. * @apiParam {int} id 动态ID不存在
  111. * @apiParam {int} u_id @用户id
  112. * @apiParam {int} to_user_id 发布动态的梦想者id
  113. * @apiParam {int} [comment_user_id] //已经评论者id
  114. * @apiParam {string} content 内容不能为空
  115. * @apiSuccessExample {json} Success-Response:
  116. * HTTP/1.1 200 OK
  117. *{
  118. * "status": true,
  119. * "status_code": 0,
  120. * "message": "",
  121. * "data": ""
  122. *}
  123. * @apiErrorExample {json} Error-Response:
  124. *HTTP/1.1 400 Bad Request
  125. * {
  126. * "state": false,
  127. * "code": 1000,
  128. * "message": "传入参数不正确",
  129. * "data": null or []
  130. * }
  131. *
  132. */
  133. public function comment(Request $request)
  134. {
  135. $validator = \Validator::make($request->all(),
  136. [
  137. 'id' => 'required',
  138. 'content' => 'required',
  139. ],
  140. [
  141. 'id.required' => '动态ID不存在',
  142. 'content.required' => '内容不能为空',
  143. ]
  144. );
  145. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  146. $user = $this->getUser();
  147. /* $user_id = $user->id;
  148. $user_avatar = $user->avatar;
  149. $user_nickname = $user->nickname;
  150. $interaction_id = $request->id;
  151. $content = $request->content;
  152. $is_read = 1;
  153. $data = compact('user_id','user_avatar','user_nickname','interaction_id','content','is_read');*/
  154. $dream_id = InteractionInfo::find($request->id)->dream_id;
  155. // $data['to_user_avatar'] = InteractionInfo::find($request->id)->dream->user_avatar;
  156. // $data['to_user_nickname'] = InteractionInfo::find($request->id)->dream->user_nickname;
  157. if (!empty($request->input('comment_user_id'))) {
  158. $to_user = UserInfoModel::find($request->input('comment_user_id'));
  159. if (!empty($to_user)){
  160. $data['to_user_id'] = $request->input('to_user_id');
  161. $data['to_user_avatar'] = $to_user->avatar;
  162. $data['to_user_nickname'] = $to_user->nickname;
  163. //点击去看看
  164. $message = $user->nickname.'在你的互动上留言啦!点击去看看';
  165. $info = [
  166. 'to_user_id' => $data['to_user_id'],
  167. 'message' => $message,
  168. 'is_url' => 1,
  169. 'type_id' => 1,
  170. 'attr_id' => 3,
  171. 'dream_id' => $dream_id,
  172. 'interaction_id' => $request->input('id'),
  173. ];
  174. SystemInfoModel::create($info);
  175. // 长连接
  176. $this->jPush($message,'',$data['to_user_id']);
  177. }
  178. }
  179. if (!empty($request->input('u_id'))) {
  180. $message = $user->nickname.'提起了你哦~点击看看!';
  181. $info = [
  182. 'to_user_id' => $request->input('u_id'),
  183. 'message' => $message,
  184. 'is_url' => 1,
  185. 'type_id' => 1,
  186. 'attr_id' => 8,
  187. 'dream_id' => $dream_id,
  188. 'interaction_id' => $request->input('id'),
  189. ];
  190. SystemInfoModel::create($info);
  191. }
  192. $data['user_id'] = $user->id;
  193. $data['user_avatar'] =$user->avatar;
  194. $data['user_nickname'] = $user->nickname;
  195. $data['interaction_id'] = $request->id;
  196. $data['content'] = $request->content;
  197. $data['is_read'] = 1;
  198. $ok = CommentInfoModel::create($data);
  199. if ($ok) {
  200. // 评论动态也会出现在首页用户
  201. UserCareDream::where('dream_id',$dream_id)->increment('interaction_number',1);
  202. return $this->api('');
  203. }else{
  204. return $this->error(ErrorCode::OPERATION_FAILED);
  205. }
  206. }
  207. // 回复评论
  208. /**
  209. * @api {post} /api/interaction/reply 我的回复
  210. * @apiDescription 我的回复
  211. * @apiGroup Interaction
  212. * @apiParam {text} content 回复内容
  213. * @apiParam {int} comment_id 评论ID
  214. * @apiParam {int} interaction_id 动态ID
  215. * @apiPermission Passport
  216. * @apiVersion 0.1.0
  217. * @apiSuccessExample {json} Success-Response:
  218. * {
  219. * "status": true,
  220. * "status_code": 0,
  221. * "message": "",
  222. * "data": ""
  223. *}
  224. * HTTP/1.1 200 OK
  225. * @apiErrorExample {json} Error-Response:
  226. * {
  227. * "status": false,
  228. * "status_code": 1000,
  229. * "message": "输入不正确",
  230. * "data": null
  231. *}
  232. * HTTP/1.1 400 Bad Request
  233. */
  234. public function reply(Request $request)
  235. {
  236. $validator = \Validator::make($request->all(),
  237. [
  238. 'id' => 'required',
  239. 'interaction_id' => 'required',
  240. 'content' => 'required',
  241. ],
  242. [
  243. 'id.required' => '评论ID不存在',
  244. 'content.required' => '内容不能为空',
  245. ]
  246. );
  247. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  248. $user = $this->getUser();
  249. /* $data['to_user_id'] = $user->id;
  250. $data['to_user_avatar'] = $user->avatar;
  251. $data['to_user_nickname'] = $user->nickname; */
  252. $data['to_user_id'] = CommentInfoModel::find($request->id)->user_id;
  253. // $data['to_user_avatar'] = CommentInfoModel::find($request->id)->user_avatar;
  254. $data['to_user_nickname'] = CommentInfoModel::find($request->id)->user_nickname;
  255. $data['user_id'] = $user->id;
  256. $data['user_avatar'] =$user->avatar;
  257. $data['user_nickname'] = $user->nickname;
  258. $data['interaction_id'] = $request->id;
  259. $data['content'] = $request->content;
  260. $data['is_read'] = 1;
  261. if (!$request->content)
  262. return $this->error(ErrorCode::CONNET_NOT_EXIST);
  263. $ok = CommentInfoModel::create($data);
  264. if ($ok) {
  265. return $this->api('');
  266. }else{
  267. return $this->error(ErrorCode::OPERATION_FAILED);
  268. }
  269. }
  270. /**
  271. * @api {get} /api/comment/delete 删除评论
  272. * @apiDescription 删除评论
  273. * @apiGroup Interaction
  274. * @apiParam {int} id 评论ID
  275. * @apiPermission Passport
  276. * @apiVersion 0.1.0
  277. * @apiSuccessExample {json} Success-Response:
  278. * {
  279. * "status": true,
  280. * "status_code": 0,
  281. * "message": "",
  282. * "data": ""
  283. *}
  284. * HTTP/1.1 200 OK
  285. * @apiErrorExample {json} Error-Response:
  286. * {
  287. * "status": false,
  288. * "status_code": 700,
  289. * "message": "操作失败",
  290. * "data": null
  291. *}
  292. * HTTP/1.1 400 Bad Request
  293. */
  294. public function delete(Request $request)
  295. {
  296. $validator = \Validator::make($request->all(),
  297. [
  298. 'id' => 'required',
  299. ],
  300. [
  301. 'id.required' => '评论ID不存在',
  302. ]
  303. );
  304. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  305. $user = $this->getUser();
  306. $ok = CommentInfoModel::where('user_id',$user->id)->where('id',$request->id)->delete();
  307. if ($ok) {
  308. return $this->api('');
  309. }else{
  310. return $this->error(ErrorCode::OPERATION_FAILED);
  311. }
  312. }
  313. /**
  314. * @api {get} /api/interaction/destroy 删除动态
  315. * @apiDescription 删除动态
  316. * @apiGroup Interaction
  317. * @apiParam {int} id 动态ID
  318. * @apiPermission Passport
  319. * @apiVersion 0.1.0
  320. * @apiSuccessExample {json} Success-Response:
  321. * {
  322. * "status": true,
  323. * "status_code": 0,
  324. * "message": "",
  325. * "data": ""
  326. *}
  327. * HTTP/1.1 200 OK
  328. * @apiErrorExample {json} Error-Response:
  329. * {
  330. * "status": false,
  331. * "status_code": 700,
  332. * "message": "操作失败",
  333. * "data": null
  334. *}
  335. * HTTP/1.1 400 Bad Request
  336. */
  337. public function destroy(Request $request)
  338. {
  339. $validator = \Validator::make($request->all(),
  340. [
  341. 'id' => 'required',
  342. ],
  343. [
  344. 'id.required' => '动态ID不存在',
  345. ]
  346. );
  347. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  348. // $user = $this->getUser();
  349. $id = $request->input('id');
  350. CommentInfoModel::where('interaction_id',$id)->delete();
  351. $ok = InteractionInfo::destroy($id);
  352. if ($ok) {
  353. return $this->api('');
  354. }else{
  355. return $this->error(ErrorCode::OPERATION_FAILED);
  356. }
  357. }
  358. }