本文档引用的文件
AI有声书生成平台的播放记录功能是一个关键的用户体验优化模块,它通过精确记录用户的播放进度、音频时长和章节关联信息,实现了无缝的跨设备播放体验。该系统支持实时进度更新、离线播放恢复、播放历史管理和跨设备同步等功能。
播放记录数据模型采用Prisma ORM进行数据库持久化,通过User和BookChapter两个实体建立了强关联关系,确保了数据的一致性和完整性。系统设计充分考虑了移动端播放场景的特殊需求,提供了灵活的API接口和高效的查询机制。
播放记录功能涉及前后端多个层次的协作:
graph TB
subgraph "前端层"
FE_Player[播放器页面<br/>index.vue]
FE_Store[音频状态管理<br/>audio.ts]
FE_API[API调用封装<br/>request.ts]
end
subgraph "后端层"
BE_Controller[播放记录控制器<br/>player.controller.ts]
BE_Service[播放记录服务<br/>player.service.ts]
BE_DB[(数据库)]
end
subgraph "数据模型"
DM_PlayRecord[PlayRecord模型]
DM_User[User模型]
DM_Chapter[BookChapter模型]
end
FE_Player --> FE_Store
FE_Store --> BE_Controller
BE_Controller --> BE_Service
BE_Service --> BE_DB
BE_DB --> DM_PlayRecord
DM_PlayRecord --> DM_User
DM_PlayRecord --> DM_Chapter
图表来源
章节来源
PlayRecord模型是整个播放记录系统的核心,它定义了用户播放行为的完整数据结构:
| 字段名 | 类型 | 默认值 | 约束 | 描述 |
|---|---|---|---|---|
| id | Int | 自增主键 | @id | 记录唯一标识符 |
| userId | Int | - | - | 关联用户ID |
| chapterId | Int | - | - | 关联章节ID |
| progress | Float | 0 | - | 播放进度(秒) |
| duration | Float | 0 | - | 音频总时长(秒) |
| createdAt | DateTime | now() | @default | 创建时间 |
| updatedAt | DateTime | @updatedAt | - | 更新时间 |
erDiagram
User ||--o{ PlayRecord : "拥有"
BookChapter ||--o{ PlayRecord : "被播放"
PlayRecord {
int id PK
int userId FK
int chapterId FK
float progress
float duration
datetime createdAt
datetime updatedAt
}
User {
int id PK
string phone
string openid
string nickname
string avatar
int memberLevel
datetime createdAt
datetime updatedAt
}
BookChapter {
int id PK
int bookId FK
int parentId
int level
int number
string title
string audioUrl
int audioDuration
datetime createdAt
datetime updatedAt
}
图表来源
系统采用了多层次的索引策略来优化查询性能:
userId_chapterId 确保每个用户对特定章节只有一个播放记录userId 索引支持用户播放历史的快速检索chapterId 索引支持章节维度的统计分析章节来源
播放记录系统的整体架构采用分层设计,从前端交互到后端服务再到数据库存储形成了清晰的职责分离:
sequenceDiagram
participant Client as "客户端应用"
participant Store as "音频状态管理"
participant Controller as "播放记录控制器"
participant Service as "播放记录服务"
participant DB as "数据库"
Client->>Store : 播放状态变化
Store->>Controller : 更新播放进度请求
Controller->>Service : savePlayProgress(userId, chapterId, progress, duration)
Service->>DB : upsert PlayRecord
DB-->>Service : 更新结果
Service-->>Controller : 播放记录
Controller-->>Store : 响应结果
Store-->>Client : 更新UI状态
Note over Client,DB : 实时同步机制
图表来源
系统提供了完整的播放进度管理API集合:
flowchart TD
Start([API请求到达]) --> Auth{身份验证}
Auth --> |通过| Route{路由分发}
Auth --> |失败| Error401[401 未授权]
Route --> GetProgress[GET /progress<br/>获取播放进度]
Route --> SaveProgress[POST /progress<br/>保存播放进度]
Route --> UpdateProgress[PUT /progress/:chapterId<br/>更新播放进度]
Route --> DeleteRecord[DELETE /progress/:chapterId<br/>删除播放记录]
Route --> BatchDelete[DELETE /progress/batch<br/>批量删除]
GetProgress --> ServiceCall1[调用服务层]
SaveProgress --> ServiceCall2[调用服务层]
UpdateProgress --> ServiceCall3[调用服务层]
DeleteRecord --> ServiceCall4[调用服务层]
BatchDelete --> ServiceCall5[调用服务层]
ServiceCall1 --> DB1[数据库查询]
ServiceCall2 --> DB2[数据库upsert]
ServiceCall3 --> DB3[数据库更新]
ServiceCall4 --> DB4[数据库删除]
ServiceCall5 --> DB5[批量删除]
DB1 --> Response1[返回进度列表]
DB2 --> Response2[返回更新记录]
DB3 --> Response3[返回更新结果]
DB4 --> Response4[删除成功]
DB5 --> Response5[批量删除成功]
Response1 --> End([响应客户端])
Response2 --> End
Response3 --> End
Response4 --> End
Response5 --> End
Error401 --> End
图表来源
系统还提供了专门的播放历史获取功能:
sequenceDiagram
participant Client as "客户端"
participant HistoryCtrl as "历史控制器"
participant DB as "数据库"
Client->>HistoryCtrl : GET /history
HistoryCtrl->>HistoryCtrl : 解析查询参数
HistoryCtrl->>DB : 查询AudioRecord
DB-->>HistoryCtrl : 历史记录列表
HistoryCtrl->>HistoryCtrl : 格式化响应数据
HistoryCtrl-->>Client : 历史记录JSON
图表来源
章节来源
服务层实现了智能的播放进度更新策略,采用upsert语义确保数据一致性:
flowchart TD
Input[接收更新请求] --> Validate{验证参数}
Validate --> |失败| Error[抛出错误]
Validate --> |成功| CheckExisting[检查记录是否存在]
CheckExisting --> |存在| UpdateRecord[更新现有记录]
CheckExisting --> |不存在| CreateRecord[创建新记录]
UpdateRecord --> SetFields[设置progress和duration字段]
CreateRecord --> BuildData[构建完整数据]
SetFields --> SaveChanges[保存到数据库]
BuildData --> SaveChanges
SaveChanges --> ReturnResult[返回更新结果]
Error --> ReturnError[返回错误信息]
图表来源
系统提供了最近播放记录的聚合查询功能:
classDiagram
class PlayRecordService {
+getPlayProgress(userId, chapterId) Promise~PlayRecord[]~
+savePlayProgress(userId, chapterId, progress, duration) Promise~PlayRecord~
+updatePlayProgress(userId, chapterId, progress, duration) Promise~PlayRecord~
+deletePlayRecord(userId, chapterId) Promise~PlayRecord~
+getSingleProgress(userId, chapterId) Promise~PlayRecord~
+getRecentPlayRecords(userId, limit) Promise~RecentRecord[]~
}
class RecentRecord {
+string id
+string title
+string coverUrl
+number progress
+string updatedAt
}
PlayRecordService --> RecentRecord : "返回"
图表来源
章节来源
前端使用Pinia状态管理库实现播放器的全局状态控制:
stateDiagram-v2
[*] --> 初始化
初始化 --> 空闲 : 应用启动
空闲 --> 播放中 : 开始播放
播放中 --> 暂停 : 用户暂停
暂停 --> 播放中 : 继续播放
播放中 --> 结束 : 播放完成
结束 --> 空闲 : 重置状态
暂停 --> 空闲 : 停止播放
播放中 --> 空闲 : 切换音频
播放中 --> 发送进度 : 定时器触发
发送进度 --> 播放中 : 更新UI
state 发送进度 {
[*] --> 准备数据
准备数据 --> 验证状态
验证状态 --> 调用API
调用API --> [*]
}
图表来源
前端实现了基于定时器的播放进度同步机制:
sequenceDiagram
participant Timer as "定时器"
participant Store as "音频状态管理"
participant API as "播放记录API"
participant Server as "服务器"
Timer->>Store : onTimeUpdate回调
Store->>Store : 更新currentTime
Store->>Store : 计算进度百分比
Store->>API : 保存播放进度
API->>Server : POST /player/progress
Server-->>API : 进度更新结果
API-->>Store : 响应数据
Store-->>Timer : 继续监听
Note over Timer,Store : 每30秒同步一次进度
图表来源
章节来源
播放记录系统的核心依赖关系如下:
graph LR
subgraph "核心模型"
PR[PlayRecord]
U[User]
BC[BookChapter]
end
subgraph "业务逻辑"
PC[播放记录控制器]
PS[播放记录服务]
HC[历史控制器]
end
subgraph "前端集成"
FE[播放器页面]
AS[音频状态]
end
U --> PR
BC --> PR
PC --> PS
PS --> PR
HC --> PR
FE --> PC
AS --> PC
图表来源
系统主要依赖以下外部组件:
章节来源
系统采用了多种查询优化策略来提升性能:
userId和chapterId字段上建立索引,支持高频查询userId_chapterId唯一约束避免重复记录include选项按需加载关联数据虽然当前实现主要依赖数据库查询,但可以考虑以下缓存优化:
系统通过数据库事务和唯一约束确保并发安全性:
flowchart TD
Request[并发请求] --> CheckLock{检查锁}
CheckLock --> |无锁| AcquireLock[获取锁]
CheckLock --> |有锁| WaitQueue[等待队列]
AcquireLock --> ProcessRequest[处理请求]
ProcessRequest --> ReleaseLock[释放锁]
ReleaseLock --> NotifyQueue[通知等待队列]
WaitQueue --> CheckLock
问题描述: 播放进度与实际播放位置不一致
可能原因:
解决方案:
问题描述: 播放记录无法保存到数据库
可能原因:
解决方案:
问题描述: 播放器UI状态与实际播放状态不符
可能原因:
解决方案:
章节来源
AI有声书生成平台的播放记录数据模型设计充分体现了现代Web应用的最佳实践。通过精心设计的数据模型、完善的API接口和高效的前端集成,系统实现了以下核心价值:
该播放记录数据模型为AI有声书平台奠定了坚实的技术基础,通过持续优化和功能扩展,将为用户提供更加优质的音频播放体验。