Login.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/Think.Admin
  12. // +----------------------------------------------------------------------
  13. namespace app\admin\controller;
  14. use controller\BasicAdmin;
  15. use service\LogService;
  16. use service\NodeService;
  17. use think\Db;
  18. /**
  19. * 系统登录控制器
  20. * class Login
  21. * @package app\admin\controller
  22. * @author Anyon <zoujingli@qq.com>
  23. * @date 2017/02/10 13:59
  24. */
  25. class Login extends BasicAdmin
  26. {
  27. /**
  28. * 控制器基础方法
  29. */
  30. public function _initialize()
  31. {
  32. if (session('user') && $this->request->action() !== 'out') {
  33. $this->redirect('@admin');
  34. }
  35. }
  36. /**
  37. * 用户登录
  38. * @return string
  39. */
  40. public function index()
  41. {
  42. if ($this->request->isGet()) {
  43. return $this->fetch('', ['title' => '用户登录']);
  44. }
  45. // 输入数据效验
  46. $username = $this->request->post('username', '', 'trim');
  47. $password = $this->request->post('password', '', 'trim');
  48. strlen($username) < 4 && $this->error('登录账号长度不能少于4位有效字符!');
  49. strlen($password) < 4 && $this->error('登录密码长度不能少于4位有效字符!');
  50. // 用户信息验证
  51. $user = Db::name('SystemUser')->where('username', $username)->find();
  52. empty($user) && $this->error('登录账号不存在,请重新输入!');
  53. ($user['password'] !== md5($password)) && $this->error('登录密码与账号不匹配,请重新输入!');
  54. empty($user['status']) && $this->error('账号已经被禁用,请联系管理!');
  55. // 更新登录信息
  56. $data = ['login_at' => ['exp', 'now()'], 'login_num' => ['exp', 'login_num+1']];
  57. Db::name('SystemUser')->where(['id' => $user['id']])->update($data);
  58. session('user', $user);
  59. !empty($user['authorize']) && NodeService::applyAuthNode();
  60. LogService::write('系统管理', '用户登录系统成功');
  61. $this->success('登录成功,正在进入系统...', '@admin');
  62. }
  63. /**
  64. * 退出登录
  65. */
  66. public function out()
  67. {
  68. LogService::write('系统管理', '用户退出系统成功');
  69. session('user', null);
  70. session_destroy();
  71. $this->success('退出登录成功!', '@admin/login');
  72. }
  73. }