Login.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | EasyAdmin
  4. // +----------------------------------------------------------------------
  5. // | PHP交流群: 763822524
  6. // +----------------------------------------------------------------------
  7. // | 开源协议 https://mit-license.org
  8. // +----------------------------------------------------------------------
  9. // | github开源项目:https://github.com/zhongshaofa/EasyAdmin
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller;
  12. use app\common\model\SystemAdmin;
  13. use app\common\controller\AdminController;
  14. use think\captcha\facade\Captcha;
  15. use think\facade\Env;
  16. /**
  17. * Class Login
  18. * @package app\admin\controller
  19. */
  20. class Login extends AdminController
  21. {
  22. /**
  23. * 初始化方法
  24. */
  25. public function initialize()
  26. {
  27. parent::initialize();
  28. $action = $this->request->action();
  29. if (!empty(session('admin')) && !in_array($action, ['out'])) {
  30. $adminModuleName = config('app.admin_alias_name');
  31. $this->success('已登录,无需再次登录', [], __url("@{$adminModuleName}"));
  32. }
  33. }
  34. /**
  35. * 用户登录
  36. * @return string
  37. * @throws \Exception
  38. */
  39. public function index()
  40. {
  41. $captcha = Env::get('easyadmin.captcha', 1);
  42. if ($this->request->isPost()) {
  43. $post = $this->request->post();
  44. $rule = [
  45. 'username|用户名' => 'require',
  46. 'password|密码' => 'require',
  47. 'keep_login|是否保持登录' => 'require',
  48. ];
  49. $captcha == 1 && $rule['captcha|验证码'] = 'require|captcha';
  50. $this->validate($post, $rule);
  51. $admin = SystemAdmin::where(['username' => $post['username']])->find();
  52. if (empty($admin)) {
  53. $this->error('用户不存在');
  54. }
  55. if (password($post['password']) != $admin->password) {
  56. $this->error('密码输入有误');
  57. }
  58. if ($admin->status == 0) {
  59. $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 ? true : time() + 7200;
  66. session('admin', $admin);
  67. $this->success('登录成功');
  68. }
  69. $this->assign('captcha', $captcha);
  70. $this->assign('demo', $this->isDemo);
  71. return $this->fetch();
  72. }
  73. /**
  74. * 用户退出
  75. * @return mixed
  76. */
  77. public function out()
  78. {
  79. session('admin', null);
  80. $this->success('退出登录成功');
  81. }
  82. /**
  83. * 验证码
  84. * @return \think\Response
  85. */
  86. public function captcha()
  87. {
  88. return Captcha::create();
  89. }
  90. }