174 lines
4.8 KiB
PHP
174 lines
4.8 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace app\api\controller\v1;
|
|
|
|
use app\api\common\Response;
|
|
use app\api\controller\BaseController;
|
|
use app\api\service\AuthService;
|
|
use think\App;
|
|
use think\exception\ValidateException;
|
|
|
|
/**
|
|
* v1 认证控制器
|
|
*/
|
|
class Auth extends BaseController
|
|
{
|
|
protected AuthService $authService;
|
|
|
|
public function __construct(App $app)
|
|
{
|
|
parent::__construct($app);
|
|
$this->authService = new AuthService();
|
|
}
|
|
|
|
/**
|
|
* 用户登录
|
|
* POST /api/v1/auth/login
|
|
*/
|
|
public function login()
|
|
{
|
|
try {
|
|
$data = $this->request->post();
|
|
|
|
validate([
|
|
'username' => 'require',
|
|
'password' => 'require',
|
|
], [
|
|
'username.require' => '用户名不能为空',
|
|
'password.require' => '密码不能为空',
|
|
])->check($data);
|
|
|
|
$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
|
|
*/
|
|
public function register()
|
|
{
|
|
try {
|
|
$data = $this->request->post();
|
|
|
|
validate([
|
|
'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' => '邮箱格式不正确',
|
|
])->check($data);
|
|
|
|
$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
|
|
*/
|
|
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
|
|
*/
|
|
public function me()
|
|
{
|
|
try {
|
|
$userid = $this->getLoginUserId();
|
|
|
|
$result = $this->authService->getUserInfo($userid);
|
|
|
|
return Response::success($result);
|
|
} catch (\Exception $e) {
|
|
return Response::error($e->getMessage(), $e->getCode() ?: 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 退出登录
|
|
* POST /api/v1/auth/logout
|
|
*/
|
|
public function logout()
|
|
{
|
|
return Response::success([], '退出成功');
|
|
}
|
|
|
|
/**
|
|
* 修改密码
|
|
* POST /api/v1/auth/password
|
|
*/
|
|
public function password()
|
|
{
|
|
try {
|
|
$userid = $this->getLoginUserId();
|
|
|
|
$data = $this->request->post();
|
|
|
|
validate([
|
|
'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' => '两次密码输入不一致',
|
|
])->check($data);
|
|
|
|
$this->authService->changePassword(
|
|
$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);
|
|
}
|
|
}
|
|
}
|