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);
}
}
+37
View File
@@ -0,0 +1,37 @@
<?php
declare (strict_types = 1);
namespace app\api\controller;
/**
* API 示例控制器
*/
class Index extends BaseController
{
/**
* 首页接口
* @return \think\response\Json
*/
public function index()
{
$data = [
'name' => 'ThinkPHP API',
'version' => app()->version(),
'message' => 'Welcome to ThinkPHP API Application',
];
return $this->success($data);
}
/**
* 健康检查接口
* @return \think\response\Json
*/
public function health()
{
return $this->success([
'status' => 'ok',
'timestamp' => date('Y-m-d H:i:s'),
]);
}
}
+81
View File
@@ -0,0 +1,81 @@
<?php
declare (strict_types = 1);
namespace app\api\controller;
/**
* 用户控制器示例
*/
class User extends BaseController
{
/**
* 用户登录
* @return \think\response\Json
*/
public function login()
{
$data = $this->request->post();
// 验证数据
$this->validate($data, [
'username' => 'require',
'password' => 'require',
], [
'username.require' => '用户名不能为空',
'password.require' => '密码不能为空',
]);
// TODO: 实际的登录逻辑
return $this->success([
'token' => 'example_token_' . md5($data['username']),
'username' => $data['username'],
], '登录成功');
}
/**
* 获取用户信息
* @return \think\response\Json
*/
public function info()
{
// TODO: 从 token 或 session 中获取用户信息
$userInfo = [
'id' => 1,
'username' => 'demo_user',
'nickname' => '演示用户',
'avatar' => '',
'email' => 'demo@example.com',
'created_at' => date('Y-m-d H:i:s'),
];
return $this->success($userInfo);
}
/**
* 用户注册
* @return \think\response\Json
*/
public function register()
{
$data = $this->request->post();
// 验证数据
$this->validate($data, [
'username' => 'require|length:3,20',
'password' => 'require|length:6,20',
'email' => 'require|email',
], [
'username.require' => '用户名不能为空',
'username.length' => '用户名长度3-20位',
'password.require' => '密码不能为空',
'password.length' => '密码长度6-20位',
'email.require' => '邮箱不能为空',
'email.email' => '邮箱格式不正确',
]);
// TODO: 实际的注册逻辑
return $this->success([
'user_id' => rand(1000, 9999),
], '注册成功');
}
}
+193
View File
@@ -0,0 +1,193 @@
<?php
declare(strict_types=1);
namespace app\api\controller;
use app\api\common\Jwt;
use app\api\common\Response;
use app\api\controller\BaseController;
use app\api\service\AuthService;
use think\exception\ValidateException;
/**
* 认证控制器 (v1版本)
* 处理用户登录、注册、Token 刷新等
*/
class Auth extends BaseController
{
/**
* @var AuthService
*/
protected AuthService $authService;
public function __construct()
{
parent::__construct();
$this->authService = new AuthService();
}
/**
* 用户登录
* POST /api/v1/auth/login
* @return \think\response\Json
*/
public function login()
{
try {
$data = $this->request->post();
// 验证参数
validate($data, [
'username' => 'require',
'password' => 'require',
], [
'username.require' => '用户名不能为空',
'password.require' => '密码不能为空',
]);
$result = $this->authService->login(
$data['username'],
$data['password']
);
return Response::success($result, '登录成功');
} catch (ValidateException $e) {
return Response::error($e->getMessage(), 400);
} catch (\Exception $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
/**
* 用户注册
* POST /api/v1/auth/register
* @return \think\response\Json
*/
public function register()
{
try {
$data = $this->request->post();
// 验证参数
validate($data, [
'username' => 'require|length:3,20|alphaNum',
'password' => 'require|length:6,20',
'email' => 'email',
], [
'username.require' => '用户名不能为空',
'username.length' => '用户名长度3-20位',
'username.alphaNum' => '用户名只能包含字母和数字',
'password.require' => '密码不能为空',
'password.length' => '密码长度6-20位',
'email.email' => '邮箱格式不正确',
]);
$result = $this->authService->register(
$data['username'],
$data['password'],
$data['email'] ?? null,
$data['formtypeid'] ?? null
);
return Response::success($result, '注册成功');
} catch (ValidateException $e) {
return Response::error($e->getMessage(), 400);
} catch (\Exception $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
/**
* 刷新 Token
* POST /api/v1/auth/refresh
* @return \think\response\Json
*/
public function refresh()
{
try {
$data = $this->request->post();
if (empty($data['refresh_token'])) {
return Response::error('刷新令牌不能为空', 400);
}
$result = $this->authService->refreshToken($data['refresh_token']);
return Response::success($result, '刷新成功');
} catch (\Exception $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
/**
* 获取当前用户信息
* GET /api/v1/auth/me
* @return \think\response\Json
*/
public function me()
{
try {
$payload = $this->request->payload ?? null;
if (!$payload) {
return Response::error('未登录', 401);
}
$result = $this->authService->getUserInfo($payload['userid']);
return Response::success($result);
} catch (\Exception $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
/**
* 退出登录
* POST /api/v1/auth/logout
* @return \think\response\Json
*/
public function logout()
{
// JWT 无状态,退出只需客户端删除 Token
// 如果需要服务端失效,可以将 Token 加入黑名单(需要 Redis 支持)
return Response::success([], '退出成功');
}
/**
* 修改密码
* POST /api/v1/auth/password
* @return \think\response\Json
*/
public function password()
{
try {
$payload = $this->request->payload ?? null;
if (!$payload) {
return Response::error('未登录', 401);
}
$data = $this->request->post();
validate($data, [
'old_password' => 'require',
'new_password' => 'require|length:6,20|confirm:confirm_password',
], [
'old_password.require' => '原密码不能为空',
'new_password.require' => '新密码不能为空',
'new_password.length' => '新密码长度6-20位',
'new_password.confirm' => '两次密码输入不一致',
]);
$this->authService->changePassword(
$payload['userid'],
$data['old_password'],
$data['new_password']
);
return Response::success([], '密码修改成功');
} catch (ValidateException $e) {
return Response::error($e->getMessage(), 400);
} catch (\Exception $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
}