webman后台

This commit is contained in:
2026-03-07 19:42:22 +08:00
parent 9ed4c1bc58
commit 83725aef88
181 changed files with 19115 additions and 1 deletions

View File

@@ -0,0 +1,94 @@
<?php
declare(strict_types=1);
namespace app\support;
use support\Response;
use Webman\Http\Request as WebmanRequest;
use function response;
/**
* 控制器基础类
* - 适配 Webman Request 注入子类方法签名public function xxx(Webman\Http\Request $request)
* - 移除对 think\App 的依赖,用注入的 $request 替代 $this->app->request
* - 子类在方法开头调用 setRequest($request),之后可用 $this->request 访问
*/
abstract class BaseController
{
/**
* 当前请求实例(由 setRequest($request) 设置,来源于方法参数 $request非 think\App
* @var WebmanRequest|null
*/
protected ?WebmanRequest $request = null;
/**
* 是否批量验证
* @var bool
*/
protected bool $batchValidate = false;
/**
* 初始化
* @access protected
*/
protected function initialize(): void
{
}
/**
* 设置当前请求(子类方法开头调用)
* @param WebmanRequest $request
*/
protected function setRequest(WebmanRequest $request): void
{
$this->request = $request;
}
/**
* 操作成功(封装为返回 Response 的方法,替代原 $this->success() 抛异常)
* @param string $msg 提示消息
* @param mixed $data 返回数据
* @param int $code 业务状态码1=成功)
* @param array $header 可含 statusCode 指定 HTTP 状态
*/
protected function success(string $msg = '', mixed $data = null, int $code = 1, array $header = []): Response
{
return $this->result($msg, $data, $code, $header);
}
/**
* 操作失败(封装为返回 Response 的方法,替代原 $this->error() 抛异常)
* @param string $msg 提示消息
* @param mixed $data 返回数据
* @param int $code 业务状态码0=失败)
* @param array $header 可含 statusCode 指定 HTTP 状态(如 401、409
*/
protected function error(string $msg = '', mixed $data = null, int $code = 0, array $header = []): Response
{
return $this->result($msg, $data, $code, $header);
}
/**
* 返回 API 数据BuildAdmin 格式code, msg, time, data
* 使用 response() 生成 JSON Response
*/
protected function result(string $msg, mixed $data = null, int $code = 0, array $header = []): Response
{
$body = [
'code' => $code,
'msg' => $msg,
'time' => time(),
'data' => $data,
];
$statusCode = $header['statusCode'] ?? 200;
unset($header['statusCode']);
$headers = array_merge(['Content-Type' => 'application/json'], $header);
$jsonBody = json_encode($body, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
return response($jsonBody, $statusCode, $headers);
}
}