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

23
internal/models/admin.go Normal file
View File

@@ -0,0 +1,23 @@
package models
import (
"database/sql"
"time"
)
// Admin 管理员表
type Admin struct {
ID int `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
PasswordHash string `json:"-"` // 密码哈希不输出到 JSON
IsSuperAdmin bool `json:"is_super_admin"`
IsActive bool `json:"is_active"`
CreatedAt time.Time `json:"created_at"`
LastLogin sql.NullTime `json:"last_login"`
}
// TableName 返回表名
func (Admin) TableName() string {
return "admins"
}

View File

@@ -0,0 +1,25 @@
package models
import (
"database/sql"
"time"
)
// APIKey API 密钥表
type APIKey struct {
ID int `json:"id"`
Key string `json:"key"` // 格式: sk_live_xxx
Name string `json:"name"`
CreatedByID int `json:"created_by_id"`
IsActive bool `json:"is_active"`
RateLimit int `json:"rate_limit"` // 次/分钟
AllowedIPs string `json:"allowed_ips"` // JSON 数组
LastUsed sql.NullTime `json:"last_used"`
RequestCount int `json:"request_count"`
CreatedAt time.Time `json:"created_at"`
}
// TableName 返回表名
func (APIKey) TableName() string {
return "api_keys"
}

View File

@@ -0,0 +1,42 @@
package models
import (
"time"
)
// CardKey 卡密表
type CardKey struct {
ID int `json:"id"`
Key string `json:"key"` // 格式: XXXX-XXXX-XXXX-XXXX
MaxUses int `json:"max_uses"`
UsedCount int `json:"used_count"`
ValidityType string `json:"validity_type"` // month/quarter/year/custom
ExpiresAt time.Time `json:"expires_at"`
IsActive bool `json:"is_active"`
CreatedByID int `json:"created_by_id"`
CreatedAt time.Time `json:"created_at"`
}
// TableName 返回表名
func (CardKey) TableName() string {
return "card_keys"
}
// IsExpired 检查卡密是否过期
func (c *CardKey) IsExpired() bool {
return time.Now().After(c.ExpiresAt)
}
// IsUsable 检查卡密是否可用
func (c *CardKey) IsUsable() bool {
return c.IsActive && !c.IsExpired() && c.UsedCount < c.MaxUses
}
// RemainingUses 返回剩余使用次数
func (c *CardKey) RemainingUses() int {
remaining := c.MaxUses - c.UsedCount
if remaining < 0 {
return 0
}
return remaining
}

View File

@@ -0,0 +1,109 @@
package models
import (
"database/sql"
"encoding/json"
"time"
)
// ChatGPTAccount ChatGPT Team 账号表
type ChatGPTAccount struct {
ID int `json:"id"`
Name string `json:"name"`
AuthToken string `json:"-"` // Token 不输出到 JSON
TeamAccountID string `json:"team_account_id"`
SeatsInUse int `json:"seats_in_use"`
SeatsEntitled int `json:"seats_entitled"`
ActiveStart sql.NullTime `json:"-"`
ActiveUntil sql.NullTime `json:"-"`
IsActive bool `json:"is_active"`
ConsecutiveFailures int `json:"consecutive_failures"`
LastCheck sql.NullTime `json:"-"`
LastUsed sql.NullTime `json:"-"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt sql.NullTime `json:"-"`
}
// ChatGPTAccountJSON 用于 JSON 序列化的结构
type ChatGPTAccountJSON struct {
ID int `json:"id"`
Name string `json:"name"`
TeamAccountID string `json:"team_account_id"`
SeatsInUse int `json:"seats_in_use"`
SeatsEntitled int `json:"seats_entitled"`
ActiveStart *string `json:"active_start"`
ActiveUntil *string `json:"active_until"`
IsActive bool `json:"is_active"`
ConsecutiveFailures int `json:"consecutive_failures"`
LastCheck *string `json:"last_check"`
LastUsed *string `json:"last_used"`
CreatedAt string `json:"created_at"`
UpdatedAt *string `json:"updated_at"`
AvailableSeats int `json:"available_seats"`
UsagePercentage float64 `json:"usage_percentage"`
}
// MarshalJSON 自定义 JSON 序列化
func (a ChatGPTAccount) MarshalJSON() ([]byte, error) {
j := ChatGPTAccountJSON{
ID: a.ID,
Name: a.Name,
TeamAccountID: a.TeamAccountID,
SeatsInUse: a.SeatsInUse,
SeatsEntitled: a.SeatsEntitled,
IsActive: a.IsActive,
ConsecutiveFailures: a.ConsecutiveFailures,
CreatedAt: a.CreatedAt.Format(time.RFC3339),
AvailableSeats: a.AvailableSeats(),
UsagePercentage: a.UsagePercentage(),
}
if a.ActiveStart.Valid {
s := a.ActiveStart.Time.Format(time.RFC3339)
j.ActiveStart = &s
}
if a.ActiveUntil.Valid {
s := a.ActiveUntil.Time.Format(time.RFC3339)
j.ActiveUntil = &s
}
if a.LastCheck.Valid {
s := a.LastCheck.Time.Format(time.RFC3339)
j.LastCheck = &s
}
if a.LastUsed.Valid {
s := a.LastUsed.Time.Format(time.RFC3339)
j.LastUsed = &s
}
if a.UpdatedAt.Valid {
s := a.UpdatedAt.Time.Format(time.RFC3339)
j.UpdatedAt = &s
}
return json.Marshal(j)
}
// TableName 返回表名
func (ChatGPTAccount) TableName() string {
return "chatgpt_accounts"
}
// AvailableSeats 返回可用席位数量
func (a *ChatGPTAccount) AvailableSeats() int {
return a.SeatsEntitled - a.SeatsInUse
}
// UsagePercentage 返回席位使用百分比
func (a *ChatGPTAccount) UsagePercentage() float64 {
if a.SeatsEntitled == 0 {
return 0
}
return float64(a.SeatsInUse) / float64(a.SeatsEntitled) * 100
}
// IsSubscriptionValid 检查订阅是否有效
func (a *ChatGPTAccount) IsSubscriptionValid() bool {
if !a.ActiveUntil.Valid {
return false
}
return time.Now().Before(a.ActiveUntil.Time)
}

