webman后台
This commit is contained in:
36
dafuweng-webman/app/common/middleware/AdminLog.php
Normal file
36
dafuweng-webman/app/common/middleware/AdminLog.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\middleware;
|
||||
|
||||
use Webman\MiddlewareInterface;
|
||||
use Webman\Http\Request;
|
||||
use Webman\Http\Response;
|
||||
use app\admin\model\AdminLog as AdminLogModel;
|
||||
|
||||
/**
|
||||
* 管理员操作日志中间件(Webman 迁移版)
|
||||
* 仅对 /admin 路由的 POST、DELETE 请求记录日志
|
||||
*/
|
||||
class AdminLog implements MiddlewareInterface
|
||||
{
|
||||
public function process(Request $request, callable $handler): Response
|
||||
{
|
||||
$response = $handler($request);
|
||||
|
||||
$path = trim($request->path(), '/');
|
||||
if (str_starts_with($path, 'admin/') && config('buildadmin.auto_write_admin_log', true)) {
|
||||
$method = $request->method();
|
||||
if ($method === 'POST' || $method === 'DELETE') {
|
||||
try {
|
||||
AdminLogModel::instance($request)->record();
|
||||
} catch (\Throwable $e) {
|
||||
\support\Log::warning('[AdminLog] ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
55
dafuweng-webman/app/common/middleware/AllowCrossDomain.php
Normal file
55
dafuweng-webman/app/common/middleware/AllowCrossDomain.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\middleware;
|
||||
|
||||
use Webman\MiddlewareInterface;
|
||||
use Webman\Http\Request;
|
||||
use Webman\Http\Response;
|
||||
|
||||
/**
|
||||
* 跨域请求支持(Webman 迁移版)
|
||||
* 安全起见,只支持配置中的域名
|
||||
*/
|
||||
class AllowCrossDomain implements MiddlewareInterface
|
||||
{
|
||||
protected array $header = [
|
||||
'Access-Control-Allow-Credentials' => 'true',
|
||||
'Access-Control-Max-Age' => '1800',
|
||||
'Access-Control-Allow-Methods' => '*',
|
||||
'Access-Control-Allow-Headers' => '*',
|
||||
];
|
||||
|
||||
public function process(Request $request, callable $handler): Response
|
||||
{
|
||||
$path = trim($request->path(), '/');
|
||||
if (!str_starts_with($path, 'api/') && !str_starts_with($path, 'admin/')) {
|
||||
return $handler($request);
|
||||
}
|
||||
|
||||
$header = $this->header;
|
||||
|
||||
$origin = $request->header('origin');
|
||||
if ($origin) {
|
||||
$info = parse_url($origin);
|
||||
$corsDomain = explode(',', config('buildadmin.cors_request_domain', ''));
|
||||
$corsDomain[] = $request->host(true);
|
||||
|
||||
if (
|
||||
in_array('*', $corsDomain)
|
||||
|| in_array($origin, $corsDomain)
|
||||
|| (isset($info['host']) && in_array($info['host'], $corsDomain))
|
||||
) {
|
||||
$header['Access-Control-Allow-Origin'] = $origin;
|
||||
}
|
||||
}
|
||||
|
||||
if ($request->method() === 'OPTIONS') {
|
||||
return response('', 204, $header);
|
||||
}
|
||||
|
||||
$response = $handler($request);
|
||||
return $response->withHeaders($header);
|
||||
}
|
||||
}
|
||||
49
dafuweng-webman/app/common/middleware/LoadLangPack.php
Normal file
49
dafuweng-webman/app/common/middleware/LoadLangPack.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\middleware;
|
||||
|
||||
use Webman\MiddlewareInterface;
|
||||
use Webman\Http\Request;
|
||||
use Webman\Http\Response;
|
||||
|
||||
/**
|
||||
* 加载控制器语言包中间件(Webman 迁移版,等价 ThinkPHP LoadLangPack)
|
||||
* 根据当前路由加载对应控制器的语言包到 Translator
|
||||
*/
|
||||
class LoadLangPack implements MiddlewareInterface
|
||||
{
|
||||
public function process(Request $request, callable $handler): Response
|
||||
{
|
||||
$path = trim($request->path(), '/');
|
||||
if (str_starts_with($path, 'api/') || str_starts_with($path, 'admin/')) {
|
||||
$this->loadLang($request);
|
||||
}
|
||||
return $handler($request);
|
||||
}
|
||||
|
||||
protected function loadLang(Request $request): void
|
||||
{
|
||||
$controllerPath = get_controller_path($request);
|
||||
if (!$controllerPath) {
|
||||
return;
|
||||
}
|
||||
|
||||
$langSet = config('lang.default_lang', config('translation.locale', 'zh-cn'));
|
||||
$langSet = str_replace('_', '-', strtolower($langSet));
|
||||
|
||||
$path = trim($request->path(), '/');
|
||||
$parts = explode('/', $path);
|
||||
$app = $parts[0] ?? 'api';
|
||||
|
||||
$langFile = base_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . $app
|
||||
. DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR . $langSet
|
||||
. DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $controllerPath) . '.php';
|
||||
|
||||
if (is_file($langFile) && class_exists(\support\Translation::class)) {
|
||||
$translator = \support\Translation::instance();
|
||||
$translator->addResource('phpfile', $langFile, $langSet, $controllerPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user