API 密钥: 支持编辑与高级选项, 修复 jsonb 白名单更新

- 前端密钥页新增"编辑": 改名/每日配额/模型白名单/状态切换
- 新建密钥展开"高级选项": 每日 Token/请求上限、模型白名单
- 修复 PATCH 更新 allowed_models(jsonb)不走序列化导致失败,
  改为 JSON 字符串写入, 跨 SQLite/Postgres 可靠

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Sakurasan
2026-08-15 20:55:26 +08:00
co-authored by Claude
parent d0bc28a4fe
commit eb57a09c5d
2 changed files with 130 additions and 13 deletions
+9 -3
View File
@@ -1,6 +1,7 @@
package api
import (
"encoding/json"
"net/http"
"strconv"
"time"
@@ -135,9 +136,6 @@ func (h *Handler) PatchKey(c *gin.Context) {
if req.QuotaRequestsPerDay != nil {
updates["quota_requests_per_day"] = *req.QuotaRequestsPerDay
}
if req.AllowedModels != nil {
updates["allowed_models"] = *req.AllowedModels
}
if req.Status != nil {
if *req.Status != store.KeyStatusActive && *req.Status != store.KeyStatusRevoked {
resp.Fail(c, http.StatusBadRequest, "status must be active or revoked")
@@ -151,6 +149,14 @@ func (h *Handler) PatchKey(c *gin.Context) {
return
}
}
// allowed_models 是 jsonb:手动序列化为 JSON 字符串写入(跨 SQLite/Postgres)
if req.AllowedModels != nil {
raw, _ := json.Marshal(*req.AllowedModels)
if err := h.a.DB.Model(&k).Update("allowed_models", string(raw)).Error; err != nil {
resp.Fail(c, http.StatusInternalServerError, "failed to update key")
return
}
}
resp.OK(c, gin.H{"ok": true})
}