Skip to content

帖子接口

帖子接口用于创建、查询、更新和删除帖子内容。

获取帖子列表

http
GET /api/posts?page=1&limit=10&category=tech&tag=vue&sort=latest

查询参数:

参数类型必填说明
pagenumber页码,默认1
limitnumber每页数量,默认10
categorystring分类ID
tagstring标签名称
sortstring排序方式:latest(最新)、popular(热门)、comments(评论多)
searchstring搜索关键词

响应示例:

json
{
  "success": true,
  "data": {
    "posts": [
      {
        "id": "post-123",
        "title": "帖子标题",
        "content": "帖子内容...",
        "author": {
          "id": "user-123",
          "username": "user123",
          "avatar": "https://example.com/avatar.jpg"
        },
        "category": {
          "id": "cat-1",
          "name": "技术讨论"
        },
        "tags": ["vue", "javascript"],
        "likesCount": 42,
        "commentsCount": 15,
        "viewsCount": 234,
        "createdAt": "2024-03-22T10:00:00.000Z",
        "updatedAt": "2024-03-22T12:00:00.000Z"
      }
    ],
    "pagination": {
      "page": 1,
      "limit": 10,
      "total": 156,
      "totalPages": 16
    }
  },
  "timestamp": "2024-03-22T12:00:00.000Z"
}

获取帖子详情

http
GET /api/posts/{postId}

路径参数:

参数类型说明
postIdstring帖子ID

创建帖子

http
POST /api/posts
Authorization: Bearer <token>
Content-Type: application/json

{
  "title": "我的第一篇帖子",
  "content": "帖子内容...",
  "categoryId": "category-id",
  "tags": ["vue", "javascript"]
}

请求参数:

参数类型必填说明
titlestring帖子标题
contentstring帖子内容,支持Markdown
categoryIdstring分类ID
tagsarray标签数组

更新帖子

http
PUT /api/posts/{postId}
Authorization: Bearer <token>
Content-Type: application/json

{
  "title": "更新后的标题",
  "content": "更新后的内容",
  "categoryId": "new-category-id",
  "tags": ["vue", "typescript"]
}

删除帖子

http
DELETE /api/posts/{postId}
Authorization: Bearer <token>

点赞帖子

http
POST /api/posts/{postId}/like
Authorization: Bearer <token>

响应示例:

json
{
  "success": true,
  "data": {
    "liked": true,
    "likesCount": 43
  },
  "timestamp": "2024-03-22T12:00:00.000Z"
}

取消点赞

http
DELETE /api/posts/{postId}/like
Authorization: Bearer <token>

收藏帖子

http
POST /api/posts/{postId}/bookmark
Authorization: Bearer <token>

取消收藏

http
DELETE /api/posts/{postId}/bookmark
Authorization: Bearer <token>

获取用户帖子

http
GET /api/users/{userId}/posts?page=1&limit=10

搜索帖子

http
GET /api/posts/search?q=vue&page=1&limit=10

查询参数:

参数类型必填说明
qstring搜索关键词
pagenumber页码
limitnumber每页数量

获取热门帖子

http
GET /api/posts/hot?days=7&limit=10

获取推荐帖子

http
GET /api/posts/recommended
Authorization: Bearer <token>

错误码

错误码说明
POST_NOT_FOUND帖子不存在
NO_PERMISSION无权限操作
INVALID_CONTENT内容无效
POST_LOCKED帖子已锁定

下一步

基于 MIT 许可证发布