278 lines
7.0 KiB
Markdown
278 lines
7.0 KiB
Markdown
# 观看历史功能 - zhibo-h后台实现说明
|
||
|
||
## 📌 重要说明
|
||
|
||
**zhibo-h后台项目已经实现了完整的观看历史功能!**
|
||
|
||
不需要在 `live-streaming` 项目中实现,所有功能都在 `Zhibo/zhibo-h` 项目中。
|
||
|
||
---
|
||
|
||
## ✅ 已实现的功能
|
||
|
||
### 后台实现(zhibo-h项目)
|
||
|
||
#### 1. UserActivityRecordController
|
||
**位置**: `Zhibo/zhibo-h/crmeb-front/src/main/java/com/zbkj/front/controller/UserActivityRecordController.java`
|
||
|
||
**API路由**: `/api/front/activity`
|
||
|
||
**功能列表**:
|
||
|
||
| 功能 | 方法 | 路径 | 说明 |
|
||
|------|------|------|------|
|
||
| 记录观看历史 | POST | `/activity/view/record` | 记录用户观看直播间/作品 |
|
||
| 获取观看历史 | GET | `/activity/view/history` | 分页获取观看历史列表 |
|
||
| 删除单条历史 | DELETE | `/activity/view/history/{historyId}` | 删除指定的观看历史 |
|
||
| 清空观看历史 | DELETE | `/activity/view/history` | 清空用户的观看历史 |
|
||
| 获取点赞记录 | GET | `/activity/like/records` | 获取点赞记录列表 |
|
||
| 获取关注记录 | GET | `/activity/follow/records` | 获取关注记录列表 |
|
||
| 获取收藏记录 | GET | `/activity/collect/works` | 获取收藏的作品列表 |
|
||
| Token调试 | GET | `/activity/debug/token` | 调试Token状态 |
|
||
|
||
#### 2. WatchHistoryController(备用)
|
||
**位置**: `Zhibo/zhibo-h/crmeb-front/src/main/java/com/zbkj/front/controller/WatchHistoryController.java`
|
||
|
||
**API路由**: `/api/front/watch`
|
||
|
||
---
|
||
|
||
## 🔧 Android端需要调整的地方
|
||
|
||
### 当前问题
|
||
|
||
Android端的 `RoomDetailActivity.java` 中调用的API路径是:
|
||
```java
|
||
POST /api/front/activity/record-view
|
||
```
|
||
|
||
但是zhibo-h后台的实际路径是:
|
||
```java
|
||
POST /api/front/activity/view/record
|
||
```
|
||
|
||
### 解决方案
|
||
|
||
#### 方案1:修改Android端API调用(推荐)
|
||
|
||
修改 `android-app/app/src/main/java/com/example/livestreaming/net/ApiService.java`:
|
||
|
||
```java
|
||
// 当前(错误)
|
||
@POST("api/front/activity/record-view")
|
||
Call<ApiResponse<Map<String, Object>>> recordViewHistoryNew(@Body Map<String, Object> body);
|
||
|
||
// 修改为(正确)
|
||
@POST("api/front/activity/view/record")
|
||
Call<ApiResponse<Map<String, Object>>> recordViewHistoryNew(@Body Map<String, Object> body);
|
||
```
|
||
|
||
同时修改获取观看历史的API:
|
||
|
||
```java
|
||
// 当前(错误)
|
||
@GET("api/front/activity/view-history")
|
||
Call<ApiResponse<PageResponse<Map<String, Object>>>> getViewHistory(
|
||
@Query("type") String type,
|
||
@Query("page") int page,
|
||
@Query("pageSize") int pageSize);
|
||
|
||
// 修改为(正确)
|
||
@GET("api/front/activity/view/history")
|
||
Call<ApiResponse<PageResponse<Map<String, Object>>>> getViewHistory(
|
||
@Query("targetType") String targetType, // 参数名改为targetType
|
||
@Query("page") int page,
|
||
@Query("pageSize") int pageSize);
|
||
```
|
||
|
||
#### 方案2:在后台添加兼容路由(不推荐)
|
||
|
||
在 `UserActivityRecordController` 中添加兼容的路由映射,但这会增加维护成本。
|
||
|
||
---
|
||
|
||
## 📊 API接口详细说明
|
||
|
||
### 1. 记录观看历史
|
||
|
||
**请求**:
|
||
```http
|
||
POST /api/front/activity/view/record
|
||
Content-Type: application/json
|
||
Authorization: Bearer {token}
|
||
|
||
{
|
||
"targetType": "room", // 类型:room-直播间, work-作品, profile-用户主页
|
||
"targetId": "room-123", // 目标ID
|
||
"targetTitle": "精彩直播间", // 标题
|
||
"duration": 120 // 观看时长(秒)
|
||
}
|
||
```
|
||
|
||
**响应**:
|
||
```json
|
||
{
|
||
"code": 200,
|
||
"message": "success",
|
||
"data": {
|
||
"success": true,
|
||
"message": "记录成功"
|
||
}
|
||
}
|
||
```
|
||
|
||
### 2. 获取观看历史
|
||
|
||
**请求**:
|
||
```http
|
||
GET /api/front/activity/view/history?targetType=room&page=1&pageSize=20
|
||
Authorization: Bearer {token}
|
||
```
|
||
|
||
**响应**:
|
||
```json
|
||
{
|
||
"code": 200,
|
||
"message": "success",
|
||
"data": {
|
||
"list": [
|
||
{
|
||
"id": 1,
|
||
"userId": 43,
|
||
"targetType": "room",
|
||
"targetId": "8",
|
||
"targetTitle": "火影忍者",
|
||
"createTime": "2026-01-05 10:00:00",
|
||
"updateTime": "2026-01-05 10:30:00"
|
||
}
|
||
],
|
||
"total": 10,
|
||
"pageNum": 1,
|
||
"pageSize": 20
|
||
}
|
||
}
|
||
```
|
||
|
||
### 3. 清空观看历史
|
||
|
||
**请求**:
|
||
```http
|
||
DELETE /api/front/activity/view/history?targetType=room
|
||
Authorization: Bearer {token}
|
||
```
|
||
|
||
**响应**:
|
||
```json
|
||
{
|
||
"code": 200,
|
||
"message": "清空成功",
|
||
"data": null
|
||
}
|
||
```
|
||
|
||
---
|
||
|
||
## 🗄️ 数据库表结构
|
||
|
||
观看历史数据存储在数据库表中(不是JSON文件):
|
||
|
||
**表名**: `eb_user_activity_record` 或 `eb_watch_history`
|
||
|
||
**字段**:
|
||
- `id` - 主键
|
||
- `user_id` - 用户ID
|
||
- `target_type` - 目标类型(room/work/profile)
|
||
- `target_id` - 目标ID
|
||
- `target_title` - 目标标题
|
||
- `duration` - 观看时长
|
||
- `create_time` - 创建时间
|
||
- `update_time` - 更新时间
|
||
|
||
---
|
||
|
||
## ✅ 优势
|
||
|
||
使用zhibo-h后台的优势:
|
||
|
||
1. **统一管理**: 所有用户数据都在同一个数据库中
|
||
2. **完整功能**: 不仅有观看历史,还有点赞、关注、收藏等记录
|
||
3. **数据持久化**: 使用MySQL数据库,不是JSON文件
|
||
4. **已经实现**: 代码已经写好,只需要调整Android端的API路径
|
||
5. **日志完善**: 有详细的日志记录,方便调试
|
||
|
||
---
|
||
|
||
## 🚀 快速修复步骤
|
||
|
||
### 步骤1:修改Android端ApiService.java
|
||
|
||
找到文件:`android-app/app/src/main/java/com/example/livestreaming/net/ApiService.java`
|
||
|
||
修改以下接口:
|
||
|
||
```java
|
||
// 记录观看历史(新版)
|
||
@POST("api/front/activity/view/record") // 修改路径
|
||
Call<ApiResponse<Map<String, Object>>> recordViewHistoryNew(@Body Map<String, Object> body);
|
||
|
||
// 获取观看历史
|
||
@GET("api/front/activity/view/history") // 修改路径
|
||
Call<ApiResponse<PageResponse<Map<String, Object>>>> getViewHistory(
|
||
@Query("targetType") String targetType, // 修改参数名
|
||
@Query("page") int page,
|
||
@Query("pageSize") int pageSize);
|
||
|
||
// 清除观看历史
|
||
@DELETE("api/front/activity/view/history") // 修改路径
|
||
Call<ApiResponse<String>> clearViewHistory(@Query("targetType") String targetType);
|
||
```
|
||
|
||
### 步骤2:修改RoomDetailActivity.java
|
||
|
||
找到 `recordWatchHistory()` 方法,确保参数名正确:
|
||
|
||
```java
|
||
body.put("targetType", "room"); // 确保是targetType,不是type
|
||
body.put("targetId", roomId);
|
||
body.put("targetTitle", "直播间");
|
||
body.put("duration", 0); // 确保是duration,不是viewDuration
|
||
```
|
||
|
||
### 步骤3:测试
|
||
|
||
1. 重新编译Android应用
|
||
2. 登录应用
|
||
3. 进入直播间
|
||
4. 查看日志,确认API调用成功
|
||
5. 进入"我的记录" -> "观看历史"查看
|
||
|
||
---
|
||
|
||
## 📝 注意事项
|
||
|
||
1. ⚠️ **不要修改live-streaming项目**:所有功能都在zhibo-h中
|
||
2. ⚠️ **确保后台服务运行**:zhibo-h后台需要在8081端口运行
|
||
3. ⚠️ **Token认证**:确保Android端正确传递Token
|
||
4. ⚠️ **参数名称**:注意参数名是 `targetType` 和 `duration`,不是 `type` 和 `viewDuration`
|
||
|
||
---
|
||
|
||
## 🔍 调试方法
|
||
|
||
如果遇到问题,可以使用调试接口:
|
||
|
||
```http
|
||
GET /api/front/activity/debug/token
|
||
Authorization: Bearer {token}
|
||
```
|
||
|
||
这个接口会返回:
|
||
- Token状态
|
||
- 用户ID
|
||
- 是否登录
|
||
|
||
---
|
||
|
||
**最后更新**: 2026-01-05
|
||
**状态**: ✅ 后台已实现,需要调整Android端API路径
|