TaoLer/app/middleware/Auth.php

118 lines
3.8 KiB
PHP
Raw Permalink Normal View History

2020-01-01 13:17:19 +08:00
<?php
2022-08-02 18:46:05 +08:00
/*
* @Author: TaoLer <alipey_tao@qq.com>
* @Date: 2021-12-06 16:04:50
* @LastEditTime: 2022-04-22 06:24:03
* @LastEditors: TaoLer
* @Description: 搜索引擎SEO优化设置
* @FilePath: \TaoLer\app\middleware\Auth.php
* Copyright (c) 2020~2022 http://www.aieok.com All rights reserved.
*/
2020-01-01 13:17:19 +08:00
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;
2021-12-15 15:46:04 +08:00
use think\facade\Cookie;
use think\facade\Db;
use think\facade\Config;
2023-03-16 22:30:36 +08:00
use think\facade\Request;
2020-01-01 13:17:19 +08:00
class Auth
{
/**
* 处理请求
*
* @param Request $request
* @param \Closure $next
* @return Response
*/
public function handle($request, \Closure $next)
{
2023-03-16 22:30:36 +08:00
// var_dump(Request::url(),Request::pathinfo(),$request->baseUrl(),$request->controller());
2021-12-15 15:46:04 +08:00
//访问路径
2023-03-16 22:30:36 +08:00
// $path = app('http')->getName().'/'.stristr($request->pathinfo(),".html",true);
2023-03-16 22:40:15 +08:00
$path = stristr($request->pathinfo(),".html",true) ?: Request::pathinfo();
// var_dump($path);
2021-12-15 15:46:04 +08:00
//登陆前获取加密的Cookie
$cooAuth = Cookie::get('adminAuth');
if(!empty($cooAuth)){
$resArr = explode(':',$cooAuth);
$userId = end($resArr);
//检验用户
$user = Db::name('admin')->where('id',$userId)->find();
if(!empty($user)){
//验证cookie
$salt = Config::get('taoler.salt');
$auth = md5($user['username'].$salt).":".$userId;
2023-03-16 22:30:36 +08:00
if($auth == $cooAuth){
2021-12-15 15:46:04 +08:00
Session::set('admin_name',$user['username']);
Session::set('admin_id',$userId);
}
}
}
2023-03-16 22:30:36 +08:00
// //没有登录及当前非登录页重定向登录页
// if(!Session::has('admin_id') && $path !== 'admin/login/index' && !(stristr($request->pathinfo(),"captcha.html") || stristr($request->pathinfo(),"addons")) )
// {
// return redirect((string) url('login/index'));
// }
// //登陆后无法访问登录页
// if(Session::has('admin_id') && $path == 'admin/login/index'){
// return redirect((string) url('index/index'));
// }
// // 排除公共权限
// $not_check = ['admin/','index/index', 'admin/menu/getMenuNavbar','admin/login/index','admin/index/index','admin/index/home','admin/Admin/info','admin/Admin/repass','admin/Admin/logout','admin/Index/news','admin/Index/cunsult','admin/Index/replys','admin/Index/reply','admin/captcha','addons/socail/','admin/addons/social/oauth/login','admin/addons/bacimg/index/getImages'];
//没有登录及当前非登录页重定向登录页
if(!Session::has('admin_id') && $path !== 'login/index' && !(stristr($request->pathinfo(),"captcha.html") || stristr($request->pathinfo(),"addons")) )
{
return redirect((string) url('login/index'));
}
//登陆后无法访问登录页
if(Session::has('admin_id') && $path == 'login/index' || $path == ''){
return redirect((string) url('index/index'));
}
// 排除公共权限
$not_check = [
'captcha',
2023-03-16 22:40:15 +08:00
'login/index',
2023-03-16 22:30:36 +08:00
'admin/index',
2023-03-16 22:40:15 +08:00
'system.menu/getnav',
2023-03-16 22:30:36 +08:00
'index/index',
2023-03-16 22:40:15 +08:00
'index/console1',
'index/console2',
'index/news',
2023-03-16 22:30:36 +08:00
'menu/getMenuNavbar',
'index/home',
'Admin/info',
2023-03-16 22:40:15 +08:00
'system.admin/repass',
'system.admin/logout',
2023-03-16 22:30:36 +08:00
'Index/cunsult',
'Index/replys',
'Index/reply',
'admin/captcha',
'addons/socail/',
'addons/social/oauth/login',
'addons/bacimg/index/getImages'
];
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 response("<script>alert('没有操作权限')</script>");
return json(['code'=>-1,'msg'=>'无权限']);
2020-01-01 13:17:19 +08:00
}
2020-11-01 18:13:05 +08:00
}
2021-12-15 15:46:04 +08:00
return $next($request);
2020-01-01 13:17:19 +08:00
}
}