Blame view

src/views/components/imagePreview.vue 15.2 KB
renchao@pashanhoo.com committed
1
<!--
xiaomiao committed
2
 * @Description:
renchao@pashanhoo.com committed
3
 * @Autor: renchao
4
 * @LastEditTime: 2023-12-14 16:42:28
renchao@pashanhoo.com committed
5
-->
任超 committed
6 7
<template>
  <div class="rlPopup">
renchao@pashanhoo.com committed
8
    <div class="prev handle-btn" v-if="!isScan" @click="prev()">
任超 committed
9 10
      <i class="el-icon-arrow-left"></i>
    </div>
renchao@pashanhoo.com committed
11
    <div class="next handle-btn" v-if="!isScan" @click="next()">
任超 committed
12 13
      <i class="el-icon-arrow-right"></i>
    </div>
14
    <div class="img-list-wrap" v-Loading="loading">
15 16
      <img id="photo" src="" v-if="isScan && this.BASE_API.gaopaiyi=='jy'" alt="高拍仪捷宇">
      <img src="http://127.0.0.1:38088/video=stream&camidx=0" v-else-if="isScan && this.BASE_API.gaopaiyi=='lt'" alt="高拍仪良田">
renchao@pashanhoo.com committed
17
      <div v-for="(img, i) in previewImg.imgList" :key="i" v-else>
任超 committed
18
        <photo-zoom :url="img.fjurl" :bigWidth="165" v-if="i === previewImg.index" :scale="2"
任超 committed
19
          overlayStyle="width: 100%;height:100%">
任超 committed
20 21 22 23 24 25
        </photo-zoom>
      </div>
    </div>
    <!--缩略图-->
    <div class="thumb-wrap">
      <div class="thumb-wrap-button">
26
        <el-button type="primary" @click="clickImage" v-if="previewImg.imgList.length>0">(放大) 显示(缩小)</el-button>
renchao@pashanhoo.com committed
27
        <el-upload class="fileUpdate" ref="upload" :key="key" action="" :show-file-list="false" :multiple="true" :auto-upload="false"
28
          :on-change="handleChange"
29
          accept=".JPG, .PNG, .JPEG,.jpg, .png, .jpeg">
30
          <el-button icon="el-icon-upload" type="primary" :loading="upDateloading" v-if="ableOperation">上传</el-button>
任超 committed
31
        </el-upload>
32
        <!-- 左移右移 -->
33 34
        <el-button type="primary" @click="handleMove('left')" v-if="ableOperation && thumbnailImages.length>0">左移</el-button>
        <el-button type="primary" @click="handleMove('right')" v-if="ableOperation && thumbnailImages.length>0">右移</el-button>
35
        <el-button type="primary" icon="el-icon-delete-solid" @click="handleDelete"
xiaomiao committed
36 37
          v-if="thumbnailImages.length>0 && ableOperation">删除</el-button>
        <div v-if="ableOperation" class="pl-5">
38
          <el-button type="primary" @click="handleOpenScan" v-if="ableOperation" :loading="loading">{{scanTitle}}</el-button>
39
          <el-button type="primary" @click="handleViewScan" v-if="isScan && ableOperation">拍照</el-button>
40
        </div>
任超 committed
41 42
      </div>
      <ul>
任超 committed
43
        <li v-for="(img, index) in thumbnailImages" :key="index" :class="{ active: previewImg.index === index }"
任超 committed
44
          @click="showCurrent(index)">
任超 committed
45
          <img :src="img.fjurl">
任超 committed
46 47 48 49 50 51 52 53 54
        </li>
      </ul>
    </div>
    <!-- 点开后的视图 -->
    <publicPicture v-if="showViewer" :url-list="allLi" :initialIndex="initialIndex" @close-viewer="closeViewer">
    </publicPicture>
  </div>
</template>
<script>
55
  import PhotoZoom from '@/components/PhotoZoom'
56
  import { getAltimeterInfo, getUuid } from '@/utils/operation.js'
57
  import { uploadBatch, deleteClmx, move, uploadSjClmx } from "@/api/clxx.js";
