BaseController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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)->stateless()->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 unsubscribe(string $type)
  63. {
  64. $user = Auth::user();
  65. if ($user && $user->userAuths()->whereType($type)->delete()) {
  66. return redirect()->route('profile')->with('successMsg', '解绑成功');
  67. }
  68. return redirect()->route('profile')->with('successMsg', '解绑失败');
  69. }
  70. public function binding($type)
  71. {
  72. $info = Socialite::driver($type)->stateless()->user();
  73. if ($info) {
  74. $user = Auth::user();
  75. if ($user) {
  76. return $this->bind($type, $user, $info);
  77. }
  78. return redirect()->route('profile')->withErrors('绑定失败');
  79. }
  80. return redirect()->route('login')->withErrors('第三方登录失败!');
  81. }
  82. public function logining($type)
  83. {
  84. $info = Socialite::driver($type)->stateless()->user();
  85. if ($info) {
  86. return $this->login($type, $info);
  87. }
  88. return redirect()->route('login')->withErrors('第三方登录失败!');
  89. }
  90. public function register($type)
  91. {
  92. $info = Socialite::driver($type)->stateless()->user();
  93. // 排除重复用户注册
  94. if ($info) {
  95. $user = User::whereUsername($info->getEmail())->first();
  96. if (! $user) {
  97. $user = UserOauth::whereIdentifier($info->getId())->first();
  98. if (! $user) {
  99. $user = Helpers::addUser($info->getEmail(), Str::random(), MB * ((int) sysConfig('default_traffic')), null, $user->getNickname());
  100. if ($user) {
  101. $user->userAuths()->create([
  102. 'type' => $type,
  103. 'identifier' => $info->getId(),
  104. 'credential' => $info->token,
  105. ]);
  106. Auth::login($user);
  107. return redirect()->route('login');
  108. }
  109. return redirect()->route('register')->withErrors('注册失败');
  110. }
  111. }
  112. return redirect()->route('login')->withErrors('已注册,请直接登录');
  113. }
  114. return redirect()->route('login')->withErrors('第三方登录失败!');
  115. }
  116. }