点赞或取消点赞
1. 接口定位
- 接口名称: 点赞或取消点赞
- 所属域: client/moments
- 业务目标: 对指定动态点赞,或取消自己之前的点赞
2. 请求定义
- Method:
POST - Path:
/chatx/moments/like - Content-Type: 推荐
application/json - operationID: 必填,请通过 Header
operationID传入 - 鉴权: 必填,请通过 Header
token传入有效登录令牌 - 幂等性: 对同一
postID + 当前用户 + cancel组合可视为幂等
3. 请求参数
Header 参数
| 字段 | 必填 | 类型 | 说明 |
|---|---|---|---|
| operationID | 是 | string | 链路追踪 ID |
| token | 是 | string | 用户登录 token |
Body 参数
| 字段 | 必填 | 类型 | 说明 |
|---|---|---|---|
| postID | 是 | string | 动态 ID |
| cancel | 否 | bool | false 或不传表示点赞;true 表示取消点赞 |
字段说明
- API 层会在转发前把当前登录用户的
userID注入请求体,客户端无需传入。 - 当前实现会先校验动态是否存在。
- 点赞写入采用
postID + userID唯一键 upsert;取消点赞按postID + userID删除。
4. 响应结构
通用响应包裹
| 字段 | 类型 | 说明 |
|---|---|---|
| errCode | int | 错误码,0 表示成功 |
| errMsg | string | 错误简述 |
| errDlt | string | 错误详情 |
| data | object | 业务数据 |
data 字段
| 字段 | 类型 | 说明 |
|---|---|---|
| likeCount | int64 | 操作完成后的最新点赞数 |
5. 业务规则
- 路由统一要求
token。 - API 层从 token 中解析当前用户,并把该用户作为点赞操作者。
- 服务端 会先确认目标动态存在。
cancel = false时执行点赞 upsert,同一用户重复点赞不会产生重复记录。cancel = true时删除当前用户对该动态的点赞记录。- 返回值始终是操作后的最新点赞总数。
6. 错误码与失败场景
| 错误码 | 场景 | 典型报错 |
|---|---|---|
| 1001 | Header 缺少 operationID | header must have operationID |
| 1001 | Header 缺少 token | token is empty |
| 1001 | postID 为空 | postID and userID are required |
| - | 动态不存在 | 由动态查询链路返回 |
| - | 点赞或取消点赞失败 | 由数据库层返回 |
7. 示例
fetch 请求示例
javascript
fetch("http://localhost:10010/chatx/moments/like", {
method: "POST",
headers: {
operationID: "moments-like-001",
token: "user-token",
"Content-Type": "application/json",
},
body: JSON.stringify({
postID: "p_10001",
cancel: false,
}),
})
.then((res) => res.json())
.then((data) => console.log(data));请求示例(JSON)
json
{
"postID": "p_10001",
"cancel": false
}成功响应示例
json
{
"errCode": 0,
"errMsg": "",
"errDlt": "",
"data": {
"likeCount": 12
}
}失败响应示例
json
{
"errCode": 1001,
"errMsg": "ArgsError",
"errDlt": "postID and userID are required"
}8. 时序流程
- 校验
operationID和token。 - API 层解析 token,并把当前用户
userID注入请求。 - 服务端 校验
postID和userID,再确认动态存在。 - 根据
cancel选择点赞 upsert 或取消点赞删除。 - 统计该动态当前点赞总数。
- 返回最新
likeCount。
9. 变更记录
- 2026-03-31: 首版发布,基于 chatx moments API、协议定义和 服务端、数据库实现整理。