thinkphp3自定义登录

文章描述:

thinkphp3自定义方法样式登录

首先下载thinkphp3.2框架,把代码放到根目录下面,然后去下载layui、layer、jquery、bootstrap框架,把下载后的文件放到根目录下面Public/states里面,下面开始项目操作

 

数据库配置

打开项目根目录Application/Common/Conf/config.php文件,配置代码如下:

<?php
return array(
    //'配置项'=>'配置值'
    'DB_TYPE'   => 'mysql',
    'DB_HOST'   => '127.0.0.1',
    'DB_NAME'   => 'tp3',
    'DB_USER'   => 'root',
    'DB_PWD'    => '123456',
    'DB_PORT'   => 3306,
    'DB_PREFIX' => 'sys_',
    'DB_CHARSET'=> 'utf8',
    'DB_DEBUG'  =>  TRUE,
 
);

公共方法

Application/Admin/Common/目录下面新建一个function.php文件,在里面自定义判断用户是否登录方法,代码如下:

/* 判断登录 */
function is_login(){
    $user = session ( 'UID' );
    if (empty ($user)){
        return 0;
    } else {
        return $user;
    }
}

公共控制器

Application/Admin/Controller/目录下面新建一个BaseController.class.php文件,代码如下:

<?php
namespace Admin\Controller;
use Think\Controller;
class BaseController extends Controller {
    private $model;
    function __construct()
    {
        parent::__construct();
    }
    protected function _initialize()
    {
        /* 获取当前用户ID */
        define('UID', is_login());
 
        /* 还没登录 跳转到登录页面 */
        if (!UID) {
            $this->redirect('Login/index');
        }
    }
 
}

登录控制器

<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller
{
    private $model;
    public function index()
    {
        if(IS_AJAX){
 
            /* 条件 */
            $condition['mobile'] = trim(I("phone"));
            $pass = trim(I("code"));
 
            $manage_model = M("manage");
            $account_item = $manage_model->where($condition)->find();
            /* 判断用户 */
            if(!$account_item){
                $data['msg'] = "账号不存在";
                $data['state']= 400;
                $this->ajaxReturn($data,'JSON');
            }
            if($account_item['pass']!=$pass){
                $data['msg'] = "密码错误";
                $data['state']= 401;
                $this->ajaxReturn($data,'JSON');
            }
            if($account_item['status']==0){
                $data['msg'] = "账号已被锁";
                $data['state']= 402;
                $this->ajaxReturn($data,'JSON');
            }
            session("UID",$account_item["id"],7200);
            session("UNAME",$account_item["username"],7200);
            session("ROLEID",$account_item["role_id"],7200);
 
            /* 指定cookie保存时间 */
            cookie('UID',$account_item["id"],36000);
 
            $data['msg'] = "登录成功";
            $data['state']=200;
            $this->ajaxReturn($data,'JSON');
            exit;
        }else{
            layout(false);
            $this->display('index');
        }
    }
 
    public function logout(){
        session("UID",null);
        session("UNAME",null);
        $this->redirect("index");
    }
 
}

登录模板

Application/Admin/View/Login下面新建index.html文件,代码如下:

<html lang="zh-CN"><head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="applicable-device" content="pc,mobile">
<title>登录</title>
<link rel="stylesheet" type="text/css" href="__BOOTSTRAP__/3.3.7/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="__LAYUI__/css/layui.css" />
<link rel="stylesheet" type="text/css" href="__ADMIN__/css/login.css" />
<script src="__JS__/jquery-2.1.1.min.js"></script>
<script src="__BOOTSTRAP__/js/bootstrap.min.js"></script>
<script src="__LAYER__/layer.js"></script>
<script src="__LAYUI__/layui.js"></script>
</head>
<body>
 
    <div class="login">
        <div class="item">
            <input type="text" name="mobile" lay-verify="user" autocomplete="off" id="mobile" maxlength="20" placeholder="手机" class="layui-input input_txt ui-input" />
        </div>
        <div class="item">
            <input type="password" name="code" lay-verify="code" autocomplete="off" id="code" maxlength="30" placeholder="密码 / 验证码" class="input_code ui-input" />
            <input type="button" class="layui-btn layui-btn-normal get_code" value="发送验证码" id="btnSendCode" >
        </div>
        <button class="layui-btn layui-btn-normal login_btn">登录</button>
    </div>
 
<script src="__ADMIN__/js/login.js"></script>
</body>
</html>

自定义路径配置

Application/Common/Conf/config.php文件里面添加自定义路径

/* PUBLIC */
'TMPL_PARSE_STRING'  =>array(
 
    '__PUBLIC__' =>__ROOT__.'/Public',
    '__LAYER__'=>__ROOT__.'/Public/statics/layer',
    '__LAYUI__'=>__ROOT__.'/Public/statics/layui',
    '__JS__'=>__ROOT__.'/Public/statics/js',
    '__BOOTSTRAP__'=>__ROOT__.'/Public/statics/bootstrap/',
    /* Admin */
    '__ADMIN__'=>__ROOT__.'/Public/statics/admin/',
 
),

自定义jump配置

在config.php文件里面添加自定义提示路径及命名

/* MESSAGE */
'LAYOUT_ON'=>false,
'LAYOUT_NAME'=>'layout',
 
'TMPL_ACTION_SUCCESS'=>'Public:dispatch_jump',
'TMPL_ACTION_ERROR'=>'Public:dispatch_jump',

jump模板

Application/Admin/Public目录下面新建一个dispatch_jump.html文件

{__NOLAYOUT__}
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>{$message}</title>
    <link rel="stylesheet" type="text/css" href="__BOOTSTRAP__/3.3.7/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="__LAYUI__/css/layui.css" />
    <link rel="stylesheet" type="text/css" href="__ADMIN__/css/admin.css" />
    <script src="__JS__/jquery-2.1.1.min.js"></script>
    <script src="__BOOTSTRAP__/js/bootstrap.min.js"></script>
    <script src="__LAYER__/layer.js"></script>
    <script src="__LAYUI__/layui.js"></script>
</head>
<body>
 
 
<?php
if(isset($message)){
    $icon = 6;
}elseif($error){
    $icon = 5;
}
?>
<script>
    $(function() {
        layer.msg("<?php echo($message); ?>", {
            icon: '<?php echo $icon;?>',
            time: '3000'
        });
        setTimeout(function() {
                    self.location.href = "<?php echo($jumpUrl); ?>"
                },
                2000)
    });
</script>
 
</body>
</html>

 

发布时间:2021/08/10

发表评论