2020-01-01 13:17:19 +08:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace app\middleware;
|
|
|
|
|
2020-02-12 17:20:07 +08:00
|
|
|
use taoser\think\Auth as UserAuth;
|
2020-01-01 13:17:19 +08:00
|
|
|
use think\facade\Session;
|
|
|
|
|
|
|
|
class Auth
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 处理请求
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @param \Closure $next
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function handle($request, \Closure $next)
|
|
|
|
{
|
2020-11-01 18:13:05 +08:00
|
|
|
$path = app('http')->getName().'/'.stristr($request->pathinfo(),".html",true);
|
2020-01-01 13:17:19 +08:00
|
|
|
|
2020-11-01 18:13:05 +08:00
|
|
|
//没有登录及当前非登录页重定向登录页
|
|
|
|
if(!Session::has('admin_id') && $path !== 'admin/login/index' && !stristr($request->pathinfo(),"captcha.html") )
|
|
|
|
{
|
|
|
|
return redirect((string) url('admin/login/index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
//登陆后无法访问登录页
|
|
|
|
if(Session::has('admin_id') && $path == 'admin/login/index'){
|
|
|
|
return redirect((string) url('admin/index/index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// 排除公共权限
|
|
|
|
$not_check = ['admin/login/index','admin/index/index','admin/index/home','admin/Set/info','admin/Set/password','admin/Admin/logout','admin/captcha'];
|
2020-01-01 13:17:19 +08:00
|
|
|
|
2020-11-01 18:13:05 +08:00
|
|
|
if (!in_array($path, $not_check)) {
|
|
|
|
$auth = new UserAuth();
|
|
|
|
$admin_id = Session::get('admin_id'); //登录用户的id
|
2020-01-01 13:17:19 +08:00
|
|
|
|
2020-11-01 18:13:05 +08:00
|
|
|
if (!$auth->check($path, $admin_id) && $admin_id != 1) {
|
|
|
|
return view('public/auth');
|
|
|
|
//return json(['status'=>-1,'msg'=>'没有权限!']);
|
|
|
|
//return response("<script> alert('没有权限!'); </script>");
|
2020-01-01 13:17:19 +08:00
|
|
|
}
|
2020-11-01 18:13:05 +08:00
|
|
|
}
|
|
|
|
return $next($request);
|
2020-01-01 13:17:19 +08:00
|
|
|
}
|
|
|
|
}
|