Skip to content

Commit

Permalink
feat: 引入到期时间概念,当非default的分组时将有到期时间概念
Browse files Browse the repository at this point in the history
fix: 修复旧用户的vip被降级,尽量不对原先用户做变动
  • Loading branch information
jinjianming committed Jul 17, 2024
1 parent adba54a commit b86a3ac
Show file tree
Hide file tree
Showing 4 changed files with 287 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()

Check warning on line 99 in model/option.go

View check run for this annotation

Codecov / codecov/patch

model/option.go#L98-L99

Added lines #L98 - L99 were not covered by tests
}
loadOptionsFromDatabase()
}
}
Expand Down
59 changes: 59 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,25 @@ func (user *User) ValidateAndFill() (err error) {
if !okay || user.Status != UserStatusEnabled {
return errors.New("用户名或密码错误,或用户已被封禁")
}
// 校验用户是不是非default,如果是非default,判断到期时间如果过期了降级为default
if user.ExpirationDate > 0 {

Check warning on line 217 in model/user.go

View check run for this annotation

Codecov / codecov/patch

model/user.go#L217

Added line #L217 was not covered by tests
// 将时间戳转换为 time.Time 类型
expirationTime := time.Unix(user.ExpirationDate, 0)

Check warning on line 219 in model/user.go

View check run for this annotation

Codecov / codecov/patch

model/user.go#L219

Added line #L219 was not covered by tests
// 获取当前时间
currentTime := time.Now()

Check warning on line 221 in model/user.go

View check run for this annotation

Codecov / codecov/patch

model/user.go#L221

Added line #L221 was not covered by tests

// 比较当前时间和到期时间
if expirationTime.Before(currentTime) {

Check warning on line 224 in model/user.go

View check run for this annotation

Codecov / codecov/patch

model/user.go#L224

Added line #L224 was not covered by tests
// 降级为 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

Check warning on line 230 in model/user.go

View check run for this annotation

Codecov / codecov/patch

model/user.go#L226-L230

Added lines #L226 - L230 were not covered by tests
}
fmt.Printf("用户: %s, 特权组过期降为 default\n", user.Username)

Check warning on line 232 in model/user.go

View check run for this annotation

Codecov / codecov/patch

model/user.go#L232

Added line #L232 was not covered by tests
}
}
return nil
}

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

func checkAndDowngradeUsers() {
var users []User

Check warning on line 462 in model/user.go

View check run for this annotation

Codecov / codecov/patch

model/user.go#L461-L462

Added lines #L461 - L462 were not covered by tests

// 查询所有 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

Check warning on line 469 in model/user.go

View check run for this annotation

Codecov / codecov/patch

model/user.go#L466-L469

Added lines #L466 - L469 were not covered by tests

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

Check warning on line 474 in model/user.go

View check run for this annotation

Codecov / codecov/patch

model/user.go#L472-L474

Added lines #L472 - L474 were not covered by tests
}

currentTime := time.Now()

Check warning on line 477 in model/user.go

View check run for this annotation

Codecov / codecov/patch

model/user.go#L477

Added line #L477 was not covered by tests

for _, user := range users {
if user.Group != "default" {

Check warning on line 480 in model/user.go

View check run for this annotation

Codecov / codecov/patch

model/user.go#L479-L480

Added lines #L479 - L480 were not covered by tests
// 将时间戳转换为 time.Time 类型
expirationTime := time.Unix(user.ExpirationDate, 0)

Check warning on line 482 in model/user.go

View check run for this annotation

Codecov / codecov/patch

model/user.go#L482

Added line #L482 was not covered by tests

// 比较当前时间和到期时间
if expirationTime.Before(currentTime) {

Check warning on line 485 in model/user.go

View check run for this annotation

Codecov / codecov/patch

model/user.go#L485

Added line #L485 was not covered by tests
// 降级为 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)

Check warning on line 491 in model/user.go

View check run for this annotation

Codecov / codecov/patch

model/user.go#L487-L491

Added lines #L487 - L491 were not covered by tests
}
}
}
}
}
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 b86a3ac

Please sign in to comment.