电脑端-AI回复,可以在线预览excel/ppt/word/pdf格式

This commit is contained in:
runny committed 2026-04-23 14:18:50 +08:00
1 parent ef0ae7fe08
commit 2675f447bb
7 files changed
+218 -35

No files matched your search

+60 -7
View File
@@ -57,8 +57,10 @@ const officeObjectUrl = ref<string>('');
*/
const getExt = (raw: string) => {
const clean = (raw.split('?')[0] ?? '').toLowerCase();
const i = clean.lastIndexOf('.');
return i >= 0 ? clean.slice(i + 1) : '';
const parts = clean.split('/');
const lastPart = parts[parts.length - 1];
const i = lastPart.lastIndexOf('.');
return i >= 0 ? lastPart.slice(i + 1) : '';
};
const ext = computed(() => {
@@ -71,8 +73,8 @@ const ext = computed(() => {
const preferredMode = computed<'markdown' | 'image' | 'iframe' | 'sheet' | 'office'>(() => {
if (['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg', 'bmp'].includes(ext.value)) return 'image';
if (['md', 'markdown', 'txt', 'csv', 'vtt', 'properties', 'html', 'htm'].includes(ext.value)) return 'markdown';
if (['xlsx'].includes(ext.value)) return 'office';
if (['docx'].includes(ext.value)) return 'office';
if (['xlsx', 'xls'].includes(ext.value)) return 'office';
if (['docx', 'doc', 'pdf'].includes(ext.value)) return 'iframe';
return 'iframe';
});
const resolvedMode = ref<'markdown' | 'image' | 'iframe' | 'sheet' | 'office'>('iframe');
@@ -148,6 +150,46 @@ const iframeSrc = computed(() => {
const close = () => emit('update:modelValue', false);
const isDownloading = ref(false);
const downloadFile = async () => {
if (!props.file?.url || !props.agentId) return;
isDownloading.value = true;
try {
const params = new URLSearchParams({
agent_id: String(props.agentId),
url: props.file.url,
name: props.file.name || '',
download: '1'
});
const res = await fetch(`/api/chat/preview-file-raw?${params.toString()}`, {
headers: props.authHeaders || {},
});
if (!res.ok) {
const err = await res.json().catch(() => ({}));
throw new Error(err?.error || `HTTP ${res.status}`);
}
const blob = await res.blob();
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = props.file.name || 'download';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
} catch (err: any) {
console.error('Download failed:', err);
alert('下载失败: ' + (err.message || '未知错误'));
} finally {
isDownloading.value = false;
}
};
const clearOfficeObjectUrl = () => {
if (officeObjectUrl.value) {
URL.revokeObjectURL(officeObjectUrl.value);
@@ -179,8 +221,8 @@ const loadOfficeContent = async () => {
officeObjectUrl.value = URL.createObjectURL(blob);
officeSrc.value = officeObjectUrl.value;
if (ext.value === 'docx') officeKind.value = 'docx';
else if (ext.value === 'xlsx') officeKind.value = 'excel';
if (ext.value === 'docx' || ext.value === 'doc') officeKind.value = 'docx';
else if (ext.value === 'xlsx' || ext.value === 'xls') officeKind.value = 'excel';
else officeKind.value = null;
};
@@ -270,6 +312,7 @@ const loadContent = async () => {
const params = new URLSearchParams({
agent_id: String(props.agentId),
url: props.file.url,
name: props.file.name || '',
limit: '100',
});
const res = await fetch(`/api/chat/preview-excel?${params.toString()}`, {
@@ -300,6 +343,7 @@ const loadContent = async () => {
const params = new URLSearchParams({
agent_id: String(props.agentId),
url: props.file.url,
name: props.file.name || '',
});
const res = await fetch(`/api/chat/preview-text?${params.toString()}`, {
headers: props.authHeaders || {},
@@ -349,7 +393,16 @@ watch(
<div class="min-w-0">
<div class="text-sm font-medium text-slate-500 dark:text-slate-400 tracking-wide">文件预览</div>
<div class="mt-1 font-semibold text-slate-900 dark:text-slate-100 break-words">{{ file.name }}</div>
<a :href="file.url" target="_blank" class="mt-1 block text-xs text-brand-600 hover:text-brand-700 break-all leading-6">{{ file.url }}</a>
<button type="button" @click="downloadFile" :disabled="isDownloading" class="mt-2 inline-flex items-center justify-center px-3 py-1.5 text-xs font-medium rounded-lg bg-brand-50 text-brand-700 hover:bg-brand-100 border border-brand-200 dark:bg-brand-900/30 dark:text-brand-300 dark:border-brand-800 transition-colors disabled:opacity-50 disabled:cursor-not-allowed">
<svg v-if="!isDownloading" xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-1.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4" />
</svg>
<svg v-else class="animate-spin -ml-1 mr-2 h-4 w-4 text-brand-700 dark:text-brand-300" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg>
{{ isDownloading ? '下载中...' : '下载文件' }}
</button>
</div>
<button type="button" @click="close" class="shrink-0 whitespace-nowrap inline-flex items-center justify-center px-4 py-2 rounded-xl border border-slate-200 dark:border-slate-700 text-slate-600 dark:text-slate-300 bg-white/80 dark:bg-slate-900/80 hover:bg-slate-100 dark:hover:bg-slate-800">关闭</button>
</div>