Login.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\controller;
  15. use think\admin\Controller;
  16. use think\admin\service\AdminService;
  17. use think\admin\service\CaptchaService;
  18. use think\admin\service\SystemService;
  19. /**
  20. * 用户登录管理
  21. * Class Login
  22. * @package app\admin\controller
  23. */
  24. class Login extends Controller
  25. {
  26. /**
  27. * 后台登录入口
  28. * @throws \think\db\exception\DataNotFoundException
  29. * @throws \think\db\exception\DbException
  30. * @throws \think\db\exception\ModelNotFoundException
  31. */
  32. public function index()
  33. {
  34. if ($this->app->request->isGet()) {
  35. if (AdminService::instance()->isLogin()) {
  36. $this->redirect(url('@admin')->build());
  37. } else {
  38. $this->title = '系统登录';
  39. $this->captcha = CaptchaService::instance()->getAttrs();
  40. $this->devmode = SystemService::instance()->checkRunMode('dev');
  41. $this->fetch();
  42. }
  43. }
  44. $data = $this->_vali([
  45. 'username.require' => '登录账号不能为空!',
  46. 'username.min:4' => '登录账号长度不能少于4位有效字符!',
  47. 'password.require' => '登录密码不能为空!',
  48. 'password.min:4' => '登录密码长度不能少于4位有效字符!',
  49. 'verify.require' => '图形验证码不能为空!',
  50. 'uniqid.require' => '图形验证标识不能为空!'
  51. ]);
  52. if (!CaptchaService::instance()->check($data['verify'], $data['uniqid'])) {
  53. $this->error('图形验证码验证失败,请重新输入!');
  54. }
  55. // 用户信息验证
  56. $map = ['username' => $data['username'], 'is_deleted' => '0'];
  57. $user = $this->app->db->name('SystemUser')->where($map)->order('id desc')->find();
  58. if (empty($user)) {
  59. $this->error('登录账号或密码错误,请重新输入!');
  60. }
  61. if (md5("{$user['password']}{$data['uniqid']}") !== $data['password']) {
  62. $this->error('登录账号或密码错误,请重新输入!');
  63. }
  64. if (empty($user['status'])) {
  65. $this->error('账号已经被禁用,请联系管理员!');
  66. }
  67. $this->app->db->name('SystemUser')->where(['id' => $user['id']])->update([
  68. 'login_ip' => $this->app->request->ip(),
  69. 'login_at' => $this->app->db->raw('now()'),
  70. 'login_num' => $this->app->db->raw('login_num+1'),
  71. ]);
  72. $this->app->session->set('user', $user);
  73. sysoplog('用户登录', "用户登录系统后台成功");
  74. $this->success('登录成功', url('@admin')->build());
  75. }
  76. /**
  77. * 生成验证码
  78. */
  79. public function captcha()
  80. {
  81. $image = CaptchaService::instance();
  82. $this->success('生成验证码成功', [
  83. 'image' => $image->getData(),
  84. 'uniqid' => $image->getUniqid(),
  85. ]);
  86. }
  87. /**
  88. * 退出登录
  89. */
  90. public function out()
  91. {
  92. $this->app->session->clear();
  93. $this->app->session->destroy();
  94. $this->success('退出登录成功!', url('@admin/login')->build());
  95. }
  96. }