评论接口
评论管理相关的 API 接口文档。
获取评论列表
接口信息
GET /api/comments
Authorization: Bearer {token}
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| page | integer | 否 | 页码,默认1 |
| page_size | integer | 否 | 每页数量,默认20 |
| article_id | integer | 否 | 文章ID |
| status | integer | 否 | 状态(0待审/1通过/2拒绝) |
| keyword | string | 否 | 关键词搜索 |
响应示例
{
"code": 200,
"data": {
"list": [
{
"id": 1,
"article_id": 10,
"article_title": "Vue 3 新特性",
"user": {
"id": 1,
"nickname": "张三",
"avatar": "https://example.com/avatar.jpg"
},
"content": "写得很好!",
"parent_id": 0,
"like_count": 5,
"status": 1,
"ip": "127.0.0.1",
"create_time": "2024-01-15 10:00:00"
}
],
"total": 100,
"page": 1,
"page_size": 20
}
}
获取文章评论
接口信息
GET /api/articles/{id}/comments
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| page | integer | 否 | 页码 |
| page_size | integer | 否 | 每页数量 |
| order | string | 否 | 排序(latest/hot) |
响应示例
{
"code": 200,
"data": {
"list": [
{
"id": 1,
"user": {
"id": 1,
"nickname": "张三",
"avatar": "https://example.com/avatar.jpg"
},
"content": "写得很好!",
"parent_id": 0,
"replies": [
{
"id": 2,
"user": {
"id": 2,
"nickname": "李四",
"avatar": "https://example.com/avatar2.jpg"
},
"content": "同意",
"parent_id": 1,
"create_time": "2024-01-15 11:00:00"
}
],
"like_count": 5,
"create_time": "2024-01-15 10:00:00"
}
],
"total": 50
}
}
发表评论
接口信息
POST /api/comments
Authorization: Bearer {token}
Content-Type: application/json
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| article_id | integer | 是 | 文章ID |
| content | string | 是 | 评论内容 |
| parent_id | integer | 否 | 父评论ID(回复) |
请求示例
{
"article_id": 10,
"content": "写得很好!",
"parent_id": 0
}
响应示例
{
"code": 201,
"message": "评论成功",
"data": {
"id": 1,
"content": "写得很好!",
"status": 1,
"create_time": "2024-01-15 10:00:00"
}
}
更新评论
接口信息
PUT /api/comments/{id}
Authorization: Bearer {token}
Content-Type: application/json
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| content | string | 是 | 评论内容 |
响应示例
{
"code": 200,
"message": "更新成功"
}
删除评论
接口信息
DELETE /api/comments/{id}
Authorization: Bearer {token}
响应示例
{
"code": 200,
"message": "删除成功"
}
评论审核
审核评论
PUT /api/comments/{id}/audit
Authorization: Bearer {token}
Content-Type: application/json
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| status | integer | 是 | 状态(1通过/2拒绝) |
| reason | string | 否 | 拒绝原因 |
请求示例
{
"status": 1
}
批量审核
PUT /api/comments/batch/audit
Authorization: Bearer {token}
Content-Type: application/json
请求示例
{
"ids": [1, 2, 3],
"status": 1
}
点赞评论
接口信息
POST /api/comments/{id}/like
Authorization: Bearer {token}
响应示例
{
"code": 200,
"message": "点赞成功",
"data": {
"like_count": 6
}
}
取消点赞
接口信息
DELETE /api/comments/{id}/like
Authorization: Bearer {token}
响应示例
{
"code": 200,
"message": "取消成功",
"data": {
"like_count": 5
}
}
举报评论
接口信息
POST /api/comments/{id}/report
Authorization: Bearer {token}
Content-Type: application/json
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| reason | string | 是 | 举报原因 |
| description | string | 否 | 详细描述 |
请求示例
{
"reason": "spam",
"description": "垃圾广告"
}
代码示例
JavaScript
import axios from 'axios'
// 发表评论
const postComment = async (data) => {
const res = await axios.post('/api/comments', data, {
headers: {
Authorization: `Bearer ${localStorage.getItem('token')}`
}
})
return res.data
}
// 获取文章评论
const getArticleComments = async (articleId, params) => {
const res = await axios.get(`/api/articles/${articleId}/comments`, {
params
})
return res.data
}
// 点赞评论
const likeComment = async (id) => {
const res = await axios.post(`/api/comments/${id}/like`, {}, {
headers: {
Authorization: `Bearer ${localStorage.getItem('token')}`
}
})
return res.data
}
PHP
<?php
$token = 'YOUR_JWT_TOKEN';
// 发表评论
$url = 'http://your-domain.com/api/comments';
$data = [
'article_id' => 10,
'content' => '写得很好!',
'parent_id' => 0
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $token,
]);
$response = curl_exec($ch);
curl_close($ch);
错误处理
常见错误
400 内容为空
{
"code": 400,
"message": "评论内容不能为空"
}
403 评论已关闭
{
"code": 403,
"message": "该文章已关闭评论"
}
429 评论过于频繁
{
"code": 429,
"message": "评论过于频繁,请稍后再试"
}
