index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <template>
  2. <div class="file-preview">
  3. <!-- 加载中 -->
  4. <div v-if="loading" class="fp-status">
  5. <i class="el-icon-loading" /> 附件加载中...
  6. </div>
  7. <!-- 加载失败 -->
  8. <div v-else-if="errorMsg" class="fp-status">{{ errorMsg }}</div>
  9. <!-- 无附件 -->
  10. <div v-else-if="!fileList.length" class="fp-status">暂无附件</div>
  11. <div v-else class="fp-body">
  12. <!-- 多个附件时的文件切换列表 -->
  13. <div v-if="fileList.length > 1" class="fp-list">
  14. <div
  15. v-for="(item, index) in fileList"
  16. :key="item.key"
  17. class="fp-list-item"
  18. :class="{ 'is-active': index === currentIndex }"
  19. :title="item.name"
  20. @click="handleSelect(index)"
  21. >
  22. <i :class="item.icon" />
  23. <span class="fp-list-name">{{ item.name }}</span>
  24. </div>
  25. </div>
  26. <!-- 文件预览区 -->
  27. <div class="fp-main">
  28. <div class="fp-toolbar">
  29. <i :class="currentFile.icon" />
  30. <span class="fp-title" :title="currentFile.name">{{
  31. currentFile.name
  32. }}</span>
  33. <el-button
  34. v-if="currentFile.url"
  35. type="text"
  36. icon="el-icon-view"
  37. @click="handleOpenWindow"
  38. >新窗口打开</el-button
  39. >
  40. <el-button
  41. type="text"
  42. icon="el-icon-download"
  43. :disabled="!currentFile.storePath"
  44. @click="handleDownload"
  45. >下载</el-button
  46. >
  47. </div>
  48. <div class="fp-viewer">
  49. <!-- 视频:原生播放器,铺满预览区 -->
  50. <video
  51. v-if="currentFile.category === 'video'"
  52. class="fp-video"
  53. :src="currentFile.url"
  54. controls
  55. preload="metadata"
  56. ></video>
  57. <!-- 图片:支持点击放大 -->
  58. <el-image
  59. v-else-if="currentFile.category === 'image'"
  60. class="fp-media"
  61. :src="currentFile.url"
  62. :preview-src-list="previewList"
  63. fit="contain"
  64. >
  65. <div slot="placeholder" class="fp-status">
  66. <i class="el-icon-picture-outline" />
  67. </div>
  68. <div slot="error" class="fp-status">图片加载失败</div>
  69. </el-image>
  70. <!-- pdf / 文本:按 blob 渲染,不会触发浏览器下载 -->
  71. <div v-else-if="isNativePreview" class="fp-doc">
  72. <div v-if="blobLoading" class="fp-status">
  73. <i class="el-icon-loading" /> 文件加载中...
  74. </div>
  75. <iframe
  76. v-else
  77. class="fp-iframe"
  78. :src="blobUrl || currentFile.url"
  79. frameborder="0"
  80. ></iframe>
  81. </div>
  82. <!-- word / excel 等:kkFileView 转换后预览 -->
  83. <iframe
  84. v-else-if="currentFile.previewUrl"
  85. class="fp-iframe"
  86. :src="currentFile.previewUrl"
  87. frameborder="0"
  88. ></iframe>
  89. <!-- 其它格式:仅支持下载 -->
  90. <div v-else class="fp-status">
  91. <i class="el-icon-document" />
  92. <p>该格式暂不支持在线预览,请下载后查看</p>
  93. <el-button type="primary" size="small" @click="handleDownload"
  94. >下载文件</el-button
  95. >
  96. </div>
  97. </div>
  98. </div>
  99. </div>
  100. </div>
  101. </template>
  102. <script>
  103. import { queryIds } from '@/api/file/index.js';
  104. import { getFile } from '@/api/system/file/index.js';
  105. import {
  106. getFileCategory,
  107. getFileUrl,
  108. getKkFileUrl,
  109. getFileApiUrls
  110. } from '@/utils/file';
  111. import request from '@/utils/request';
  112. // 各类文件的图标
  113. const ICON_MAP = {
  114. video: 'el-icon-video-camera',
  115. image: 'el-icon-picture-outline',
  116. pdf: 'el-icon-document',
  117. text: 'el-icon-tickets',
  118. office: 'el-icon-notebook-2',
  119. other: 'el-icon-tickets'
  120. };
  121. // 浏览器可直接展示的类型(pdf、文本),按 blob 渲染
  122. const isNativeCategory = (category) =>
  123. category === 'pdf' || category === 'text';
  124. export default {
  125. name: 'FilePreview',
  126. props: {
  127. // 文件id集合(数字库附件id)
  128. ids: {
  129. type: [Array, String],
  130. default: () => []
  131. }
  132. },
  133. data() {
  134. return {
  135. loading: false,
  136. errorMsg: '',
  137. fileList: [],
  138. currentIndex: 0,
  139. // 文档 blob 地址
  140. blobUrl: '',
  141. blobLoading: false,
  142. // storePath -> blob 地址缓存
  143. blobCache: {}
  144. };
  145. },
  146. computed: {
  147. // 当前展示的文件
  148. currentFile() {
  149. return this.fileList[this.currentIndex] || {};
  150. },
  151. // 图片预览列表
  152. previewList() {
  153. return this.fileList
  154. .filter((item) => item.category === 'image')
  155. .map((item) => item.url);
  156. },
  157. // 浏览器可直接展示的类型(pdf、文本)按 blob 渲染
  158. isNativePreview() {
  159. return isNativeCategory(this.currentFile.category);
  160. }
  161. },
  162. watch: {
  163. ids: {
  164. immediate: true,
  165. handler() {
  166. this.load();
  167. }
  168. }
  169. },
  170. beforeDestroy() {
  171. this.revokeBlob();
  172. },
  173. methods: {
  174. // 加载附件信息
  175. async load() {
  176. const ids = this.normalizeIds(this.ids);
  177. this.currentIndex = 0;
  178. this.fileList = [];
  179. this.errorMsg = '';
  180. this.blobUrl = '';
  181. if (!ids.length) {
  182. return;
  183. }
  184. this.loading = true;
  185. try {
  186. const list = await queryIds(ids);
  187. this.fileList = (list || []).map((item, index) =>
  188. this.mapFile(item, index)
  189. );
  190. this.loadBlob(this.currentFile);
  191. } catch (error) {
  192. this.errorMsg = error.message || '附件加载失败';
  193. } finally {
  194. this.loading = false;
  195. }
  196. },
  197. // 附件id标准化,兼容数组和逗号分隔字符串
  198. normalizeIds(ids) {
  199. const list = Array.isArray(ids) ? ids : String(ids || '').split(',');
  200. return list
  201. .map((item) => String(item || '').trim())
  202. .filter((item) => item);
  203. },
  204. // 文件信息转换
  205. mapFile(item, index) {
  206. const storage = (item.storagePath && item.storagePath[0]) || {};
  207. const fileName = storage.name || item.name || '';
  208. const category = getFileCategory(fileName);
  209. return {
  210. key: item.id || `${index}-${fileName}`,
  211. id: item.id,
  212. name: fileName,
  213. storage,
  214. storePath: storage.storePath || '',
  215. category,
  216. icon: ICON_MAP[category] || ICON_MAP.other,
  217. url: getFileUrl(storage),
  218. previewUrl:
  219. category === 'office' ? getKkFileUrl(storage, fileName) : ''
  220. };
  221. },
  222. // 切换文件
  223. handleSelect(index) {
  224. this.currentIndex = index;
  225. this.loadBlob(this.currentFile);
  226. },
  227. // pdf / 文本按 blob 渲染:带 token 鉴权,且浏览器不会直接下载
  228. async loadBlob(file) {
  229. this.blobUrl = '';
  230. if (!file || !isNativeCategory(file.category)) {
  231. return;
  232. }
  233. const cacheKey = file.storePath || file.url;
  234. if (!cacheKey) {
  235. return;
  236. }
  237. const cached = this.blobCache[cacheKey];
  238. if (cached) {
  239. this.blobUrl = cached;
  240. return;
  241. }
  242. this.blobLoading = true;
  243. try {
  244. const url = await this.fetchBlob(file.storage);
  245. if (url) {
  246. this.blobCache[cacheKey] = url;
  247. this.blobUrl = url;
  248. }
  249. } finally {
  250. this.blobLoading = false;
  251. }
  252. },
  253. // 依次尝试候选地址拉取文件二进制
  254. async fetchBlob(storage) {
  255. const urls = getFileApiUrls(storage);
  256. for (const url of urls) {
  257. try {
  258. const res = await request.get(url, {
  259. responseType: 'blob'
  260. });
  261. if (res.data && res.data.size) {
  262. return URL.createObjectURL(res.data);
  263. }
  264. } catch (error) {
  265. // 当前地址不可用,继续尝试下一个
  266. }
  267. }
  268. return '';
  269. },
  270. // 释放 blob 地址
  271. revokeBlob() {
  272. Object.keys(this.blobCache).forEach((key) => {
  273. URL.revokeObjectURL(this.blobCache[key]);
  274. });
  275. this.blobCache = {};
  276. this.blobUrl = '';
  277. },
  278. // 新窗口打开文件
  279. handleOpenWindow() {
  280. const file = this.currentFile;
  281. if (!file) {
  282. this.$message.warning('文件地址不存在');
  283. return;
  284. }
  285. // pdf / 文本优先用 blob 地址,避免新窗口直接触发下载
  286. // word / excel 等走 kkFileView 转换预览
  287. const url =
  288. (isNativeCategory(file.category) && this.blobUrl) ||
  289. file.previewUrl ||
  290. file.url;
  291. if (!url) {
  292. this.$message.warning('文件地址不存在');
  293. return;
  294. }
  295. window.open(url, '_blank');
  296. },
  297. // 使用 kkFile 预览(word、excel 等需要转换的格式)
  298. handleKkPreview() {
  299. const file = this.currentFile;
  300. if (!file || !file.previewUrl) {
  301. this.$message.warning('预览地址不存在');
  302. return;
  303. }
  304. window.open(file.previewUrl, '_blank');
  305. },
  306. // 下载文件
  307. handleDownload() {
  308. const file = this.currentFile;
  309. if (!file) {
  310. this.$message.warning('文件地址不存在,无法下载');
  311. return;
  312. }
  313. if (file.storePath) {
  314. getFile({ objectName: file.storePath }, file.name);
  315. return;
  316. }
  317. if (file.url) {
  318. window.open(file.url, '_blank');
  319. return;
  320. }
  321. this.$message.warning('文件地址不存在,无法下载');
  322. }
  323. }
  324. };
  325. </script>
  326. <style lang="scss" scoped>
  327. .file-preview {
  328. width: 100%;
  329. height: 100%;
  330. overflow: hidden;
  331. .fp-status {
  332. width: 100%;
  333. height: 100%;
  334. display: flex;
  335. flex-direction: column;
  336. align-items: center;
  337. justify-content: center;
  338. gap: 10px;
  339. color: #909399;
  340. font-size: 13px;
  341. i {
  342. font-size: 26px;
  343. }
  344. }
  345. .fp-body {
  346. width: 100%;
  347. height: 100%;
  348. display: flex;
  349. }
  350. .fp-list {
  351. width: 200px;
  352. flex-shrink: 0;
  353. border-right: 1px solid #ebeef5;
  354. background-color: #fff;
  355. overflow-y: auto;
  356. padding: 6px 0;
  357. .fp-list-item {
  358. display: flex;
  359. align-items: center;
  360. padding: 8px 12px;
  361. font-size: 13px;
  362. color: #606266;
  363. cursor: pointer;
  364. &:hover {
  365. background-color: #f5f7fa;
  366. }
  367. &.is-active {
  368. background-color: #ecf5ff;
  369. color: #409eff;
  370. }
  371. .fp-list-name {
  372. margin-left: 6px;
  373. overflow: hidden;
  374. text-overflow: ellipsis;
  375. white-space: nowrap;
  376. }
  377. }
  378. }
  379. .fp-main {
  380. flex: 1;
  381. min-width: 0;
  382. display: flex;
  383. flex-direction: column;
  384. .fp-toolbar {
  385. display: flex;
  386. align-items: center;
  387. padding: 6px 10px;
  388. font-size: 13px;
  389. color: #606266;
  390. border-bottom: 1px solid #ebeef5;
  391. background-color: #fff;
  392. .fp-title {
  393. flex: 1;
  394. margin-left: 6px;
  395. overflow: hidden;
  396. text-overflow: ellipsis;
  397. white-space: nowrap;
  398. }
  399. }
  400. .fp-viewer {
  401. flex: 1;
  402. min-height: 0;
  403. display: flex;
  404. align-items: center;
  405. justify-content: center;
  406. background-color: #eef5f9;
  407. overflow: hidden;
  408. }
  409. // 图片:完整展示,不裁剪
  410. .fp-media {
  411. max-width: 100%;
  412. max-height: 100%;
  413. }
  414. // 视频:铺满预览区
  415. .fp-video {
  416. width: 100%;
  417. height: 100%;
  418. object-fit: cover;
  419. }
  420. // 文档:撑满预览区
  421. .fp-doc {
  422. width: 100%;
  423. height: 100%;
  424. display: flex;
  425. align-items: center;
  426. justify-content: center;
  427. }
  428. .fp-iframe {
  429. width: 100%;
  430. height: 100%;
  431. border: none;
  432. }
  433. }
  434. }
  435. </style>