Skip to content

Commit

Permalink
feat: 引入到期时间概念,当非default的分组时将有到期时间概念
Browse files Browse the repository at this point in the history
  • Loading branch information
jinjianming committed Jul 17, 2024
1 parent adba54a commit 25e1af2
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 148 deletions.
3 changes: 3 additions & 0 deletions model/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ func SyncOptions(frequency int) {
for {
time.Sleep(time.Duration(frequency) * time.Second)
logger.SysLog("syncing options from database")
if config.IsMasterNode {
checkAndDowngradeUsers()
}
loadOptionsFromDatabase()
}
}
Expand Down
56 changes: 56 additions & 0 deletions model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"github.com/songquanpeng/one-api/common/logger"
"github.com/songquanpeng/one-api/common/random"
"gorm.io/gorm"
"log"
"strings"
"time"
)

const (
Expand Down Expand Up @@ -47,6 +49,7 @@ type User struct {
Group string `json:"group" gorm:"type:varchar(32);default:'default'"`
AffCode string `json:"aff_code" gorm:"type:varchar(32);column:aff_code;uniqueIndex"`
InviterId int `json:"inviter_id" gorm:"type:int;column:inviter_id;index"`
ExpirationDate int64 `json:"expiration_date" gorm:"column:expiration_date"` // Expiration date of the user's subscription or account.
}

func GetMaxUserId() int {
Expand Down Expand Up @@ -210,6 +213,22 @@ func (user *User) ValidateAndFill() (err error) {
if !okay || user.Status != UserStatusEnabled {
return errors.New("用户名或密码错误,或用户已被封禁")
}
// 校验用户是不是非default,如果是非default,判断到期时间如果过期了降级为default
if user.Group != "default" {
// 将时间戳转换为 time.Time 类型
expirationTime := time.Unix(user.ExpirationDate, 0)
// 获取当前时间
currentTime := time.Now()

// 比较当前时间和到期时间
if expirationTime.Before(currentTime) {
// 降级为default
user.Group = "default"
err = DB.Model(user).Updates(user).Error
fmt.Printf("用户: %s,特权组过期降为default", user.Username)
return err
}
}
return nil
}

Expand Down Expand Up @@ -435,3 +454,40 @@ func GetUsernameById(id int) (username string) {
DB.Model(&User{}).Where("id = ?", id).Select("username").Find(&username)
return username
}

func checkAndDowngradeUsers() {
var users []User

// 查询所有 Group 不为 "default" 的用户
// 构建查询条件
query := DB.Where("`Group` <> ?", "default"). // Group 不等于 "default"
Where("`username` <> ?", "root"). // username 不等于 "root"
Where("`expiration_date` IS NOT NULL"). // expiration_date 不为空
Where("`expiration_date` != ?", -1) // expiration_date 不等于 -1

// 执行查询并处理错误
if err := query.Find(&users).Error; err != nil {
log.Printf("查询用户失败: %v", err)
return
}

currentTime := time.Now()

for _, user := range users {
if user.Group != "default" {
// 将时间戳转换为 time.Time 类型
expirationTime := time.Unix(user.ExpirationDate, 0)

// 比较当前时间和到期时间
if expirationTime.Before(currentTime) {
// 降级为 default
user.Group = "default"
if err := DB.Model(&user).Updates(user).Error; err != nil {
log.Printf("更新用户 %s 失败: %v", user.Username, err)
} else {
fmt.Printf("用户: %s, 特权组过期降为 default\n", user.Username)
}
}
}
}
}
2 changes: 2 additions & 0 deletions web/berry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@tabler/icons-react": "^2.44.0",
"apexcharts": "3.35.3",
"axios": "^0.27.2",
"date-fns": "^3.6.0",
"dayjs": "^1.11.10",
"formik": "^2.2.9",
"framer-motion": "^6.3.16",
Expand All @@ -27,6 +28,7 @@
"prop-types": "^15.8.1",
"react": "^18.2.0",
"react-apexcharts": "1.4.0",
"react-datepicker": "^7.3.0",
"react-device-detect": "^2.2.2",
"react-dom": "^18.2.0",
"react-perfect-scrollbar": "^1.5.8",
Expand Down
Loading

0 comments on commit 25e1af2

Please sign in to comment.