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
558e67d9
authored
Jun 16, 2025
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
✨
feat(channel): enhance AddChannel functionality with structured request handling
parent
1c4819b2
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
92 additions
and
22 deletions
+92
-22
controller/channel.go
+83
-20
model/channel.go
+8
-1
model/main.go
+1
-1
No files found.
controller/channel.go
View file @
558e67d9
...
@@ -250,9 +250,14 @@ func GetChannel(c *gin.Context) {
...
@@ -250,9 +250,14 @@ func GetChannel(c *gin.Context) {
return
return
}
}
type
AddChannelRequest
struct
{
Mode
string
`json:"mode"`
Channel
*
model
.
Channel
`json:"channel"`
}
func
AddChannel
(
c
*
gin
.
Context
)
{
func
AddChannel
(
c
*
gin
.
Context
)
{
channel
:=
model
.
Channel
{}
addChannelRequest
:=
AddChannelRequest
{}
err
:=
c
.
ShouldBindJSON
(
&
channel
)
err
:=
c
.
ShouldBindJSON
(
&
addChannelRequest
)
if
err
!=
nil
{
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"success"
:
false
,
...
@@ -260,19 +265,35 @@ func AddChannel(c *gin.Context) {
...
@@ -260,19 +265,35 @@ func AddChannel(c *gin.Context) {
})
})
return
return
}
}
channel
.
CreatedTime
=
common
.
GetTimestamp
()
if
addChannelRequest
.
Channel
==
nil
||
addChannelRequest
.
Channel
.
Key
==
""
{
keys
:=
strings
.
Split
(
channel
.
Key
,
"
\n
"
)
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
if
channel
.
Type
==
common
.
ChannelTypeVertexAi
{
"success"
:
false
,
if
channel
.
Other
==
""
{
"message"
:
"channel cannot be empty"
,
})
return
}
// Validate the length of the model name
for
_
,
m
:=
range
addChannelRequest
.
Channel
.
GetModels
()
{
if
len
(
m
)
>
255
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"message"
:
fmt
.
Sprintf
(
"模型名称过长: %s"
,
m
),
})
return
}
}
if
addChannelRequest
.
Channel
.
Type
==
common
.
ChannelTypeVertexAi
{
if
addChannelRequest
.
Channel
.
Other
==
""
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"success"
:
false
,
"message"
:
"部署地区不能为空"
,
"message"
:
"部署地区不能为空"
,
})
})
return
return
}
else
{
}
else
{
if
common
.
IsJsonStr
(
c
hannel
.
Other
)
{
if
common
.
IsJsonStr
(
addChannelRequest
.
C
hannel
.
Other
)
{
// must have default
// must have default
regionMap
:=
common
.
StrToMap
(
c
hannel
.
Other
)
regionMap
:=
common
.
StrToMap
(
addChannelRequest
.
C
hannel
.
Other
)
if
regionMap
[
"default"
]
==
nil
{
if
regionMap
[
"default"
]
==
nil
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"success"
:
false
,
...
@@ -282,27 +303,69 @@ func AddChannel(c *gin.Context) {
...
@@ -282,27 +303,69 @@ func AddChannel(c *gin.Context) {
}
}
}
}
}
}
keys
=
[]
string
{
channel
.
Key
}
}
}
channels
:=
make
([]
model
.
Channel
,
0
,
len
(
keys
))
for
_
,
key
:=
range
keys
{
addChannelRequest
.
Channel
.
CreatedTime
=
common
.
GetTimestamp
()
if
key
==
""
{
keys
:=
make
([]
string
,
0
)
switch
addChannelRequest
.
Mode
{
case
"multi_to_single"
:
addChannelRequest
.
Channel
.
ChannelInfo
.
MultiKeyMode
=
true
if
addChannelRequest
.
Channel
.
Type
==
common
.
ChannelTypeVertexAi
{
if
!
common
.
IsJsonStr
(
addChannelRequest
.
Channel
.
Key
)
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"message"
:
"Vertex AI 批量添加模式必须使用标准的JsonArray格式,例如[{key1}, {key2}...],请检查输入"
,
})
return
}
}
keys
=
[]
string
{
addChannelRequest
.
Channel
.
Key
}
case
"batch"
:
if
addChannelRequest
.
Channel
.
Type
==
common
.
ChannelTypeVertexAi
{
// multi json
if
!
common
.
IsJsonStr
(
addChannelRequest
.
Channel
.
Key
)
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"message"
:
"Vertex AI 批量添加模式必须使用标准的JsonArray格式,例如[{key1}, {key2}...],请检查输入"
,
})
return
}
toMap
:=
common
.
StrToMap
(
addChannelRequest
.
Channel
.
Key
)
if
toMap
==
nil
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"message"
:
"Vertex AI 批量添加模式必须使用标准的JsonArray格式,例如[{key1}, {key2}...],请检查输入"
,
})
return
}
keys
=
make
([]
string
,
0
,
len
(
toMap
))
for
k
:=
range
toMap
{
if
k
==
""
{
continue
continue
}
}
localChannel
:=
channel
keys
=
append
(
keys
,
k
)
localChannel
.
Key
=
key
}
// Validate the length of the model name
}
else
{
models
:=
strings
.
Split
(
localChannel
.
Models
,
","
)
keys
=
strings
.
Split
(
addChannelRequest
.
Channel
.
Key
,
"
\n
"
)
for
_
,
model
:=
range
models
{
}
if
len
(
model
)
>
255
{
case
"single"
:
keys
=
[]
string
{
addChannelRequest
.
Channel
.
Key
}
default
:
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"success"
:
false
,
"message"
:
fmt
.
Sprintf
(
"模型名称过长: %s"
,
model
)
,
"message"
:
"不支持的添加模式"
,
})
})
return
return
}
}
channels
:=
make
([]
model
.
Channel
,
0
,
len
(
keys
))
for
_
,
key
:=
range
keys
{
if
key
==
""
{
continue
}
}
channels
=
append
(
channels
,
localChannel
)
localChannel
:=
addChannelRequest
.
Channel
localChannel
.
Key
=
key
channels
=
append
(
channels
,
*
localChannel
)
}
}
err
=
model
.
BatchInsertChannels
(
channels
)
err
=
model
.
BatchInsertChannels
(
channels
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
model/channel.go
View file @
558e67d9
...
@@ -9,6 +9,11 @@ import (
...
@@ -9,6 +9,11 @@ import (
"gorm.io/gorm"
"gorm.io/gorm"
)
)
type
ChannelInfo
struct
{
MultiKeyMode
bool
`json:"multi_key_mode"`
// 是否多Key模式
MultiKeyStatusList
map
[
int
]
int
`json:"multi_key_status_list"`
// key状态列表,key index -> status
}
type
Channel
struct
{
type
Channel
struct
{
Id
int
`json:"id"`
Id
int
`json:"id"`
Type
int
`json:"type" gorm:"default:0"`
Type
int
`json:"type" gorm:"default:0"`
...
@@ -35,8 +40,10 @@ type Channel struct {
...
@@ -35,8 +40,10 @@ type Channel struct {
AutoBan
*
int
`json:"auto_ban" gorm:"default:1"`
AutoBan
*
int
`json:"auto_ban" gorm:"default:1"`
OtherInfo
string
`json:"other_info"`
OtherInfo
string
`json:"other_info"`
Tag
*
string
`json:"tag" gorm:"index"`
Tag
*
string
`json:"tag" gorm:"index"`
Setting
*
string
`json:"setting" gorm:"type:text"`
Setting
*
string
`json:"setting" gorm:"type:text"`
// 渠道额外设置
ParamOverride
*
string
`json:"param_override" gorm:"type:text"`
ParamOverride
*
string
`json:"param_override" gorm:"type:text"`
// add after v0.8.5
ChannelInfo
ChannelInfo
`json:"channel_info" gorm:"type:json"`
}
}
func
(
channel
*
Channel
)
GetModels
()
[]
string
{
func
(
channel
*
Channel
)
GetModels
()
[]
string
{
...
...
model/main.go
View file @
558e67d9
...
@@ -48,7 +48,7 @@ func initCol() {
...
@@ -48,7 +48,7 @@ func initCol() {
}
}
}
}
// log sql type and database type
// log sql type and database type
common
.
SysLog
(
"Using Log SQL Type: "
+
common
.
LogSqlType
)
//
common.SysLog("Using Log SQL Type: " + common.LogSqlType)
}
}
var
DB
*
gorm
.
DB
var
DB
*
gorm
.
DB
...
...
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