123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace App\Http\Controllers\OAuth;
- use App\Components\Helpers;
- use App\Components\IP;
- use App\Http\Controllers\Controller;
- use App\Models\User;
- use App\Models\UserOauth;
- use Auth;
- use Laravel\Socialite\Facades\Socialite;
- use Str;
- class BaseController extends Controller
- {
- public function route($type, $action = null)
- {
- if ($action === 'binding') {
- return Socialite::driver($type)->with(['redirect_uri' => route('oauth.bind', ['type' => $type])])->redirect();
- }
- if ($action === 'register') {
- return Socialite::driver($type)->with(['redirect_uri' => route('oauth.register', ['type' => $type])])->redirect();
- }
- return Socialite::driver($type)->with(['redirect_uri' => route('oauth.login', ['type' => $type])])->redirect();
- }
- public function simple(string $type)
- {
- $info = Socialite::driver($type)->user();
- if ($info) {
- $user = Auth::user();
- if ($user) {
- return $this->bind($type, $user, $info);
- }
- return $this->login($type, $info);
- }
- return redirect()->route('login')->withErrors('第三方登录失败!');
- }
- private function bind(string $type, $user, $info)
- {
- $user->userAuths()->create([
- 'type' => $type,
- 'identifier' => $info->getId(),
- 'credential' => $info->token,
- ]);
- return redirect()->route('profile')->with('successMsg', '绑定成功');
- }
- private function login(string $type, $info)
- {
- $user = User::whereUsername($info->getEmail())->first();
- if (! isset($user)) {
- $auth = UserOauth::whereType($type)->whereIdentifier($info->getId())->first();
- if (isset($auth)) {
- $user = $auth->user;
- }
- }
- if (isset($user)) {
- Auth::login($user);
- Helpers::userLoginAction($user, IP::getClientIp()); // 用户登录后操作
- return redirect()->route('login');
- }
- return redirect()->route('login')->withErrors(trans('auth.error.not_found_user'));
- }
- public function binding($type)
- {
- $info = Socialite::driver($type)->stateless()->user();
- if ($info) {
- $user = Auth::user();
- if ($user) {
- return $this->bind($type, $user, $info);
- }
- return redirect()->route('profile')->withErrors('绑定失败');
- }
- return redirect()->route('login')->withErrors('第三方登录失败!');
- }
- public function logining($type)
- {
- $info = Socialite::driver($type)->user();
- if ($info) {
- return $this->login($type, $info);
- }
- return redirect()->route('login')->withErrors('第三方登录失败!');
- }
- public function register($type)
- {
- $info = Socialite::driver($type)->stateless()->user();
- // 排除重复用户注册
- if ($info) {
- $user = User::whereUsername($info->getEmail())->first();
- if (! $user) {
- $user = UserOauth::whereIdentifier($info->getId())->first();
- if (! $user) {
- $user = Helpers::addUser($info->getEmail(), Str::random(), MB * ((int) sysConfig('default_traffic')), null, $user->getNickname());
- if ($user) {
- $user->userAuths()->create([
- 'type' => $type,
- 'identifier' => $info->getId(),
- 'credential' => $info->token,
- ]);
- Auth::login($user);
- return redirect()->route('login');
- }
- return redirect()->route('register')->withErrors('注册失败');
- }
- }
- return redirect()->route('login')->withErrors('已注册,请直接登录');
- }
- return redirect()->route('login')->withErrors('第三方登录失败!');
- }
- }
|