feat: 添加浏览器扩展下载功能

This commit is contained in:
2026-08-30 23:32:26 +08:00
parent 7fcb9abf69
commit c5de3499c0
15 changed files with 293 additions and 74 deletions
@@ -32,3 +32,7 @@ export async function enableOpenApiKeyApi(id: number) {
export async function deleteOpenApiKeyApi(id: number) {
return requestClient.delete<void>(`/sys/open-api-key/${id}`);
}
/** 下载浏览器扩展安装包(需 open:api-key:read) */
export const OPEN_EXTENSION_DOWNLOAD_PATH = '/sys/open-extension/download';
export const OPEN_EXTENSION_ZIP_NAME = 'ecbi-data-sync-extension.zip';
@@ -2,12 +2,11 @@
* 通用文件导出工具
* 用于下载 Excel、PDF 等二进制文件
*/
import { useAppConfig } from '@vben/hooks';
import { useAccessStore } from '@vben/stores';
import { message } from 'ant-design-vue';
import { useAppConfig } from '@vben/hooks';
const { apiURL } = useAppConfig(import.meta.env, import.meta.env.PROD);
function formatToken(token: null | string) {
@@ -34,6 +33,67 @@ function parseFilenameFromContentDisposition(
return null;
}
async function triggerBlobDownload(
response: Response,
fallbackFilename: string,
) {
const contentType = response.headers.get('Content-Type') || '';
const blob = await response.blob();
if (blob.size === 0) {
throw new Error('下载文件为空');
}
if (contentType.includes('application/json')) {
const text = await blob.text();
try {
const json = JSON.parse(text);
message.error(json.message || '下载失败');
} catch {
message.error('下载失败');
}
return;
}
const headerName =
parseFilenameFromContentDisposition(
response.headers.get('Content-Disposition'),
) || fallbackFilename;
const downloadUrl = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = downloadUrl;
link.download = headerName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(downloadUrl);
}
export async function exportByGet(url: string, filename: string) {
const accessStore = useAccessStore();
const fullUrl = `${apiURL}${url}`;
try {
const response = await fetch(fullUrl, {
method: 'GET',
headers: {
Authorization: formatToken(accessStore.accessToken) || '',
},
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
await triggerBlobDownload(response, filename);
message.success('下载成功');
} catch (error) {
console.error('下载失败:', error);
message.error('下载失败,请稍后重试');
}
}
export async function exportByPost(
url: string,
params: Record<string, unknown>,
@@ -56,39 +116,7 @@ export async function exportByPost(
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const contentType = response.headers.get('Content-Type') || '';
const blob = await response.blob();
if (blob.size === 0) {
throw new Error('下载文件为空');
}
// 如果返回的是 JSON 错误(而非文件),提示错误信息
if (contentType.includes('application/json')) {
const text = await blob.text();
try {
const json = JSON.parse(text);
message.error(json.message || '导出失败');
} catch {
message.error('导出失败');
}
return;
}
const headerName =
parseFilenameFromContentDisposition(
response.headers.get('Content-Disposition'),
) || filename;
// 触发浏览器下载
const downloadUrl = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = downloadUrl;
link.download = headerName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(downloadUrl);
await triggerBlobDownload(response, filename);
message.success('导出成功');
} catch (error) {
console.error('导出失败:', error);
@@ -15,12 +15,15 @@ import {
disableOpenApiKeyApi,
enableOpenApiKeyApi,
getOpenApiKeyListApi,
OPEN_EXTENSION_DOWNLOAD_PATH,
OPEN_EXTENSION_ZIP_NAME,
} from '#/api/open/api-key';
import { PermissionGate } from '#/components/permission';
import {
syncVxeGridColumns,
useActionColumnVisible,
} from '#/composables/use-vxe-action-column';
import { exportByGet } from '#/utils/export';
import CreateApiKeyModal from './components/CreateApiKeyModal.vue';
import OpenApiBaseUrlDisplay from './components/OpenApiBaseUrlDisplay.vue';
@@ -28,7 +31,9 @@ import OpenApiBaseUrlDisplay from './components/OpenApiBaseUrlDisplay.vue';
defineOptions({ name: 'OpenApiKeyManagement' });
const PlusIcon = createIconifyIcon('lucide:plus');
const DownloadIcon = createIconifyIcon('lucide:download');
const loading = ref(false);
const downloading = ref(false);
const createModalRef = ref<InstanceType<typeof CreateApiKeyModal>>();
function formatExpireAt(cellValue?: null | string) {
@@ -139,6 +144,16 @@ function openCreateModal() {
createModalRef.value?.open();
}
async function handleDownloadExtension() {
if (downloading.value) return;
downloading.value = true;
try {
await exportByGet(OPEN_EXTENSION_DOWNLOAD_PATH, OPEN_EXTENSION_ZIP_NAME);
} finally {
downloading.value = false;
}
}
function getPermissionCount(row: OpenApiKeyVO.OpenApiKeyInfoVO) {
return row.permissionCodes?.length ?? row.permissions?.length ?? 0;
}
@@ -157,12 +172,20 @@ function getPermissionTooltip(row: OpenApiKeyVO.OpenApiKeyInfoVO) {
<Grid :loading="loading">
<template #toolbar-tools>
<PermissionGate code="open:api-key:create">
<Button type="primary" @click="openCreateModal">
<PlusIcon class="size-4" />
创建 API Key
</Button>
</PermissionGate>
<Space>
<PermissionGate code="open:api-key:read">
<Button :loading="downloading" @click="handleDownloadExtension">
<DownloadIcon class="size-4" />
下载浏览器扩展
</Button>
</PermissionGate>
<PermissionGate code="open:api-key:create">
<Button type="primary" @click="openCreateModal">
<PlusIcon class="size-4" />
创建 API Key
</Button>
</PermissionGate>
</Space>
</template>
<template #permissions="{ row }">