张晓波
2023-09-19 164694c47c35d6654df69b533e8dbf8b5423efc5
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<template>
    <div>
        <el-dialog class="prop_dialog" title="查看附件" width="45%" :before-close="() => {$emit('update:asyncFile', false)}" :visible.sync="asyncFile">
            <div class="dialog-center">
                <div class="img-flex">
                    <div class="file-list" v-for="(item,index) in fileList" :key="index" @mouseenter="getClose(item)"
                        @mouseleave="setClose">
                        <div :class="{close_icon:close == item.id,close_icons: close != item.id}">
                            <el-tooltip class="item" effect="dark" content="预览文件" placement="left">
                                <i class="el-icon-zoom-in" @click="lookFile(item)"></i>
                            </el-tooltip>
                            <div class="look-line"></div>
                            <el-tooltip class="item" effect="dark" content="下载文件" placement="right">
                                <i class="el-icon-download" @click="downloadFile(item)"></i>
                            </el-tooltip>
                        </div>
                        <img src="../assets/p.jpg" class="imgs" v-if="item.type == 'pdf'">
                        <img src="../assets/w.jpg" style="width: 100%; height: 100%;"
                            v-if="item.type == 'doc'||item.type == 'docx'">
                        <img src="../assets/e.jpg" style="width: 100%; height: 100%;"
                            v-if="item.type == 'xls'||item.type == 'xlsx'">
                        <img src="../assets/z.png" style="width: 100%; height: 100%;"
                            v-if="item.type == 'zip'||item.type == '7z'">
                        <img src="../assets/t.png" style="width: 100%; height: 100%;" v-if="item.type == 'txt'">
                        <img src="../assets/ppt.png" style="width: 100%; height: 100%;"
                            v-if="item.type == 'ppt'||item.type == 'pptx'">
                        <img :src="`https://pipe.thhy-tj.com/${item.url}`" style="width: 100%; height: 100%;"
                            v-if="['png','jpg','gif'].includes(item.type)">
                        <div class="img-text">{{item.name}}</div>
                    </div>
                </div>
            </div>
        </el-dialog>
        <el-dialog append-to-body :title="fileName" width="40%" class="pdf-dialog prop_dialog" :visible.sync="asyncLook">
            <div v-if="!imgs&&!excel" style="text-align: center; color: #fff">{{pdfPage}}/{{pageCount}}</div>
            <div class="left-page" @click="currentPage(-1)" v-if="!imgs&&!excel">
                <img src="../assets/left.png" class="page-img">
            </div>
            <div class="right-page" @click="currentPage(1)" v-if="!imgs&&!excel">
                <img src="../assets/right.png" class="page-img">
            </div>
            <div class="pdf-center" v-if="!imgs">
                <div v-if="excel" v-html="pdfUrl"></div>
                <canvas v-else id="files"></canvas>
            </div>
            <div class="pdf-center" v-if="imgs">
                <img :src="`https://pipe.thhy-tj.com/${imgUrl}`" style="width: 100%; height: 100%;">
            </div>
        </el-dialog>
    </div>
</template>
 
<script>
const pdfjsLib = window['pdfjs-dist/build/pdf'];
import { downLoadFile } from '../plugins/public';
    export default {
        name: "LookFile",
        props: {
            fileList: {
                type: Array
            },
            asyncFile: {
                tyope: Boolean
            }
        },
        data() {
            return {
                imgs: false,
                excel: false,
                imgUrl: "",
                fileName: "",
                asyncLook: false,
                pdfUrl: "",
                pageCount: 0,
                pdfPage: 1,
                close: "",
                loading: null,
                pdfData: null
            }
        },
        methods: {
            //出现删除样式
            getClose(item) {
                this.close = item.id
            },
            //去除删除样式
            setClose() {
                this.close = ""
            },
            // 获取导出权限
            async getExportToken(params) {
                const { data } = await this.$api.System.GETEXPORTTOKENDATA(params);
                return data;
            },
            //预览文件
            lookFile(item) {
                let that = this;
                that.pdfUrl = ''
                that.asyncLook = true;
                that.createLoadling();
                if(['png','jpg','gif'].includes(item.type)) {
                    that.imgs = true;
                    that.imgUrl = item.url
                } else {
                    that.imgs = false;
                    that.excel = false;
                    that.getExportToken({
                        fullpath: item.url,
                    }).then((res) => {
                        if(res) {
                            that.fileName = item.name;
                                that.pdfUrl = `${process.env.VUE_APP_BASE_URL}/file/file/preview?authcode=${res}`;
                                const    pdfRes = pdfjsLib.getDocument(that.pdfUrl);
                                // 获取pdf内容
                                pdfRes.promise.then(function(page) {
                                    that.pdfData = page;
                                    that.pageCount = page.numPages;
                                    that.createPdf();
                                    that.loading.close();
                                }).catch(() => {
                                    that.loading.close();
                                    var canvas = document.getElementById('files');
                                    var context = canvas.getContext('2d');
                                    context.clearRect(0, 0, canvas.width, canvas.height);
                                })
                        } else {
                            that.$message.warning(res.statusMsg);
                            that.loading.close();
                        }
                    }).catch(() => {
                        that.loading.close();
                    })
                }
            },
            //渲染pdf
            createPdf() {
                // 获取第一页
                this.pdfData.getPage(this.pdfPage).then(function(page1) {
                    // 获取文件宽高内容  设置放大缩小  
                    const view = page1.getViewport({
                        scale: 1
                    })
                    // 获取canvas
                    var canvas = document.getElementById('files');
                    var context = canvas.getContext('2d');
                    canvas.height = view.height;
                    canvas.width = view.width;
                    // 没看懂  应该是设置canvas标签大小
                    var renderContext = {
                        canvasContext: context,
                        viewport: view
                    };
                    // 没看懂  应该是渲染到标签上
                    page1.render(renderContext);
                })
            },
            //预览分页
            currentPage(num) {
                if(this.pdfPage === 1 && num === -1) return
                if(num === 1 && this.pdfPage === this.pageCount) return
                this.pdfPage += num
                this.createPdf()
            },
            //下载文件
            downloadFile(item) {
                this.createLoadling()
                this.getExportToken({
                    fullpath: item.url,
                    fileName: item.name
                }).then((data) => {
                    downLoadFile(data, '/file/file/down');
                    this.loading.close();
                }).catch(() => {
                    this.loading.close();
                    this.$message.error("该文件无法下载....");
                })
            },
            createLoadling() {
                this.loading = this.$loading({
                    lock: true,
                    text: '正在加载中....',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.7)'
                })
            }
        }
    }
</script>
 
<style lang="scss" scoped>
    @import '../style/upload-file.scss';
    @import '../style/layout-main.scss';
 
    .img-flex {
        width: 100%;
    }
 
    .pdf-center {
        text-align: center;
    }
</style>