| | |
| | | > |
| | | <!-- 加载状态 --> |
| | | <div v-if="loading" class="preview-loading"> |
| | | <i class="el-icon-loading" style="font-size: 40px; margin-bottom: 16px;"></i> |
| | | <i |
| | | class="el-icon-loading" |
| | | style="font-size: 40px; margin-bottom: 16px;" |
| | | ></i> |
| | | <span>文件加载中...</span> |
| | | </div> |
| | | |
| | |
| | | <div v-else-if="fileType === 'image'" class="preview-container"> |
| | | <div class="image-toolbar"> |
| | | <el-button-group> |
| | | <el-button size="mini" @click="zoomImageIn" :disabled="imageScale >= 300"> |
| | | <el-button |
| | | size="mini" |
| | | @click="zoomImageIn" |
| | | :disabled="imageScale >= 300" |
| | | > |
| | | <i class="el-icon-zoom-in"></i> 放大 |
| | | </el-button> |
| | | <el-button size="mini" disabled>{{ imageScale }}%</el-button> |
| | | <el-button size="mini" @click="zoomImageOut" :disabled="imageScale <= 50"> |
| | | <el-button |
| | | size="mini" |
| | | @click="zoomImageOut" |
| | | :disabled="imageScale <= 50" |
| | | > |
| | | <i class="el-icon-zoom-out"></i> 缩小 |
| | | </el-button> |
| | | <el-button size="mini" @click="resetImageZoom"> |
| | |
| | | </el-button> |
| | | </el-button-group> |
| | | |
| | | <el-button |
| | | size="mini" |
| | | type="success" |
| | | @click="handleDownload" |
| | | > |
| | | <el-button size="mini" type="success" @click="handleDownload"> |
| | | <i class="el-icon-download"></i> 下载 |
| | | </el-button> |
| | | </div> |
| | |
| | | </div> |
| | | |
| | | <!-- Office文档预览 --> |
| | | <div v-else-if="fileType === 'office'" class="preview-container office-preview"> |
| | | <div |
| | | v-else-if="fileType === 'office'" |
| | | class="preview-container office-preview" |
| | | > |
| | | <div class="office-toolbar"> |
| | | <el-alert |
| | | title="Office文档预览提示" |
| | |
| | | |
| | | <div class="office-content"> |
| | | <iframe |
| | | :src="`https://view.officeapps.live.com/op/view.aspx?src=${encodeURIComponent(fileUrl)}`" |
| | | :src=" |
| | | `https://view.officeapps.live.com/op/view.aspx?src=${encodeURIComponent( |
| | | fileUrl |
| | | )}` |
| | | " |
| | | width="100%" |
| | | height="600px" |
| | | frameborder="0" |
| | |
| | | type: Object, |
| | | default: () => ({}) |
| | | } |
| | | // 添加baseUrlHt作为props传入 |
| | | // baseUrlHt: { |
| | | // type: String, |
| | | // default: process.env.VUE_APP_BASE_API |
| | | // } |
| | | }, |
| | | data() { |
| | | return { |
| | |
| | | // 加载状态 |
| | | loading: false, |
| | | pdfLoading: false, |
| | | baseUrlHt: "", |
| | | // 文件信息 |
| | | fileUrl: "", |
| | | fileName: "", |
| | |
| | | } |
| | | }, |
| | | previewVisible(newVal) { |
| | | this.$emit('update:visible', newVal); |
| | | this.$emit("update:visible", newVal); |
| | | if (!newVal) { |
| | | this.handleClose(); |
| | | } |
| | | } |
| | | }, |
| | | created() { |
| | | this.calculateBaseUrl(); |
| | | }, |
| | | methods: { |
| | | /** URL处理函数 - 将完整URL替换为baseUrlHt */ |
| | | processFileUrl(url) { |
| | | if (!url) return ""; |
| | | |
| | | // 如果已经是完整的http或https链接 |
| | | if (url.startsWith("http://") || url.startsWith("https://")) { |
| | | // 找到第三个斜杠后的位置,提取路径部分 |
| | | const thirdSlashIndex = url.indexOf("/", 8); // 从http://或https://之后开始找 |
| | | if (thirdSlashIndex !== -1) { |
| | | return `${this.baseUrlHt}${url.substring(thirdSlashIndex)}`; |
| | | } |
| | | return this.baseUrlHt; // 如果没有路径部分,只返回baseUrlHt |
| | | } |
| | | |
| | | // 相对路径处理 |
| | | if (url.startsWith("/")) { |
| | | return `${this.baseUrlHt}${url}`; |
| | | } |
| | | |
| | | return `${this.baseUrlHt}/${url}`; |
| | | }, |
| | | /** 计算 baseUrlHt */ |
| | | calculateBaseUrl() { |
| | | // 获取当前浏览器地址 |
| | | const currentUrl = window.location.href; |
| | | console.log("当前浏览器地址:", currentUrl); |
| | | |
| | | try { |
| | | // 使用 URL 对象解析 |
| | | const urlObj = new URL(currentUrl); |
| | | // 获取主机名(IP 或域名) |
| | | const hostname = urlObj.hostname; |
| | | // 拼接端口 :9095 |
| | | this.baseUrlHt = `http://${hostname}:9095`; |
| | | console.log("计算得到的 baseUrlHt:", this.baseUrlHt); |
| | | } catch (error) { |
| | | console.error("解析URL失败:", error); |
| | | // 备用方案:使用正则提取 |
| | | const match = currentUrl.match(/https?:\/\/([^:\/]+)/); |
| | | if (match) { |
| | | this.baseUrlHt = `http://${match[1]}:9095`; |
| | | } else { |
| | | // 最后的备用方案 |
| | | this.baseUrlHt = |
| | | process.env.VUE_APP_BASE_API || "http://localhost:9095"; |
| | | } |
| | | } |
| | | }, |
| | | |
| | | /** 初始化预览 */ |
| | | initPreview() { |
| | | if (!this.file) return; |
| | | |
| | | this.fileName = this.file.fileName || this.file.name || "未知文件"; |
| | | this.fileUrl = this.file.fileUrl || this.file.path || this.file.url; |
| | | const originalUrl = this.file.fileUrl || this.file.path || this.file.url; |
| | | |
| | | // 处理URL,替换域名部分 |
| | | this.fileUrl = this.processFileUrl(originalUrl); |
| | | |
| | | this.fileType = this.getFileType(this.fileName); |
| | | |
| | | this.loading = true; |
| | |
| | | getFileType(fileName) { |
| | | if (!fileName) return "other"; |
| | | |
| | | const extension = fileName.split('.').pop().toLowerCase(); |
| | | const extension = fileName |
| | | .split(".") |
| | | .pop() |
| | | .toLowerCase(); |
| | | const imageTypes = ["jpg", "jpeg", "png", "gif", "bmp", "webp"]; |
| | | const pdfTypes = ["pdf"]; |
| | | const officeTypes = ["doc", "docx", "xls", "xlsx", "ppt", "pptx"]; |
| | |
| | | |
| | | /** 获取文件扩展名 */ |
| | | getFileExtension(filename) { |
| | | return filename.split('.').pop().toLowerCase() || "未知"; |
| | | return ( |
| | | filename |
| | | .split(".") |
| | | .pop() |
| | | .toLowerCase() || "未知" |
| | | ); |
| | | }, |
| | | |
| | | /** PDF加载完成 */ |
| | |
| | | this.imageScale = 100; |
| | | }, |
| | | |
| | | /** 下载文件 */ |
| | | /** 下载文件 - 也需要处理URL */ |
| | | handleDownload() { |
| | | if (!this.fileUrl) { |
| | | this.$message.warning("文件路径不存在,无法下载"); |