用户接口
用户管理相关的 API 接口文档。
获取用户列表
接口信息
GET /api/users
Authorization: Bearer {token}
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| page | integer | 否 | 页码,默认1 |
| page_size | integer | 否 | 每页数量,默认20 |
| keyword | string | 否 | 关键词搜索 |
| status | integer | 否 | 状态(0禁用/1正常) |
| role_id | integer | 否 | 角色ID |
响应示例
{
"code": 200,
"data": {
"list": [
{
"id": 1,
"username": "admin",
"nickname": "管理员",
"email": "admin@example.com",
"avatar": "https://example.com/avatar.jpg",
"role": {
"id": 1,
"name": "管理员"
},
"status": 1,
"last_login_time": "2024-01-15 10:00:00",
"create_time": "2024-01-01 00:00:00"
}
],
"total": 100,
"page": 1,
"page_size": 20
}
}
获取用户详情
接口信息
GET /api/users/{id}
Authorization: Bearer {token}
响应示例
{
"code": 200,
"data": {
"id": 1,
"username": "admin",
"nickname": "管理员",
"email": "admin@example.com",
"phone": "13800138000",
"avatar": "https://example.com/avatar.jpg",
"gender": 1,
"birthday": "1990-01-01",
"bio": "个人简介",
"role": {
"id": 1,
"name": "管理员"
},
"status": 1,
"article_count": 50,
"comment_count": 100,
"last_login_ip": "127.0.0.1",
"last_login_time": "2024-01-15 10:00:00",
"create_time": "2024-01-01 00:00:00"
}
}
获取当前用户信息
接口信息
GET /api/user/profile
Authorization: Bearer {token}
响应示例
{
"code": 200,
"data": {
"id": 1,
"username": "admin",
"nickname": "管理员",
"email": "admin@example.com",
"avatar": "https://example.com/avatar.jpg",
"permissions": [
"article.create",
"article.update",
"article.delete"
]
}
}
创建用户
接口信息
POST /api/users
Authorization: Bearer {token}
Content-Type: application/json
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| username | string | 是 | 用户名 |
| password | string | 是 | 密码 |
| nickname | string | 是 | 昵称 |
| string | 是 | 邮箱 | |
| phone | string | 否 | 手机号 |
| role_id | integer | 是 | 角色ID |
| status | integer | 否 | 状态,默认1 |
请求示例
{
"username": "zhangsan",
"password": "password123",
"nickname": "张三",
"email": "zhangsan@example.com",
"phone": "13800138001",
"role_id": 2,
"status": 1
}
响应示例
{
"code": 201,
"message": "创建成功",
"data": {
"id": 10,
"username": "zhangsan",
"nickname": "张三"
}
}
更新用户信息
接口信息
PUT /api/users/{id}
Authorization: Bearer {token}
Content-Type: application/json
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| nickname | string | 否 | 昵称 |
| string | 否 | 邮箱 | |
| phone | string | 否 | 手机号 |
| avatar | string | 否 | 头像URL |
| gender | integer | 否 | 性别(0未知/1男/2女) |
| birthday | string | 否 | 生日 |
| bio | string | 否 | 个人简介 |
| role_id | integer | 否 | 角色ID |
| status | integer | 否 | 状态 |
响应示例
{
"code": 200,
"message": "更新成功"
}
更新当前用户信息
接口信息
PUT /api/user/profile
Authorization: Bearer {token}
Content-Type: application/json
请求参数
与更新用户信息相同,但不能修改 role_id 和 status。
修改密码
接口信息
PUT /api/user/password
Authorization: Bearer {token}
Content-Type: application/json
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| old_password | string | 是 | 旧密码 |
| new_password | string | 是 | 新密码 |
| confirm_password | string | 是 | 确认密码 |
请求示例
{
"old_password": "old123",
"new_password": "new123",
"confirm_password": "new123"
}
响应示例
{
"code": 200,
"message": "密码修改成功"
}
重置密码
接口信息
POST /api/users/{id}/reset-password
Authorization: Bearer {token}
Content-Type: application/json
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| new_password | string | 是 | 新密码 |
响应示例
{
"code": 200,
"message": "重置成功"
}
删除用户
接口信息
DELETE /api/users/{id}
Authorization: Bearer {token}
响应示例
{
"code": 200,
"message": "删除成功"
}
批量操作
批量更新状态
PUT /api/users/batch
Authorization: Bearer {token}
Content-Type: application/json
请求示例
{
"ids": [1, 2, 3],
"data": {
"status": 1
}
}
批量删除
DELETE /api/users/batch
Authorization: Bearer {token}
Content-Type: application/json
请求示例
{
"ids": [1, 2, 3]
}
用户文章
获取用户文章列表
GET /api/users/{id}/articles
Authorization: Bearer {token}
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| page | integer | 否 | 页码 |
| page_size | integer | 否 | 每页数量 |
| status | integer | 否 | 状态筛选 |
响应示例
{
"code": 200,
"data": {
"list": [
{
"id": 1,
"title": "文章标题",
"summary": "文章摘要",
"view_count": 100,
"create_time": "2024-01-15 10:00:00"
}
],
"total": 50
}
}
用户收藏
获取用户收藏列表
GET /api/user/favorites
Authorization: Bearer {token}
收藏文章
POST /api/user/favorites
Authorization: Bearer {token}
Content-Type: application/json
请求示例
{
"article_id": 1
}
取消收藏
DELETE /api/user/favorites/{article_id}
Authorization: Bearer {token}
代码示例
JavaScript
import axios from 'axios'
// 获取当前用户信息
const getUserProfile = async () => {
const res = await axios.get('/api/user/profile', {
headers: {
Authorization: `Bearer ${localStorage.getItem('token')}`
}
})
return res.data
}
// 更新个人信息
const updateProfile = async (data) => {
const res = await axios.put('/api/user/profile', data, {
headers: {
Authorization: `Bearer ${localStorage.getItem('token')}`
}
})
return res.data
}
// 修改密码
const changePassword = async (data) => {
const res = await axios.put('/api/user/password', data, {
headers: {
Authorization: `Bearer ${localStorage.getItem('token')}`
}
})
return res.data
}
PHP
<?php
$token = 'YOUR_JWT_TOKEN';
// 获取用户列表
$url = 'http://your-domain.com/api/users?page=1&page_size=20';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $token,
]);
$response = curl_exec($ch);
curl_close($ch);
错误处理
常见错误
400 参数错误
{
"code": 400,
"message": "用户名不能为空"
}
404 用户不存在
{
"code": 404,
"message": "用户不存在"
}
409 用户名已存在
{
"code": 409,
"message": "用户名已被使用"
}
422 验证失败
{
"code": 422,
"message": "数据验证失败",
"errors": {
"email": ["邮箱格式不正确"],
"password": ["密码长度至少6位"]
}
}
