Files
tp/app/admin/BaseController.php
T
2026-01-26 09:29:36 +08:00

166 lines
4.3 KiB
PHP

<?php
declare (strict_types = 1);
namespace app\admin;
use think\App;
use think\exception\ValidateException;
use think\Validate;
use think\facade\Request;
use think\facade\Cache;
use app\admin\controller\OperationLog\OperationLogger;
use app\service\JwtService;
/**
* 控制器基础类
*/
abstract class BaseController
{
/**
* Request实例
* @var \think\Request
*/
protected $request;
/**
* 应用实例
* @var \think\App
*/
protected $app;
/**
* 是否批量验证
* @var bool
*/
protected $batchValidate = false;
/**
* 控制器中间件
* @var array
*/
protected $middleware = [];
/**
* 构造方法
* @access public
* @param App $app 应用对象
*/
public function __construct(App $app)
{
$this->app = $app;
$this->request = $this->app->request;
// 控制器初始化
$this->initialize();
}
// 初始化
protected function initialize()
{}
/**
* 验证数据
* @access protected
* @param array $data 数据
* @param string|array $validate 验证器名或者验证规则数组
* @param array $message 提示信息
* @param bool $batch 是否批量验证
* @return array|string|true
* @throws ValidateException
*/
protected function validate(array $data, string|array $validate, array $message = [], bool $batch = false)
{
if (is_array($validate)) {
$v = new Validate();
$v->rule($validate);
} else {
if (strpos($validate, '.')) {
// 支持场景
[$validate, $scene] = explode('.', $validate);
}
$class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
$v = new $class();
if (!empty($scene)) {
$v->scene($scene);
}
}
$v->message($message);
// 是否批量验证
if ($batch || $this->batchValidate) {
$v->batch(true);
}
return $v->failException(true)->check($data);
}
/**
* 记录操作日志
*
* @param string $module 模块名称(如:文件管理)
* @param string $action 操作名称(如:创建文件分组)
* @param array $responseData 响应数据(可选)
* @param array $userInfo 用户信息(可选,如果不提供则从 Session 获取)
* @return bool
*/
protected function logSuccess(string $module, string $action, array $responseData = [], array $userInfo = []): bool
{
return OperationLogger::success($module, $action, $responseData, $userInfo);
}
/**
* 记录失败日志
*
* @param string $module 模块名称
* @param string $action 操作名称
* @param string $errorMessage 错误信息
* @param array $userInfo 用户信息(可选,如果不提供则从 Session 获取)
* @return bool
*/
protected function logFail(string $module, string $action, string $errorMessage, array $userInfo = []): bool
{
return OperationLogger::fail($module, $action, $errorMessage, $userInfo);
}
/**
* 记录操作日志
*
* @param string $module 操作模块
* @param string $action 操作动作
* @param array $requestData 请求数据(为空则自动获取)
* @param array $responseData 响应数据
* @param int $status 操作状态:1-成功,0-失败
* @param string $errorMessage 错误信息
* @return bool
*/
protected function logOperation(
string $module,
string $action,
array $requestData = [],
array $responseData = [],
int $status = 1,
string $errorMessage = ''
): bool {
return OperationLogger::record(
$module,
$action,
$requestData,
$responseData,
$status,
$errorMessage
);
}
/**
* 获取当前登录管理员信息
*
* @return array 用户信息数组,包含 id、account、name 等字段
*/
protected function getAdminUserInfo(): array
{
return JwtService::getUserFromHeader($this->request->header('Authorization', ''));
}
}