feat: 开放密钥管理也增加调用地址显示功能

This commit is contained in:
2026-08-27 09:46:59 +08:00
parent c768b14912
commit bf602ed4b2
4 changed files with 121 additions and 6 deletions
@@ -0,0 +1,8 @@
/** 开放接口 Base URL:当前站点 origin + /api(扩展、脚本等站外调用时复制) */
export function getOpenApiBaseUrl(): string {
if (typeof window === 'undefined') {
return '/api';
}
const origin = window.location.origin.replace(/\/+$/, '');
return `${origin}/api`;
}
@@ -4,9 +4,11 @@ import type { OpenApiKeyDTO, OpenApiKeyVO } from '#/api/open/api-key';
import { computed, ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { createIconifyIcon } from '@vben/icons';
import {
Alert,
Button,
DatePicker,
Empty,
Form,
@@ -14,7 +16,6 @@ import {
InputNumber,
message,
Select,
Tag,
Tree,
} from 'ant-design-vue';
@@ -23,6 +24,10 @@ import {
getOpenApiKeyAvailablePermissionsApi,
} from '#/api/open/api-key';
import OpenApiBaseUrlDisplay from './OpenApiBaseUrlDisplay.vue';
const CopyIcon = createIconifyIcon('lucide:copy');
interface Emits {
(e: 'success', result: OpenApiKeyVO.CreateOpenApiKeyResultVO): void;
}
@@ -57,6 +62,7 @@ const permissionTree = ref<OpenApiKeyVO.OpenApiAvailablePermissionVO[]>([]);
const checkedPermissionIds = ref<number[]>([]);
const expandedKeys = ref<number[]>([]);
const createdKey = ref<string | null>(null);
const copyingKey = ref(false);
const formData = ref<OpenApiKeyDTO.CreateOpenApiKeyDTO>({
name: '',
@@ -233,11 +239,14 @@ async function copyText(text: string) {
async function copyKey() {
if (!createdKey.value) return;
copyingKey.value = true;
try {
await copyText(createdKey.value);
message.success('已复制到剪贴板');
message.success('已复制 API Key');
} catch {
message.error('复制失败,请手动选择文本复制');
message.error('复制失败,请手动选择复制');
} finally {
copyingKey.value = false;
}
}
@@ -260,9 +269,18 @@ defineExpose({
type="warning"
/>
<div v-if="createdKey" class="space-y-3">
<Input.TextArea :rows="3" :value="createdKey" readonly />
<Tag color="blue" class="cursor-pointer" @click="copyKey">点击复制 Key</Tag>
<div v-if="createdKey" class="space-y-4">
<OpenApiBaseUrlDisplay compact />
<div class="rounded-lg border border-border bg-muted/30 p-3">
<div class="mb-2 block text-xs font-medium text-foreground">API Key(仅显示一次)</div>
<div class="flex gap-2">
<Input :value="createdKey" readonly class="font-mono text-xs" />
<Button type="default" size="small" :loading="copyingKey" @click="copyKey">
<CopyIcon class="size-4" />
</Button>
</div>
</div>
</div>
<Form v-else ref="formRef" :model="formData" layout="vertical" class="mb-0">
@@ -0,0 +1,86 @@
<script lang="ts" setup>
import { computed, ref } from 'vue';
import { createIconifyIcon } from '@vben/icons';
import { Button, Input, message, Typography } from 'ant-design-vue';
import { getOpenApiBaseUrl } from '#/utils/open-api-base-url';
defineOptions({ name: 'OpenApiBaseUrlDisplay' });
withDefaults(
defineProps<{
/** 紧凑模式:用于创建成功弹窗 */
compact?: boolean;
}>(),
{
compact: false,
},
);
const CopyIcon = createIconifyIcon('lucide:copy');
const copying = ref(false);
const baseUrl = computed(() => getOpenApiBaseUrl());
async function copyBaseUrl() {
copying.value = true;
try {
if (navigator.clipboard?.writeText) {
await navigator.clipboard.writeText(baseUrl.value);
} else {
const textarea = document.createElement('textarea');
textarea.value = baseUrl.value;
textarea.style.position = 'fixed';
textarea.style.left = '-9999px';
document.body.appendChild(textarea);
textarea.focus();
textarea.select();
const ok = document.execCommand('copy');
document.body.removeChild(textarea);
if (!ok) throw new Error('copy failed');
}
message.success('已复制 Open API Base URL');
} catch {
message.error('复制失败,请手动选择复制');
} finally {
copying.value = false;
}
}
</script>
<template>
<div
class="rounded-lg border border-border bg-muted/30"
:class="compact ? 'p-3' : 'p-4'"
>
<Typography.Text
:class="compact ? 'mb-2 block text-xs font-medium' : 'mb-2 block text-sm font-medium'"
>
Open API 调用地址
</Typography.Text>
<Typography.Text
v-if="!compact"
type="secondary"
class="mb-3 block text-xs leading-relaxed"
>
站外调用(浏览器扩展、脚本、Postman)请使用下方 Base URL,请求头携带
<code class="rounded bg-muted px-1">X-Api-Key</code>,勿使用登录 JWT。
</Typography.Text>
<div class="flex gap-2">
<Input :value="baseUrl" readonly class="font-mono text-xs" />
<Button
type="default"
:size="compact ? 'small' : 'middle'"
:loading="copying"
@click="copyBaseUrl"
>
<CopyIcon class="size-4" />
<span v-if="!compact" class="ml-1">复制</span>
</Button>
</div>
</div>
</template>
@@ -18,6 +18,7 @@ import {
} from '#/api/open/api-key';
import CreateApiKeyModal from './components/CreateApiKeyModal.vue';
import OpenApiBaseUrlDisplay from './components/OpenApiBaseUrlDisplay.vue';
defineOptions({ name: 'OpenApiKeyManagement' });
@@ -127,6 +128,8 @@ function getPermissionTooltip(row: OpenApiKeyVO.OpenApiKeyInfoVO) {
<template>
<Page auto-content-height>
<OpenApiBaseUrlDisplay class="mb-4" />
<Grid :loading="loading">
<template #toolbar-tools>
<Button type="primary" @click="openCreateModal">