260323 function add report chat and front
This commit is contained in:
1 parent
605317c2b2
commit
5e8ea03b6c
16 files changed
+2351
-100
No files matched your search
@@ -0,0 +1,133 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref, watch } from 'vue';
|
||||
import { marked } from 'marked';
|
||||
|
||||
interface PreviewFile {
|
||||
name: string;
|
||||
url: string;
|
||||
}
|
||||
|
||||
const props = defineProps<{
|
||||
modelValue: boolean;
|
||||
file: PreviewFile | null;
|
||||
}>();
|
||||
|
||||
const emit = defineEmits<{
|
||||
(e: 'update:modelValue', value: boolean): void;
|
||||
}>();
|
||||
|
||||
const isLoading = ref(false);
|
||||
const error = ref('');
|
||||
const markdownHtml = ref('');
|
||||
|
||||
const getExt = (raw: string) => {
|
||||
const clean = (raw.split('?')[0] ?? '').toLowerCase();
|
||||
const i = clean.lastIndexOf('.');
|
||||
return i >= 0 ? clean.slice(i + 1) : '';
|
||||
};
|
||||
|
||||
const ext = computed(() => {
|
||||
// URL extension has higher priority than display name.
|
||||
const byUrl = getExt(props.file?.url || '');
|
||||
if (byUrl) return byUrl;
|
||||
return getExt(props.file?.name || '');
|
||||
});
|
||||
|
||||
const preferredMode = computed<'markdown' | 'image' | 'iframe'>(() => {
|
||||
if (['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg', 'bmp'].includes(ext.value)) return 'image';
|
||||
if (['md', 'markdown', 'txt'].includes(ext.value)) return 'markdown';
|
||||
return 'iframe';
|
||||
});
|
||||
const resolvedMode = ref<'markdown' | 'image' | 'iframe'>('iframe');
|
||||
|
||||
const iframeSrc = computed(() => {
|
||||
if (!props.file?.url) return '';
|
||||
if (['doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx'].includes(ext.value)) {
|
||||
return `https://view.officeapps.live.com/op/embed.aspx?src=${encodeURIComponent(props.file.url)}`;
|
||||
}
|
||||
return props.file.url;
|
||||
});
|
||||
|
||||
const close = () => emit('update:modelValue', false);
|
||||
|
||||
const loadContent = async () => {
|
||||
error.value = '';
|
||||
markdownHtml.value = '';
|
||||
if (!props.modelValue || !props.file) return;
|
||||
if (!props.file?.url) return;
|
||||
resolvedMode.value = preferredMode.value;
|
||||
if (preferredMode.value !== 'markdown') return;
|
||||
|
||||
isLoading.value = true;
|
||||
try {
|
||||
const res = await fetch(props.file.url);
|
||||
if (!res.ok) {
|
||||
throw new Error(`HTTP ${res.status}`);
|
||||
}
|
||||
const contentType = (res.headers.get('content-type') || '').toLowerCase();
|
||||
if (contentType.startsWith('image/')) {
|
||||
resolvedMode.value = 'image';
|
||||
return;
|
||||
}
|
||||
if (
|
||||
contentType &&
|
||||
!contentType.includes('text/') &&
|
||||
!contentType.includes('markdown') &&
|
||||
!contentType.includes('application/json')
|
||||
) {
|
||||
resolvedMode.value = 'iframe';
|
||||
return;
|
||||
}
|
||||
const text = await res.text();
|
||||
markdownHtml.value = marked.parse(text) as string;
|
||||
} catch (e: any) {
|
||||
error.value = e?.message || 'Failed to load file';
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
watch(
|
||||
() => [props.modelValue, props.file?.url, props.file?.name],
|
||||
() => {
|
||||
loadContent();
|
||||
},
|
||||
{ immediate: true }
|
||||
);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div v-if="modelValue && file" class="fixed inset-0 z-[80] flex items-center justify-center bg-black/50 backdrop-blur-sm p-4">
|
||||
<div class="bg-white rounded-2xl shadow-xl w-full max-w-5xl h-[85vh] flex flex-col overflow-hidden">
|
||||
<div class="px-6 py-4 border-b border-slate-200 flex items-center justify-between gap-4">
|
||||
<div class="min-w-0">
|
||||
<div class="text-sm text-slate-500 uppercase tracking-wide">文件预览</div>
|
||||
<div class="font-semibold text-slate-900 truncate">{{ file.name }}</div>
|
||||
<a :href="file.url" target="_blank" class="text-xs text-brand-600 hover:text-brand-700 break-all">{{ file.url }}</a>
|
||||
</div>
|
||||
<button @click="close" class="px-3 py-1.5 rounded-lg text-slate-600 hover:bg-slate-100">关闭</button>
|
||||
</div>
|
||||
|
||||
<div class="flex-1 overflow-auto p-4 bg-slate-50">
|
||||
<div v-if="isLoading" class="text-sm text-slate-500">加载中...</div>
|
||||
<div v-else-if="error" class="text-sm text-red-600 bg-red-50 border border-red-200 rounded-lg p-3">
|
||||
预览失败:{{ error }}
|
||||
</div>
|
||||
<div
|
||||
v-else-if="resolvedMode === 'markdown'"
|
||||
class="prose prose-sm max-w-none bg-white border border-slate-200 rounded-lg p-5"
|
||||
v-html="markdownHtml"
|
||||
></div>
|
||||
<div v-else-if="resolvedMode === 'image'" class="h-full flex items-center justify-center bg-white border border-slate-200 rounded-lg p-2">
|
||||
<img :src="file.url" :alt="file.name" class="max-w-full max-h-full object-contain" />
|
||||
</div>
|
||||
<iframe
|
||||
v-else
|
||||
:src="iframeSrc"
|
||||
class="w-full h-full min-h-[480px] bg-white rounded-lg border border-slate-200"
|
||||
allow="clipboard-read; clipboard-write"
|
||||
></iframe>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
Reference in new issue
Block a user