中间件命令快速生成:tp6手册指引
php think make:middleware Check
1、中间件:app/middleware/Check
namespace app\middleware;
use think\facade\Session;
class Check{
public function handle($request, \Closure $next){
$name = Session::get("lenzePromUser");
$response = $next($request);
// 未登录 重定向登录页
if(empty($name) && $request->action() != "login"){
return redirect("/admin/loginPage");
}
// 已登录 重定向后台页
if($name && $request->action() == "login"){
return redirect("/admin/bg");
}
return $response;
}
}
在应用模块admin下的 middleware.php文件放入(上图没截到该文件)下面的代码,是应用 Session 会话的:
return [
//"auth" => app\admin\middleware\Auth::class
// Session初始化
\think\middleware\SessionInit::class
];
2、控制器:admin/controller/Index.php
use app\BaseController;
use think\Exception;
use think\facade\Db;
use think\facade\Session;
use think\facade\View;
class Index extends BaseController{
protected $middleware = ['app\middleware\Check'=>['except'=> ['hello','loginApi']],];
// 登录页
public function login(){
return View::fetch();
}
// 后台页
public function admin(){
return View::fetch();
}
// 登录请求
public function loginApi(){
$name = input("username");
$paswd = input("password");
if($name && $paswd){
try{
$val = Db::name("user")->field("password")->where("name",$name)->where("status",1)->find();
if($val){
if($paswd == $val["password"]){
Session::set("lenzePromUser",$name);
return json(["msg"=>"登录成功!", "code"=>200, "url"=>"/admin/bg"]);
}else{
return json(["msg"=>"密码错误!", "code"=>402, "url"=>""]);
}
}
return json(["msg"=>"用户名不存在!","code"=>401,"url"=>""]);
}catch (Exception $e){
return json(["msg"=>"出错了!","code"=>400,"url"=>""]);
}
}
return json(["msg"=>"请求参数不可为空!","code"=>400]);
}
}
3、路由:admin/route/index.php
use think\facade\Route;
Route::rule('loginPage','Index/login'); // 登录页面
Route::rule('login','Index/loginApi'); // 登录api
Route::rule('bg','Index/admin'); // 后台页面
5、登录页面view/index/login.html:下载拼图前端框架2.0
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>登录</title>
<link rel="stylesheet" href="/资源目录/pintuer-2.0.min.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
body {margin: 0;overflow: hidden;background: aliceblue;}
.vertical_horizontal{position: absolute;top:50%;left:50%;transform: translate(-50%,-50%)}
.from_box{width:330px;padding:20px 30px;background-color:lavender;opacity: 0.86;border-radius:6px;}
.from_tle{font-size:26px;font-weight:500;text-align:center;margin-bottom:15px;}
@media (max-width:540px){.from_box{width:300px;padding:12px 20px;} }
canvas {position: absolute;}
</style>
</head>
<body>
<div class="container">
<div class="vertical_horizontal">
<form id="form" action="login" method="post">
<div class="from_box">
<div class="from_tle">后台登录</div>
<div class="input-group margin-bottom">
<label class="input-required">账号:</label>
<div class="input-block">
<input type="text" class="input bg-white" name="username" placeholder="登录账号" data-validate='{"required":true,"length":">=3","invalid":"请输入账号"}' />
</div>
</div>
<div class="input-group margin-bottom">
<label class="input-required">密码:</label>
<div class="input-block">
<input type="password" class="input bg-white password" name="password" placeholder="登录密码" data-validate='{"required":true,"length":">=3","invalid":"请输入密码"}' />
</div>
</div>
<input class="w12 button bg-main radius-big margin-bottom" type="submit" value="登录"/>
</div>
</form>
</div>
</div>
<script type="text/javascript" src="/资源目录/pintuer-2.0.min.js"></script>
<script>
// 检验提交
$("#form").validate({
"valid":"通过校验",
"toggle":"#submit",
"confirm":{
"target":{"id":"dialog","text":"确认提交?","confirm":"提交","cancel":"取消",'block':true},
"dismiss":false
},
"async":true,
"success":function(res){
if(res.code == 200){
location.href = res.url;
}else if(res.code == 402){
$(".password").val("");
dialog(res.msg);
}else{
$(".input").val("");
dialog(res.msg);
}
$("#submit").attr('disabled',false);
}
});
function dialog(msg) {
$().dialog({
"target":{
"id":"dialog_center_top",
"text": msg,
"confirm":"确定"
},
"show":"fixed-top-center",
"style":"margin-top",
});
}
</script>
</body>
</html>
干货在这了!