feat: allow viewing API keys in internal admin

This commit is contained in:
QiuSW
2026-09-12 08:55:23 +08:00
parent 1e2a06c8e2
commit cfd6bb93d4
3 changed files with 4 additions and 0 deletions
+1
View File
@@ -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 {
+1
View File
@@ -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
+2
View File
@@ -14,6 +14,7 @@
<el-table-column label="创建时间" min-width="180"><template #default="{ row }">{{ formatDate(row.createdAt) }}</template></el-table-column>
<el-table-column label="最后使用时间" min-width="180"><template #default="{ row }">{{ formatDate(row.lastUsedAt, '从未使用') }}</template></el-table-column>
<el-table-column label="操作" width="200" fixed="right"><template #default="{ row }">
<el-button size="small" :disabled="busy || loading" @click="viewKey(row)">查看密钥</el-button>
<el-button size="small" :disabled="busy || loading" @click="toggle(row)">{{ row.enabled ? '禁用' : '启用' }}</el-button>
<el-button size="small" type="danger" plain :disabled="busy || loading" @click="remove(row)">删除</el-button>
</template></el-table-column>
@@ -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' })