58 59 60 61 62 63 64
  import publicPicture from '@/components/publicPicture/index.vue'
  export default {
    name: 'PreviewImage',
    props: {
      previewImg: {
        type: Object,
        default: () => { }
65 66 67 68
      },
      ableOperation: {
        type: Boolean,
        default: true
任超 committed
69 70
      }
    },
71 72 73
    components: {
      PhotoZoom,
      publicPicture
任超 committed
74
    },
75 76
    data () {
      return {
77
        upDateloading: false,
78
        loading: false,
renchao@pashanhoo.com committed
79
        key: 0,
renchao@pashanhoo.com committed
80 81 82
        isScan: false,
        // 打开高拍仪
        scanTitle: '打开高拍仪',
83 84 85 86
        transform: {
          scale: 1,
          degree: 0
        },
1  
renchao@pashanhoo.com committed
87
        maxFileLength: 0,
88 89 90
        // 缩略图
        thumbnailImages: [],
        showViewer: false,
91
        initialIndex: 0,
92
        allLi: [],
93
        webSocket: null
94
      }
任超 committed
95
    },
96 97 98
    watch: {
      previewImg: {
        handler (newValue, oldValue) {
99
          if (newValue.imgList && newValue.imgList.length > 0) {
1  
renchao@pashanhoo.com committed
100 101
            this.allLi = _.cloneDeep(newValue.imgList).map(item => item.fjurl)
            this.thumbnailImages = newValue.imgList
102 103 104
          } else {
            this.allLi = []
            this.thumbnailImages = []
1  
renchao@pashanhoo.com committed
105
          }
106
        },
107 108
        deep: true,
        immediate: true
109
      }
任超 committed
110
    },
111 112 113 114
    created () {
      this.maxLength = 0;
      this.allLi = _.cloneDeep(this.previewImg.imgList).map(item => item.fjurl)
      this.thumbnailImages = this.previewImg.imgList
任超 committed
115
    },
116 117 118 119 120 121
    computed: {
      isFirst () {
        return this.previewImg.index === 0
      },
      isLast () {
        return this.previewImg.index === this.previewImg.imgList.length - 1
任超 committed
122 123
      }
    },
124
    methods: {
renchao@pashanhoo.com committed
125 126 127 128 129
      /**
       * @description: 打开高拍仪
       * @author: renchao
       */
      handleOpenScan () {
130
        let that = this
131 132
        this.isScan = !this.isScan
        if (this.isScan) {
133
          this.loading = true
134 135 136 137 138 139
          this.$message({
            message: '正在启动程序请稍等',
            type: 'success'
          })
          setTimeout(() => {
            this.scanTitle = '关闭高拍仪'
140
            this.loading = false
141 142 143 144
          }, 4000)
        } else {
          this.scanTitle = '打开高拍仪'
        }
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
        if (this.BASE_API.gaopaiyi == 'jy') {
          let webSocket = new WebSocket('ws://localhost:1818');
          this.webSocket = webSocket
          webSocket.onopen = function (event) {
            webSocket.send('bStartPlay')
            webSocket.send('vSetPreviewRect(1600,1200)')
          };
          webSocket.onmessage = function (event) {
            let begin_data = "data:image/jpeg;base64,";
            document.getElementById('photo').src = begin_data + event.data;
            if (event.data.indexOf('BeginsGetBase64') >= 0) {
              let blob = that.dataURLtoBlob('data:image/png;base64,' + event.data.replace('BeginsGetBase64', '').replace('EndsGetBase64', ''));
              let file = that.blobToFile(blob);
              var formData = new FormData();
              formData.append('file', file)
              formData.append("bsmSj", that.previewImg.bsmSj);
              formData.append("bsmSlsq", that.previewImg.bsmSlsq);
              if (that.previewImg.imgList.length > 0) {
                formData.append("ssBsmClmx", that.previewImg.imgList[that.previewImg.index].bsmClmx);
              }
              uploadSjClmx(formData).then((res) => {
                if (res.code == 200) {
                  that.$emit('updateList', { children: res.result, bsmSj: that.previewImg.bsmSj })
                  that.$message({
                    message: '上传成功!',
                    type: 'success'
                  })
                }
              })
            }
          }
        }
renchao@pashanhoo.com committed
177
      },
178
      // 左右移动
179
      handleMove (direction) {
180 181 182 183 184 185 186
        move(direction, this.previewImg.imgList[this.previewImg.index].bsmClmx).then(res => {
          if (res.code == 200) {
            if (direction == 'left') {
              this.previewImg.index = this.previewImg.index - 1
            } else {
              this.previewImg.index = this.previewImg.index + 1
            }
187
            this.initialIndex = this.previewImg.index
188 189 190 191 192 193 194 195 196
            this.$emit('updateList', { children: res.result, bsmSj: this.previewImg.bsmSj })
            this.$message({
              message: '移动成功!',
              type: 'success'
            })
          } else {
            this.$message.error(res.message);
          }
        })
197
      },
renchao@pashanhoo.com committed
198 199 200 201
      /**
       * @description: 拍照
       * @author: renchao
       */
202 203 204 205 206 207 208 209 210 211 212 213 214 215
      dataURLtoBlob (base64String) {
        const arr = base64String.split(',');
        if (arr.length !== 2) {
          throw new Error('Invalid Base64 format');
        }
        const mime = arr[0].match(/:(.*?);/)[1];
        if (!mime) {
          throw new Error('Cannot retrieve MIME type');
        }
        const bstr = atob(arr[1]);
        const n = bstr.length;
        const u8arr = new Uint8Array(n);
        for (let i = 0; i < n; i++) {
          u8arr[i] = bstr.charCodeAt(i);
216
        }
217 218 219 220 221 222 223 224 225 226 227
        return new Blob([u8arr], { type: mime });
      },
      blobToFile (blob) {
        let name = getUuid(8) + '.jpg'
        const file = new File([blob], name);
        return file;
      },
      handleViewScan () {
        if (this.BASE_API.gaopaiyi == 'jy') {
          this.webSocket.send('sGetBase64');
          return
228
        }
229
        getAltimeterInfo().then(res => {
230 231
          let blob = this.dataURLtoBlob('data:image/png;base64,' + res.data.photoBase64);
          let file = this.blobToFile(blob);
232
          var formData = new FormData();
233
          formData.append('file', file)
234 235
          formData.append("bsmSj", this.previewImg.bsmSj);
          formData.append("bsmSlsq", this.previewImg.bsmSlsq);
236
          if (this.previewImg.imgList.length > 0) {
237
            formData.append("index", this.previewImg.imgList[this.previewImg.index].sxh);
238
          }
239 240
          uploadSjClmx(formData).then((res) => {
            if (res.code == 200) {
241
              this.$emit('updateList', { children: res.result, bsmSj: this.previewImg.bsmSj })
242 243 244 245 246 247
              this.$message({
                message: '上传成功!',
                type: 'success'
              })
            }
          })
renchao@pashanhoo.com committed
248 249
        })
      },
yuanbo committed
250 251 252 253
      /**
       * @description: prev
       * @author: renchao
       */
254 255 256 257 258 259
      prev () {
        let len = this.previewImg.imgList.length
        if (this.isFirst || len == 0) {
          this.$emit('prevPriview')
        } else {
          this.$parent.previewImg.index = (this.$parent.previewImg.index - 1 + len) % len
任超 committed
260
        }
261
      },
yuanbo committed
262 263 264 265
      /**
       * @description: next
       * @author: renchao
       */
266 267 268 269 270 271 272 273
      next () {
        let len = this.previewImg.imgList.length
        if (this.isLast || len == 0) {
          this.$emit('nextPriview')
        } else {
          this.$parent.previewImg.index = (this.$parent.previewImg.index + 1) % len
        }
      },
yuanbo committed
274 275 276 277 278
      /**
       * @description: showCurrent
       * @param {*} index
       * @author: renchao
       */
279 280
      showCurrent (index) {
        this.previewImg.index = index
281
        this.initialIndex = index
282
      },
yuanbo committed
283 284 285 286
      /**
       * @description: closeViewer
       * @author: renchao
       */
287 288 289
      closeViewer () {
        this.showViewer = false
      },
yuanbo committed
290 291 292 293
      /**
       * @description: clickImage
       * @author: renchao
       */
294 295 296
      clickImage () {
        this.showViewer = true
      },
yuanbo committed
297 298 299 300 301 302
      /**
       * @description: handleChange
       * @param {*} file
       * @param {*} files
       * @author: renchao
       */
1  
renchao@pashanhoo.com committed
303 304 305
      async handleChange (file, fileList) {
        let length = fileList.length;
        this.maxFileLength = Math.max(length, this.maxFileLength)
306
        var formData = new FormData();
307
        setTimeout(() => {
1  
renchao@pashanhoo.com committed
308 309 310
          if (this.maxFileLength !== length) {
            return
          }
311 312
          let num = 0, max = 0;
          const isLt5M = file.size / 1024 / 1024 < 5;
1  
renchao@pashanhoo.com committed
313
          fileList.forEach(item => {
314 315 316
            if (!isLt5M) {
              max++
            }
317 318
            if (!['image/jpeg', 'image/png', 'image/jpg', 'image/gif'].includes(item.raw.type)) {
              num++
renchao@pashanhoo.com committed
319 320
            } else {
              formData.append('file', item.raw)
321
            }
322
          })
323 324
          if (num >= 1) {
            this.$message.error("请选择jpeg/png/jpg/bmp/gif格式的图片后重试")
renchao@pashanhoo.com committed
325 326 327
            // 移除不支持的文件类型
            this.key++
            return;
328
          }
329 330 331 332 333
          if (max >= 1) {
            this.$message.error('上传图片大小不能超过 5MB!');
            this.key++
            return;
          }
334
          this.upDateloading = true
335 336
          formData.append("bsmSj", this.previewImg.bsmSj);
          formData.append("bsmSlsq", this.previewImg.bsmSlsq);
337 338 339
          if (this.previewImg.imgList.length > 0) {
            formData.append("index", this.previewImg.imgList[this.previewImg.index].sxh);
          }
340
          uploadBatch(formData).then((res) => {
341
            if (res.code == 200) {
renchao@pashanhoo.com committed
342
              this.$emit('updateList', { children: res.result, bsmSj: this.previewImg.bsmSj })
343 344 345 346
              this.$message({
                message: '上传成功!',
                type: 'success'
              })
347
              this.upDateloading = false
348 349
              this.$refs.upload.clearFiles();
              this.maxFileLength = 0
350 351
            }
          })
352
        }, 0)
353
      },
yuanbo committed
354 355 356 357
      /**
       * @description: handleDelete
       * @author: renchao
       */
358 359 360 361 362 363 364 365
      handleDelete () {
        let that = this
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          let bsmClmx = this.previewImg.imgList[this.previewImg.index].bsmClmx
366 367
          let bsmSj = this.previewImg.imgList[this.previewImg.index].bsmSj
          this.previewImg.imgList = this.previewImg.imgList.filter(item => item.bsmClmx != bsmClmx)
368 369
          deleteClmx(bsmClmx).then(res => {
            if (res.code == 200) {
370
              that.$emit('updateList', { children: this.previewImg.imgList, bsmSj: bsmSj })
371
              that.initialIndex = that.previewImg.index
372 373 374 375 376 377 378 379 380 381 382
              that.$message({
                message: '删除成功!',
                type: 'success'
              })
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
任超 committed
383
        })
384
      }
任超 committed
385 386 387 388
    }
  }
</script>
<style lang="scss" scoped>
389 390 391
  // 查看大图
  .rlPopup {
    position: relative;
392
    width: 80%;
任超 committed
393
    text-align: center;
394
    height: 100%;
任超 committed
395

396 397 398 399 400 401 402 403
    .handle-btn {
      position: absolute;
      top: 50%;
      transform: translateY(-100%);
      width: 66px;
      height: 66px;
      line-height: 75px;
      color: #fff;
404
      background-color: #ccc;
405 406 407 408
      border-radius: 50%;
      cursor: pointer;
      text-align: center;
      transition: all 0.3s;
任超 committed
409

410 411 412 413
      i {
        font-size: 24px;
      }
    }
任超 committed
414

415 416 417
    .handle-btn:hover {
      background-color: rgb(185, 183, 183);
    }
任超 committed
418

419 420 421
    .prev {
      left: 1%;
    }
任超 committed
422

423 424
    .next {
      right: 1%;
任超 committed
425 426
    }

427 428
    .img-list-wrap {
      width: 100%;
任超 committed
429 430
      display: flex;
      justify-content: center;
431 432 433 434
      height: calc(100% - 80px);
      align-items: center;
      background: rgba(194, 190, 190, 0.1);
      overflow: scroll;
任超 committed
435

436 437 438 439
      img {
        display: block;
        object-fit: scale-down;
        transition: all 0.3s;
renchao@pashanhoo.com committed
440 441
        width: 100%;
        height: 100%;
任超 committed
442 443 444
      }
    }

445 446 447 448
    .thumb-wrap {
      &-button {
        display: flex;
        justify-content: center;
449
        flex-wrap: wrap;
450 451 452
        .fileUpdate {
          margin: 0 10px;
        }
任超 committed
453 454
      }

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
      li {
        float: left;
        width: 60px;
        height: 45px;
        border: solid 1px #ececec;
        position: relative;
        margin-right: 5px;
        cursor: pointer;

        &:last-child {
          margin-right: 0;
        }

        img {
          max-width: 57px;
          max-height: 42px;
          display: block;
          object-fit: scale-down;
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
        }
任超 committed
478 479
      }

480 481 482
      .active {
        border-color: #409eff;
      }
任超 committed
483 484 485 486
    }
  }
</style>
<style>
487 488 489 490
  .zoom-on-hover {
    position: relative;
    overflow: hidden;
  }
任超 committed
491

492 493 494
  .zoom-on-hover .normal {
    width: 100%;
  }
任超 committed
495

496 497 498 499 500
  .zoom-on-hover .zoom {
    position: absolute;
    opacity: 0;
    transform-origin: top left;
  }
任超 committed
501

502 503 504
  .zoom-on-hover.zoomed .zoom {
    opacity: 1;
  }
任超 committed
505

506 507 508
  .zoom-on-hover.zoomed .normal {
    opacity: 0;
  }
任超 committed
509
</style>