Initial commit: ThinkPHP refactor (tp)
Made-with: Cursor
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user