Files
pdm.sa.com/app/HomeController.php
2024-07-12 22:44:33 +08:00

127 lines
3.1 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\SpiderLog;
use app\common\service\user\UserService;
use app\common\service\user\UserTokenService;
use think\response\View;
/*
* 前台全局控制器基类
*/
class HomeController extends BaseController
{
/**
* 控制器/类名
* @var string
*/
public string $controller;
/**
* 控制器方法
* @var string
*/
public string $action;
/**
* 用户登录ID
* @var int
*/
public int $userId = 0;
/**
* 用户数据
* @var array
*/
public array $userInfo = [];
/**
* 控制器登录鉴权
* @var bool
*/
public bool $needLogin = false;
/**
* 禁止登录重复
* @var array
*/
public array $repeatLogin = ['login', 'register'];
/**
* 非鉴权方法
* @var array
*/
public array $noNeedLogin = ['index', 'home'];
/**
* 跳转URL地址
* @var string
*/
public string $JumpUrl = '/user/index';
/**
* 初始化函数
*/
public function initialize()
{
parent::initialize();
// 获取请求控制器
$this->action = request()->action();
$this->controller = request()->controller();
$this->userInfo = UserTokenService::isLogin();
if (!empty($this->userInfo)) {
$this->userId = $this->userInfo['id'];
if (in_array($this->action, $this->repeatLogin)) {
$this->redirect($this->JumpUrl);
}
\think\facade\View::assign('user', $this->userInfo);
} else { // 非鉴权方法
if ($this->needLogin && !in_array($this->action, $this->noNeedLogin)) {
return $this->error('请登录后访问', '/');
}
}
// 全域蜘蛛爬虫日志
SpiderLog::SpiderTraceLogs();
}
/**
* 视图过滤
*
* @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()
{
UserService::logout();
return $this->success('退出成功', '/');
}
}