GroupController.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Http\Controllers\Syj;
  3. use App\Http\Controllers\Core\Auth;
  4. use App\Models\Syj\GroupModel;
  5. use App\Models\Syj\GroupMemberModel;
  6. use Illuminate\Http\Request;
  7. use App\Http\Controllers\Controller;
  8. class GroupController extends Controller
  9. {
  10. /**
  11. * 显示分配的群组
  12. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  13. */
  14. public function show(Request $request)
  15. {
  16. $uid = Auth::auth();
  17. if (!$group_user = GroupMemberModel::where('uid', $uid)->first()) {
  18. $group = GroupModel::where('state', '1')
  19. ->where('expired_at', '>', time())
  20. ->whereRaw("capacity_number > total_number")->first();
  21. if (!$group) {
  22. return response([
  23. 'code' => 404,
  24. 'message' => '暂时群已满员'
  25. ]);
  26. }
  27. $group->increment('total_number');
  28. $group->save();
  29. GroupMemberModel::create([
  30. 'uid' => $uid,
  31. 'group_id' => $group->id,
  32. ]);
  33. } else {
  34. $group = GroupModel::find($group_user->group_id);
  35. if ($group->expired_at < time()) {
  36. return response([
  37. 'code' => 410,
  38. 'message' => '当前已暂停入群'
  39. ]);
  40. }
  41. }
  42. return response([
  43. 'code' => 200,
  44. 'message' => 'OK',
  45. 'data' => $group
  46. ]);
  47. }
  48. }