RoomController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace App\Http\Controllers\Gteam;
  3. use App\Http\Controllers\Miniprogram\Auth;
  4. use App\Http\Resources\Gteam\RoomResource;
  5. use App\Models\Gteam\CommentModel;
  6. use App\Models\Gteam\RoomModel;
  7. use App\Models\Gteam\RoomMemberModel;
  8. use App\Models\Gteam\UserModel;
  9. use App\Models\PartnerModel;
  10. use Illuminate\Http\Request;
  11. use App\Http\Controllers\Controller;
  12. class RoomController extends Controller
  13. {
  14. //
  15. private $type = [
  16. "wz" => ["wz_wx", "wz_qq"],
  17. "cj" => ["cj_wx", "cj_qq"],
  18. ];
  19. private $gametype = [
  20. "cj_wx" => [
  21. "game" => "cj",
  22. "area" => "wx",
  23. ],
  24. "cj_qq" => [
  25. "game" => "cj",
  26. "area" => "qq",
  27. ],
  28. "wz_wx" => [
  29. "game" => "wz",
  30. "area" => "wx",
  31. ],
  32. "wz_qq" => [
  33. "game" => "wz",
  34. "area" => "qq",
  35. ],
  36. ];
  37. public function index(Request $request)
  38. {
  39. $uid = Auth::auth();
  40. $this->validate($request, [
  41. 'type' => 'required|in:wz,cj',
  42. ]);
  43. $builder = RoomModel::whereIn(
  44. 'id',
  45. RoomMemberModel::where('uid', $uid)->whereIn('type', $this->type[$request->type])->pluck('room_id')
  46. );
  47. $rooms = $builder->orderBy('created_at', 'desc')->paginate($request->get('per_page', 20));
  48. foreach ($rooms as $room) {
  49. $room->game = $this->gametype[$room->type]['game'];
  50. $room->area = $this->gametype[$room->type]['area'];
  51. foreach ($room->members as $member) {
  52. $user = $member->user;
  53. $user_info = \App\Models\User\UserModel::select('sex', 'nickname', 'headimgurl')->where(
  54. 'uid',
  55. $user->uid
  56. )->first();
  57. $user->nickname = $user_info->nickname;
  58. $user->headimgurl = $user_info->headimgurl;
  59. $user->sex = $user_info->sex;
  60. }
  61. }
  62. return response()->json([
  63. 'code' => 200,
  64. 'data' => $rooms
  65. ]);
  66. }
  67. public function show(Request $request, int $id)
  68. {
  69. $room = RoomModel::findOrFail($id);
  70. $room->game = $this->gametype[$room->type]['game'];
  71. $room->area = $this->gametype[$room->type]['area'];
  72. $members = $room->members;
  73. foreach ($members as $member) {
  74. $user = UserModel::find($member->uid);
  75. $user_info = \App\Models\User\UserModel::find($member->uid);
  76. $comments = CommentModel::where(['uid' => $member->uid, 'room_id' => $room->id])->get();
  77. $member->comments = $comments;
  78. $member->is_comment = $comments ? 1 : 0;
  79. $member->nickname = $user_info->nickname;
  80. $member->headimgurl = $user_info->headimgurl;
  81. $member->login_at = $user->login_at;
  82. $member->weixin = $user->weixin;
  83. $member->qq = $user->qq;
  84. $member->credit = $user->credit;
  85. $member->succ_cnt = $user->succ_cnt;
  86. $member->friend_cnt = $user->friend_cnt;
  87. $member->cj_wx_rank = $user->cj_wx_rank;
  88. $member->cj_qq_rank = $user->cj_qq_rank;
  89. $member->wz_wx_rank = $user->wz_wx_rank;
  90. $member->wz_qq_rank = $user->wz_qq_rank;
  91. $member->partner_id = PartnerModel::where('uid', $member->uid)->value('id');
  92. }
  93. $room->users = $members;
  94. return new RoomResource($room);
  95. }
  96. }