Files
pdmtest.sa.com/app/HomeController.php
2024-07-26 17:51:40 +08:00

148 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
// +----------------------------------------------------------------------
// | swiftAdmin 极速开发框架 [基于ThinkPHP6开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2030 http://www.swiftadmin.net
// +----------------------------------------------------------------------
// | swiftAdmin.NET High Speed Development Framework
// +----------------------------------------------------------------------
// | Author: meystack <coolsec@foxmail.com> Apache 2.0 License Code
// +----------------------------------------------------------------------
namespace app;
use app\common\library\Auth;
use think\response\View;
/*
* 前台全局控制器基类
*
*/
class HomeController extends BaseController
{
/**
* 接口权限
* @var object
*/
public object $auth;
/**
* 控制器/类名
* @var string
*/
public string $controller;
/**
* 控制器方法
* @var string
*/
public string $action;
/**
* 用户登录ID
* @var mixed
*/
public mixed $userId;
/**
* 用户数据
* @var mixed
*/
public mixed $userInfo;
/**
* 控制器登录鉴权
* @var bool
*/
public bool $needLogin = false;
/**
* 非鉴权方法
* @var array
*/
public array $noNeedAuth = [];
/**
* 禁止登录重复
* @var array
*/
public array $repeatLogin = ['login', 'register'];
/**
* 非鉴权方法
* @var array
*/
public array $noNeedLogin = ['index', 'home'];
/**
* 跳转URL地址
* @var string
*/
public string $JumpUrl = '/user/index';
/**
* 初始化函数
* return void
*/
public function initialize()
{
parent::initialize();
// 获取权限实例
$this->auth = Auth::instance();
// 获取请求控制器
$this->action = request()->action();
$this->controller = request()->controller();
// 是否验证登录器
if ($this->auth->isLogin()) {
$this->userId = $this->auth->userInfo['id'];
$this->userInfo = $this->auth->userInfo;
if (in_array($this->action, $this->repeatLogin)) {
$this->redirect($this->JumpUrl);
}
$this->app->view->assign('user', $this->auth->userInfo);
} else { // 非鉴权方法
if ($this->needLogin && !in_array($this->action, $this->noNeedLogin)) {
return $this->error('请登录后访问', '/');
}
}
}
// 计算
/**
* 视图过滤
*
* @param string $template
* @param array $argc
* @return View
*/
public function view(string $template = '', array $argc = []): View
{
return view($template, $argc)->filter(function ($content) {
if (saenv('compression_page')) {
$content = preg_replace('/\s+/i', ' ', $content);
}
return $content;
});
}
/**
* 退出登录
* @access public
*/
public function logOut()
{
$this->auth->logOut();
return $this->success('退出成功','/');
}
}