Appearance
收藏和点赞接口
类说明
Star 类提供文章收藏(star)和点赞(like)功能。
功能特点:
- 支持收藏和点赞两种类型
- 未登录用户可以点赞,但收藏需要登录
- 自动统计数量
- 支持分页查询收藏/点赞列表
使用说明:
type参数只能是star(收藏)或like(点赞)- 收藏操作需要登录
- 点赞操作未登录也可进行(user_id=0)
获取收藏/点赞列表
Method : GET
URL : /wp-json/vtheme/v1/stars
Auth required : True
Query Parameters :
type: [string] 必填,star或likepage: [integer] 页码,默认1per_page: [integer] 每页数量,默认20(范围1-50)
Success Response
Code : 200 OK
Content example :
json
[
{
"id": 1,
"user_id": 1,
"object_id": 123,
"type": "star",
"created_at": "2023-01-01 12:00:00",
"post_title": "文章标题"
},
{
"id": 2,
"user_id": 1,
"object_id": 456,
"type": "star",
"created_at": "2023-01-02 12:00:00",
"post_title": "另一篇文章"
}
]Error Response
Code : 401 Unauthorized
Content example :
json
{
"error": "401"
}Code : 404 Not Found
Content example :
json
{
"error": "type 是 star 或者 like"
}添加收藏/点赞
Method : POST
URL : /wp-json/vtheme/v1/stars
Auth required : False (点赞不需要登录,收藏需要登录)
Headers :
Content-Type: application/json
Body :
json
{
"object_id": 123,
"type": "star"
}参数说明:
object_id: [integer] 文章IDtype: [string] 类型,star=收藏,like=点赞
Success Response
Code : 201 Created
Content example :
json
{
"user_id": 1,
"object_id": 123,
"type": "star",
"counter": 10
}返回字段说明:
user_id: 用户ID(未登录时为0)object_id: 文章IDtype: 类型counter: 该文章的总收藏/点赞数
Error Response
Code : 400 Bad Request
Content examples :
json
{
"error": "对象ID不能为空"
}json
{
"error": "类型不能为空"
}Code : 401 Unauthorized
Content example :
json
{
"error": "401"
}Code : 404 Not Found
Content examples :
json
{
"error": "对象ID不存在"
}json
{
"error": "type 是 star 或者 like"
}取消收藏/点赞
Method : DELETE
URL : /wp-json/vtheme/v1/stars/{id}
Auth required : True
URL Parameters :
id: [integer] 文章ID(object_id)
Headers :
Authorization: BearerContent-Type: application/json
Body :
json
{
"type": "star"
}参数说明:
type: [string] 类型,star=收藏,like=点赞
Success Response
Code : 200 OK
Content example :
json
[]Response Headers :
X-VT-Counter: 剩余数量
Error Response
Code : 401 Unauthorized
Content example :
json
{
"error": "401"
}Code : 404 Not Found
Content example :
json
{
"error": "type 是 star 或者 like"
}使用示例
JavaScript Fetch API
javascript
// 添加收藏
fetch('/wp-json/vtheme/v1/stars', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
},
body: JSON.stringify({
object_id: 123,
type: 'star'
})
})
.then(response => response.json())
.then(data => {
console.log('收藏成功,当前总数:', data.counter);
});
// 取消收藏
fetch('/wp-json/vtheme/v1/stars/123', {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
},
body: JSON.stringify({
type: 'star'
})
})
.then(response => {
const counter = response.headers.get('X-VT-Counter');
console.log('取消成功,剩余数量:', counter);
});
// 获取收藏列表
fetch('/wp-json/vtheme/v1/stars?type=star&page=1&per_page=20', {
headers: {
'Authorization': 'Bearer ' + token
}
})
.then(response => response.json())
.then(data => {
console.log('收藏列表:', data);
});注意事项
- 重复操作:重复添加收藏/点赞不会报错,只会返回已有的记录
- 未登录点赞:未登录用户可以点赞,user_id 为 0
- 数量统计:每次操作后都会返回最新的总数量
- 删除响应:删除操作通过响应头
X-VT-Counter返回剩余数量