Common.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 听雨 < 389625819@qq.com >
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller;
  12. use app\admin\controller\base\Permissions;
  13. use app\common\behavior\AdminLogBehavior;
  14. use think\Cache;
  15. use think\Controller;
  16. use think\Cookie;
  17. use think\Db;
  18. use think\Hook;
  19. use think\Session;
  20. class Common extends Controller
  21. {
  22. const USE_REMEMBER = 'usermember';
  23. protected function _initialize()
  24. {
  25. stopCC();
  26. if (\think\Env::get('online_status', 'open') == 'close') {
  27. exit("网站后台维护中 ...");
  28. }
  29. Hook::listen('admin_log');
  30. }
  31. /**
  32. * 登录
  33. * //不适用表单令牌,验证码输错时,token不会变,导致要刷新页面并重新输入
  34. * @return mixed
  35. */
  36. public function login()
  37. {
  38. if (Session::has(Permissions::ADMIN_ID) == false) {
  39. if ($this->request->isPost()) {
  40. $post = $this->request->post();
  41. $validate = new \think\Validate([
  42. ['name|账号', 'require|alphaDash|max:30'],
  43. ['password|密码', 'require|length:6,32'],
  44. ['captcha', 'require|captcha', '验证码不能为空|验证码不正确'],
  45. ]);
  46. if (!$validate->check($post)) {
  47. $this->error('提交失败:' . $validate->getError());
  48. }
  49. //连续错误5次账号暂停
  50. $error_count = Cache::get('error_count' . $post['name']);
  51. if ($error_count >= 5) {
  52. (new AdminLogBehavior())->updateLastLog("登入频繁,请休息10分钟", false);
  53. $this->error('登入频繁,请休息10分钟');
  54. }
  55. $name = Db::name('admin')->where('name', $post['name'])->find();
  56. if (empty($name)) {
  57. //不存在该用户名
  58. (new AdminLogBehavior())->updateLastLog("账号不存在", false);
  59. $this->error('账号不存在');
  60. } else {
  61. //验证密码
  62. $post['password'] = password($post['password']);
  63. if ($name['password'] != $post['password']) {
  64. //记录次数
  65. if (empty($error_count)) {
  66. Cache::set('error_count' . $post['name'], 1, 600);
  67. } else {
  68. Cache::set('error_count' . $post['name'], ++$error_count, 600);
  69. }
  70. (new AdminLogBehavior())->updateLastLog("密码错误", false);
  71. $this->error('密码错误');
  72. } else {
  73. //是否记住账号
  74. if (!empty($post['remember']) and $post['remember'] == 1) {
  75. //检查当前有没有记住的账号
  76. if (Cookie::has(self::USE_REMEMBER)) {
  77. Cookie::delete(self::USE_REMEMBER);
  78. }
  79. //保存新的
  80. Cookie::forever(self::USE_REMEMBER, $post['name']);
  81. } else {
  82. //未选择记住账号,或取消操作
  83. if (Cookie::has(self::USE_REMEMBER)) {
  84. Cookie::delete(self::USE_REMEMBER);
  85. }
  86. }
  87. Session::set(Permissions::ADMIN_ID, $name['id']); //保存admin_id
  88. Session::set(Permissions::ADMIN_NAME, $name['nickname']); //保存admin_name
  89. Session::set(Permissions::ADMIN_CATE_ID, $name['admin_cate_id']); //保存admin_cate_id
  90. //记录登录时间和ip
  91. Db::name('admin')->where('id', $name['id'])->update(['login_ip' => $this->request->ip(), 'login_time' => time()]);
  92. //清空次数
  93. Cache::rm('error_count' . $post['name']);
  94. (new AdminLogBehavior())->updateLastLog("登录成功", false);
  95. $this->success('登录成功,正在跳转...', 'admin/index/index');
  96. }
  97. }
  98. } else {
  99. if (Cookie::has(self::USE_REMEMBER)) {
  100. $this->assign(self::USE_REMEMBER, Cookie::get(self::USE_REMEMBER));
  101. }
  102. return $this->fetch();
  103. }
  104. } else {
  105. $this->redirect('admin/index/index');
  106. }
  107. }
  108. }