Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
a3ad4f52
authored
Aug 24, 2024
by
Jin Weihan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加环境变量GENERATE_DEFAULT_TOKEN 设置之后将生成初始令牌,默认关闭。
parent
58c9d08f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
18 deletions
+32
-18
constant/env.go
+3
-0
controller/user.go
+29
-18
No files found.
constant/env.go
View file @
a3ad4f52
...
@@ -44,3 +44,6 @@ func InitEnv() {
...
@@ -44,3 +44,6 @@ func InitEnv() {
}
}
}
}
}
}
// 是否生成初始令牌,默认关闭。
var
GenerateDefaultToken
=
common
.
GetEnvOrDefaultBool
(
"GENERATE_DEFAULT_TOKEN"
,
false
)
controller/user.go
View file @
a3ad4f52
...
@@ -11,6 +11,7 @@ import (
...
@@ -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) {
...
@@ -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
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment