增加环境变量GENERATE_DEFAULT_TOKEN 设置之后将生成初始令牌,默认关闭。

This commit is contained in:
Jin Weihan 2024-08-24 18:44:37 +00:00
parent 2422eb2820
commit 716bf6f48a
2 changed files with 32 additions and 18 deletions

View File

@ -44,3 +44,6 @@ func InitEnv() {
} }
} }
} }
// 是否生成初始令牌,默认关闭。
var GenerateDefaultToken = common.GetEnvOrDefaultBool("GENERATE_DEFAULT_TOKEN", false)

View File

@ -11,6 +11,7 @@ import (
"github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"one-api/constant"
) )
type LoginRequest struct { type LoginRequest struct {
@ -197,24 +198,34 @@ func Register(c *gin.Context) {
return return
} }
// 生成默认令牌 // 生成默认令牌
// tokenName := cleanUser.Username + "的初始令牌" if constant.GenerateDefaultToken {
token := model.Token{ var insertedUser model.User
UserId: insertedUser.Id, // 使用插入后的用户ID if err := model.DB.Where("username = ?", cleanUser.Username).First(&insertedUser).Error; err != nil {
Name: cleanUser.Username + "的初始令牌", c.JSON(http.StatusOK, gin.H{
Key: common.GenerateKey(), "success": false,
CreatedTime: common.GetTimestamp(), "message": "用户注册失败或用户ID获取失败",
AccessedTime: common.GetTimestamp(), })
ExpiredTime: -1, // 永不过期 return
RemainQuota: 500000, // 示例额度 }
UnlimitedQuota: true, // 生成默认令牌
ModelLimitsEnabled: false, token := model.Token{
} UserId: insertedUser.Id, // 使用插入后的用户ID
if err := token.Insert(); err != nil { Name: cleanUser.Username + "的初始令牌",
c.JSON(http.StatusOK, gin.H{ Key: common.GenerateKey(),
"success": false, CreatedTime: common.GetTimestamp(),
"message": "创建默认令牌失败", AccessedTime: common.GetTimestamp(),
}) ExpiredTime: -1, // 永不过期
return RemainQuota: 500000, // 示例额度
UnlimitedQuota: true,
ModelLimitsEnabled: false,
}
if err := token.Insert(); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "创建默认令牌失败",
})
return
}
} }
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{