From cfd6bb93d4a45ffe2e4c765db593549fa47daee9 Mon Sep 17 00:00:00 2001
From: QiuSW <105186638@qq.com>
Date: Sat, 12 Sep 2026 08:55:23 +0800
Subject: [PATCH] feat: allow viewing API keys in internal admin
---
app/admin/apis/api_key.go | 1 +
app/admin/models/api_key.go | 1 +
web/src/views/admin/api-key/index.vue | 2 ++
3 files changed, 4 insertions(+)
diff --git a/app/admin/apis/api_key.go b/app/admin/apis/api_key.go
index 7201e92..0a6338a 100644
--- a/app/admin/apis/api_key.go
+++ b/app/admin/apis/api_key.go
@@ -45,6 +45,7 @@ func (e APIKey) Create(c *gin.Context) {
plain := hex.EncodeToString(b)
h := sha256.Sum256([]byte(plain))
row := models.APIKey{Name: strings.TrimSpace(in.Name), KeyHash: hex.EncodeToString(h[:]), Enabled: true}
+ row.KeyValue = plain
row.CreateBy = user.GetUserId(c)
db := primaryDB()
if err := db.Create(&row).Error; err != nil {
diff --git a/app/admin/models/api_key.go b/app/admin/models/api_key.go
index a404a83..db2bb49 100644
--- a/app/admin/models/api_key.go
+++ b/app/admin/models/api_key.go
@@ -9,6 +9,7 @@ type APIKey struct {
common.Model
Name string `json:"name" gorm:"size:128;not null"`
KeyHash string `json:"-" gorm:"size:64;uniqueIndex;not null"`
+ KeyValue string `json:"apiKey" gorm:"column:key_value;size:128;not null"`
Enabled bool `json:"enabled" gorm:"default:true;index"`
LastUsedAt *time.Time `json:"lastUsedAt"`
common.ModelTime
diff --git a/web/src/views/admin/api-key/index.vue b/web/src/views/admin/api-key/index.vue
index 681ff98..d00deef 100644
--- a/web/src/views/admin/api-key/index.vue
+++ b/web/src/views/admin/api-key/index.vue
@@ -14,6 +14,7 @@
{{ formatDate(row.createdAt) }}
{{ formatDate(row.lastUsedAt, '从未使用') }}
+ 查看密钥
{{ row.enabled ? '禁用' : '启用' }}
删除
@@ -80,6 +81,7 @@ export default {
async copySecret() {
try { await navigator.clipboard.writeText(this.secret); this.copyError = false; ElMessage.success('已复制') } catch (_) { this.copyError = true }
},
+ viewKey(row) { this.secret = row.apiKey || ''; this.copyError = false; this.secretOpen = true },
async closeSecret(done) {
try {
await ElMessageBox.confirm('关闭后无法再次查看完整密钥,确认已保存?', '确认关闭', { confirmButtonText: '已保存,关闭', cancelButtonText: '继续保存', type: 'warning' })