Initial commit: ThinkPHP refactor (tp)

Made-with: Cursor
This commit is contained in:
nepiedg
2026-04-02 02:13:12 +00:00
commit 166940d5a6
127 changed files with 22225 additions and 0 deletions
+115
View File
@@ -0,0 +1,115 @@
<?php
declare (strict_types = 1);
namespace app\api\controller;
use think\App;
use think\exception\ValidateException;
use think\Validate;
/**
* API 基础控制器
*/
abstract class BaseController
{
/**
* Request实例
* @var \think\Request
*/
protected $request;
/**
* 应用实例
* @var \think\App
*/
protected $app;
/**
* 构造方法
* @access public
* @param App $app 应用对象
*/
public function __construct(App $app)
{
$this->app = $app;
$this->request = $this->app->request;
// 控制器初始化
$this->initialize();
}
// 初始化
protected function initialize()
{}
/**
* 成功响应
* @param mixed $data 返回数据
* @param string $message 提示信息
* @param int $code 状态码
* @return \think\response\Json
*/
protected function success($data = [], string $message = 'success', int $code = 200)
{
return json([
'code' => $code,
'msg' => $message,
'data' => $data,
'time' => time(),
]);
}
/**
* 失败响应
* @param string $message 提示信息
* @param int $code 状态码
* @param mixed $data 返回数据
* @return \think\response\Json
*/
protected function error(string $message = 'error', int $code = 400, $data = [])
{
return json([
'code' => $code,
'msg' => $message,
'data' => $data,
'time' => time(),
]);
}
/**
* 验证数据
* @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, $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->request->isBatchValidate()) {
$v->batch(true);
}
return $v->failException(true)->check($data);
}
}