BaseController.php 4.3 KB

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