测试session
class Index extends Controller
{
public function index()
{
halt(session(config('admin.session_user'), '', config('admin.session_user_scope')));
return $this->fetch();
}
}
halt(session(config('admin.session_user'), '', config('admin.session_user_scope')));
_header.html
<header class="navbar-wrapper">
<div class="navbar navbar-fixed-top">
<div class="container-fluid cl"><a class="logo navbar-logo f-l mr-10 hidden-xs" href="{:url('index/index')}">imooc_app_singwa</a>
<nav id="Hui-userbar" class="nav navbar-nav navbar-userbar hidden-xs">
<ul class="cl">
<li>超级管理员</li>
<li class="dropDown dropDown_hover"><a href="#" class="dropDown_A">admin <i class="Hui-iconfont"></i></a>
<ul class="dropDown-menu menu radius box-shadow">
<li><a href="{:url('login/logout')}">退出</a></li>
</ul>
</li>
<li id="Hui-msg"><a href="#" title="消息"><span class="badge badge-danger">1</span><i
class="Hui-iconfont" style="font-size:18px"></i></a></li>
<li id="Hui-skin" class="dropDown right dropDown_hover"><a href="javascript:;" class="dropDown_A"
title="换肤"><i class="Hui-iconfont"
style="font-size:18px"></i></a>
<ul class="dropDown-menu menu radius box-shadow">
<li><a href="javascript:;" data-val="default" title="默认(黑色)">默认(黑色)</a></li>
<li><a href="javascript:;" data-val="blue" title="蓝色">蓝色</a></li>
<li><a href="javascript:;" data-val="green" title="绿色">绿色</a></li>
<li><a href="javascript:;" data-val="red" title="红色">红色</a></li>
<li><a href="javascript:;" data-val="yellow" title="黄色">黄色</a></li>
<li><a href="javascript:;" data-val="orange" title="橙色">橙色</a></li>
</ul>
</li>
</ul>
</nav>
</div>
</div>
</header>
<li><a href="{:url('login/logout')}">退出</a></li>
Login.php
<?php
/**
* Created by PhpStorm.
* User: tong
* Date: 2017/11/1
* Time: 14:25
*/
namespace app\admin\controller;
use app\common\lib\IAuth;
use think\Controller;
class Login extends Controller
{
public function 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');
}
}
/**
* 清空登陆的逻辑
* 1.清空session
* 2.跳转到登陆页面
*/s
public function logout()
{
//清空作用域
session(null, config('admin.session_user_scope'));
//跳转
$this->redirect('login/index');
}