166940d5a6
Made-with: Cursor
82 lines
2.1 KiB
PHP
82 lines
2.1 KiB
PHP
<?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),
|
|
], '注册成功');
|
|
}
|
|
}
|