123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace app\admin\controller;
- use app\admin\controller\base\Permissions;
- use app\common\behavior\AdminLogBehavior;
- use think\Cache;
- use think\Controller;
- use think\Cookie;
- use think\Db;
- use think\Hook;
- use think\Session;
- class Common extends Controller
- {
- const USE_REMEMBER = 'usermember';
- protected function _initialize()
- {
- stopCC();
- if (\think\Env::get('online_status', 'open') == 'close') {
- exit("网站后台维护中 ...");
- }
- Hook::listen('admin_log');
- }
-
- public function login()
- {
- if (Session::has(Permissions::ADMIN_ID) == false) {
- if ($this->request->isPost()) {
- $post = $this->request->post();
- $validate = new \think\Validate([
- ['name|账号', 'require|alphaDash|max:30'],
- ['password|密码', 'require|length:6,32'],
- ['captcha', 'require|captcha', '验证码不能为空|验证码不正确'],
- ]);
- if (!$validate->check($post)) {
- $this->error('提交失败:' . $validate->getError());
- }
-
- $error_count = Cache::get('error_count' . $post['name']);
- if ($error_count >= 5) {
- (new AdminLogBehavior())->updateLastLog("登入频繁,请休息10分钟", false);
- $this->error('登入频繁,请休息10分钟');
- }
- $name = Db::name('admin')->where('name', $post['name'])->find();
- if (empty($name)) {
-
- (new AdminLogBehavior())->updateLastLog("账号不存在", false);
- $this->error('账号不存在');
- } else {
-
- $post['password'] = password($post['password']);
- if ($name['password'] != $post['password']) {
-
- if (empty($error_count)) {
- Cache::set('error_count' . $post['name'], 1, 600);
- } else {
- Cache::set('error_count' . $post['name'], ++$error_count, 600);
- }
- (new AdminLogBehavior())->updateLastLog("密码错误", false);
- $this->error('密码错误');
- } else {
-
- if (!empty($post['remember']) and $post['remember'] == 1) {
-
- if (Cookie::has(self::USE_REMEMBER)) {
- Cookie::delete(self::USE_REMEMBER);
- }
-
- Cookie::forever(self::USE_REMEMBER, $post['name']);
- } else {
-
- if (Cookie::has(self::USE_REMEMBER)) {
- Cookie::delete(self::USE_REMEMBER);
- }
- }
- Session::set(Permissions::ADMIN_ID, $name['id']);
- Session::set(Permissions::ADMIN_NAME, $name['nickname']);
- Session::set(Permissions::ADMIN_CATE_ID, $name['admin_cate_id']);
-
- Db::name('admin')->where('id', $name['id'])->update(['login_ip' => $this->request->ip(), 'login_time' => time()]);
-
- Cache::rm('error_count' . $post['name']);
- (new AdminLogBehavior())->updateLastLog("登录成功", false);
- $this->success('登录成功,正在跳转...', 'admin/index/index');
- }
- }
- } else {
- if (Cookie::has(self::USE_REMEMBER)) {
- $this->assign(self::USE_REMEMBER, Cookie::get(self::USE_REMEMBER));
- }
- return $this->fetch();
- }
- } else {
- $this->redirect('admin/index/index');
- }
- }
- }
|