feat: api key manage page funciton is ready

This commit is contained in:
RockYang
2024-01-04 10:48:04 +08:00
parent 79f6a43019
commit 21f2622a4b
6 changed files with 133 additions and 19 deletions

View File

@@ -29,6 +29,9 @@ func (h *ApiKeyHandler) Save(c *gin.Context) {
Platform string `json:"platform"`
Type string `json:"type"`
Value string `json:"value"`
ApiURL string `json:"api_url"`
Enabled bool `json:"enabled"`
UseProxy bool `json:"use_proxy"`
}
if err := c.ShouldBindJSON(&data); err != nil {
resp.ERROR(c, types.InvalidArgs)
@@ -42,6 +45,9 @@ func (h *ApiKeyHandler) Save(c *gin.Context) {
apiKey.Platform = data.Platform
apiKey.Value = data.Value
apiKey.Type = data.Type
apiKey.ApiURL = data.ApiURL
apiKey.Enabled = data.Enabled
apiKey.UseProxy = data.UseProxy
res := h.db.Save(&apiKey)
if res.Error != nil {
resp.ERROR(c, "更新数据库失败!")
@@ -80,6 +86,26 @@ func (h *ApiKeyHandler) List(c *gin.Context) {
resp.SUCCESS(c, keys)
}
func (h *ApiKeyHandler) Set(c *gin.Context) {
var data struct {
Id uint `json:"id"`
Filed string `json:"filed"`
Value interface{} `json:"value"`
}
if err := c.ShouldBindJSON(&data); err != nil {
resp.ERROR(c, types.InvalidArgs)
return
}
res := h.db.Model(&model.ApiKey{}).Where("id = ?", data.Id).Update(data.Filed, data.Value)
if res.Error != nil {
resp.ERROR(c, "更新数据库失败!")
return
}
resp.SUCCESS(c)
}
func (h *ApiKeyHandler) Remove(c *gin.Context) {
id := h.GetInt(c, "id", 0)

View File

@@ -255,6 +255,7 @@ func main() {
group := s.Engine.Group("/api/admin/apikey/")
group.POST("save", h.Save)
group.GET("list", h.List)
group.POST("set", h.Set)
group.GET("remove", h.Remove)
}),
fx.Invoke(func(s *core.AppServer, h *admin.UserHandler) {

View File

@@ -6,5 +6,8 @@ type ApiKey struct {
Platform string
Type string // 用途 chat => 聊天img => 绘图
Value string // API Key 的值
ApiURL string // 当前 KEY 的 API 地址
Enabled bool // 是否启用
UseProxy bool // 是否使用代理访问 API URL
LastUsedAt int64 // 最后使用时间
}

View File

@@ -5,6 +5,9 @@ type ApiKey struct {
BaseVo
Platform string `json:"platform"`
Type string `json:"type"`
Value string `json:"value"` // API Key 的值
Value string `json:"value"` // API Key 的值
ApiURL string `json:"api_url"`
Enabled bool `json:"enabled"`
UseProxy bool `json:"use_proxy"`
LastUsedAt int64 `json:"last_used_at"` // 最后使用时间
}