View File

@@ -0,0 +1,43 @@
package models
import (
"database/sql"
"time"
)
// InvitationStatus 邀请状态枚举
type InvitationStatus string
const (
StatusPending InvitationStatus = "pending"
StatusSent InvitationStatus = "sent"
StatusAccepted InvitationStatus = "accepted"
StatusFailed InvitationStatus = "failed"
StatusExpired InvitationStatus = "expired"
)
// Invitation 邀请记录表
type Invitation struct {
ID int `json:"id"`
CardKeyID sql.NullInt64 `json:"card_key_id"`
AccountID int `json:"account_id"`
InvitedEmail string `json:"invited_email"`
Status InvitationStatus `json:"status"`
ErrorMessage sql.NullString `json:"error_message"`
ExpiresAt sql.NullTime `json:"expires_at"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt sql.NullTime `json:"updated_at"`
}
// TableName 返回表名
func (Invitation) TableName() string {
return "invitations"
}
// IsExpired 检查邀请是否过期
func (i *Invitation) IsExpired() bool {
if !i.ExpiresAt.Valid {
return false
}
return time.Now().After(i.ExpiresAt.Time)
}

View File

@@ -0,0 +1,42 @@
package models
import (
"database/sql"
)
// ValueType 配置值类型枚举
type ValueType string
const (
ValueTypeString ValueType = "string"
ValueTypeInt ValueType = "int"
ValueTypeFloat ValueType = "float"
ValueTypeBool ValueType = "bool"
ValueTypeJSON ValueType = "json"
)
// SystemSetting 系统配置表
type SystemSetting struct {
ID int `json:"id"`
Key string `json:"key"`
Value string `json:"value"`
ValueType ValueType `json:"value_type"`
Description sql.NullString `json:"description"`
UpdatedAt sql.NullTime `json:"updated_at"`
}
// TableName 返回表名
func (SystemSetting) TableName() string {
return "system_settings"
}
// 默认配置键名常量
const (
SettingTurnstileEnabled = "turnstile_enabled"
SettingTurnstileSiteKey = "turnstile_site_key"
SettingTurnstileSecretKey = "turnstile_secret_key"
SettingTokenCheckInterval = "token_check_interval"
SettingTokenFailureThreshold = "token_failure_threshold"
SettingInvitationValidityDays = "invitation_validity_days"
SettingSiteTitle = "site_title"
)