123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <template>
- <el-dialog class="show-file" :visible="dialogVisibleShowFile" width="900px" @close="dialogShowFileClose">
- <div slot="title">查看文件【{{ fileName }}】</div>
- <template v-if="fileType === 'pdf'">
- <pdf v-for="i in numPages" :key="i" :src="pdfSrc" :page="i" />
- </template>
- <template v-else-if="isImage(fileType)">
- <div class="image-parent">
- <el-image fit="contain" :src="fileUrl" />
- </div>
- </template>
- <div v-else-if="fileType === 'mp3'" class="audio-file">
- <audio :src="fileUrl" controls></audio>
- </div>
- <div v-else-if="fileType === 'mp4'" class="video-file">
- <video :src="fileUrl" controls></video>
- </div>
- <div v-else-if="fileType === 'txt'" class="text-file">
- <el-input v-model="text" type="textarea" :readonly="true" resize="none" />
- </div>
- <template v-else>
- <iframe
- :src="'https://view.officeapps.live.com/op/view.aspx?src=' + `${fileUrl}`"
- width="100%"
- height="490px"
- scrolling="no"
- />
- </template>
- <div slot="footer">
- <el-button @click="dialogShowFileClose">关闭</el-button>
- </div>
- </el-dialog>
- </template>
- <script>
- import pdf from 'vue-pdf';
- import { GetFileStoreInfo } from '@/api/app';
- import { getFileType } from '@/utils/filter';
- export default {
- components: { pdf },
- props: {
- fileName: {
- default: '',
- type: String
- },
- fileId: {
- default: '',
- type: String
- }
- },
- data() {
- return {
- dialogVisibleShowFile: false,
- pdfSrc: '',
- numPages: 1,
- fileUrl: '',
- text: ''
- };
- },
- computed: {
- fileType() {
- return getFileType(this.fileName);
- }
- },
- watch: {
- fileId(newVal) {
- if (newVal.length > 0) {
- this.getFileStoreInfo();
- }
- }
- },
- methods: {
- getFileStoreInfo() {
- GetFileStoreInfo({ file_id: this.fileId }).then(({ file_url_https, file_relative_path }) => {
- this.fileUrl = file_url_https;
- if (this.fileType === 'pdf') {
- this.getNumPages(file_relative_path);
- }
- if (this.fileType === 'txt') {
- fetch(`${process.env.VUE_APP_PDF}${file_relative_path}`).then(async res => {
- if (!res.ok) return;
- this.text = await res.text();
- });
- }
- });
- },
- getNumPages(url) {
- let loadingTask = pdf.createLoadingTask(`${process.env.VUE_APP_PDF}${url}`);
- loadingTask.promise
- .then(pdf => {
- this.pdfSrc = loadingTask;
- this.numPages = pdf.numPages;
- })
- .catch(err => {
- console.error('pdf加载失败', err);
- this.$message.error('pdf加载失败');
- });
- },
- showDialog() {
- this.dialogVisibleShowFile = true;
- },
- dialogShowFileClose() {
- this.dialogVisibleShowFile = false;
- this.$emit('dialogShowFileClose');
- },
- isImage(type) {
- return ['jpeg', 'gif', 'jpg', 'png', 'bmp', 'pic', 'svg'].includes(type);
- }
- }
- };
- </script>
- <style lang="scss">
- @import '~@/styles/mixin';
- .show-file {
- @include dialog;
- .el-dialog__header {
- font-weight: bold;
- }
- %image-parent,
- .image-parent {
- display: flex;
- justify-content: center;
- }
- .audio-file {
- @extend %image-parent;
- }
- .video-file {
- @extend %image-parent;
- }
- .text-file {
- height: 60vh;
- .el-textarea,
- textarea {
- height: 100%;
- }
- }
- }
- </style>
|