BaseController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. $user->userAuths()->create([
  38. 'type' => $type,
  39. 'identifier' => $info->getId(),
  40. 'credential' => $info->token,
  41. ]);
  42. return redirect()->route('profile')->with('successMsg', '绑定成功');
  43. }
  44. private function login(string $type, $info)
  45. {
  46. $user = User::whereUsername($info->getEmail())->first();
  47. if (! isset($user)) {
  48. $auth = UserOauth::whereType($type)->whereIdentifier($info->getId())->first();
  49. if (isset($auth)) {
  50. $user = $auth->user;
  51. }
  52. }
  53. if (isset($user)) {
  54. Auth::login($user);
  55. Helpers::userLoginAction($user, IP::getClientIp()); // 用户登录后操作
  56. return redirect()->route('login');
  57. }
  58. return redirect()->route('login')->withErrors(trans('auth.error.not_found_user'));
  59. }
  60. public function binding($type)
  61. {
  62. $info = Socialite::driver($type)->stateless()->user();
  63. if ($info) {
  64. $user = Auth::user();
  65. if ($user) {
  66. return $this->bind($type, $user, $info);
  67. }
  68. return redirect()->route('profile')->withErrors('绑定失败');
  69. }
  70. return redirect()->route('login')->withErrors('第三方登录失败!');
  71. }
  72. public function logining($type)
  73. {
  74. $info = Socialite::driver($type)->user();
  75. if ($info) {
  76. return $this->login($type, $info);
  77. }
  78. return redirect()->route('login')->withErrors('第三方登录失败!');
  79. }
  80. public function register($type)
  81. {
  82. $info = Socialite::driver($type)->stateless()->user();
  83. // 排除重复用户注册
  84. if ($info) {
  85. $user = User::whereUsername($info->getEmail())->first();
  86. if (! $user) {
  87. $user = UserOauth::whereIdentifier($info->getId())->first();
  88. if (! $user) {
  89. $user = Helpers::addUser($info->getEmail(), Str::random(), MB * ((int) sysConfig('default_traffic')), null, $user->getNickname());
  90. if ($user) {
  91. $user->userAuths()->create([
  92. 'type' => $type,
  93. 'identifier' => $info->getId(),
  94. 'credential' => $info->token,
  95. ]);
  96. Auth::login($user);
  97. return redirect()->route('login');
  98. }
  99. return redirect()->route('register')->withErrors('注册失败');
  100. }
  101. }
  102. return redirect()->route('login')->withErrors('已注册,请直接登录');
  103. }
  104. return redirect()->route('login')->withErrors('第三方登录失败!');
  105. }
  106. }