BaseController.php 4.2 KB

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