generated from songquanpeng/gin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: 引入到期时间概念,当非default的分组时将有到期时间概念 #1659
Open
jinjianming
wants to merge
4
commits into
songquanpeng:main
Choose a base branch
from
jinjianming:User_Rights
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,9 @@ | |
"github.com/songquanpeng/one-api/common/logger" | ||
"github.com/songquanpeng/one-api/common/random" | ||
"gorm.io/gorm" | ||
"log" | ||
"strings" | ||
"time" | ||
) | ||
|
||
const ( | ||
|
@@ -47,6 +49,7 @@ | |
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 { | ||
|
@@ -210,6 +213,25 @@ | |
if !okay || user.Status != UserStatusEnabled { | ||
return errors.New("用户名或密码错误,或用户已被封禁") | ||
} | ||
// 校验用户是不是非default,如果是非default,判断到期时间如果过期了降级为default | ||
if user.ExpirationDate > 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 前面有定时任务。这里登陆不用判断了。 |
||
// 将时间戳转换为 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 | ||
} | ||
|
||
|
@@ -435,3 +457,40 @@ | |
DB.Model(&User{}).Where("id = ?", id).Select("username").Find(&username) | ||
return username | ||
} | ||
|
||
func checkAndDowngradeUsers() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 一条sql就能解决的事情 为什么要查询出来遍历? |
||
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) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
没有必要放这里。 每天只要执行一次就够了