up
This commit is contained in:
37
app/middleware/Auth.php
Normal file
37
app/middleware/Auth.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\middleware;
|
||||
|
||||
/**
|
||||
* 登录验证中间件
|
||||
*/
|
||||
class Auth
|
||||
{
|
||||
/**
|
||||
* 处理请求
|
||||
*
|
||||
* @param \think\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, \Closure $next)
|
||||
{
|
||||
// 检查是否已登录
|
||||
$username = session('username');
|
||||
|
||||
if (empty($username)) {
|
||||
// 如果是AJAX请求,返回JSON
|
||||
if ($request->isAjax()) {
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '请先登录',
|
||||
]);
|
||||
}
|
||||
// 否则跳转到登录页
|
||||
return redirect('/login');
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user