rwadurian/backend/services/identity-service/README.md

182 lines
4.3 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.

# Identity Service
RWA平台用户身份管理微服务 - 基于NestJS + Prisma + Clean Architecture
## 技术栈
- **框架**: NestJS 10
- **ORM**: Prisma 5
- **数据库**: PostgreSQL 15
- **缓存**: Redis 7 (ioredis)
- **消息队列**: Kafka
- **区块链**: ethers.js 6
- **架构**: Clean Architecture / Hexagonal Architecture / DDD / CQRS
## 功能特性
- ✅ 自动创建账户首次打开APP
- ✅ 多设备支持最多5个设备同时登录
- ✅ 助记词生成与恢复
- ✅ 手机号绑定与恢复
- ✅ 三链钱包地址派生KAVA/DST/BSC
- ✅ JWT Token认证
- ✅ Token自动刷新账户永不过期
- ✅ Kafka事件发布
- ✅ 死信队列与自动重试
## 目录结构
```
src/
├── api/ # 表现层
│ ├── controllers/ # HTTP控制器
│ └── dto/ # 数据传输对象
├── application/ # 应用层
│ ├── commands/ # 命令处理器
│ ├── queries/ # 查询处理器
│ └── services/ # 应用服务
├── domain/ # 领域层
│ ├── aggregates/ # 聚合根
│ ├── entities/ # 实体
│ ├── value-objects/ # 值对象
│ ├── events/ # 领域事件
│ ├── repositories/ # 仓储接口
│ └── services/ # 领域服务
├── infrastructure/ # 基础设施层
│ ├── persistence/ # 数据持久化
│ ├── redis/ # Redis缓存
│ ├── kafka/ # Kafka消息
│ └── external/ # 外部服务
└── shared/ # 共享层
├── decorators/ # 装饰器
├── guards/ # 守卫
├── filters/ # 过滤器
└── exceptions/ # 异常
```
## 快速开始
### 1. 安装依赖
```bash
npm install
```
### 2. 配置环境变量
```bash
cp .env.example .env
# 编辑.env文件配置数据库连接等
```
### 3. 数据库迁移
```bash
# 生成Prisma客户端
npm run prisma:generate
# 运行迁移
npm run prisma:migrate
```
### 4. 启动服务
```bash
# 开发模式
npm run start:dev
# 生产模式
npm run build
npm run start:prod
```
## Docker部署
### 启动所有服务
```bash
docker-compose up -d
```
### 仅启动依赖服务
```bash
docker-compose up -d postgres redis kafka zookeeper
```
## API文档
启动服务后访问: http://localhost:3000/api/docs
## 主要API接口
| 方法 | 路径 | 描述 | 认证 |
|------|------|------|------|
| POST | /api/v1/user/auto-create | 自动创建账户 | 否 |
| POST | /api/v1/user/recover-by-mnemonic | 助记词恢复 | 否 |
| POST | /api/v1/user/recover-by-phone | 手机号恢复 | 否 |
| POST | /api/v1/user/refresh-token | 刷新Token | 否 |
| POST | /api/v1/user/send-sms-code | 发送验证码 | 否 |
| POST | /api/v1/user/bind-phone | 绑定手机号 | 是 |
| GET | /api/v1/user/my-profile | 我的资料 | 是 |
| GET | /api/v1/user/my-devices | 我的设备 | 是 |
| DELETE | /api/v1/user/remove-device | 移除设备 | 是 |
| POST | /api/v1/user/logout | 退出登录 | 是 |
## Kafka Topics
| Topic | 描述 |
|-------|------|
| identity.user-account.created | 用户账户创建 |
| identity.device.added | 设备添加 |
| identity.device.removed | 设备移除 |
| identity.phone.bound | 手机号绑定 |
| identity.kyc.submitted | KYC提交 |
| identity.kyc.approved | KYC通过 |
| identity.kyc.rejected | KYC拒绝 |
| identity.account.frozen | 账户冻结 |
| identity.wallet.bound | 钱包绑定 |
## 测试
```bash
# 单元测试
npm run test
# E2E测试
npm run test:e2e
# 测试覆盖率
npm run test:cov
```
## 开发命令
```bash
# 格式化代码
npm run format
# Lint检查
npm run lint
# 打开Prisma Studio
npm run prisma:studio
```
## 环境变量
| 变量 | 描述 | 默认值 |
|------|------|--------|
| PORT | 服务端口 | 3000 |
| DATABASE_URL | 数据库连接 | - |
| REDIS_HOST | Redis主机 | localhost |
| REDIS_PORT | Redis端口 | 6379 |
| JWT_SECRET | JWT密钥 | - |
| JWT_ACCESS_EXPIRATION | AccessToken有效期 | 2h |
| JWT_REFRESH_EXPIRATION | RefreshToken有效期 | 30d |
| KAFKA_BROKERS | Kafka地址 | localhost:9092 |
## License
MIT