Files
dafuweng-buildadmin/dafuweng-webman/app/support/BaseController.php
2026-03-18 15:10:40 +08:00

102 lines
3.1 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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);
$options = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
if (defined('JSON_INVALID_UTF8_SUBSTITUTE')) {
$options |= JSON_INVALID_UTF8_SUBSTITUTE;
}
$jsonBody = json_encode($body, $options);
if ($jsonBody === false) {
$jsonBody = '{"code":0,"msg":"JSON encode error","time":' . time() . ',"data":[]}';
}
return response($jsonBody, $statusCode, $headers);
}
}