zhangwencui
8 天以前 7619c19a67c2ac824f803090bab753fc5ea14408
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<script lang="ts" setup>
import type { PreviewFileType } from '@vben/utils';
 
import { computed, defineAsyncComponent, ref, watch } from 'vue';
 
import { getFileIcon } from '@vben/utils';
 
import { Button, Modal, Spin } from 'ant-design-vue';
 
import { IconifyIcon } from '@vben/icons';
 
const VueOfficePdf = defineAsyncComponent(() => import('@vue-office/pdf'));
const VueOfficeDocx = defineAsyncComponent(() => import('@vue-office/docx'));
const VueOfficeExcel = defineAsyncComponent(() => import('@vue-office/excel'));
const VueOfficePptx = defineAsyncComponent(() => import('@vue-office/pptx'));
 
// @vue-office/excel 需要额外导入 CSS
// @vue-office/excel 需要额外导入 CSS
// @vue-office/excel 需要额外导入 CSS
import '@vue-office/excel-css';
 
const props = withDefaults(
  defineProps<{
    fileUrl?: string;
    fileName?: string;
    visible?: boolean;
    width?: number | string;
  }>(),
  {
    fileUrl: '',
    fileName: '',
    visible: false,
    width: '90vw',
  },
);
 
const emit = defineEmits<{
  'update:visible': [value: boolean];
}>();
 
const loading = ref(false);
const hasError = ref(false);
 
const open = computed({
  get: () => props.visible,
  set: (val) => emit('update:visible', val),
});
 
const displayName = computed(() => props.fileName || '文件预览');
 
// 根据 fileName 或 fileUrl 判断文件类型
const fileType = computed<PreviewFileType>(() => {
  const name = props.fileName || props.fileUrl;
  if (!name) return 'unknown';
  const ext = name.split('.').pop()?.toLowerCase() || '';
  if (ext === 'pdf') return 'pdf';
  if (['doc', 'docx'].includes(ext)) return 'docx';
  if (['xls', 'xlsx'].includes(ext)) return 'excel';
  if (['ppt', 'pptx'].includes(ext)) return 'pptx';
  return 'unknown';
});
 
const canPreview = computed(
  () => ['pdf', 'docx', 'excel', 'pptx'].includes(fileType.value),
);
 
watch(open, (val) => {
  if (val) {
    hasError.value = false;
    loading.value = canPreview.value;
  }
});
 
function onRendered() {
  loading.value = false;
}
 
function onError() {
  loading.value = false;
  hasError.value = true;
}
 
function openInNewTab() {
  if (props.fileUrl) {
    window.open(props.fileUrl, '_blank');
  }
}
</script>
 
<template>
  <Modal
    v-model:open="open"
    :title="displayName"
    :width="width"
    :footer="null"
    destroy-on-close
    centered
    :style="{ top: '20px' }"
    :body-style="{ height: '80vh', overflow: 'auto' }"
  >
    <!-- Loading -->
    <div
      v-if="loading"
      class="flex h-full items-center justify-center"
    >
      <Spin size="large" tip="加载中..." />
    </div>
 
    <!-- Error -->
    <div
      v-else-if="hasError"
      class="flex h-full flex-col items-center justify-center"
    >
      <IconifyIcon icon="lucide:file-x" :size="64" class="text-red-400" />
      <p class="mt-4 text-gray-500">文件预览失败</p>
      <Button type="primary" class="mt-4" @click="openInNewTab">
        在新标签页打开
      </Button>
    </div>
 
    <!-- Unsupported -->
    <div
      v-else-if="!canPreview"
      class="flex h-full flex-col items-center justify-center"
    >
      <IconifyIcon
        :icon="getFileIcon(fileName || fileUrl)"
        :size="64"
        class="text-gray-400"
      />
      <p class="mt-4 text-gray-500">不支持预览此文件类型</p>
      <Button type="primary" class="mt-4" @click="openInNewTab">
        下载文件
      </Button>
    </div>
 
    <!-- PDF -->
    <VueOfficePdf
      v-else-if="fileType === 'pdf'"
      :src="fileUrl"
      @rendered="onRendered"
      @error="onError"
    />
 
    <!-- Word -->
    <VueOfficeDocx
      v-else-if="fileType === 'docx'"
      :src="fileUrl"
      @rendered="onRendered"
      @error="onError"
    />
 
    <!-- Excel -->
    <VueOfficeExcel
      v-else-if="fileType === 'excel'"
      :src="fileUrl"
      @rendered="onRendered"
      @error="onError"
    />
 
    <!-- PowerPoint -->
    <VueOfficePptx
      v-else-if="fileType === 'pptx'"
      :src="fileUrl"
      @rendered="onRendered"
      @error="onError"
    />
  </Modal>
</template>