[PHP高可用后端]⑩--登陆权限控制

Paste_Image.png
Paste_Image.png

Base.php

<?php

pp\admin\controller;
use think\Controller;

/**
 * 后台登陆基类
 * Class Base
 * @package app\admin\controller
 */
class Base extends Controller
{
    /**
     * 初始化方法
     */
    protected function _initialize()
    {
        //判断用户是否登陆
        $isLogin = $this->isLogin();
        if (!$isLogin) {
            return $this->redirect('login/index');
        }

    }


    /**
     * 判断是否登陆
     * @return bool
     */
    public function isLogin()
    {
        //获取session
        $user = session(config('admin.session_user'), '', config('admin.session_user_scope'));
        if ($user && $user->id) {
            return true;
        }
        return false;
    }

}

Login.php

<?php
/**
 * Created by PhpStorm.
 * User: tong
 * Date: 2017/11/1
 * Time: 14:25
 */

namespace app\admin\controller;

use app\common\lib\IAuth;

class Login extends Base
{

    protected function _initialize()
    {
        //避免死循环
    }

    public function index()
    {
        //如果后台已经登陆,直接跳道首页
        $isLogin = $this->isLogin();
        if ($isLogin) {
            return $this->redirect('index/index');
        }
        return $this->fetch();
    }

    public function check()
    {
        if (request()->isPost()) {
            $data = input('post.');
            if (!captcha_check($data['code'])) {
                $this->error('验证码不正确');
            }

            $validate = validate('Login');
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }

            try {
                $user = model('AdminUser')->get(
                    ['username' => $data['username']]
                );
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }

            if (!$user || $user->status != config('code.status_normal')) {
                // $this->error 内部会throw一个Exception 所以不需要放在try catch中
                $this->error('该用户不存在');
            }

            if (IAuth::setPassword($data['password']) != $user['password']) {
                $this->error("密码不正确");
            }

            //更新数据库 登陆时间 登陆ip
            $udata = [
                'last_login_time' => time(),
                'last_login_ip' => request()->ip(),
            ];
            try {
                model('AdminUser')->save($udata, ['id' => $user->id]);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            //2.session
            session(config('admin.session_user'), $user, config('admin.session_user_scope'));
            $this->success('登陆成功', 'index/index');
        } else {
            $this->error("请求不合法");
        }

    }

    /**
     * 清空登陆的逻辑
     * 1.清空session
     * 2.跳转到登陆页面
     */
    public function logout()
    {
        //清空作用域
        session(null, config('admin.session_user_scope'));
        //跳转
        $this->redirect('login/index');
    }

}
protected function _initialize()
    {
        //避免死循环
    }

    public function index()
    {
        //如果后台已经登陆,直接跳道首页
        $isLogin = $this->isLogin();
        if ($isLogin) {
            return $this->redirect('index/index');
        }
        return $this->fetch();
    }

Index.php

class Index extends Base{
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容