zhaoxiaoqiang1
2026-01-04 f1d30d03186c79ca2cbcfe60d6d2ce7d73fba97b
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<template>
  <div>
    <ul id="images" class="photo-viewer-images">
      <li v-for="(item, index) in imgList" :key="index">
        <!-- <iframe :src="item.url" v-if="fileType === 'pdf'"></iframe>
        <img :src="item.url" :alt="item.fileName +' . ' +item.imagetype" v-else /> -->
        <img
          :data-original="item.url"
          :src="item.resizeUrl?`${item.resizeUrl}/60/90`:item.url"
          :alt="item.fileName"
        />
      </li>
    </ul>
    <canvas id="canvas"></canvas>
  </div>
</template>
<script>
// 图片查看器
 
import Viewer from 'viewerjs'
import 'viewerjs/dist/viewer.css'
import qs from 'qs'
 
import deleteFile from '@/controller/deleteFile'
import queryEcmPageList from '@/controller/queryEcmPageList'
// import downloadFile from '@/controller/downloadFile'
import { downloadFile } from '@/utils/axiosRequest'
 
// const pdfFile = require('@/assets/office/pdf_icon.png')
const pdfFile = require('@/assets/office/PDF.svg')
const wordFile = require('@/assets/office/word.svg')
const excelFile = require('@/assets/office/excel.svg')
const pptFile = require('@/assets/office/ppt.svg')
const txtFile = require('@/assets/office/TXT.svg')
const Filepdf = require('@/assets/office/PDF.svg')
const mp4File = require('@/assets/office/mp4.svg')
const none = require('@/assets/office/none.svg')
 
