新建Base.php控制器,全部的页面继承自它
<?php
namespace app\index\controller;
use think\Controller;
class Base extends Controller
{
public function __construct(){
/**
* 不验证用户登录的页面
*/
//不验证用户登录的页面
$exception_arth_list=[
'member/users/login', //登录页面
'member/users/reg' //注册页面
];
$redirect_url='member/users/login';
$this->checkUserLogin($redirect_url,$exception_arth_list);
}
/**
* 检查用户是否登录,登录时跳转到登录页面
* $redirect_url 要跳的url (不区别大小写) [str] 例: 'member/Users/login'
* $exception_arth_list [array] 不验证用户登录的页面地址(不区别大小写) 例: ['member/user/login','member/Users/reg']
* $msg 跳转前的提示信息
*/
protected function checkUserLogin($redirect_url,$exception_arth_list=[],$msg='')
{
if(!is_string($redirect_url) || !is_array($exception_arth_list) || !is_string($msg) ){
die('传入的参数错误.');
}
//获取到当前访问的页面
$module=request()->module();//获取当前访问的模块
$controller=request()->controller();//获取当前访问的控制器
$action= request()->action();//获取当前访问的方法
$current_auth_str=$module.'/'.$controller.'/'.$action; //转成字符串
//不验证用户登录的页面
//把数组里的所有转小写
if(!empty($exception_arth_list) && is_array($exception_arth_list)){
foreach($exception_arth_list as &$v){
if(!is_string($v)){
die('不验证页面数组里的值只能为字符串类型.');
}
$v=strtolower($v);
}
}
//当前访问的页面$current_auth_str转为全小写后,若是不在$exception_arth_list客户中就验证用户是否登录
if(!empty($exception_arth_list) && is_array($exception_arth_list)){
if(!in_array(strtolower($current_auth_str),$exception_arth_list)){
if (!session('user_id')) {
if($msg == ''){
$this->redirect($redirect_url);
}else{
$this->error('请先登录.', $redirect_url);
}
}
}
}
}
}