mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-08 09:43:42 +08:00
Merge ade00eed1c into 09911a301d
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/go-co-op/gocron"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
billingratio "github.com/songquanpeng/one-api/relay/billing/ratio"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -79,6 +81,21 @@ func InitOptionMap() {
|
||||
loadOptionsFromDatabase()
|
||||
}
|
||||
|
||||
func ScheduleCheckAndDowngrade() {
|
||||
s := gocron.NewScheduler(time.UTC)
|
||||
|
||||
// 设置每天0点执行
|
||||
_, err := s.Every(1).Day().At("00:00").Do(checkAndDowngradeUsers)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("创建调度任务失败: %v", err)
|
||||
}
|
||||
|
||||
// 开始调度
|
||||
s.StartBlocking()
|
||||
log.Printf("开始调度")
|
||||
}
|
||||
|
||||
func loadOptionsFromDatabase() {
|
||||
options, _ := AllOption()
|
||||
for _, option := range options {
|
||||
|
||||
@@ -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 (
|
||||
@@ -48,6 +50,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:"bigint;default:0;column:expiration_date"` // Expiration date of the user's subscription or account.
|
||||
}
|
||||
|
||||
func GetMaxUserId() int {
|
||||
@@ -211,6 +214,25 @@ func (user *User) ValidateAndFill() (err error) {
|
||||
if !okay || user.Status != UserStatusEnabled {
|
||||
return errors.New("用户名或密码错误,或用户已被封禁")
|
||||
}
|
||||
// 校验用户是不是非default,如果是非default,判断到期时间如果过期了降级为default
|
||||
if !(user.ExpirationDate > 0 && user.Username == "root") {
|
||||
// 将时间戳转换为 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
|
||||
if err != nil {
|
||||
fmt.Printf("用户: %s, 降级为 default 时发生错误: %v\n", user.Username, err)
|
||||
return err
|
||||
}
|
||||
fmt.Printf("用户: %s, 特权组过期降为 default\n", user.Username)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -448,3 +470,48 @@ func GetUsernameById(id int) (username string) {
|
||||
DB.Model(&User{}).Where("id = ?", id).Select("username").Find(&username)
|
||||
return username
|
||||
}
|
||||
|
||||
func checkAndDowngradeUsers() {
|
||||
// 获取昨天的时间戳
|
||||
yesterdayTimestamp := time.Now().AddDate(0, 0, -1).Unix()
|
||||
|
||||
// 获取需要降级的用户ID列表
|
||||
var userList []int
|
||||
query := DB.Model(&User{}).
|
||||
Where("`Group` != ?", "default").
|
||||
Where("`username` != ?", "root").
|
||||
Where("`expiration_date` > 0").
|
||||
Where("`expiration_date` <= ?", yesterdayTimestamp).
|
||||
Select("id").
|
||||
Find(&userList)
|
||||
|
||||
// 处理查询错误
|
||||
if query.Error != nil {
|
||||
log.Printf("查询用户列表失败: %v", query.Error)
|
||||
return
|
||||
}
|
||||
|
||||
// 如果没有用户需要降级,直接返回
|
||||
if len(userList) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
// 批量降级用户
|
||||
updateQuery := DB.Model(&User{}).Where("id IN ?", userList).Update("Group", "default")
|
||||
|
||||
// 处理更新错误
|
||||
if updateQuery.Error != nil {
|
||||
log.Printf("批量更新用户分组失败: %v", updateQuery.Error)
|
||||
return
|
||||
}
|
||||
|
||||
// 删除已过期用户的Redis缓存
|
||||
if common.RedisEnabled {
|
||||
for _, userId := range userList {
|
||||
err := common.RedisSet(fmt.Sprintf("user_group:%d", userId), "default", time.Duration(UserId2GroupCacheSeconds)*time.Second)
|
||||
if err != nil {
|
||||
log.Printf("更新用户: %d, 权益缓存失败, Error: %v", userId, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user