Files
wwjcloud-nest-v1/.trae/documents/douyin_app_tech_architecture.md
wanwu 6eb9ea687d feat: 初始化项目代码
- 迁移 NestJS 项目结构
- 添加 uniappx 前端代码
- 配置数据库连接
- 添加核心业务模块
2026-04-02 21:25:02 +08:00

547 lines
12 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 抖音App技术架构文档
## 1. 架构设计
### 1.1 整体架构
```mermaid
graph TD
A[用户设备] --> B[React Native App]
B --> C[API Gateway]
C --> D[用户服务]
C --> E[内容服务]
C --> F[推荐服务]
C --> G[直播服务]
C --> H[消息服务]
D --> I[用户数据库]
E --> J[内容存储]
F --> K[推荐引擎]
G --> L[直播CDN]
H --> M[消息队列]
subgraph "客户端层"
B
end
subgraph "服务层"
C
D
E
F
G
H
end
subgraph "数据层"
I
J
K
L
M
end
```
### 1.2 客户端架构
```mermaid
graph TD
A[React Native] --> B[Redux Store]
A --> C[React Navigation]
A --> D[Native Modules]
B --> E[用户状态]
B --> F[内容状态]
B --> G[UI状态]
D --> H[相机模块]
D --> I[音视频处理]
D --> J[推送通知]
D --> K[本地存储]
subgraph "状态管理"
B
E
F
G
end
subgraph "原生功能"
D
H
I
J
K
end
```
## 2. 技术栈描述
### 2.1 前端技术栈
- **跨平台框架**: React Native 0.72 + TypeScript 5.0
- **状态管理**: Redux Toolkit + RTK Query
- **导航**: React Navigation 6.0
- **UI组件**: React Native Elements + 自定义组件库
- **动画**: React Native Reanimated 3.0
- **手势**: React Native Gesture Handler
- **视频播放**: react-native-video 6.0
- **相机**: react-native-vision-camera 3.0
- **图片处理**: react-native-fast-image
### 2.2 后端技术栈
- **API网关**: Kong Gateway
- **用户服务**: Node.js + Express + TypeScript
- **内容服务**: Node.js + Express + TypeScript
- **推荐服务**: Python + FastAPI + TensorFlow
- **直播服务**: Node.js + WebRTC + FFmpeg
- **消息服务**: Node.js + Socket.io + Redis
- **数据库**: PostgreSQL 14 + Redis 7.0
- **文件存储**: AWS S3 + CDN
### 2.3 基础设施
- **容器化**: Docker + Kubernetes
- **CI/CD**: GitLab CI + ArgoCD
- **监控**: Prometheus + Grafana
- **日志**: ELK Stack (Elasticsearch + Logstash + Kibana)
- **错误追踪**: Sentry
- **性能监控**: New Relic
## 3. 路由定义
### 3.1 应用内路由
| 路由 | 页面组件 | 说明 |
|------|----------|------|
| / | HomeScreen | 首页推荐页 |
| /following | FollowingScreen | 关注页 |
| /create | CreateScreen | 创作页 |
| /inbox | InboxScreen | 消息页 |
| /profile | ProfileScreen | 个人主页 |
| /discover | DiscoverScreen | 发现页 |
| /live | LiveScreen | 直播页 |
| /video/:id | VideoDetailScreen | 视频详情页 |
| /user/:id | UserProfileScreen | 用户主页 |
| /search | SearchScreen | 搜索页 |
| /settings | SettingsScreen | 设置页 |
| /camera | CameraScreen | 拍摄页 |
| /editor | EditorScreen | 视频编辑页 |
| /publish | PublishScreen | 发布页 |
| /login | LoginScreen | 登录页 |
| /register | RegisterScreen | 注册页 |
### 3.2 嵌套路由结构
```
TabNavigator
├── HomeStack
│ ├── HomeScreen
│ ├── VideoDetailScreen
│ └── UserProfileScreen
├── FollowingStack
│ └── FollowingScreen
├── CreateStack
│ ├── CameraScreen
│ ├── EditorScreen
│ └── PublishScreen
├── InboxStack
│ ├── InboxScreen
│ └── ChatScreen
└── ProfileStack
├── ProfileScreen
├── SettingsScreen
└── EditProfileScreen
```
## 4. API定义
### 4.1 用户相关API
#### 用户注册
```
POST /api/auth/register
```
请求参数:
| 参数名 | 类型 | 必需 | 描述 |
|--------|------|------|------|
| phone | string | 是 | 手机号 |
| password | string | 是 | 密码 |
| verificationCode | string | 是 | 验证码 |
| nickname | string | 是 | 昵称 |
响应示例:
```json
{
"code": 200,
"message": "success",
"data": {
"userId": "123456",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"refreshToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
}
```
#### 用户登录
```
POST /api/auth/login
```
请求参数:
| 参数名 | 类型 | 必需 | 描述 |
|--------|------|------|------|
| phone | string | 是 | 手机号 |
| password | string | 是 | 密码 |
#### 获取用户信息
```
GET /api/user/profile
```
请求头:
```
Authorization: Bearer {token}
```
响应示例:
```json
{
"code": 200,
"data": {
"userId": "123456",
"nickname": "用户名",
"avatar": "https://example.com/avatar.jpg",
"bio": "个人简介",
"followerCount": 1000,
"followingCount": 500,
"likeCount": 5000,
"videoCount": 50
}
}
```
### 4.2 内容相关API
#### 获取推荐视频
```
GET /api/feed/recommend
```
请求参数:
| 参数名 | 类型 | 必需 | 描述 |
|--------|------|------|------|
| page | number | 否 | 页码默认1 |
| limit | number | 否 | 每页数量默认10 |
| lastId | string | 否 | 最后一条视频ID |
#### 上传视频
```
POST /api/video/upload
```
请求参数FormData
| 参数名 | 类型 | 必需 | 描述 |
|--------|------|------|------|
| video | file | 是 | 视频文件 |
| cover | file | 是 | 封面图片 |
| title | string | 是 | 视频标题 |
| description | string | 否 | 视频描述 |
| tags | array | 否 | 标签数组 |
### 4.3 互动相关API
#### 点赞视频
```
POST /api/video/:id/like
```
#### 评论视频
```
POST /api/video/:id/comment
```
请求参数:
| 参数名 | 类型 | 必需 | 描述 |
|--------|------|------|------|
| content | string | 是 | 评论内容 |
| parentId | string | 否 | 回复的评论ID |
## 5. 服务器架构
### 5.1 微服务架构图
```mermaid
graph TD
A[API Gateway] --> B[Auth Service]
A --> C[User Service]
A --> D[Content Service]
A --> E[Feed Service]
A --> F[Live Service]
A --> G[Message Service]
B --> H[Redis Cache]
C --> I[PostgreSQL]
D --> J[Object Storage]
E --> K[Recommendation Engine]
F --> L[CDN Network]
G --> M[Message Queue]
subgraph "Gateway Layer"
A
end
subgraph "Business Services"
B
C
D
E
F
G
end
subgraph "Data Layer"
H
I
J
K
L
M
end
```
### 5.2 服务间通信
- **同步通信**: HTTP/HTTPS + REST API
- **异步通信**: Apache Kafka + RabbitMQ
- **服务发现**: Consul + gRPC
- **负载均衡**: Nginx + HAProxy
## 6. 数据模型
### 6.1 核心数据实体
#### 用户实体
```typescript
interface User {
id: string;
phone: string;
nickname: string;
avatar: string;
bio: string;
gender: 'male' | 'female' | 'other';
birthday: Date;
location: string;
followerCount: number;
followingCount: number;
likeCount: number;
videoCount: number;
isVerified: boolean;
verifiedType: 'personal' | 'enterprise' | 'institution';
level: number;
experience: number;
status: 'active' | 'inactive' | 'banned';
createdAt: Date;
updatedAt: Date;
}
```
#### 视频实体
```typescript
interface Video {
id: string;
userId: string;
title: string;
description: string;
videoUrl: string;
coverUrl: string;
duration: number;
width: number;
height: number;
size: number;
format: string;
tags: string[];
location: {
address: string;
latitude: number;
longitude: number;
};
musicId: string;
effects: string[];
filters: string[];
stickers: string[];
visibility: 'public' | 'private' | 'friends';
allowComment: boolean;
allowDownload: boolean;
viewCount: number;
likeCount: number;
commentCount: number;
shareCount: number;
downloadCount: number;
status: 'processing' | 'published' | 'rejected' | 'deleted';
moderationStatus: 'pending' | 'approved' | 'rejected';
createdAt: Date;
updatedAt: Date;
publishedAt: Date;
}
```
#### 互动实体
```typescript
interface Like {
id: string;
userId: string;
videoId: string;
createdAt: Date;
}
interface Comment {
id: string;
userId: string;
videoId: string;
parentId: string | null;
content: string;
likeCount: number;
replyCount: number;
status: 'active' | 'deleted';
createdAt: Date;
updatedAt: Date;
}
interface Follow {
id: string;
followerId: string;
followingId: string;
createdAt: Date;
}
```
### 6.2 数据库设计
#### 用户表
```sql
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
phone VARCHAR(20) UNIQUE NOT NULL,
email VARCHAR(100),
nickname VARCHAR(50) NOT NULL,
avatar TEXT,
bio TEXT,
gender VARCHAR(10),
birthday DATE,
location VARCHAR(100),
follower_count INTEGER DEFAULT 0,
following_count INTEGER DEFAULT 0,
like_count INTEGER DEFAULT 0,
video_count INTEGER DEFAULT 0,
is_verified BOOLEAN DEFAULT FALSE,
verified_type VARCHAR(20),
level INTEGER DEFAULT 1,
experience INTEGER DEFAULT 0,
status VARCHAR(20) DEFAULT 'active',
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
CREATE INDEX idx_users_phone ON users(phone);
CREATE INDEX idx_users_nickname ON users(nickname);
CREATE INDEX idx_users_status ON users(status);
```
#### 视频表
```sql
CREATE TABLE videos (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES users(id),
title VARCHAR(200) NOT NULL,
description TEXT,
video_url TEXT NOT NULL,
cover_url TEXT NOT NULL,
duration INTEGER NOT NULL,
width INTEGER NOT NULL,
height INTEGER NOT NULL,
size BIGINT NOT NULL,
format VARCHAR(20) NOT NULL,
tags TEXT[],
location_address VARCHAR(200),
location_latitude DECIMAL(10, 8),
location_longitude DECIMAL(11, 8),
music_id UUID,
effects TEXT[],
filters TEXT[],
stickers TEXT[],
visibility VARCHAR(20) DEFAULT 'public',
allow_comment BOOLEAN DEFAULT TRUE,
allow_download BOOLEAN DEFAULT TRUE,
view_count INTEGER DEFAULT 0,
like_count INTEGER DEFAULT 0,
comment_count INTEGER DEFAULT 0,
share_count INTEGER DEFAULT 0,
download_count INTEGER DEFAULT 0,
status VARCHAR(20) DEFAULT 'processing',
moderation_status VARCHAR(20) DEFAULT 'pending',
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
published_at TIMESTAMP WITH TIME ZONE
);
CREATE INDEX idx_videos_user_id ON videos(user_id);
CREATE INDEX idx_videos_status ON videos(status);
CREATE INDEX idx_videos_created_at ON videos(created_at DESC);
CREATE INDEX idx_videos_published_at ON videos(published_at DESC);
```
## 7. 第三方服务集成
### 7.1 云服务
- **视频存储**: AWS S3 / 阿里云OSS
- **CDN加速**: CloudFront / 阿里云CDN
- **直播服务**: Agora / 腾讯云直播
- **推送服务**: Firebase Cloud Messaging / 极光推送
### 7.2 AI服务
- **内容审核**: 阿里云内容安全 / 腾讯云天御
- **人脸识别**: Face++ / 腾讯云人脸识别
- **语音识别**: 科大讯飞 / 百度语音识别
- **推荐算法**: 自研推荐引擎 + 机器学习平台
### 7.3 支付服务
- **应用内购买**: Apple App Store / Google Play
- **第三方支付**: 支付宝 / 微信支付 / PayPal
## 8. 部署和发布策略
### 8.1 部署架构
```mermaid
graph TD
A[Git Repository] --> B[CI/CD Pipeline]
B --> C[Build Stage]
B --> D[Test Stage]
B --> E[Deploy Stage]
C --> F[Docker Image]
D --> G[Test Results]
E --> H[Kubernetes Cluster]
H --> I[Staging Environment]
H --> J[Production Environment]
I --> K[Integration Tests]
J --> L[Monitoring & Alerting]
```
### 8.2 发布流程
1. **开发阶段**: 功能开发 → 单元测试 → 代码审查
2. **测试阶段**: 集成测试 → 性能测试 → 安全测试
3. **灰度发布**: 5%用户 → 20%用户 → 50%用户 → 100%用户
4. **监控回滚**: 实时监控 → 异常告警 → 快速回滚
### 8.3 环境配置
- **开发环境**: 本地开发 + 开发服务器
- **测试环境**: 功能测试 + 集成测试
- **预发布环境**: 生产数据副本 + 真实流量测试
- **生产环境**: 多区域部署 + 负载均衡
### 8.4 监控告警
- **应用监控**: 错误率、响应时间、吞吐量
- **基础设施监控**: CPU、内存、磁盘、网络
- **业务指标监控**: 用户活跃度、内容发布量、互动数据
- **告警机制**: 实时告警 + 分级处理 + 自动恢复
### 8.5 备份策略
- **数据备份**: 每日全量备份 + 实时增量备份
- **灾备方案**: 异地多活 + 数据同步 + 故障切换
- **恢复测试**: 定期演练 + 恢复时间验证 + 数据完整性检查