InteractionController.php 16 KB

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