feat: 实现前端卡密管理界面
- 卡密列表展示与分页功能 - 单个/批量创建卡密 - 卡密删除与批量删除 - 卡密导出功能 (file-saver) - 启用/禁用状态切换 - 状态判断 (有效/已使用/已失效) - Toast 通知系统 (vue-sonner) - 登录页面错误提示优化 - 后端登录错误消息中文化
This commit is contained in:
23
backend/internal/models/admin.go
Normal file
23
backend/internal/models/admin.go
Normal 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"
|
||||
}
|
||||
25
backend/internal/models/api_key.go
Normal file
25
backend/internal/models/api_key.go
Normal 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"
|
||||
}
|
||||
42
backend/internal/models/card_key.go
Normal file
42
backend/internal/models/card_key.go
Normal 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
|
||||
}
|
||||
109
backend/internal/models/chatgpt_account.go
Normal file
109
backend/internal/models/chatgpt_account.go
Normal 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)
|
||||
}
|
||||
43
backend/internal/models/invitation.go
Normal file
43
backend/internal/models/invitation.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user