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
aa18ac48
authored
Apr 28, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: use quota instead of times
parent
567ff735
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
36 additions
and
36 deletions
+36
-36
controller/relay.go
+3
-3
controller/token.go
+7
-7
middleware/auth.go
+1
-1
model/token.go
+8
-8
web/src/components/TokensTable.js
+4
-4
web/src/pages/Token/EditToken.js
+13
-13
No files found.
controller/relay.go
View file @
aa18ac48
...
...
@@ -14,7 +14,7 @@ import (
func
Relay
(
c
*
gin
.
Context
)
{
channelType
:=
c
.
GetInt
(
"channel"
)
tokenId
:=
c
.
GetInt
(
"token_id"
)
isUnlimited
Times
:=
c
.
GetBool
(
"unlimited_times
"
)
isUnlimited
Quota
:=
c
.
GetBool
(
"unlimited_quota
"
)
baseURL
:=
common
.
ChannelBaseURLs
[
channelType
]
if
channelType
==
common
.
ChannelTypeCustom
{
baseURL
=
c
.
GetString
(
"base_url"
)
...
...
@@ -56,8 +56,8 @@ func Relay(c *gin.Context) {
if
err
!=
nil
{
common
.
SysError
(
"Error closing request body: "
+
err
.
Error
())
}
if
!
isUnlimited
Times
&&
requestURL
==
"/v1/chat/completions"
{
err
:=
model
.
DecreaseTokenRemain
Times
ById
(
tokenId
)
if
!
isUnlimited
Quota
&&
requestURL
==
"/v1/chat/completions"
{
err
:=
model
.
DecreaseTokenRemain
Quota
ById
(
tokenId
)
if
err
!=
nil
{
common
.
SysError
(
"Error decreasing token remain times: "
+
err
.
Error
())
}
...
...
controller/token.go
View file @
aa18ac48
...
...
@@ -102,8 +102,8 @@ func AddToken(c *gin.Context) {
ExpiredTime
:
token
.
ExpiredTime
,
}
if
isAdmin
{
cleanToken
.
Remain
Times
=
token
.
RemainTimes
cleanToken
.
Unlimited
Times
=
token
.
UnlimitedTimes
cleanToken
.
Remain
Quota
=
token
.
RemainQuota
cleanToken
.
Unlimited
Quota
=
token
.
UnlimitedQuota
}
else
{
userId
:=
c
.
GetInt
(
"id"
)
quota
,
err
:=
model
.
GetUserQuota
(
userId
)
...
...
@@ -115,7 +115,7 @@ func AddToken(c *gin.Context) {
return
}
if
quota
>
0
{
cleanToken
.
Remain
Times
=
quota
cleanToken
.
Remain
Quota
=
quota
}
}
err
=
cleanToken
.
Insert
()
...
...
@@ -128,7 +128,7 @@ func AddToken(c *gin.Context) {
}
if
!
isAdmin
{
// update user quota
err
=
model
.
DecreaseUserQuota
(
c
.
GetInt
(
"id"
),
cleanToken
.
Remain
Times
)
err
=
model
.
DecreaseUserQuota
(
c
.
GetInt
(
"id"
),
cleanToken
.
Remain
Quota
)
}
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
true
,
...
...
@@ -184,7 +184,7 @@ func UpdateToken(c *gin.Context) {
})
return
}
if
cleanToken
.
Status
==
common
.
TokenStatusExhausted
&&
cleanToken
.
Remain
Times
<=
0
&&
!
cleanToken
.
UnlimitedTimes
{
if
cleanToken
.
Status
==
common
.
TokenStatusExhausted
&&
cleanToken
.
Remain
Quota
<=
0
&&
!
cleanToken
.
UnlimitedQuota
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"message"
:
"令牌可用次数已用尽,无法启用,请先修改令牌剩余次数,或者设置为无限次数"
,
...
...
@@ -199,8 +199,8 @@ func UpdateToken(c *gin.Context) {
cleanToken
.
Name
=
token
.
Name
cleanToken
.
ExpiredTime
=
token
.
ExpiredTime
if
isAdmin
{
cleanToken
.
Remain
Times
=
token
.
RemainTimes
cleanToken
.
Unlimited
Times
=
token
.
UnlimitedTimes
cleanToken
.
Remain
Quota
=
token
.
RemainQuota
cleanToken
.
Unlimited
Quota
=
token
.
UnlimitedQuota
}
}
err
=
cleanToken
.
Update
()
...
...
middleware/auth.go
View file @
aa18ac48
...
...
@@ -110,7 +110,7 @@ func TokenAuth() func(c *gin.Context) {
}
c
.
Set
(
"id"
,
token
.
UserId
)
c
.
Set
(
"token_id"
,
token
.
Id
)
c
.
Set
(
"unlimited_
times"
,
token
.
UnlimitedTimes
)
c
.
Set
(
"unlimited_
quota"
,
token
.
UnlimitedQuota
)
if
len
(
parts
)
>
1
{
if
model
.
IsAdmin
(
token
.
UserId
)
{
c
.
Set
(
"channelId"
,
parts
[
1
])
...
...
model/token.go
View file @
aa18ac48
...
...
@@ -17,8 +17,8 @@ type Token struct {
CreatedTime
int64
`json:"created_time" gorm:"bigint"`
AccessedTime
int64
`json:"accessed_time" gorm:"bigint"`
ExpiredTime
int64
`json:"expired_time" gorm:"bigint;default:-1"`
// -1 means never expired
Remain
Times
int
`json:"remain_times
" gorm:"default:0"`
Unlimited
Times
bool
`json:"unlimited_times
" gorm:"default:false"`
Remain
Quota
int
`json:"remain_quota
" gorm:"default:0"`
Unlimited
Quota
bool
`json:"unlimited_quota
" gorm:"default:false"`
}
func
GetAllUserTokens
(
userId
int
,
startIdx
int
,
num
int
)
([]
*
Token
,
error
)
{
...
...
@@ -52,13 +52,13 @@ func ValidateUserToken(key string) (token *Token, err error) {
}
return
nil
,
errors
.
New
(
"该 token 已过期"
)
}
if
!
token
.
Unlimited
Times
&&
token
.
RemainTimes
<=
0
{
if
!
token
.
Unlimited
Quota
&&
token
.
RemainQuota
<=
0
{
token
.
Status
=
common
.
TokenStatusExhausted
err
:=
token
.
SelectUpdate
()
if
err
!=
nil
{
common
.
SysError
(
"更新 token 状态失败:"
+
err
.
Error
())
}
return
nil
,
errors
.
New
(
"该 token
可用次数
已用尽"
)
return
nil
,
errors
.
New
(
"该 token
额度
已用尽"
)
}
go
func
()
{
token
.
AccessedTime
=
common
.
GetTimestamp
()
...
...
@@ -91,7 +91,7 @@ func (token *Token) Insert() error {
// Update Make sure your token's fields is completed, because this will update non-zero values
func
(
token
*
Token
)
Update
()
error
{
var
err
error
err
=
DB
.
Model
(
token
)
.
Select
(
"name"
,
"status"
,
"expired_time"
,
"remain_
times"
,
"unlimited_times
"
)
.
Updates
(
token
)
.
Error
err
=
DB
.
Model
(
token
)
.
Select
(
"name"
,
"status"
,
"expired_time"
,
"remain_
quota"
,
"unlimited_quota
"
)
.
Updates
(
token
)
.
Error
return
err
}
...
...
@@ -119,12 +119,12 @@ func DeleteTokenById(id int, userId int) (err error) {
return
token
.
Delete
()
}
func
DecreaseTokenRemain
Times
ById
(
id
int
)
(
err
error
)
{
err
=
DB
.
Model
(
&
Token
{})
.
Where
(
"id = ?"
,
id
)
.
Update
(
"remain_
times"
,
gorm
.
Expr
(
"remain_times
- ?"
,
1
))
.
Error
func
DecreaseTokenRemain
Quota
ById
(
id
int
)
(
err
error
)
{
err
=
DB
.
Model
(
&
Token
{})
.
Where
(
"id = ?"
,
id
)
.
Update
(
"remain_
quota"
,
gorm
.
Expr
(
"remain_quota
- ?"
,
1
))
.
Error
return
err
}
func
TopUpToken
(
id
int
,
times
int
)
(
err
error
)
{
err
=
DB
.
Model
(
&
Token
{})
.
Where
(
"id = ?"
,
id
)
.
Update
(
"remain_
times"
,
gorm
.
Expr
(
"remain_times
+ ?"
,
times
))
.
Error
err
=
DB
.
Model
(
&
Token
{})
.
Where
(
"id = ?"
,
id
)
.
Update
(
"remain_
quota"
,
gorm
.
Expr
(
"remain_quota
+ ?"
,
times
))
.
Error
return
err
}
web/src/components/TokensTable.js
View file @
aa18ac48
...
...
@@ -164,7 +164,7 @@ const TokensTable = () => {
showSuccess
(
'充值成功!'
);
let
newTokens
=
[...
tokens
];
let
realIdx
=
(
activePage
-
1
)
*
ITEMS_PER_PAGE
+
targetTokenIdx
;
newTokens
[
realIdx
].
remain_
times
+=
data
;
newTokens
[
realIdx
].
remain_
quota
+=
data
;
setTokens
(
newTokens
);
setRedemptionCode
(
''
);
setShowTopUpModal
(
false
);
...
...
@@ -217,10 +217,10 @@ const TokensTable = () => {
<
Table
.
HeaderCell
style
=
{{
cursor
:
'pointer'
}}
onClick
=
{()
=>
{
sortToken
(
'remain_
times
'
);
sortToken
(
'remain_
quota
'
);
}}
>
剩余次数
额度
<
/Table.HeaderCell
>
<
Table
.
HeaderCell
style
=
{{
cursor
:
'pointer'
}}
...
...
@@ -255,7 +255,7 @@ const TokensTable = () => {
<
Table
.
Cell
>
{
token
.
id
}
<
/Table.Cell
>
<
Table
.
Cell
>
{
token
.
name
?
token
.
name
:
'无'
}
<
/Table.Cell
>
<
Table
.
Cell
>
{
renderStatus
(
token
.
status
)}
<
/Table.Cell
>
<
Table
.
Cell
>
{
token
.
unlimited_
times
?
'无限制'
:
token
.
remain_times
}
<
/Table.Cell
>
<
Table
.
Cell
>
{
token
.
unlimited_
quota
?
'无限制'
:
token
.
remain_quota
}
<
/Table.Cell
>
<
Table
.
Cell
>
{
renderTimestamp
(
token
.
created_time
)}
<
/Table.Cell
>
<
Table
.
Cell
>
{
token
.
expired_time
===
-
1
?
'永不过期'
:
renderTimestamp
(
token
.
expired_time
)}
<
/Table.Cell
>
<
Table
.
Cell
>
...
...
web/src/pages/Token/EditToken.js
View file @
aa18ac48
...
...
@@ -10,13 +10,13 @@ const EditToken = () => {
const
[
loading
,
setLoading
]
=
useState
(
isEdit
);
const
originInputs
=
{
name
:
''
,
remain_
times
:
0
,
remain_
quota
:
0
,
expired_time
:
-
1
,
unlimited_
times
:
false
unlimited_
quota
:
false
};
const
isAdminUser
=
isAdmin
();
const
[
inputs
,
setInputs
]
=
useState
(
originInputs
);
const
{
name
,
remain_
times
,
expired_time
,
unlimited_times
}
=
inputs
;
const
{
name
,
remain_
quota
,
expired_time
,
unlimited_quota
}
=
inputs
;
const
handleInputChange
=
(
e
,
{
name
,
value
})
=>
{
setInputs
((
inputs
)
=>
({
...
inputs
,
[
name
]:
value
}));
...
...
@@ -37,8 +37,8 @@ const EditToken = () => {
}
};
const
setUnlimited
Times
=
()
=>
{
setInputs
({
...
inputs
,
unlimited_
times
:
!
unlimited_times
});
const
setUnlimited
Quota
=
()
=>
{
setInputs
({
...
inputs
,
unlimited_
quota
:
!
unlimited_quota
});
};
const
loadToken
=
async
()
=>
{
...
...
@@ -63,7 +63,7 @@ const EditToken = () => {
const
submit
=
async
()
=>
{
if
(
!
isEdit
&&
inputs
.
name
===
''
)
return
;
let
localInputs
=
inputs
;
localInputs
.
remain_
times
=
parseInt
(
localInputs
.
remain_times
);
localInputs
.
remain_
quota
=
parseInt
(
localInputs
.
remain_quota
);
if
(
localInputs
.
expired_time
!==
-
1
)
{
let
time
=
Date
.
parse
(
localInputs
.
expired_time
);
if
(
isNaN
(
time
))
{
...
...
@@ -111,19 +111,19 @@ const EditToken = () => {
isAdminUser
&&
<>
<
Form
.
Field
>
<
Form
.
Input
label
=
'
剩余次数
'
name
=
'remain_
times
'
placeholder
=
{
'请输入
剩余次数
'
}
label
=
'
额度
'
name
=
'remain_
quota
'
placeholder
=
{
'请输入
额度
'
}
onChange
=
{
handleInputChange
}
value
=
{
remain_
times
}
value
=
{
remain_
quota
}
autoComplete
=
'off'
type
=
'number'
disabled
=
{
unlimited_
times
}
disabled
=
{
unlimited_
quota
}
/
>
<
/Form.Field
>
<
Button
type
=
{
'button'
}
style
=
{{
marginBottom
:
'14px'
}}
onClick
=
{()
=>
{
setUnlimited
Times
();
}}
>
{
unlimited_
times
?
'取消无限次'
:
'设置为无限次
'
}
<
/Button
>
setUnlimited
Quota
();
}}
>
{
unlimited_
quota
?
'取消无限额度'
:
'设置为无限额度
'
}
<
/Button
>
<
/
>
}
<
Form
.
Field
>
...
...
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