File size: 1,994 Bytes
644c352
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package model

import (
	"time"
)

// MorphCookie Morph Cookie 模型
type MorphCookie struct {
	ID            uint       `gorm:"primaryKey" json:"id"`
	UserID        uint       `gorm:"not null;index" json:"user_id"`
	User          User       `gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE" json:"-"`
	Name          string     `gorm:"size:100;not null" json:"name"`
	APIKey        string     `gorm:"column:api_key;type:text;not null" json:"api_key"`
	SessionKey    string     `gorm:"column:session_key;type:text" json:"session_key"`
	IsValid       bool       `gorm:"default:true;index" json:"is_valid"`
	LastValidated *time.Time `gorm:"column:last_validated" json:"last_validated"`
	LastUsed      *time.Time `gorm:"column:last_used" json:"last_used"`
	Priority      int        `gorm:"default:0;index" json:"priority"`
	UsageCount    int64      `gorm:"default:0" json:"usage_count"`
	ErrorCount    int        `gorm:"default:0" json:"error_count"`
	CreatedAt     time.Time  `json:"created_at"`
	UpdatedAt     time.Time  `json:"updated_at"`
}

// CookieStats Cookie 统计信息
type CookieStats struct {
	TotalCount   int64 `json:"total_count"`
	ValidCount   int64 `json:"valid_count"`
	InvalidCount int64 `json:"invalid_count"`
	TotalUsage   int64 `json:"total_usage"`
}

// TableName 指定表名
func (MorphCookie) TableName() string {
	return "morph_cookies"
}

// MarkUsed 标记 Cookie 已使用
func (c *MorphCookie) MarkUsed() {
	now := time.Now()
	c.LastUsed = &now
	c.UsageCount++
	c.ErrorCount = 0 // 成功使用后重置错误计数
}

// MarkError 标记 Cookie 错误
func (c *MorphCookie) MarkError() {
	c.ErrorCount++
}

// MarkInvalid 标记 Cookie 无效
func (c *MorphCookie) MarkInvalid() {
	c.IsValid = false
	now := time.Now()
	c.LastValidated = &now
}

// MarkValid 标记 Cookie 有效
func (c *MorphCookie) MarkValid() {
	c.IsValid = true
	c.ErrorCount = 0
	now := time.Now()
	c.LastValidated = &now
}