feat: 初始化 ChatGPT Team 管理后端项目

- 添加用户认证模块 (JWT + 密码管理)
- 添加 ChatGPT 账户管理功能
- 添加卡密管理功能 (创建、批量生成、查询)
- 添加邀请功能
- 配置数据库迁移和路由系统
This commit is contained in:
sar
2026-01-13 14:42:56 +08:00
commit 42c423bd32
29 changed files with 2969 additions and 0 deletions

156
database_schema.md Normal file
View File

@@ -0,0 +1,156 @@
# GPT Manager 数据库表结构
此项目共有 **6 张表**,用于实现 ChatGPT Team 账号管理与邀请系统。
---
## 1. `admins` - 管理员表
| 字段名 | 类型 | 约束 | 说明 |
|--------|------|------|------|
| `id` | Integer | PK | 主键 |
| `username` | String(50) | UNIQUE, NOT NULL, INDEX | 用户名 |
| `email` | String(120) | UNIQUE, NOT NULL, INDEX | 邮箱 |
| `password_hash` | String(128) | NOT NULL | 密码哈希 (bcrypt) |
| `is_super_admin` | Boolean | NOT NULL, DEFAULT FALSE | 是否超级管理员 |
| `is_active` | Boolean | NOT NULL, DEFAULT TRUE | 是否激活 |
| `created_at` | DateTime | NOT NULL | 创建时间 |
| `last_login` | DateTime | NULLABLE | 最后登录时间 |
**关系**: → `card_keys` (一对多) / → `api_keys` (一对多)
---
## 2. `chatgpt_accounts` - ChatGPT Team 账号表
| 字段名 | 类型 | 约束 | 说明 |
|--------|------|------|------|
| `id` | Integer | PK | 主键 |
| `name` | String(100) | NOT NULL | 账号名称 |
| `email` | String(120) | NOT NULL | 邮箱地址 |
| `auth_token` | Text | NOT NULL | 授权 Token |
| `team_account_id` | String(100) | NOT NULL | Team Account ID (UUID) |
| `max_invitations` | Integer | NOT NULL, DEFAULT 50 | 最大邀请数 |
| `current_invitations` | Integer | NOT NULL, DEFAULT 0 | 当前已用邀请数 |
| `is_active` | Boolean | NOT NULL, DEFAULT TRUE, INDEX | 是否激活 |
| `consecutive_failures` | Integer | NOT NULL, DEFAULT 0 | 连续失败次数 |
| `last_check` | DateTime | NULLABLE | 最后检测时间 |
| `last_used` | DateTime | NULLABLE | 最后使用时间 |
| `created_at` | DateTime | NOT NULL | 创建时间 |
| `updated_at` | DateTime | NULLABLE | 更新时间 |
**关系**: → `invitations` (一对多)
---
## 3. `card_keys` - 卡密表
| 字段名 | 类型 | 约束 | 说明 |
|--------|------|------|------|
| `id` | Integer | PK | 主键 |
| `key` | String(19) | UNIQUE, NOT NULL, INDEX | 卡密 (XXXX-XXXX-XXXX-XXXX) |
| `max_uses` | Integer | NOT NULL, DEFAULT 1 | 最大使用次数 |
| `used_count` | Integer | NOT NULL, DEFAULT 0 | 已使用次数 |
| `validity_type` | String(20) | NOT NULL | 有效期类型 (month/quarter/year/custom) |
| `expires_at` | DateTime | NOT NULL, INDEX | 过期时间 |
| `is_active` | Boolean | NOT NULL, DEFAULT TRUE, INDEX | 是否激活 |
| `created_by_id` | Integer | FK → `admins.id`, NOT NULL | 创建者 |
| `created_at` | DateTime | NOT NULL | 创建时间 |
**关系**: → `invitations` (一对多)
---
## 4. `invitations` - 邀请记录表
| 字段名 | 类型 | 约束 | 说明 |
|--------|------|------|------|
| `id` | Integer | PK | 主键 |
| `card_key_id` | Integer | FK → `card_keys.id`, NULLABLE, INDEX | 关联卡密 |
| `account_id` | Integer | FK → `chatgpt_accounts.id`, NOT NULL, INDEX | 关联账号 |
| `invited_email` | String(120) | NOT NULL, INDEX | 被邀请邮箱 |
| `status` | String(20) | NOT NULL, DEFAULT 'pending', INDEX | 状态 |
| `error_message` | Text | NULLABLE | 错误信息 |
| `expires_at` | DateTime | NULLABLE | 邀请过期时间 |
| `created_at` | DateTime | NOT NULL, INDEX | 创建时间 |
| `updated_at` | DateTime | NULLABLE | 更新时间 |
| `api_response` | Text | NULLABLE | API 响应 (JSON) |
**状态值**: `pending` / `sent` / `accepted` / `failed` / `expired`
---
## 5. `api_keys` - API 密钥表
| 字段名 | 类型 | 约束 | 说明 |
|--------|------|------|------|
| `id` | Integer | PK | 主键 |
| `key` | String(64) | UNIQUE, NOT NULL, INDEX | API Key (sk_live_xxx) |
| `name` | String(100) | NOT NULL | 名称 |
| `created_by_id` | Integer | FK → `admins.id`, NOT NULL | 创建者 |
| `is_active` | Boolean | NOT NULL, DEFAULT TRUE, INDEX | 是否激活 |
| `rate_limit` | Integer | NOT NULL, DEFAULT 60 | 速率限制 (次/分钟) |
| `allowed_ips` | Text | DEFAULT '[]' | IP 白名单 (JSON 数组) |
| `last_used` | DateTime | NULLABLE | 最后使用时间 |
| `request_count` | Integer | NOT NULL, DEFAULT 0 | 请求计数 |
| `created_at` | DateTime | NOT NULL | 创建时间 |
---
## 6. `system_settings` - 系统配置表
| 字段名 | 类型 | 约束 | 说明 |
|--------|------|------|------|
| `id` | Integer | PK | 主键 |
| `key` | String(100) | UNIQUE, NOT NULL, INDEX | 配置键名 |
| `value` | Text | NOT NULL | 配置值 |
| `value_type` | String(20) | NOT NULL, DEFAULT 'string' | 值类型 (string/int/float/bool/json) |
| `description` | String(255) | NULLABLE | 配置描述 |
| `updated_at` | DateTime | NULLABLE | 更新时间 |
**默认配置项**:
| 键名 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `turnstile_enabled` | bool | false | Cloudflare Turnstile 开关 |
| `turnstile_site_key` | string | '' | Turnstile Site Key |
| `turnstile_secret_key` | string | '' | Turnstile Secret Key |
| `token_check_interval` | int | 6 | Token 检测间隔(小时)|
| `token_failure_threshold` | int | 2 | 连续失败禁用阈值 |
| `invitation_validity_days` | int | 30 | 邀请有效期(天)|
| `site_title` | string | 'ChatGPT Team 邀请' | 站点标题 |
---
## ER 关系图
```
┌─────────────┐ ┌──────────────────┐ ┌─────────────┐
│ admins │ │ chatgpt_accounts │ │ card_keys │
├─────────────┤ ├──────────────────┤ ├─────────────┤
│ id (PK) │───┐ │ id (PK) │───┐ │ id (PK) │
│ username │ │ │ name │ │ │ key │
│ email │ │ │ auth_token │ │ │ max_uses │
│ password │ │ │ team_account_id │ │ │ expires_at │
└─────────────┘ │ └──────────────────┘ │ └─────────────┘
│ │ │ │ │
│ │ │ │ │
▼ │ ▼ │ ▼
┌─────────────┐ │ ┌──────────────────┐ │ │
│ api_keys │ │ │ invitations │◄──┴─────────┘
├─────────────┤ │ ├──────────────────┤
│ id (PK) │ │ │ id (PK) │
│ key │ │ │ account_id (FK) │
│created_by_id│◄──┘ │ card_key_id (FK) │
└─────────────┘ │ invited_email │
│ status │
└──────────────────┘
┌──────────────────┐
│ system_settings │
├──────────────────┤
│ id (PK) │
│ key │
│ value │
│ value_type │
└──────────────────┘
```