BaseController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Http\Controllers\OAuth;
  3. use App\Components\Helpers;
  4. use App\Components\IP;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\User;
  7. use App\Models\UserOauth;
  8. use Auth;
  9. use Illuminate\Http\Request;
  10. use Laravel\Socialite\Facades\Socialite;
  11. use Str;
  12. class BaseController extends Controller
  13. {
  14. public function route(Request $request, string $type)
  15. {
  16. $action = $request->input('action');
  17. if ($action === 'binding') {
  18. return Socialite::driver($type)->with(['redirect_uri' => route('oauth.bind', ['type' => $type])])->redirect();
  19. }
  20. if ($action === 'register') {
  21. return Socialite::driver($type)->with(['redirect_uri' => route('oauth.register', ['type' => $type])])->redirect();
  22. }
  23. return Socialite::driver($type)->with(['redirect_uri' => route('oauth.login', ['type' => $type])])->redirect();
  24. }
  25. public function simple(string $type)
  26. {
  27. $info = Socialite::driver($type)->stateless()->user();
  28. if ($info) {
  29. $user = Auth::user();
  30. if ($user) {
  31. return $this->bind($type, $user, $info);
  32. }
  33. return $this->login($type, $info);
  34. }
  35. return redirect()->route('login')->withErrors('第三方登录失败!');
  36. }
  37. private function bind(string $type, $user, $info)
  38. {
  39. $auth = $user->userAuths()->whereType($type)->first();
  40. $data = ['type' => $type, 'identifier' => $info->getId(), 'credential' => $info->token];
  41. if ($auth) {
  42. $user->userAuths()->whereType($type)->update($data);
  43. return redirect()->route('profile')->with('successMsg', '重新绑定成功');
  44. }
  45. $user->userAuths()->create($data);
  46. return redirect()->route('profile')->with('successMsg', '绑定成功');
  47. }
  48. private function login(string $type, $info)
  49. {
  50. $user = User::whereUsername($info->getEmail())->first();
  51. if (! isset($user)) {
  52. $auth = UserOauth::whereType($type)->whereIdentifier($info->getId())->first();
  53. if (isset($auth)) {
  54. $user = $auth->user;
  55. }
  56. }
  57. if (isset($user)) {
  58. Auth::login($user);
  59. Helpers::userLoginAction($user, IP::getClientIp()); // 用户登录后操作
  60. return redirect()->route('login');
  61. }
  62. return redirect()->route('login')->withErrors(trans('auth.error.not_found_user'));
  63. }
  64. public function unsubscribe(string $type)
  65. {
  66. $user = Auth::user();
  67. if ($user && $user->userAuths()->whereType($type)->delete()) {
  68. return redirect()->route('profile')->with('successMsg', '解绑成功');
  69. }
  70. return redirect()->route('profile')->with('successMsg', '解绑失败');
  71. }
  72. public function binding($type)
  73. {
  74. $info = Socialite::driver($type)->stateless()->user();
  75. if ($info) {
  76. $user = Auth::user();
  77. if ($user) {
  78. return $this->bind($type, $user, $info);
  79. }
  80. return redirect()->route('profile')->withErrors('绑定失败');
  81. }
  82. return redirect()->route('login')->withErrors('第三方登录失败!');
  83. }
  84. public function logining($type)
  85. {
  86. $info = Socialite::driver($type)->stateless()->user();
  87. if ($info) {
  88. return $this->login($type, $info);
  89. }
  90. return redirect()->route('login')->withErrors('第三方登录失败!');
  91. }
  92. public function register($type)
  93. {
  94. $info = Socialite::driver($type)->stateless()->user();
  95. // 排除重复用户注册
  96. if ($info) {
  97. $user = User::whereUsername($info->getEmail())->first();
  98. if (! $user) {
  99. $user = UserOauth::whereIdentifier($info->getId())->first();
  100. if (! $user) {
  101. $user = Helpers::addUser($info->getEmail(), Str::random(), MB * ((int) sysConfig('default_traffic')), null, $user->getNickname());
  102. if ($user) {
  103. $user->userAuths()->create([
  104. 'type' => $type,
  105. 'identifier' => $info->getId(),
  106. 'credential' => $info->token,
  107. ]);
  108. Auth::login($user);
  109. return redirect()->route('login');
  110. }
  111. return redirect()->route('register')->withErrors('注册失败');
  112. }
  113. }
  114. return redirect()->route('login')->withErrors('已注册,请直接登录');
  115. }
  116. return redirect()->route('login')->withErrors('第三方登录失败!');
  117. }
  118. }