Update token.go
This commit is contained in:
parent
938981fb42
commit
c94f6890fd
@ -35,19 +35,19 @@ func SearchUserTokens(userId int, keyword string) (tokens []*Token, err error) {
|
|||||||
|
|
||||||
func ValidateUserToken(key string) (token *Token, err error) {
|
func ValidateUserToken(key string) (token *Token, err error) {
|
||||||
if key == "" {
|
if key == "" {
|
||||||
return nil, errors.New("未提供 token")
|
return nil, errors.New("N/A (no text provided) token")
|
||||||
}
|
}
|
||||||
token = &Token{}
|
token = &Token{}
|
||||||
err = DB.Where("`key` = ?", key).First(token).Error
|
err = DB.Where("`key` = ?", key).First(token).Error
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if token.Status != common.TokenStatusEnabled {
|
if token.Status != common.TokenStatusEnabled {
|
||||||
return nil, errors.New("该 token 状态不可用")
|
return nil, errors.New("The token status is not enabled.")
|
||||||
}
|
}
|
||||||
if token.ExpiredTime != -1 && token.ExpiredTime < common.GetTimestamp() {
|
if token.ExpiredTime != -1 && token.ExpiredTime < common.GetTimestamp() {
|
||||||
token.Status = common.TokenStatusExpired
|
token.Status = common.TokenStatusExpired
|
||||||
err := token.SelectUpdate()
|
err := token.SelectUpdate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.SysError("更新 token 状态失败:" + err.Error())
|
common.SysError("Failed to update token status.:" + err.Error())
|
||||||
}
|
}
|
||||||
return nil, errors.New("该 token 已过期")
|
return nil, errors.New("该 token 已过期")
|
||||||
}
|
}
|
||||||
@ -55,25 +55,25 @@ func ValidateUserToken(key string) (token *Token, err error) {
|
|||||||
token.Status = common.TokenStatusExhausted
|
token.Status = common.TokenStatusExhausted
|
||||||
err := token.SelectUpdate()
|
err := token.SelectUpdate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.SysError("更新 token 状态失败:" + err.Error())
|
common.SysError("Failed to update token status.:" + err.Error())
|
||||||
}
|
}
|
||||||
return nil, errors.New("该 token 额度已用尽")
|
return nil, errors.New("This token's quota has been exhausted.")
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
token.AccessedTime = common.GetTimestamp()
|
token.AccessedTime = common.GetTimestamp()
|
||||||
err := token.SelectUpdate()
|
err := token.SelectUpdate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.SysError("更新 token 失败:" + err.Error())
|
common.SysError("Failed to update token.:" + err.Error())
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
return token, nil
|
return token, nil
|
||||||
}
|
}
|
||||||
return nil, errors.New("无效的 token")
|
return nil, errors.New("Invalid token")
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTokenByIds(id int, userId int) (*Token, error) {
|
func GetTokenByIds(id int, userId int) (*Token, error) {
|
||||||
if id == 0 || userId == 0 {
|
if id == 0 || userId == 0 {
|
||||||
return nil, errors.New("id 或 userId 为空!")
|
return nil, errors.New("id or userId is empty!")
|
||||||
}
|
}
|
||||||
token := Token{Id: id, UserId: userId}
|
token := Token{Id: id, UserId: userId}
|
||||||
var err error = nil
|
var err error = nil
|
||||||
@ -83,7 +83,7 @@ func GetTokenByIds(id int, userId int) (*Token, error) {
|
|||||||
|
|
||||||
func GetTokenById(id int) (*Token, error) {
|
func GetTokenById(id int) (*Token, error) {
|
||||||
if id == 0 {
|
if id == 0 {
|
||||||
return nil, errors.New("id 为空!")
|
return nil, errors.New("id Is empty.!")
|
||||||
}
|
}
|
||||||
token := Token{Id: id}
|
token := Token{Id: id}
|
||||||
var err error = nil
|
var err error = nil
|
||||||
@ -118,7 +118,7 @@ func (token *Token) Delete() error {
|
|||||||
func DeleteTokenById(id int, userId int) (err error) {
|
func DeleteTokenById(id int, userId int) (err error) {
|
||||||
// Why we need userId here? In case user want to delete other's token.
|
// Why we need userId here? In case user want to delete other's token.
|
||||||
if id == 0 || userId == 0 {
|
if id == 0 || userId == 0 {
|
||||||
return errors.New("id 或 userId 为空!")
|
return errors.New("id or userId is empty.!")
|
||||||
}
|
}
|
||||||
token := Token{Id: id, UserId: userId}
|
token := Token{Id: id, UserId: userId}
|
||||||
err = DB.Where(token).First(&token).Error
|
err = DB.Where(token).First(&token).Error
|
||||||
@ -130,7 +130,7 @@ func DeleteTokenById(id int, userId int) (err error) {
|
|||||||
|
|
||||||
func IncreaseTokenQuota(id int, quota int) (err error) {
|
func IncreaseTokenQuota(id int, quota int) (err error) {
|
||||||
if quota < 0 {
|
if quota < 0 {
|
||||||
return errors.New("quota 不能为负数!")
|
return errors.New("quota Cannot be negative.!")
|
||||||
}
|
}
|
||||||
err = DB.Model(&Token{}).Where("id = ?", id).Update("remain_quota", gorm.Expr("remain_quota + ?", quota)).Error
|
err = DB.Model(&Token{}).Where("id = ?", id).Update("remain_quota", gorm.Expr("remain_quota + ?", quota)).Error
|
||||||
return err
|
return err
|
||||||
@ -138,7 +138,7 @@ func IncreaseTokenQuota(id int, quota int) (err error) {
|
|||||||
|
|
||||||
func DecreaseTokenQuota(id int, quota int) (err error) {
|
func DecreaseTokenQuota(id int, quota int) (err error) {
|
||||||
if quota < 0 {
|
if quota < 0 {
|
||||||
return errors.New("quota 不能为负数!")
|
return errors.New("quota Cannot be negative.!")
|
||||||
}
|
}
|
||||||
err = DB.Model(&Token{}).Where("id = ?", id).Update("remain_quota", gorm.Expr("remain_quota - ?", quota)).Error
|
err = DB.Model(&Token{}).Where("id = ?", id).Update("remain_quota", gorm.Expr("remain_quota - ?", quota)).Error
|
||||||
return err
|
return err
|
||||||
@ -146,21 +146,21 @@ func DecreaseTokenQuota(id int, quota int) (err error) {
|
|||||||
|
|
||||||
func PreConsumeTokenQuota(tokenId int, quota int) (err error) {
|
func PreConsumeTokenQuota(tokenId int, quota int) (err error) {
|
||||||
if quota < 0 {
|
if quota < 0 {
|
||||||
return errors.New("quota 不能为负数!")
|
return errors.New("quota Cannot be negative.!")
|
||||||
}
|
}
|
||||||
token, err := GetTokenById(tokenId)
|
token, err := GetTokenById(tokenId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !token.UnlimitedQuota && token.RemainQuota < quota {
|
if !token.UnlimitedQuota && token.RemainQuota < quota {
|
||||||
return errors.New("令牌额度不足")
|
return errors.New("Token quota insufficient.")
|
||||||
}
|
}
|
||||||
userQuota, err := GetUserQuota(token.UserId)
|
userQuota, err := GetUserQuota(token.UserId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if userQuota < quota {
|
if userQuota < quota {
|
||||||
return errors.New("用户额度不足")
|
return errors.New("User quota insufficient.")
|
||||||
}
|
}
|
||||||
quotaTooLow := userQuota >= common.QuotaRemindThreshold && userQuota-quota < common.QuotaRemindThreshold
|
quotaTooLow := userQuota >= common.QuotaRemindThreshold && userQuota-quota < common.QuotaRemindThreshold
|
||||||
noMoreQuota := userQuota-quota <= 0
|
noMoreQuota := userQuota-quota <= 0
|
||||||
@ -168,18 +168,18 @@ func PreConsumeTokenQuota(tokenId int, quota int) (err error) {
|
|||||||
go func() {
|
go func() {
|
||||||
email, err := GetUserEmail(token.UserId)
|
email, err := GetUserEmail(token.UserId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.SysError("获取用户邮箱失败:" + err.Error())
|
common.SysError("Failed to get user email.:" + err.Error())
|
||||||
}
|
}
|
||||||
prompt := "您的额度即将用尽"
|
prompt := "Your quota is running low."
|
||||||
if noMoreQuota {
|
if noMoreQuota {
|
||||||
prompt = "您的额度已用尽"
|
prompt = "Your quota has run out."
|
||||||
}
|
}
|
||||||
if email != "" {
|
if email != "" {
|
||||||
topUpLink := fmt.Sprintf("%s/topup", common.ServerAddress)
|
topUpLink := fmt.Sprintf("%s/topup", common.ServerAddress)
|
||||||
err = common.SendEmail(prompt, email,
|
err = common.SendEmail(prompt, email,
|
||||||
fmt.Sprintf("%s,当前剩余额度为 %d,为了不影响您的使用,请及时充值。<br/>充值链接:<a href='%s'>%s</a>", prompt, userQuota, topUpLink, topUpLink))
|
fmt.Sprintf("%s,Current remaining quota is %d,To avoid any disruption to your usage,,Please top up your account promptly.。<br/>Top-up link.:<a href='%s'>%s</a>", prompt, userQuota, topUpLink, topUpLink))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.SysError("发送邮件失败:" + err.Error())
|
common.SysError("Failed to send email.:" + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
Loading…
Reference in New Issue
Block a user