InteractionController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. $title = $request->title;
  73. $data['dream_id'] = $dream_id;
  74. $data['title'] = $title;
  75. $video = $request->video;
  76. if (!empty($video)) $data['video'] =env('APP_URL').'/attachment/'. $request->video;
  77. $ok = InteractionInfo::create($data);
  78. if ($ok) {
  79. // 新的互动应该有消息通知《支持者》
  80. $support_user = SupportDreamModel::where('dream_id',$dream_id)->get();
  81. if (!empty(count($support_user))) {
  82. $user_ids = array_column($support_user->toArray(),'user_id');
  83. foreach ($user_ids as $user_id) {
  84. $arr['user_id']=$user->id;
  85. $arr['to_user_id']=$user_id;
  86. $arr['message']='您支持的梦想又有新的动态啦';
  87. $arr['interaction_id']=$ok->id;
  88. $arr['dream_id']=$dream_id;
  89. SystemInfoModel::create($arr);
  90. // 长连接
  91. $this->jPush($arr['message'],'',$user_id);
  92. }
  93. }
  94. // 收藏梦想的最新动态加一
  95. UserCareDream::where('dream_id',$dream_id)->increment('interaction_number',1);
  96. $dream_user_id = DreamInfoModel::find($dream_id)->user_id;
  97. UserCareUser::where('other_user_id',$dream_user_id)->update(['dream_id'=>$dream_id,'dream_number'=>'1']);
  98. // UserCareUser::where('dream_id',$dream_id)->increment('interaction_number',1);
  99. return $this->api('');
  100. }else{
  101. return $this->error(ErrorCode::SAVE_USER_FAILED);
  102. }
  103. }
  104. // 评论互动
  105. /**
  106. * @api {post} /api/interaction/comment 评论动态
  107. * @apiDescription 评论动态
  108. * @apiGroup Interaction
  109. * @apiPermission Passport
  110. * @apiVersion 0.1.0
  111. * @apiParam {int} id 动态ID不存在
  112. * @apiParam {int} u_id @用户id
  113. * @apiParam {int} to_user_id 发布动态的梦想者id
  114. * @apiParam {int} [comment_user_id] //已经评论者id
  115. * @apiParam {string} content 内容不能为空
  116. * @apiSuccessExample {json} Success-Response:
  117. * HTTP/1.1 200 OK
  118. *{
  119. * "status": true,
  120. * "status_code": 0,
  121. * "message": "",
  122. * "data": ""
  123. *}
  124. * @apiErrorExample {json} Error-Response:
  125. *HTTP/1.1 400 Bad Request
  126. * {
  127. * "state": false,
  128. * "code": 1000,
  129. * "message": "传入参数不正确",
  130. * "data": null or []
  131. * }
  132. *
  133. */
  134. public function comment(Request $request)
  135. {
  136. $validator = \Validator::make($request->all(),
  137. [
  138. 'id' => 'required',
  139. 'content' => 'required',
  140. ],
  141. [
  142. 'id.required' => '动态ID不存在',
  143. 'content.required' => '内容不能为空',
  144. ]
  145. );
  146. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  147. $user = $this->getUser();
  148. /* $user_id = $user->id;
  149. $user_avatar = $user->avatar;
  150. $user_nickname = $user->nickname;
  151. $interaction_id = $request->id;
  152. $content = $request->content;
  153. $is_read = 1;
  154. $data = compact('user_id','user_avatar','user_nickname','interaction_id','content','is_read');*/
  155. $dream_id = InteractionInfo::find($request->id)->dream_id;
  156. // $data['to_user_avatar'] = InteractionInfo::find($request->id)->dream->user_avatar;
  157. // $data['to_user_nickname'] = InteractionInfo::find($request->id)->dream->user_nickname;
  158. if (!empty($request->input('comment_user_id'))) {
  159. $to_user = UserInfoModel::find($request->input('comment_user_id'));
  160. if (!empty($to_user)){
  161. $data['to_user_id'] = $request->input('to_user_id');
  162. $data['to_user_avatar'] = $to_user->avatar;
  163. $data['to_user_nickname'] = $to_user->nickname;
  164. //点击去看看
  165. $message = $user->nickname.'在你的互动上留言啦!点击去看看';
  166. $info = [
  167. 'to_user_id' => $data['to_user_id'],
  168. 'message' => $message,
  169. 'is_url' => 1,
  170. 'type_id' => 1,
  171. 'attr_id' => 3,
  172. 'dream_id' => $dream_id,
  173. 'interaction_id' => $request->input('id'),
  174. ];
  175. SystemInfoModel::create($info);
  176. // 长连接
  177. $this->jPush($message,'',$data['to_user_id']);
  178. }
  179. }
  180. if (!empty($request->input('u_id'))) {
  181. $message = $user->nickname.'提起了你哦~点击看看!';
  182. $info = [
  183. 'to_user_id' => $request->input('u_id'),
  184. 'message' => $message,
  185. 'is_url' => 1,
  186. 'type_id' => 1,
  187. 'attr_id' => 8,
  188. 'dream_id' => $dream_id,
  189. 'interaction_id' => $request->input('id'),
  190. ];
  191. SystemInfoModel::create($info);
  192. }
  193. $data['user_id'] = $user->id;
  194. $data['user_avatar'] =$user->avatar;
  195. $data['user_nickname'] = $user->nickname;
  196. $data['interaction_id'] = $request->id;
  197. $data['content'] = $request->content;
  198. $data['is_read'] = 1;
  199. $ok = CommentInfoModel::create($data);
  200. if ($ok) {
  201. // 评论动态也会出现在首页用户
  202. UserCareDream::where('dream_id',$dream_id)->increment('interaction_number',1);
  203. $this->autoCareDream($user->id,$user->nickname,$dream_id);
  204. return $this->api('');
  205. // 评论成功自动收藏梦想
  206. }else{
  207. return $this->error(ErrorCode::OPERATION_FAILED);
  208. }
  209. }
  210. // 回复评论
  211. /**
  212. * @api {post} /api/interaction/reply 我的回复
  213. * @apiDescription 我的回复
  214. * @apiGroup Interaction
  215. * @apiParam {text} content 回复内容
  216. * @apiParam {int} comment_id 评论ID
  217. * @apiParam {int} interaction_id 动态ID
  218. * @apiPermission Passport
  219. * @apiVersion 0.1.0
  220. * @apiSuccessExample {json} Success-Response:
  221. * {
  222. * "status": true,
  223. * "status_code": 0,
  224. * "message": "",
  225. * "data": ""
  226. *}
  227. * HTTP/1.1 200 OK
  228. * @apiErrorExample {json} Error-Response:
  229. * {
  230. * "status": false,
  231. * "status_code": 1000,
  232. * "message": "输入不正确",
  233. * "data": null
  234. *}
  235. * HTTP/1.1 400 Bad Request
  236. */
  237. public function reply(Request $request)
  238. {
  239. $validator = \Validator::make($request->all(),
  240. [
  241. 'id' => 'required',
  242. 'interaction_id' => 'required',
  243. 'content' => 'required',
  244. ],
  245. [
  246. 'id.required' => '评论ID不存在',
  247. 'content.required' => '内容不能为空',
  248. ]
  249. );
  250. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  251. $user = $this->getUser();
  252. /* $data['to_user_id'] = $user->id;
  253. $data['to_user_avatar'] = $user->avatar;
  254. $data['to_user_nickname'] = $user->nickname; */
  255. $data['to_user_id'] = CommentInfoModel::find($request->id)->user_id;
  256. // $data['to_user_avatar'] = CommentInfoModel::find($request->id)->user_avatar;
  257. $data['to_user_nickname'] = CommentInfoModel::find($request->id)->user_nickname;
  258. $data['user_id'] = $user->id;
  259. $data['user_avatar'] =$user->avatar;
  260. $data['user_nickname'] = $user->nickname;
  261. $data['interaction_id'] = $request->id;
  262. $data['content'] = $request->content;
  263. $data['is_read'] = 1;
  264. if (!$request->content)
  265. return $this->error(ErrorCode::CONNET_NOT_EXIST);
  266. $ok = CommentInfoModel::create($data);
  267. if ($ok) {
  268. // 评论成功自动收藏梦想
  269. $dream_id = InteractionInfo::find($request->id)->dream_id;
  270. $this->autoCareDream($user->id,$user->nickname,$dream_id);
  271. return $this->api('');
  272. }else{
  273. return $this->error(ErrorCode::OPERATION_FAILED);
  274. }
  275. }
  276. /**
  277. * @api {get} /api/comment/delete 删除评论
  278. * @apiDescription 删除评论
  279. * @apiGroup Interaction
  280. * @apiParam {int} id 评论ID
  281. * @apiPermission Passport
  282. * @apiVersion 0.1.0
  283. * @apiSuccessExample {json} Success-Response:
  284. * {
  285. * "status": true,
  286. * "status_code": 0,
  287. * "message": "",
  288. * "data": ""
  289. *}
  290. * HTTP/1.1 200 OK
  291. * @apiErrorExample {json} Error-Response:
  292. * {
  293. * "status": false,
  294. * "status_code": 700,
  295. * "message": "操作失败",
  296. * "data": null
  297. *}
  298. * HTTP/1.1 400 Bad Request
  299. */
  300. public function delete(Request $request)
  301. {
  302. $validator = \Validator::make($request->all(),
  303. [
  304. 'id' => 'required',
  305. ],
  306. [
  307. 'id.required' => '评论ID不存在',
  308. ]
  309. );
  310. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  311. $user = $this->getUser();
  312. $ok = CommentInfoModel::where('user_id',$user->id)->where('id',$request->id)->delete();
  313. if ($ok) {
  314. return $this->api('');
  315. }else{
  316. return $this->error(ErrorCode::OPERATION_FAILED);
  317. }
  318. }
  319. /**
  320. * @api {get} /api/interaction/destroy 删除动态
  321. * @apiDescription 删除动态
  322. * @apiGroup Interaction
  323. * @apiParam {int} id 动态ID
  324. * @apiPermission Passport
  325. * @apiVersion 0.1.0
  326. * @apiSuccessExample {json} Success-Response:
  327. * {
  328. * "status": true,
  329. * "status_code": 0,
  330. * "message": "",
  331. * "data": ""
  332. *}
  333. * HTTP/1.1 200 OK
  334. * @apiErrorExample {json} Error-Response:
  335. * {
  336. * "status": false,
  337. * "status_code": 700,
  338. * "message": "操作失败",
  339. * "data": null
  340. *}
  341. * HTTP/1.1 400 Bad Request
  342. */
  343. public function destroy(Request $request)
  344. {
  345. $validator = \Validator::make($request->all(),
  346. [
  347. 'id' => 'required',
  348. ],
  349. [
  350. 'id.required' => '动态ID不存在',
  351. ]
  352. );
  353. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  354. // $user = $this->getUser();
  355. $id = $request->input('id');
  356. CommentInfoModel::where('interaction_id',$id)->delete();
  357. $ok = InteractionInfo::destroy($id);
  358. if ($ok) {
  359. return $this->api('');
  360. }else{
  361. return $this->error(ErrorCode::OPERATION_FAILED);
  362. }
  363. }
  364. public function autoCareDream($user_id,$nickname,$dream_id)
  365. {
  366. $user_care_dream = UserCareDream::where('user_id',$user_id)->where('dream_id',$dream_id)->first();
  367. if (empty($user_care_dream)) {
  368. $dream = DreamInfoModel::find($dream_id);
  369. $data_info = [
  370. 'user_id' =>$user_id,
  371. 'dream_id' =>$dream_id,
  372. 'dream_user_id' =>$dream->user_id,
  373. ];
  374. UserCareDream::create($data_info);
  375. // 关注成功发送私信
  376. $message = BaseSettingsModel::where('category','message')->first();
  377. $message = empty($message) ? "[<span style='color: #00c3da'>$nickname</span>] 收藏了你的梦想《".$dream->name.'》' : $message->value;
  378. $info2 = [
  379. 'to_user_id' => $dream->user_id,
  380. 'message' => $message,
  381. 'dream_id' => $dream_id,
  382. 'user_id' => $user_id,
  383. 'is_reply' => 1,
  384. 'type_id' => 1,
  385. 'attr_id' => 2,
  386. ];
  387. SystemInfoModel::firstOrCreate($info2);
  388. }
  389. }
  390. }