FeedsService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. <?php
  2. namespace App\Services\Api;
  3. use App\Http\Requests\FeedsRequest;
  4. use App\Models\Course;
  5. use App\Models\Feed;
  6. use App\Models\FeedComment;
  7. use App\Models\FeedForward;
  8. use App\Models\FeedLike;
  9. use App\Models\FeedReport;
  10. use App\Models\Game;
  11. use App\Models\GameUser;
  12. use App\Models\UserFollow;
  13. use App\Models\UserReport;
  14. use App\Services\ToolServer;
  15. use Illuminate\Http\Request;
  16. use PHPUnit\Util\Exception;
  17. use Illuminate\Support\Facades\DB;
  18. class FeedsService
  19. {
  20. //动态列表
  21. public function getList(Request $request)
  22. {
  23. $user = auth('api')->user();
  24. $type = $request->input('type', 1); // 1全部 2关注 3热门
  25. $page = $request->input('page', 1);
  26. $teamId = $request->input('team_id', 0);
  27. $userId = $request->input('user_id', 0);
  28. //注销或被删除的用户 User::query() 不生效 , 需 DB::table('users')
  29. $disableUserId = DB::table('users')->whereNotNull('deleted_at')->pluck('id');
  30. $disableUserId = $disableUserId->isNotEmpty() ? $disableUserId->toArray() : [];
  31. $feeds = Feed::query()
  32. ->with('user:id,name,avatar,tencent_im_user_id,is_auth')
  33. ->where('team_id', $teamId)
  34. ->whereNotIn('user_id', $disableUserId)
  35. ->whereNull('deleted_at');
  36. $desc = 'id';
  37. if (!empty($type) && $type > 1) {
  38. $followIds = UserFollow::query()->where(['user_id' => $user->id, 'status' => 1])->pluck('target_id');
  39. $followIds = $followIds->isNotEmpty() ? $followIds->toArray() : [];
  40. if ($type == 2) {
  41. $feeds = $feeds->whereIn('user_id', $followIds);
  42. } elseif ($type == 3) { //热门, 只有未关注的,不显示已关注的人发的
  43. $desc = 'like_num';
  44. $feeds = $feeds->whereNotIn('user_id', $followIds);
  45. }
  46. }
  47. if (!empty($userId)) {
  48. $feeds->where('user_id', $userId);
  49. }
  50. $limit = 10;
  51. $offset = ($page - 1) * $limit;
  52. $feeds = $feeds->orderByDesc($desc)->limit($limit)->offset($offset)->get();
  53. if ($feeds->isEmpty()) {
  54. return $data = [
  55. 'stop' => 1,
  56. 'data' => [],
  57. ];
  58. }
  59. $feeds = $feeds->toArray();
  60. foreach ($feeds as &$v) {
  61. $v['have_user_course'] = UserService::haveUserCourse($v['user_id']);
  62. //查询评论数量
  63. $v['commemt_num'] = FeedComment::query()->where(['feed_id' => $v['id'], 'pid' => 0])->count();
  64. $v['file_url'] = json_decode($v['file_url'], true);
  65. $v['time'] = ToolServer::uc_time_ago(strtotime($v['created_at']));
  66. if ($user) {
  67. $v['is_like'] = self::isLike($v['id']);
  68. $v['is_follow'] = self::isFollow($v['user_id']);
  69. } else {
  70. $v['is_like'] = 0;
  71. $v['is_follow'] = 0;
  72. }
  73. //查询转发的原动态
  74. if ($v['forward_id'] > 0) {
  75. $v['forward_feed'] = $this->getDetail($v['forward_id'], 2);
  76. }
  77. //查询记分卡
  78. $v['game_score'] = null;
  79. if ($v['game_per_id'] > 0) {
  80. $scoreCard = self::queryScoreCard($v['game_per_id']);
  81. $v['game_info'] = $scoreCard['game_info'];
  82. $v['game_score'] = $scoreCard['game_score'];
  83. }
  84. }
  85. //修改消息状态为已读
  86. if (!empty($user)) {
  87. MessageService::changeStatus($user->id, [9]);
  88. }
  89. return $data = [
  90. 'stop' => 0,
  91. 'data' => $feeds,
  92. ];;
  93. }
  94. //用户动态列表
  95. public function userFeeds($user_id, $team_id = 0)
  96. {
  97. $user = auth('api')->user();
  98. $feeds = Feed::query()
  99. ->with(["user:id,name,avatar,tencent_im_user_id,is_auth"])
  100. ->where('user_id', $user_id)
  101. ->where('team_id', $team_id)
  102. ->whereNull('deleted_at')
  103. ->orderByDesc('id')
  104. ->paginate(request('perPage', 20));
  105. $feeds = $feeds->toArray();
  106. foreach ($feeds['data'] as &$v) {
  107. $v['have_user_course'] = UserService::haveUserCourse($v['user_id']);
  108. $v['file_url'] = json_decode($v['file_url'], true);
  109. $v['time'] = ToolServer::uc_time_ago(strtotime($v['created_at']));
  110. if ($user) {
  111. $v['is_like'] = self::isLike($v['id']);
  112. $v['is_follow'] = self::isFollow($v['user_id']);
  113. } else {
  114. $v['is_like'] = 0;
  115. $v['is_follow'] = 0;
  116. }
  117. //查询评论数量
  118. $v['commemt_num'] = FeedComment::query()->where(['feed_id' => $v['id'], 'pid' => 0])->count();
  119. //查询转发的原动态
  120. if ($v['forward_id'] > 0) {
  121. $v['forward_feed'] = $this->getDetail($v['forward_id'], 2);
  122. }
  123. //查询记分卡
  124. $v['game_score'] = null;
  125. if ($v['game_per_id'] > 0) {
  126. $scoreCard = self::queryScoreCard($v['game_per_id']);
  127. $v['game_info'] = $scoreCard['game_info'];
  128. $v['game_score'] = $scoreCard['game_score'];
  129. }
  130. }
  131. return $feeds;
  132. }
  133. //是否点赞
  134. public static function isLike($feedId)
  135. {
  136. $user = auth('api')->user();
  137. if (empty($user)) {
  138. return 0;
  139. }
  140. $info = FeedLike::query()->where([
  141. 'user_id' => $user->id,
  142. 'feed_id' => $feedId,
  143. 'status' => 1,
  144. ])->first();
  145. return $info ? 1 : 0;
  146. }
  147. //是否关注
  148. public static function isFollow($userId)
  149. {
  150. $user = auth('api')->user();
  151. $info = UserFollow::query()->where([
  152. 'user_id' => $user->id,
  153. 'target_id' => $userId,
  154. 'status' => 1
  155. ])->first();
  156. return $info ? 1 : 0;
  157. }
  158. //查询积分卡
  159. public static function queryScoreCard($game_user_id)
  160. {
  161. $gameId = GameUser::where('id', $game_user_id)->value('game_id');
  162. $gameInfo = Game::query()->where('id', $gameId)->whereNull('deleted_at')->first();
  163. if($gameInfo){
  164. $gameInfo = $gameInfo->toArray();
  165. $gameInfo['course_img'] = Course::find($gameInfo['course_id'])->img;
  166. }else{
  167. $gameInfo = null;
  168. }
  169. return $scoreCard = [
  170. 'game_info' => $gameInfo,
  171. 'game_score' => GameService::getGameUserScore($game_user_id)
  172. ];
  173. }
  174. //动态详情
  175. public function getDetail($feed_id, $type = 1)
  176. {
  177. if (empty($feed_id)) {
  178. throw new Exception(trans('api.PARAMS_ERROR'));
  179. }
  180. $feed = Feed::query()
  181. ->with(['user:id,name,avatar,tencent_im_user_id,is_auth'])
  182. ->where(['id' => $feed_id])
  183. ->whereNull('deleted_at')
  184. ->first();
  185. if (!$feed) {
  186. throw new Exception(trans('api.FEED_NOT_EXIST'));
  187. }
  188. //查询评论数量
  189. $feed['commemt_num'] = FeedComment::query()->where(['feed_id' => $feed['id'], 'pid' => 0])->count();
  190. $feed['file_url'] = json_decode($feed['file_url'], true);
  191. $user = auth('api')->user();
  192. if ($user) {
  193. //是否点赞
  194. $feed['is_like'] = FeedLike::query()->where([
  195. 'user_id' => $user->id,
  196. 'status' => 1,
  197. 'feed_id' => $feed['id']
  198. ])->first() ? 1 : 0;
  199. //是否关注
  200. $feed['is_follow'] = UserFollow::query()->where([
  201. 'user_id' => $user->id,
  202. 'target_id' => $feed['user_id'],
  203. 'status' => 1
  204. ])->first() ? 1 : 0;
  205. }
  206. //点赞列表
  207. $feed['like_list'] = $this->getFeedLikeList($feed['id']);
  208. //转发列表
  209. $feed['forward_list'] = $this->getFeedForwardList($feed['id']);
  210. if ($feed['forward_id'] > 0 && $type == 1) {
  211. $feed['forward_feed'] = $this->getDetail($feed['forward_id']);
  212. }
  213. //查询记分卡
  214. if ($feed['game_per_id'] > 0) {
  215. $game_id = GameUser::query()->where(['id' => $feed['game_per_id']])->value('game_id');
  216. $feed['game_info'] = Game::query()->where('id', $game_id)->first()->toArray() ?: null;
  217. $game_service = new GameService();
  218. $feed['game_score'] = $game_service->getGameUserScore($feed['game_per_id']);
  219. } else {
  220. $feed['game_score'] = null;
  221. }
  222. //先查询直接评论
  223. $comment = FeedComment::query()
  224. ->with(['user:id,name,avatar,tencent_im_user_id,is_auth', 'to_user:id,name,avatar,tencent_im_user_id,is_auth'])
  225. ->where(['feed_id' => $feed['id'], 'pid' => 0])
  226. ->orderByDesc('id')
  227. ->get()
  228. ->toArray();
  229. //根据评论查询回复内容
  230. $comment_arr = array();
  231. if (count($comment) > 0) {
  232. foreach ($comment as $k => $v) {
  233. $comment[$k]['user']['have_user_course'] = UserService::haveUserCourse($v['user']['id']);
  234. $comment[$k]['to_user']['have_user_course'] = UserService::haveUserCourse($v['to_user']['id']);
  235. $comment[$k]['son'] = $this->get_son_comment($feed['id'], $v['id'], $comment_arr);
  236. }
  237. }
  238. $feed['comment_list'] = $comment;
  239. return $feed;
  240. }
  241. //获取点赞列表
  242. public function getFeedLikeList($feed_id)
  243. {
  244. $list = FeedLike::query()
  245. ->leftJoin('users as u', 'feed_like.user_id', '=', 'u.id')
  246. ->where('feed_like.feed_id', $feed_id)
  247. ->where('feed_like.status', 1)
  248. ->select(['u.id', 'u.avatar', 'u.name', 'u.is_auth'])
  249. ->get();
  250. if ($list->isEmpty()) {
  251. return [];
  252. }
  253. foreach ($list as $key => &$val) {
  254. $val['have_user_course'] = UserService::haveUserCourse($val['id']);
  255. }
  256. return $list;
  257. }
  258. //获取评论列表
  259. public function getFeedForwardList($feed_id)
  260. {
  261. $list = FeedForward::query()
  262. ->leftJoin('users as u', 'feed_forward.user_id', '=', 'u.id')
  263. ->where('feed_forward.feed_id', $feed_id)
  264. ->select(['u.id', 'u.avatar', 'u.name', 'u.is_auth'])
  265. ->get();
  266. if ($list->isEmpty()) {
  267. return [];
  268. }
  269. foreach ($list as $key => &$val) {
  270. $val['have_user_course'] = UserService::haveUserCourse($val['id']);
  271. }
  272. return $list;
  273. }
  274. //查询子评论
  275. public function get_son_comment($feed_id, $pid, $arr)
  276. {
  277. $comment = FeedComment::query()
  278. ->with(['user:id,name,avatar,tencent_im_user_id,is_auth', 'to_user:id,name,avatar,tencent_im_user_id,is_auth'])
  279. ->where(['feed_id' => $feed_id, 'pid' => $pid])
  280. ->get()
  281. ->toArray();
  282. $arr = array_merge($arr, $comment);
  283. if (!empty($comment) && is_array($comment)) {
  284. foreach ($comment as $k => $v) {
  285. $v['user']['have_user_course'] = UserService::haveUserCourse($v['user']['id']);
  286. $comment[$k] = $v;
  287. if (FeedComment::query()->where(['feed_id' => $feed_id, 'pid' => $v['id']])->count() > 0) {
  288. $arr = $this->get_son_comment($feed_id, $v['id'], $arr);
  289. }
  290. }
  291. }
  292. return $arr;
  293. }
  294. //发布动态
  295. public function release(FeedsRequest $feedsRequest)
  296. {
  297. $user = auth('api')->user();
  298. $feed = array();
  299. $feed['user_id'] = $user->id;
  300. $feed['content'] = $feedsRequest->post('content') ?: '';
  301. $feed['file_url'] = !empty($feedsRequest->post('file_url')) ? json_encode($feedsRequest->post('file_url')) : '';
  302. $feed['address_info'] = !empty($feedsRequest->post('address_info')) ? $feedsRequest->post('address_info') : '';
  303. $feed['match_title'] = $feedsRequest->post('match_title');
  304. $game_id = $feedsRequest->post('game_per_id');
  305. if (!empty($game_id)) {
  306. $game_user = GameUser::query()->where(['game_id' => $game_id, 'user_id' => $user->id])->first();
  307. $feed['game_per_id'] = $game_user->id;
  308. }
  309. $feed['team_id'] = $feedsRequest->post('team_id', 0);
  310. $feed['score_card_bg'] = $feedsRequest->post('score_card_bg') ?: asset('static/img/df_score_card_bg.png');
  311. if (Feed::query()->create($feed)) {
  312. //给关注我的用户极光推送
  313. $myFansIds = $this->getMyFans();
  314. if (!empty($myFansIds)) {
  315. //给用户添加消息记录
  316. $notice = NoticeService::getInstance();;
  317. foreach ($myFansIds as $userId) {
  318. $notice->send_msg($userId, '', 9, $user->id, 'api.PUBLISH_FEED_NEWS', 0);
  319. }
  320. }
  321. return true;
  322. } else {
  323. throw new Exception(trans('api.SAVE_ERROR'));
  324. }
  325. }
  326. //关注我的
  327. public function getMyFans()
  328. {
  329. $user = auth('api')->user();
  330. $userIds = (new UserFollow())->setTable('uf')->from('user_follows as uf')
  331. ->join('users as u', 'u.id', '=', 'uf.user_id')
  332. ->where('uf.target_id', $user->id)
  333. ->where('u.status', 1)
  334. ->where('uf.status', 1)//0取消关注,1关注
  335. ->pluck('uf.user_id')
  336. ->toArray();
  337. if (!empty($userIds)) {
  338. $userIds = array_unique($userIds);
  339. } else {
  340. $userIds = [];
  341. }
  342. return $userIds;
  343. }
  344. //点赞 如果已经点赞 则取消点赞
  345. public function like(Request $request)
  346. {
  347. $user = auth('api')->user();
  348. $feed_id = $request->post('feed_id');
  349. if (empty($feed_id)) {
  350. throw new Exception(trans('api.PARAMS_ERROR'));
  351. }
  352. $feed = Feed::query()->where(['id' => $feed_id])->whereNull('deleted_at')->first();
  353. if (!$feed) {
  354. throw new Exception(trans('api.FEED_NOT_EXIST'));
  355. }
  356. if ($feed_like = FeedLike::query()->where(['user_id' => $user->id, 'feed_id' => $feed_id])->first()) { //已点赞
  357. if ($feed_like->status == 1) {
  358. //已点赞 取消点赞
  359. Feed::query()->where('id', $feed_id)->decrement('like_num', 1);
  360. $feed_like->status = 2;
  361. $feed_like->save();
  362. $like = 0;
  363. } else {
  364. Feed::query()->where('id', $feed_id)->increment('like_num', 1);
  365. $feed_like->status = 1;
  366. $feed_like->created_at = date('Y-m-d H:i:s');
  367. $feed_like->save();
  368. $like = 1;
  369. }
  370. } else {
  371. //点赞
  372. Feed::query()->where('id', $feed_id)->increment('like_num', 1);
  373. $feed_like_id = FeedLike::query()->insertGetId([
  374. 'user_id' => $user->id,
  375. 'feed_id' => $feed_id,
  376. 'status' => 1,
  377. 'created_at' => date('Y-m-d H:i:s'),
  378. 'updated_at' => date('Y-m-d H:i:s')
  379. ]);
  380. $like = 1;
  381. //给用户添加消息记录
  382. if ($feed->user_id != $user->id) { //自己给自己点赞不发通知
  383. $notice = NoticeService::getInstance();;
  384. $notice->send_msg($feed->user_id, $feed_like_id, 2, $user->id, 'api.PRAISED_YOUR_FEEDS', 0);
  385. }
  386. }
  387. return $like;
  388. }
  389. //删除动态
  390. public function del(Request $request)
  391. {
  392. $feed_id = $request->post('feed_id');
  393. $user = auth('api')->user();
  394. if (empty($feed_id)) {
  395. throw new Exception(trans('api.PARAMS_ERROR'));
  396. }
  397. $feed = Feed::query()->where(['id' => $feed_id, 'user_id' => $user->id])->whereNull('deleted_at')->first();
  398. if (!$feed) {
  399. throw new Exception(trans('api.FEED_NOT_EXIST'));
  400. }
  401. $feed->deleted_at = date('Y-m-d H:i:s');
  402. $feed->save();
  403. return true;
  404. }
  405. //动态评论
  406. public function comment(Request $request)
  407. {
  408. $user = auth('api')->user();
  409. $feed_id = $request->post('feed_id');
  410. $content = $request->post('content');
  411. $pid = $request->post('pid');
  412. if (empty($content)) {
  413. throw new Exception(trans('api.CONTENT_NOT_NULL'));
  414. }
  415. if (empty($feed_id)) {
  416. throw new Exception(trans('api.PARAMS_ERROR'));
  417. }
  418. $feed = Feed::query()->where(['id' => $feed_id])->whereNull('deleted_at')->first();
  419. if (!$feed) {
  420. throw new Exception(trans('api.FEED_NOT_EXIST'));
  421. }
  422. if ($pid > 0) {
  423. $comment = FeedComment::query()->where(['id' => $pid])->first();
  424. $to_uid = $comment->user_id;
  425. } else {
  426. $to_uid = $feed->user_id;
  427. }
  428. $comment_id = FeedComment::query()->insertGetId([
  429. 'feed_id' => $feed_id,
  430. 'user_id' => $user->id,
  431. 'content' => $content,
  432. 'pid' => $pid,
  433. 'to_uid' => $to_uid,
  434. 'created_at' => date('Y-m-d H:i:s'),
  435. 'updated_at' => date('Y-m-d H:i:s')
  436. ]);
  437. //如果自己给自己评论 则不发消息
  438. if ($to_uid != $user->id) {
  439. $notice = NoticeService::getInstance();;
  440. $notice->send_msg($to_uid, $comment_id, 3, $user->id, 'api.COMMENTED_ON_YOUR_FEEDS', 0);
  441. }
  442. return true;
  443. }
  444. //举报动态
  445. public function report(Request $request)
  446. {
  447. $feed_id = $request->post('feed_id');
  448. $content = $request->post('content');
  449. $user = auth('api')->user();
  450. if (empty($feed_id)) {
  451. throw new Exception(trans('api.PARAMS_ERROR'));
  452. }
  453. $feed = Feed::query()->where(['id' => $feed_id])->whereNull('deleted_at')->first();
  454. if (!$feed) {
  455. throw new Exception(trans('api.FEED_NOT_EXIST'));
  456. }
  457. $report = new UserReport();
  458. $report->user_id = $user->id;
  459. $report->type = 1;
  460. $report->to_id = $feed_id;
  461. $report->content = $content;
  462. $report->save();
  463. return true;
  464. }
  465. }