gaoluyang
2026-04-29 e40e1cf5d4aa99412ca3a87771b5d5a8ea5a105d
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
170
171
172
<template>
  <el-dialog v-model="dialogVisible" title="预览" width="100%" fullscreen align-center :before-close="handleClose" append-to-body>
    <div>
      <!-- 图片预览 -->
      <div v-if="isImage">
        <img :src="imgUrl" alt="Image Preview" />
      </div>
      
      <!-- PDF预览 -->
      <div v-if="isPdf" class="pdf-preview-wrapper">
        <iframe :src="fileUrl" class="pdf-preview-frame" frameborder="0"></iframe>
      </div>
      
      <!-- Word文档预览 -->
      <div v-if="isDoc">
        <p v-if="!isDocShow">文档无法直接预览,请下载查看。</p>
        <a :href="fileUrl" v-if="!isDocShow">下载文件</a>
        <vue-office-docx 
          v-else 
          :src="fileUrl" 
          style="height: 100vh;" 
          @rendered="renderedHandler" 
          @error="errorHandler" 
        />
      </div>
      
      <!-- Excel文档预览 -->
      <div v-if="isXls">
        <p v-if="!isDocShow">文档无法直接预览,请下载查看。</p>
        <a :href="fileUrl" v-if="!isDocShow">下载文件</a>
        <vue-office-excel 
          v-else 
          :src="fileUrl" 
          :options="options" 
          style="height: 100vh;" 
          @rendered="renderedHandler"
          @error="errorHandler" 
        />
      </div>
      
      <!-- 压缩文件处理 -->
      <div v-if="isZipOrRar">
        <p>压缩文件无法直接预览,请下载查看。</p>
        <a :href="fileUrl">下载文件</a>
      </div>
      
      <!-- 不支持的格式 -->
      <div v-if="!isSupported">
        <p>不支持的文件格式</p>
      </div>
    </div>
  </el-dialog>
</template>
 
<script setup>
import { ref, computed, getCurrentInstance } from 'vue';
import VueOfficeDocx from '@vue-office/docx';
import '@vue-office/docx/lib/index.css';
import VueOfficeExcel from '@vue-office/excel';
import '@vue-office/excel/lib/index.css';
 
// 响应式变量
const fileUrl = ref('')
const dialogVisible = ref(false)
const { proxy } = getCurrentInstance();
const javaApi = proxy.javaApi;
 
// 文档预览状态
const isDocShow = ref(true);
const imgUrl = ref('');
const options = ref({
  xls: false,
  minColLength: 0,
  minRowLength: 0,
  widthOffset: 10,
  heightOffset: 10,
  beforeTransformData: (workbookData) => workbookData,
  transformData: (workbookData) => workbookData,
});
 
// 计算属性 - 判断文件类型
const isImage = computed(() => {
  const state = /\.(jpg|jpeg|png|gif)$/i.test(fileUrl.value);
  if (state) {
    imgUrl.value = fileUrl.value.replaceAll('word', 'img');
  }
  return state;
});
 
const isPdf = computed(() => {
  console.log(fileUrl.value)
  return /\.pdf$/i.test(fileUrl.value);
});
 
const isDoc = computed(() => {
  return /\.(doc|docx)$/i.test(fileUrl.value);
});
 
const isXls = computed(() => {
  const state = /\.(xls|xlsx)$/i.test(fileUrl.value);
  if (state) {
    options.value.xls = /\.(xls)$/i.test(fileUrl.value);
  }
  return state;
});
 
const isZipOrRar = computed(() => {
  return /\.(zip|rar)$/i.test(fileUrl.value);
});
 
const isSupported = computed(() => {
  return isImage.value || isPdf.value || isDoc.value || isXls.value || isZipOrRar.value;
});
 
// 方法定义
const renderedHandler = () => {
  console.log("渲染完成");
  isDocShow.value = true;
  resetStyle();
};
 
const errorHandler = () => {
  console.log("渲染失败");
  isDocShow.value = false;
};
 
const open = (url) => {
  fileUrl.value = window.location.protocol+'//'+window.location.host+ url;
  dialogVisible.value = true;
};
const handleClose = () => {
  dialogVisible.value = false;
};
 
const resetStyle = () => {
  const elements = document.querySelectorAll('[style*="pt"]');
  for (const element of elements) {
    const style = element.getAttribute('style');
    if (style) {
      element.setAttribute('style', style.replace(/pt/g, 'px'));
    }
  }
};
 
// 暴露open方法供外部调用
defineExpose({
  open
})
</script>
 
<style scoped>
img {
  max-width: 100%;
  display: block;
  margin: 0 auto;
}
 
.pdf-preview-wrapper {
  height: 100vh;
}
 
.pdf-preview-frame {
  width: 100%;
  height: 100%;
}
 
.oneLine {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
</style>