Files
ars-backend/docs/user/user-api.md
ROOG c6d6534b63 main: 用户管理和会话功能初始实现
- 添加用户管理功能的测试,包括创建、更新、停用、激活用户及用户登录 JWT 测试
- 提供用户管理相关的请求验证类与控制器
- 引入 CORS 配置信息,支持跨域请求
- 添加数据库播种器以便创建根用户
- 配置 API 默认使用 JWT 认证
- 添加聊天会话和消息的模型、迁移文件及关联功能
2025-12-14 17:49:08 +08:00

85 lines
2.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 接口文档JWT无状态 API
基地址:`http://localhost:8000/api`(容器默认映射 8000 端口)
自然语言:中文
- 认证方式:在请求头添加 `Authorization: Bearer {token}`
- 默认账号(来自 `php artisan db:seed``root@example.com` / `Root@123456`
- 所有接口均返回 JSON失败时返回 `{ "message": "错误信息" }`
- 跨域:默认允许 `http://localhost:5173`,可通过环境变量 `CORS_ALLOWED_ORIGINS`(逗号分隔多个域名)调整。
## 健康检查
- `GET /health`
响应:`{ "status": "ok" }`
## 登录
- `POST /login`
- 请求体:
```json
{ "email": "user@example.com", "password": "Password123" }
```
- 响应 200
```json
{
"token": "jwt-token",
"token_type": "bearer",
"expires_in": 3600,
"user": { "id": 1, "name": "root", "email": "root@example.com", "is_active": true }
}
```
- 401凭证无效403用户已停用。
## 当前用户
- `GET /me`(需要 JWT
- 响应 200当前登录用户信息。
## 用户管理(需 JWT
字段约束:`name` 必填字符串(<=255`email` 邮箱唯一、`password` 最少 8 字符。
### 用户列表
- `GET /users`
- 查询参数:`page`(默认 1`per_page`(默认 15最大 100
- 响应 200分页列表`data` 为用户数组,包含 `id/name/email/is_active/created_at/updated_at`
### 创建用户
- `POST /users`
- 请求体:
```json
{ "name": "Alice", "email": "alice@example.com", "password": "Password123" }
```
- 响应 201新建用户`is_active: true`)。
### 更新用户
- `PUT /users/{id}`
- 请求体(任意字段可选):
```json
{ "name": "New Name", "email": "new@example.com", "password": "NewPass123" }
```
- 响应 200更新后的用户。
### 停用用户
- `POST /users/{id}/deactivate`
- 响应 200`is_active``false`
### 启用用户
- `POST /users/{id}/activate`
- 响应 200`is_active``true`
## 示例cURL
```bash
# 登录
curl -X POST http://localhost:8000/api/login \
-H "Content-Type: application/json" \
-d '{"email":"root@example.com","password":"Root@123456"}'
# 创建用户(替换 TOKEN
curl -X POST http://localhost:8000/api/users \
-H "Authorization: Bearer TOKEN" \
-H "Content-Type: application/json" \
-d '{"name":"Alice","email":"alice@example.com","password":"Password123"}'
# 获取用户列表(替换 TOKEN
curl -X GET http://localhost:8000/api/users \
-H "Authorization: Bearer TOKEN"
```