BaseController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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\UserLoginLog;
  8. use App\Models\UserOauth;
  9. use Auth;
  10. use Illuminate\Http\Request;
  11. use Laravel\Socialite\Facades\Socialite;
  12. use Log;
  13. use Redirect;
  14. use Str;
  15. class BaseController extends Controller
  16. {
  17. private $type;
  18. public function route($type, $action = null)
  19. {
  20. $this->type = $type;
  21. if ($action === 'binding') {
  22. return Socialite::driver($this->type)->with(['redirect_uri' => route('oauth.bind', ['type' => $type])])->redirect();
  23. }
  24. if ($action === 'register') {
  25. return Socialite::driver($this->type)->with(['redirect_uri' => route('oauth.register', ['type' => $type])])->redirect();
  26. }
  27. return Socialite::driver($this->type)->with(['redirect_uri' => route('oauth.redirect', ['type' => $type])])->redirect();
  28. }
  29. public function redirect()
  30. {
  31. $info = Socialite::driver($this->type)->user();
  32. if ($info) {
  33. $user = User::whereUsername($info->getEmail())->first();
  34. if (! $user) {
  35. $user = UserOauth::whereIdentifier($info->getId())->first();
  36. if ($user) {
  37. $user = $user->user;
  38. }
  39. }
  40. }
  41. if (isset($user)) {
  42. Auth::login($user);
  43. // 写入登录日志
  44. $this->addUserLoginLog($user->id, IP::getClientIp());
  45. // 更新登录信息
  46. $user->update(['last_login' => time()]);
  47. return Redirect::route('login');
  48. }
  49. return Redirect::route('login')->withErrors(trans('auth.error.not_found_user'));
  50. }
  51. /**
  52. * 添加用户登录日志.
  53. *
  54. * @param int $userId 用户ID
  55. * @param string $ip IP地址
  56. */
  57. private function addUserLoginLog(int $userId, string $ip): void
  58. {
  59. $ipLocation = IP::getIPInfo($ip);
  60. if (empty($ipLocation) || empty($ipLocation['country'])) {
  61. Log::warning(trans('error.get_ip').':'.$ip);
  62. }
  63. $log = new UserLoginLog();
  64. $log->user_id = $userId;
  65. $log->ip = $ip;
  66. $log->country = $ipLocation['country'] ?? '';
  67. $log->province = $ipLocation['province'] ?? '';
  68. $log->city = $ipLocation['city'] ?? '';
  69. $log->county = $ipLocation['county'] ?? '';
  70. $log->isp = $ipLocation['isp'] ?? ($ipLocation['organization'] ?? '');
  71. $log->area = $ipLocation['area'] ?? '';
  72. $log->save();
  73. }
  74. public function bbind()
  75. {
  76. $user = Auth::getUser();
  77. $info = Socialite::driver($this->type)->stateless()->user();
  78. if ($user) {
  79. if ($info) {
  80. $user->userAuths()->create([
  81. 'type' => $this->type,
  82. 'identifier' => $info->getId(),
  83. 'credential' => $info->token,
  84. ]);
  85. return redirect()->route('profile')->with('successMsg', '绑定成功');
  86. }
  87. return redirect()->route('profile')->withErrors('绑定失败');
  88. }
  89. return redirect()->route('profile')->withErrors('无用户');
  90. }
  91. public function register()
  92. {
  93. $info = Socialite::driver($this->type)->stateless()->user();
  94. // 排除重复用户注册
  95. if ($info) {
  96. $user = User::whereUsername($info->getEmail())->first();
  97. if (! $user) {
  98. $user = UserOauth::whereIdentifier($info->getId())->first();
  99. if (! $user) {
  100. $user = Helpers::addUser($info->getEmail(), Str::random(), MB * ((int) sysConfig('default_traffic')), null, $user->getNickname());
  101. if ($user) {
  102. $user->userAuths()->create([
  103. 'type' => $this->type,
  104. 'identifier' => $info->getId(),
  105. 'credential' => $info->token,
  106. ]);
  107. Auth::login($user);
  108. return redirect()->route('login');
  109. }
  110. return redirect()->route('register')->withErrors('注册失败');
  111. }
  112. }
  113. return redirect()->route('login')->withErrors('已注册,请直接登录');
  114. }
  115. return redirect()->route('register')->withErrors('绑定失败');
  116. }
  117. }