Login.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\SystemAdmin;
  4. use app\common\controller\AdminController;
  5. use app\common\utils\Helper;
  6. use think\captcha\facade\Captcha;
  7. use think\db\exception\DataNotFoundException;
  8. use think\db\exception\DbException;
  9. use think\db\exception\ModelNotFoundException;
  10. use app\Request;
  11. use think\Response;
  12. use Wolfcode\RateLimiting\Attributes\RateLimitingMiddleware;
  13. class Login extends AdminController
  14. {
  15. protected bool $ignoreLogin = true;
  16. public function initialize(): void
  17. {
  18. parent::initialize();
  19. $action = $this->request->action();
  20. if (!empty($this->adminUid) && !in_array($action, ['out'])) {
  21. $adminModuleName = config('admin.alias_name');
  22. $this->success('已登录,无需再次登录', [], __url("@{$adminModuleName}"));
  23. }
  24. }
  25. /**
  26. * 用户登录
  27. * @param Request $request
  28. * @return string
  29. * @throws DataNotFoundException
  30. * @throws DbException
  31. * @throws ModelNotFoundException
  32. */
  33. #[RateLimitingMiddleware(key: [Helper::class, 'getIp'], seconds: 1, limit: 1, message: '请求过于频繁')]
  34. public function index(Request $request): string
  35. {
  36. $captcha = env('EASYADMIN.CAPTCHA', 1);
  37. if (!$request->isPost()) return $this->fetch('', compact('captcha'));
  38. $post = $request->post();
  39. $rule = [
  40. 'username|用户名' => 'require',
  41. 'password|密码' => 'require',
  42. 'keep_login|是否保持登录' => 'require',
  43. ];
  44. $captcha == 1 && $rule['captcha|验证码'] = 'require|captcha';
  45. $this->validate($post, $rule);
  46. $admin = SystemAdmin::where(['username' => $post['username']])->find();
  47. if (empty($admin)) {
  48. $this->error('用户不存在');
  49. }
  50. if (!password_verify($post['password'], $admin->password)) {
  51. $this->error('密码输入有误');
  52. }
  53. if ($admin->status == 0) {
  54. $this->error('账号已被禁用');
  55. }
  56. if ($admin->login_type == 2) {
  57. if (empty($post['ga_code'])) $this->error('请输入谷歌验证码', ['is_ga_code' => true]);
  58. $ga = new \Wolfcode\Authenticator\google\PHPGangstaGoogleAuthenticator();
  59. if (!$ga->verifyCode($admin->ga_secret, $post['ga_code'])) $this->error('谷歌验证码错误');;
  60. }
  61. $admin->login_num += 1;
  62. $admin->save();
  63. $admin = $admin->toArray();
  64. unset($admin['password']);
  65. $admin['expire_time'] = $post['keep_login'] == 1 ? 0 : time() + 7200;
  66. session('admin', $admin);
  67. $this->success('登录成功');
  68. }
  69. /**
  70. * 用户退出
  71. */
  72. public function out(): void
  73. {
  74. session('admin', null);
  75. $this->success('退出登录成功');
  76. }
  77. /**
  78. * 验证码
  79. * @return Response
  80. */
  81. public function captcha(): Response
  82. {
  83. return Captcha::instance()->create();
  84. }
  85. }