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
b7a89340
authored
Jun 22, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: format logs
parent
b0b0ed20
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
36 additions
and
34 deletions
+36
-34
common/group-ratio.go
+2
-2
common/model-ratio.go
+2
-2
common/redis.go
+5
-2
controller/channel-test.go
+2
-2
controller/relay-text.go
+4
-4
controller/relay.go
+1
-1
main.go
+5
-6
model/cache.go
+2
-2
model/main.go
+2
-2
model/option.go
+2
-2
model/redemption.go
+1
-1
model/token.go
+5
-5
model/user.go
+3
-3
No files found.
common/group-ratio.go
View file @
b7a89340
...
@@ -11,7 +11,7 @@ var GroupRatio = map[string]float64{
...
@@ -11,7 +11,7 @@ var GroupRatio = map[string]float64{
func
GroupRatio2JSONString
()
string
{
func
GroupRatio2JSONString
()
string
{
jsonBytes
,
err
:=
json
.
Marshal
(
GroupRatio
)
jsonBytes
,
err
:=
json
.
Marshal
(
GroupRatio
)
if
err
!=
nil
{
if
err
!=
nil
{
SysError
(
"
E
rror marshalling model ratio: "
+
err
.
Error
())
SysError
(
"
e
rror marshalling model ratio: "
+
err
.
Error
())
}
}
return
string
(
jsonBytes
)
return
string
(
jsonBytes
)
}
}
...
@@ -24,7 +24,7 @@ func UpdateGroupRatioByJSONString(jsonStr string) error {
...
@@ -24,7 +24,7 @@ func UpdateGroupRatioByJSONString(jsonStr string) error {
func
GetGroupRatio
(
name
string
)
float64
{
func
GetGroupRatio
(
name
string
)
float64
{
ratio
,
ok
:=
GroupRatio
[
name
]
ratio
,
ok
:=
GroupRatio
[
name
]
if
!
ok
{
if
!
ok
{
SysError
(
"
G
roup ratio not found: "
+
name
)
SysError
(
"
g
roup ratio not found: "
+
name
)
return
1
return
1
}
}
return
ratio
return
ratio
...
...
common/model-ratio.go
View file @
b7a89340
...
@@ -40,7 +40,7 @@ var ModelRatio = map[string]float64{
...
@@ -40,7 +40,7 @@ var ModelRatio = map[string]float64{
func
ModelRatio2JSONString
()
string
{
func
ModelRatio2JSONString
()
string
{
jsonBytes
,
err
:=
json
.
Marshal
(
ModelRatio
)
jsonBytes
,
err
:=
json
.
Marshal
(
ModelRatio
)
if
err
!=
nil
{
if
err
!=
nil
{
SysError
(
"
E
rror marshalling model ratio: "
+
err
.
Error
())
SysError
(
"
e
rror marshalling model ratio: "
+
err
.
Error
())
}
}
return
string
(
jsonBytes
)
return
string
(
jsonBytes
)
}
}
...
@@ -53,7 +53,7 @@ func UpdateModelRatioByJSONString(jsonStr string) error {
...
@@ -53,7 +53,7 @@ func UpdateModelRatioByJSONString(jsonStr string) error {
func
GetModelRatio
(
name
string
)
float64
{
func
GetModelRatio
(
name
string
)
float64
{
ratio
,
ok
:=
ModelRatio
[
name
]
ratio
,
ok
:=
ModelRatio
[
name
]
if
!
ok
{
if
!
ok
{
SysError
(
"
M
odel ratio not found: "
+
name
)
SysError
(
"
m
odel ratio not found: "
+
name
)
return
30
return
30
}
}
return
ratio
return
ratio
...
...
common/redis.go
View file @
b7a89340
...
@@ -20,7 +20,7 @@ func InitRedisClient() (err error) {
...
@@ -20,7 +20,7 @@ func InitRedisClient() (err error) {
SysLog
(
"Redis is enabled"
)
SysLog
(
"Redis is enabled"
)
opt
,
err
:=
redis
.
ParseURL
(
os
.
Getenv
(
"REDIS_CONN_STRING"
))
opt
,
err
:=
redis
.
ParseURL
(
os
.
Getenv
(
"REDIS_CONN_STRING"
))
if
err
!=
nil
{
if
err
!=
nil
{
FatalLog
(
err
)
FatalLog
(
"failed to parse Redis connection string: "
+
err
.
Error
()
)
}
}
RDB
=
redis
.
NewClient
(
opt
)
RDB
=
redis
.
NewClient
(
opt
)
...
@@ -28,13 +28,16 @@ func InitRedisClient() (err error) {
...
@@ -28,13 +28,16 @@ func InitRedisClient() (err error) {
defer
cancel
()
defer
cancel
()
_
,
err
=
RDB
.
Ping
(
ctx
)
.
Result
()
_
,
err
=
RDB
.
Ping
(
ctx
)
.
Result
()
if
err
!=
nil
{
FatalLog
(
"Redis ping test failed: "
+
err
.
Error
())
}
return
err
return
err
}
}
func
ParseRedisOption
()
*
redis
.
Options
{
func
ParseRedisOption
()
*
redis
.
Options
{
opt
,
err
:=
redis
.
ParseURL
(
os
.
Getenv
(
"REDIS_CONN_STRING"
))
opt
,
err
:=
redis
.
ParseURL
(
os
.
Getenv
(
"REDIS_CONN_STRING"
))
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
err
)
FatalLog
(
"failed to parse Redis connection string: "
+
err
.
Error
()
)
}
}
return
opt
return
opt
}
}
...
...
controller/channel-test.go
View file @
b7a89340
...
@@ -129,7 +129,7 @@ func disableChannel(channelId int, channelName string, reason string) {
...
@@ -129,7 +129,7 @@ func disableChannel(channelId int, channelName string, reason string) {
content
:=
fmt
.
Sprintf
(
"通道「%s」(#%d)已被禁用,原因:%s"
,
channelName
,
channelId
,
reason
)
content
:=
fmt
.
Sprintf
(
"通道「%s」(#%d)已被禁用,原因:%s"
,
channelName
,
channelId
,
reason
)
err
:=
common
.
SendEmail
(
subject
,
common
.
RootUserEmail
,
content
)
err
:=
common
.
SendEmail
(
subject
,
common
.
RootUserEmail
,
content
)
if
err
!=
nil
{
if
err
!=
nil
{
common
.
SysError
(
fmt
.
Sprintf
(
"
发送邮件失败:
%s"
,
err
.
Error
()))
common
.
SysError
(
fmt
.
Sprintf
(
"
failed to send email:
%s"
,
err
.
Error
()))
}
}
}
}
...
@@ -176,7 +176,7 @@ func testAllChannels(c *gin.Context) error {
...
@@ -176,7 +176,7 @@ func testAllChannels(c *gin.Context) error {
}
}
err
:=
common
.
SendEmail
(
"通道测试完成"
,
common
.
RootUserEmail
,
"通道测试完成,如果没有收到禁用通知,说明所有通道都正常"
)
err
:=
common
.
SendEmail
(
"通道测试完成"
,
common
.
RootUserEmail
,
"通道测试完成,如果没有收到禁用通知,说明所有通道都正常"
)
if
err
!=
nil
{
if
err
!=
nil
{
common
.
SysError
(
fmt
.
Sprintf
(
"
发送邮件失败:
%s"
,
err
.
Error
()))
common
.
SysError
(
fmt
.
Sprintf
(
"
failed to send email:
%s"
,
err
.
Error
()))
}
}
testAllChannelsLock
.
Lock
()
testAllChannelsLock
.
Lock
()
testAllChannelsRunning
=
false
testAllChannelsRunning
=
false
...
...
controller/relay-text.go
View file @
b7a89340
...
@@ -140,7 +140,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
...
@@ -140,7 +140,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
quotaDelta
:=
quota
-
preConsumedQuota
quotaDelta
:=
quota
-
preConsumedQuota
err
:=
model
.
PostConsumeTokenQuota
(
tokenId
,
quotaDelta
)
err
:=
model
.
PostConsumeTokenQuota
(
tokenId
,
quotaDelta
)
if
err
!=
nil
{
if
err
!=
nil
{
common
.
SysError
(
"
E
rror consuming token remain quota: "
+
err
.
Error
())
common
.
SysError
(
"
e
rror consuming token remain quota: "
+
err
.
Error
())
}
}
tokenName
:=
c
.
GetString
(
"token_name"
)
tokenName
:=
c
.
GetString
(
"token_name"
)
model
.
RecordLog
(
userId
,
model
.
LogTypeConsume
,
fmt
.
Sprintf
(
"通过令牌「%s」使用模型 %s 消耗 %s(模型倍率 %.2f,分组倍率 %.2f)"
,
tokenName
,
textRequest
.
Model
,
common
.
LogQuota
(
quota
),
modelRatio
,
groupRatio
))
model
.
RecordLog
(
userId
,
model
.
LogTypeConsume
,
fmt
.
Sprintf
(
"通过令牌「%s」使用模型 %s 消耗 %s(模型倍率 %.2f,分组倍率 %.2f)"
,
tokenName
,
textRequest
.
Model
,
common
.
LogQuota
(
quota
),
modelRatio
,
groupRatio
))
...
@@ -173,7 +173,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
...
@@ -173,7 +173,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
for
scanner
.
Scan
()
{
for
scanner
.
Scan
()
{
data
:=
scanner
.
Text
()
data
:=
scanner
.
Text
()
if
len
(
data
)
<
6
{
// must be something wrong!
if
len
(
data
)
<
6
{
// must be something wrong!
common
.
SysError
(
"
I
nvalid stream response: "
+
data
)
common
.
SysError
(
"
i
nvalid stream response: "
+
data
)
continue
continue
}
}
dataChan
<-
data
dataChan
<-
data
...
@@ -184,7 +184,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
...
@@ -184,7 +184,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
var
streamResponse
ChatCompletionsStreamResponse
var
streamResponse
ChatCompletionsStreamResponse
err
=
json
.
Unmarshal
([]
byte
(
data
),
&
streamResponse
)
err
=
json
.
Unmarshal
([]
byte
(
data
),
&
streamResponse
)
if
err
!=
nil
{
if
err
!=
nil
{
common
.
SysError
(
"
E
rror unmarshalling stream response: "
+
err
.
Error
())
common
.
SysError
(
"
e
rror unmarshalling stream response: "
+
err
.
Error
())
return
return
}
}
for
_
,
choice
:=
range
streamResponse
.
Choices
{
for
_
,
choice
:=
range
streamResponse
.
Choices
{
...
@@ -194,7 +194,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
...
@@ -194,7 +194,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
var
streamResponse
CompletionsStreamResponse
var
streamResponse
CompletionsStreamResponse
err
=
json
.
Unmarshal
([]
byte
(
data
),
&
streamResponse
)
err
=
json
.
Unmarshal
([]
byte
(
data
),
&
streamResponse
)
if
err
!=
nil
{
if
err
!=
nil
{
common
.
SysError
(
"
E
rror unmarshalling stream response: "
+
err
.
Error
())
common
.
SysError
(
"
e
rror unmarshalling stream response: "
+
err
.
Error
())
return
return
}
}
for
_
,
choice
:=
range
streamResponse
.
Choices
{
for
_
,
choice
:=
range
streamResponse
.
Choices
{
...
...
controller/relay.go
View file @
b7a89340
...
@@ -118,7 +118,7 @@ func Relay(c *gin.Context) {
...
@@ -118,7 +118,7 @@ func Relay(c *gin.Context) {
"error"
:
err
.
OpenAIError
,
"error"
:
err
.
OpenAIError
,
})
})
channelId
:=
c
.
GetInt
(
"channel_id"
)
channelId
:=
c
.
GetInt
(
"channel_id"
)
common
.
SysError
(
fmt
.
Sprintf
(
"
R
elay error (channel #%d): %s"
,
channelId
,
err
.
Message
))
common
.
SysError
(
fmt
.
Sprintf
(
"
r
elay error (channel #%d): %s"
,
channelId
,
err
.
Message
))
// https://platform.openai.com/docs/guides/error-codes/api-errors
// https://platform.openai.com/docs/guides/error-codes/api-errors
if
common
.
AutomaticDisableChannelEnabled
&&
(
err
.
Type
==
"insufficient_quota"
||
err
.
Code
==
"invalid_api_key"
)
{
if
common
.
AutomaticDisableChannelEnabled
&&
(
err
.
Type
==
"insufficient_quota"
||
err
.
Code
==
"invalid_api_key"
)
{
channelId
:=
c
.
GetInt
(
"channel_id"
)
channelId
:=
c
.
GetInt
(
"channel_id"
)
...
...
main.go
View file @
b7a89340
...
@@ -6,7 +6,6 @@ import (
...
@@ -6,7 +6,6 @@ import (
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-contrib/sessions/redis"
"github.com/gin-contrib/sessions/redis"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
"log"
"one-api/common"
"one-api/common"
"one-api/middleware"
"one-api/middleware"
"one-api/model"
"one-api/model"
...
@@ -30,19 +29,19 @@ func main() {
...
@@ -30,19 +29,19 @@ func main() {
// Initialize SQL Database
// Initialize SQL Database
err
:=
model
.
InitDB
()
err
:=
model
.
InitDB
()
if
err
!=
nil
{
if
err
!=
nil
{
common
.
FatalLog
(
err
)
common
.
FatalLog
(
"failed to initialize database: "
+
err
.
Error
()
)
}
}
defer
func
()
{
defer
func
()
{
err
:=
model
.
CloseDB
()
err
:=
model
.
CloseDB
()
if
err
!=
nil
{
if
err
!=
nil
{
common
.
FatalLog
(
err
)
common
.
FatalLog
(
"failed to close database: "
+
err
.
Error
()
)
}
}
}()
}()
// Initialize Redis
// Initialize Redis
err
=
common
.
InitRedisClient
()
err
=
common
.
InitRedisClient
()
if
err
!=
nil
{
if
err
!=
nil
{
common
.
FatalLog
(
err
)
common
.
FatalLog
(
"failed to initialize Redis: "
+
err
.
Error
()
)
}
}
// Initialize options
// Initialize options
...
@@ -53,7 +52,7 @@ func main() {
...
@@ -53,7 +52,7 @@ func main() {
if
os
.
Getenv
(
"SYNC_FREQUENCY"
)
!=
""
{
if
os
.
Getenv
(
"SYNC_FREQUENCY"
)
!=
""
{
frequency
,
err
:=
strconv
.
Atoi
(
os
.
Getenv
(
"SYNC_FREQUENCY"
))
frequency
,
err
:=
strconv
.
Atoi
(
os
.
Getenv
(
"SYNC_FREQUENCY"
))
if
err
!=
nil
{
if
err
!=
nil
{
common
.
FatalLog
(
err
)
common
.
FatalLog
(
"failed to parse SYNC_FREQUENCY: "
+
err
.
Error
()
)
}
}
go
model
.
SyncOptions
(
frequency
)
go
model
.
SyncOptions
(
frequency
)
if
common
.
RedisEnabled
{
if
common
.
RedisEnabled
{
...
@@ -84,6 +83,6 @@ func main() {
...
@@ -84,6 +83,6 @@ func main() {
}
}
err
=
server
.
Run
(
":"
+
port
)
err
=
server
.
Run
(
":"
+
port
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
err
)
common
.
FatalLog
(
"failed to start HTTP server: "
+
err
.
Error
()
)
}
}
}
}
model/cache.go
View file @
b7a89340
...
@@ -137,13 +137,13 @@ func InitChannelCache() {
...
@@ -137,13 +137,13 @@ func InitChannelCache() {
channelSyncLock
.
Lock
()
channelSyncLock
.
Lock
()
group2model2channels
=
newGroup2model2channels
group2model2channels
=
newGroup2model2channels
channelSyncLock
.
Unlock
()
channelSyncLock
.
Unlock
()
common
.
SysLog
(
"
C
hannels synced from database"
)
common
.
SysLog
(
"
c
hannels synced from database"
)
}
}
func
SyncChannelCache
(
frequency
int
)
{
func
SyncChannelCache
(
frequency
int
)
{
for
{
for
{
time
.
Sleep
(
time
.
Duration
(
frequency
)
*
time
.
Second
)
time
.
Sleep
(
time
.
Duration
(
frequency
)
*
time
.
Second
)
common
.
SysLog
(
"
S
yncing channels from database"
)
common
.
SysLog
(
"
s
yncing channels from database"
)
InitChannelCache
()
InitChannelCache
()
}
}
}
}
...
...
model/main.go
View file @
b7a89340
...
@@ -54,7 +54,7 @@ func InitDB() (err error) {
...
@@ -54,7 +54,7 @@ func InitDB() (err error) {
PrepareStmt
:
true
,
// precompile SQL
PrepareStmt
:
true
,
// precompile SQL
})
})
}
}
common
.
SysLog
(
"
D
atabase connected"
)
common
.
SysLog
(
"
d
atabase connected"
)
if
err
==
nil
{
if
err
==
nil
{
DB
=
db
DB
=
db
if
!
common
.
IsMasterNode
{
if
!
common
.
IsMasterNode
{
...
@@ -88,7 +88,7 @@ func InitDB() (err error) {
...
@@ -88,7 +88,7 @@ func InitDB() (err error) {
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
common
.
SysLog
(
"
D
atabase migrated"
)
common
.
SysLog
(
"
d
atabase migrated"
)
err
=
createRootAccountIfNeed
()
err
=
createRootAccountIfNeed
()
return
err
return
err
}
else
{
}
else
{
...
...
model/option.go
View file @
b7a89340
...
@@ -75,7 +75,7 @@ func loadOptionsFromDatabase() {
...
@@ -75,7 +75,7 @@ func loadOptionsFromDatabase() {
for
_
,
option
:=
range
options
{
for
_
,
option
:=
range
options
{
err
:=
updateOptionMap
(
option
.
Key
,
option
.
Value
)
err
:=
updateOptionMap
(
option
.
Key
,
option
.
Value
)
if
err
!=
nil
{
if
err
!=
nil
{
common
.
SysError
(
"
F
ailed to update option map: "
+
err
.
Error
())
common
.
SysError
(
"
f
ailed to update option map: "
+
err
.
Error
())
}
}
}
}
}
}
...
@@ -83,7 +83,7 @@ func loadOptionsFromDatabase() {
...
@@ -83,7 +83,7 @@ func loadOptionsFromDatabase() {
func
SyncOptions
(
frequency
int
)
{
func
SyncOptions
(
frequency
int
)
{
for
{
for
{
time
.
Sleep
(
time
.
Duration
(
frequency
)
*
time
.
Second
)
time
.
Sleep
(
time
.
Duration
(
frequency
)
*
time
.
Second
)
common
.
SysLog
(
"
S
yncing options from database"
)
common
.
SysLog
(
"
s
yncing options from database"
)
loadOptionsFromDatabase
()
loadOptionsFromDatabase
()
}
}
}
}
...
...
model/redemption.go
View file @
b7a89340
...
@@ -64,7 +64,7 @@ func Redeem(key string, userId int) (quota int, err error) {
...
@@ -64,7 +64,7 @@ func Redeem(key string, userId int) (quota int, err error) {
redemption
.
Status
=
common
.
RedemptionCodeStatusUsed
redemption
.
Status
=
common
.
RedemptionCodeStatusUsed
err
:=
redemption
.
SelectUpdate
()
err
:=
redemption
.
SelectUpdate
()
if
err
!=
nil
{
if
err
!=
nil
{
common
.
SysError
(
"
更新兑换码状态失败:
"
+
err
.
Error
())
common
.
SysError
(
"
failed to update redemption status:
"
+
err
.
Error
())
}
}
RecordLog
(
userId
,
LogTypeTopup
,
fmt
.
Sprintf
(
"通过兑换码充值 %s"
,
common
.
LogQuota
(
redemption
.
Quota
)))
RecordLog
(
userId
,
LogTypeTopup
,
fmt
.
Sprintf
(
"通过兑换码充值 %s"
,
common
.
LogQuota
(
redemption
.
Quota
)))
}()
}()
...
...
model/token.go
View file @
b7a89340
...
@@ -45,7 +45,7 @@ func ValidateUserToken(key string) (token *Token, err error) {
...
@@ -45,7 +45,7 @@ func ValidateUserToken(key string) (token *Token, err error) {
token
.
Status
=
common
.
TokenStatusExpired
token
.
Status
=
common
.
TokenStatusExpired
err
:=
token
.
SelectUpdate
()
err
:=
token
.
SelectUpdate
()
if
err
!=
nil
{
if
err
!=
nil
{
common
.
SysError
(
"
更新令牌状态失败:
"
+
err
.
Error
())
common
.
SysError
(
"
failed to update token status
"
+
err
.
Error
())
}
}
return
nil
,
errors
.
New
(
"该令牌已过期"
)
return
nil
,
errors
.
New
(
"该令牌已过期"
)
}
}
...
@@ -53,7 +53,7 @@ func ValidateUserToken(key string) (token *Token, err error) {
...
@@ -53,7 +53,7 @@ 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
(
"
更新令牌状态失败:
"
+
err
.
Error
())
common
.
SysError
(
"
failed to update token status
"
+
err
.
Error
())
}
}
return
nil
,
errors
.
New
(
"该令牌额度已用尽"
)
return
nil
,
errors
.
New
(
"该令牌额度已用尽"
)
}
}
...
@@ -61,7 +61,7 @@ func ValidateUserToken(key string) (token *Token, err error) {
...
@@ -61,7 +61,7 @@ func ValidateUserToken(key string) (token *Token, err error) {
token
.
AccessedTime
=
common
.
GetTimestamp
()
token
.
AccessedTime
=
common
.
GetTimestamp
()
err
:=
token
.
SelectUpdate
()
err
:=
token
.
SelectUpdate
()
if
err
!=
nil
{
if
err
!=
nil
{
common
.
SysError
(
"
更新令牌失败:
"
+
err
.
Error
())
common
.
SysError
(
"
failed to update token
"
+
err
.
Error
())
}
}
}()
}()
return
token
,
nil
return
token
,
nil
...
@@ -166,7 +166,7 @@ func PreConsumeTokenQuota(tokenId int, quota int) (err error) {
...
@@ -166,7 +166,7 @@ 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 fetch user email:
"
+
err
.
Error
())
}
}
prompt
:=
"您的额度即将用尽"
prompt
:=
"您的额度即将用尽"
if
noMoreQuota
{
if
noMoreQuota
{
...
@@ -177,7 +177,7 @@ func PreConsumeTokenQuota(tokenId int, quota int) (err error) {
...
@@ -177,7 +177,7 @@ func PreConsumeTokenQuota(tokenId int, quota int) (err error) {
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,当前剩余额度为 %d,为了不影响您的使用,请及时充值。<br/>充值链接:<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
())
}
}
}
}
}()
}()
...
...
model/user.go
View file @
b7a89340
...
@@ -220,7 +220,7 @@ func IsAdmin(userId int) bool {
...
@@ -220,7 +220,7 @@ func IsAdmin(userId int) bool {
var
user
User
var
user
User
err
:=
DB
.
Where
(
"id = ?"
,
userId
)
.
Select
(
"role"
)
.
Find
(
&
user
)
.
Error
err
:=
DB
.
Where
(
"id = ?"
,
userId
)
.
Select
(
"role"
)
.
Find
(
&
user
)
.
Error
if
err
!=
nil
{
if
err
!=
nil
{
common
.
SysError
(
"
N
o such user "
+
err
.
Error
())
common
.
SysError
(
"
n
o such user "
+
err
.
Error
())
return
false
return
false
}
}
return
user
.
Role
>=
common
.
RoleAdminUser
return
user
.
Role
>=
common
.
RoleAdminUser
...
@@ -233,7 +233,7 @@ func IsUserEnabled(userId int) bool {
...
@@ -233,7 +233,7 @@ func IsUserEnabled(userId int) bool {
var
user
User
var
user
User
err
:=
DB
.
Where
(
"id = ?"
,
userId
)
.
Select
(
"status"
)
.
Find
(
&
user
)
.
Error
err
:=
DB
.
Where
(
"id = ?"
,
userId
)
.
Select
(
"status"
)
.
Find
(
&
user
)
.
Error
if
err
!=
nil
{
if
err
!=
nil
{
common
.
SysError
(
"
N
o such user "
+
err
.
Error
())
common
.
SysError
(
"
n
o such user "
+
err
.
Error
())
return
false
return
false
}
}
return
user
.
Status
==
common
.
UserStatusEnabled
return
user
.
Status
==
common
.
UserStatusEnabled
...
@@ -300,6 +300,6 @@ func UpdateUserUsedQuotaAndRequestCount(id int, quota int) {
...
@@ -300,6 +300,6 @@ func UpdateUserUsedQuotaAndRequestCount(id int, quota int) {
},
},
)
.
Error
)
.
Error
if
err
!=
nil
{
if
err
!=
nil
{
common
.
SysError
(
"
F
ailed to update user used quota and request count: "
+
err
.
Error
())
common
.
SysError
(
"
f
ailed to update user used quota and request count: "
+
err
.
Error
())
}
}
}
}
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