export default {
  data() {
    return {
      imgList: [],
      gallery: null,
      index: 0,
      isEdit: 0,
      query: {},
      delModel: null,
      model: null,
      downModel: null,
      pdfFile: ''
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      let { query } = this.$route
      this.delModel = deleteFile()
      this.model = queryEcmPageList()
      this.downModel = downloadFile()
 
      // type 无或为0时为影像资料,其他请自行添加
      let { isEdit } = query
      this.query = query
      if (isEdit) {
        this.isEdit = isEdit
      }
      this.getList()
    },
    // 影像资料信息1==图片列表
    async getList() {
      const { query } = this
 
      const { model } = this
      const { transLogSerialno, objectNo, objectType, typeNo, serialno } = query
 
      const { list } = await model.request({
        objectNo: transLogSerialno || objectNo,
        objectType,
        typeNo
      })
 
      let initIndex = 0
 
      // 首先基于serialno查找
      initIndex = list.findIndex(item => serialno && item.serialno === serialno)
 
      // serialno查找失败则基于typeno查找
      // if (initIndex === -1 && typeno) {
      //   initIndex = list.findIndex(item => item.typeno === typeno)
      // }
      initIndex = initIndex > -1 ? initIndex : 0
 
      this.imgList = this.changeFileImg(list)
      this.index = initIndex > -1 ? initIndex : 0
      this.$nextTick(() => {
        this.madeViewer()
      })
    },
 
    // 渲染组件
    madeViewer() {
      let { index, isEdit } = this
      let editInfo = {}
      if (Number(isEdit) === 1) {
        editInfo = {
          del: (event = {}) => {
            this.tipDel()
          }
        }
      }
 
      editInfo.download = (event = {}) => {
        this.download()
      }
 
      const gallery = new Viewer(document.getElementById('images'), {
        initialViewIndex: index,
        button: false,
        modal: false,
        backdrop: false,
        zIndex: 100,
        toolbar: {
          // zoomIn: true,
          // zoomOut: true,
          prev: true,
          rotateLeft: true,
          rotateRight: true,
          next: true,
          ...editInfo
        },
        ready: () => {
          this.setAttr(this.getEle('.viewer-prev'), 'title', '上一张')
 
          this.setAttr(this.getEle('.viewer-next'), 'title', '下一张')
 
          this.setAttr(
            this.getEle('.viewer-rotate-left'),
            'title',
            '逆时针旋转'
          )
 
          this.setAttr(
            this.getEle('.viewer-rotate-right'),
            'title',
            '顺时针旋转'
          )
 
          this.setAttr(this.getEle('.viewer-del'), 'title', '删除')
          this.setAttr(this.getEle('.viewer-download'), 'title', '下载')
        },
        view: (event = {}) => {
          const { imgList } = this
          const { detail = {} } = event
          const { index } = detail
          if (!isNaN(index)) {
            this.index = index
          }
          if (imgList[index] && imgList[index].fileUrl) {
            const contentEle = this.getEle('.viewer-canvas')
            setTimeout(() => {
              contentEle.innerHTML = `<iframe width="100%" height="100%" src="${imgList[index].fileUrl}"></iframe>`
            }, 10)
          }
        }
      })
      gallery.show(true)
      this.gallery = gallery
    },
    getEle(str) {
      return document.querySelector(str)
    },
 
    download() {
      const { index, imgList } = this
      downloadFile(qs.stringify({ serialno: imgList[index].serialno })).then(
        res => {
          let blob = new Blob([res.data])
          let fileName =
            imgList[index].fileName + '.' + imgList[index].imagetype
          if (window.navigator.msSaveBlob) {
            // ie
            window.navigator.msSaveOrOpenBlob(res, fileName)
            return
          }
          let elink = document.createElement('a')
          elink.style.display = 'none'
          elink.href = window.URL.createObjectURL(blob)
          elink.download = fileName
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href)
          document.body.removeChild(elink)
        }
      )
    },
 
    setAttr(ele, name, val) {
      if (ele) {
        ele.setAttribute(name, val)
      }
    },
    tipDel() {
      const { index, isEdit, imgList } = this
      const { serialno } = imgList[index]
      if (Number(isEdit) === 1) {
        this.$confirm('此操作将删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.deleteImg(serialno)
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      } else {
        this.$message({
          type: 'info',
          message: '不能删除该图片!'
        })
      }
    },
    async deleteImg(serialno) {
      const { delModel } = this
      await delModel.request({
        serialno
      })
      this.$message({
        type: 'success',
        message: '删除成功!'
      })
      // 推送消息,目前type类型仅有deleteImg,删除某张图片
      window.postMessage(
        JSON.stringify({
          type: 'deleteImg',
          info: {
            serialno
          }
        }),
        '/'
      )
 
      // viewerjs无法刷新(update方法会导致无法切换问题),暂时reload处理
      // this.init()
      setTimeout(() => location.reload(), 1500)
    },
    changeFileImg(list) {
      return list.reduce((pre, curr) => {
        const { imagetype, url } = curr
        const fileType = imagetype.toLocaleLowerCase()
        console.log(fileType)
        const imgFormat = ['png','jpeg','jpg','gif','tif','mp4','pdf','zip','rar','7z']
        let info = {}
        if (fileType === 'pdf') {
          let newUrl = "/rlc-cts/server/oss?bucket=" + url.match(/\/\/([^.]+)/)[1] + "&ossKey=" + url.match(/\/(\d[\w]+)/)[1]
          info = {
            fileUrl: newUrl,
            url: pdfFile
          }
        }
        if(fileType == 'ZIP' || fileType == 'RAR' || fileType == '7z'){
          info = {
            fileUrl: newUrl,
            url: pdfFile
          }
        }
        // xlsx表格处理
        if (fileType === 'xlsx') {
          curr.url = excelFile
        }
        // txt处理
        if (fileType === 'txt') {
          curr.pdfUrl = url
          curr.url = txtFile
        }
        // 视频处理
        if (fileType === 'mp4') {
          curr.resizeUrl = ''
          curr.url = mp4File
        }
        // docx和doc文档处理
        if (fileType === 'docx' || fileType === 'doc') {
          curr.url = wordFile
        }
 
        // 去除imgFormat内的图片格式
        if (!imgFormat.includes(fileType)) {
          curr.url = none
        }
        pre.push({ ...curr, fileType, ...info })
        return pre
      }, [])
    }
  }
}
</script>
<style lang="postcss">
.photo-viewer-images {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  list-style: none;
  text-align: center;
  margin: 0;
  opacity: 0;
  & li {
    width: 180px;
    margin: 50px 50px 0 0;
    & img {
      width: 150px;
      height: 100px;
    }
  }
}
 
.viewer-footer {
  & .viewer-title {
    font-size: 14px;
    color: #000;
  }
 
  & .viewer-list {
    height: 40px;
    padding: 1px 0;
  }
 
  & .viewer-list li {
    height: 40px;
    width: 60px;
    padding-right: 10px;
  }
 
  & .viewer-prev,
  & .viewer-next {
    position: fixed;
    top: 50%;
    margin-top: -12px;
  }
 
  & .viewer-prev {
    left: 20px;
  }
 
  & .viewer-next {
    right: 20px;
  }
 
  & .viewer-del {
    color: #fff;
    background-image: url(../assets/delete.svg);
    background-position: center center;
    background-repeat: no-repeat;
    background-size: 52% auto;
  }
 
  & .viewer-download {
    color: #fff;
    background-image: url(../assets/download.svg);
    background-position: center center;
    background-repeat: no-repeat;
    background-size: 52% auto;
  }
 
  & .viewer-active {
    opacity: 1;
  }
}
</style>