RoomController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace App\Http\Controllers\Goodnight;
  3. use App\Http\Resources\Goodnight\RoomCollection;
  4. use App\Http\Resources\Goodnight\RoomMemberCollection;
  5. use App\Http\Resources\Goodnight\RoomMessageCollection;
  6. use App\Http\Resources\Goodnight\RoomResource;
  7. use App\Models\User\UserModel;
  8. use App\Models\Goodnight\EnterModel;
  9. use App\Models\Goodnight\RoomMemberModel;
  10. use App\Models\Goodnight\RoomMessageModel;
  11. use App\Services\Goodnight\NoticeService;
  12. use Illuminate\Http\Request;
  13. use App\Http\Controllers\Controller;
  14. use App\Http\Controllers\Miniprogram\Auth;
  15. use App\Models\Goodnight\RoomModel;
  16. class RoomController extends Controller
  17. {
  18. //
  19. public function index(Request $request)
  20. {
  21. $uid = Auth::auth();
  22. $builder = RoomModel::whereHas('members', function ($query) use ($uid) {
  23. return $query->where('uid', $uid);
  24. });
  25. $rooms = $builder->orderBy('updated_at', 'desc')->paginate($request->get('per_page', 20));
  26. foreach ($rooms as $room) {
  27. $room->lastMessage;
  28. $room->unread_cnt = RoomMessageModel::where('room_id', $room->id)->where(
  29. 'uid',
  30. '<>',
  31. $uid
  32. )->where('read_at', 0)->count();
  33. foreach ($room->members as $member) {
  34. $member->user_info = UserModel::select('nickname', 'headimgurl', 'sex')->find($member->uid);
  35. }
  36. }
  37. return new RoomCollection($rooms);
  38. }
  39. public function show(Request $request, int $id)
  40. {
  41. $uid = Auth::auth();
  42. $room = RoomModel::findOrFail($id);
  43. foreach ($room->members as $member) {
  44. $member->user_info = UserModel::select('nickname', 'headimgurl', 'sex')->find($member->uid);
  45. $member->enter = EnterModel::where('uid', $member->uid)->where('room_id', $id)->first();
  46. }
  47. $room->unread_cnt = RoomMessageModel::where('room_id', $room->id)->where('uid', '<>', $uid)->where(
  48. 'read_at',
  49. 0
  50. )->count();
  51. $room->lastMessage;
  52. $room->activity;
  53. return new RoomResource($room);
  54. }
  55. public function messages(Request $request, $id)
  56. {
  57. $uid = Auth::auth();
  58. $room = RoomModel::findOrFail($id);
  59. $builder = RoomMessageModel::where('room_id', $id);
  60. $messages = $builder->orderBy('id', 'desc')->paginate($request->get('per_page', 20));
  61. $builder->where('read_at', 0)->where('uid', '<>', $uid)->update(['read_at' => time()]);
  62. return new RoomMessageCollection($messages);
  63. }
  64. public function members(Request $request, $id)
  65. {
  66. $room = RoomModel::findOrFail($id);
  67. $members = RoomMemberModel::where('room_id', $id)->orderBy('id', 'desc')->get();
  68. foreach ($members as $member) {
  69. $member->user_info = UserModel::select('nickname', 'headimgurl', 'sex')->find($member->uid);
  70. }
  71. return new RoomMemberCollection($members);
  72. }
  73. public function createMessage(Request $request, $id)
  74. {
  75. $this->validate($request, [
  76. 'content' => 'required',
  77. ]);
  78. $type = $request->input('type', 'text');
  79. $uid = Auth::auth();
  80. $room = RoomModel::findOrFail($id);
  81. $message = RoomMessageModel::create([
  82. 'uid' => $uid,
  83. 'type' => $type,
  84. 'content' => $request->input('content'),
  85. 'room_id' => $id,
  86. ]);
  87. $room->update(['interacted_at' => time()]);
  88. try {
  89. $to_uid = RoomMemberModel::where('uid', '<>', $uid)->where('room_id', $room->id)->value('uid');
  90. $ns = new NoticeService();
  91. $ns->receiveRoomComment($to_uid, $uid);
  92. } catch (\Exception $e) {
  93. app('sentry')->captureException($e);
  94. }
  95. return response()->json([
  96. 'code' => 200,
  97. 'message' => 'OK',
  98. 'data' => $message
  99. ]);
  100. }
  101. public function createTask(Request $request, $id)
  102. {
  103. $uid = Auth::auth();
  104. $roomMember = RoomMemberModel::where('uid', $uid)->where('room_id', $id)->firstOrFail();
  105. $roomMember->update($request->all());
  106. return response()->json([
  107. 'code' => 200,
  108. 'message' => 'OK',
  109. ]);
  110. }
  111. public function partners(Request $request)
  112. {
  113. $uid = Auth::auth();
  114. $builder = RoomMemberModel::whereIn(
  115. 'room_id',
  116. RoomModel::whereHas('members', function ($query) use ($uid) {
  117. $query->where('uid', $uid);
  118. })->orderBy('updated_at', 'desc')->pluck('id')
  119. )->where('uid', '<>', $uid);
  120. $total = $builder->count();
  121. $members = $builder->orderBy('id', 'desc')->limit(4)->get();
  122. foreach ($members as $member) {
  123. $member->user_info = UserModel::select('nickname', 'headimgurl', 'sex')->find($member->uid);
  124. }
  125. return response()->json([
  126. 'code' => 200,
  127. 'message' => 'OK',
  128. 'data' => $members,
  129. 'meta' => [
  130. 'total' => $total,
  131. ]
  132. ]);
  133. }
  134. }