Blame view

src/views/components/imagePreview.vue 6.51 KB
任超 committed
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
<template>
  <div class="rlPopup">
    <div class="prev handle-btn" v-if="!isSingle" @click="prev()">
      <i class="el-icon-arrow-left"></i>
    </div>
    <div class="next handle-btn" v-if="!isSingle" @click="next()">
      <i class="el-icon-arrow-right"></i>
    </div>
    <div class="img-list-wrap">
      <div v-for="(img, i) in previewImg.imgList" :key="i">
        <photo-zoom :url="img.url" :bigWidth="165" v-if="i === previewImg.index" :scale="2"
          overlayStyle="width: 100%;height: 563px;">
        </photo-zoom>
      </div>
    </div>
    <!--缩略图-->
    <div class="thumb-wrap">
      <div class="thumb-wrap-button">
        <el-button type="primary" @click="clickImage">(放大) 显示(缩小)</el-button>
        <el-upload class="fileUpdate" action="" :show-file-list="false" multiple :limit="5" :auto-upload="false"
          :on-change="handleChange" accept=".JPG, .PNG, .JPEG,.jpg, .png, .jpeg" :before-upload="beforeUpload">
          <el-button icon="el-icon-upload" type="primary">上传</el-button>
        </el-upload>
        <el-button type="primary" icon="el-icon-delete-solid" @click="handleDelete">删除</el-button>
      </div>
      <ul>
        <li v-for="(img, index) in thumbnailImages" :key="index" :class="{ active:previewImg.index === index}"
          @click="showCurrent(index)">
          <img :src="img.url">
        </li>
      </ul>
    </div>
    <!-- 点开后的视图 -->
    <publicPicture v-if="showViewer" :url-list="allLi" :initialIndex="initialIndex" @close-viewer="closeViewer">
    </publicPicture>
  </div>
</template>
<script>
import PhotoZoom from '@/components/photo-zoom'
import publicPicture from '@/components/publicPicture/index.vue'
export default {
  name: 'PreviewImage',
  props: {
    previewImg: {
      type: Object,
      default: () => { }
    }
  },
  components: {
    PhotoZoom,
    publicPicture
  },
  data () {
    return {
      transform: {
        scale: 1,
        degree: 0
      },
      // 缩略图
      thumbnailImages: this.previewImg.imgList,
      showViewer: false,
      initialIndex: undefined,
      allLi: [],
    }
  },
  watch: {
    previewImg: {
      handler (newValue, oldValue) {
        this.allLi = _.cloneDeep(newValue.imgList).map(item => item.url)
      },
      deep: true
    }
  },
  computed: {
    isSingle () {
      return this.previewImg.imgList.length <= 1
    },
    isFirst () {
      return this.previewImg.index === 0
    },
    isLast () {
      return this.previewImg.index === this.previewImg.imgList.length - 1
    }
  },
  methods: {
    prev () {
      if (this.isFirst) {
        if (this.previewImg.selectedIndex - 1 >= 0) {
          this.$parent.previewImg.index = 1
        }
        return
      }
      const len = this.previewImg.imgList.length
      this.$parent.previewImg.index = (this.$parent.previewImg.index - 1 + len) % len
      this.reset()
    },
    next () {
      if (this.isLast) {
        if (this.previewImg.selectedIndex + 1 < this.previewImg.attachmentList.length) {
          this.$parent.previewImg.index = 0
        }
        return
      }
      const len = this.previewImg.imgList.length
      this.$parent.previewImg.index = (this.$parent.previewImg.index + 1) % len
      this.reset()
    },
    reset () {
      this.transform = {
        scale: 1,
        degree: 0
      }
    },
    showCurrent (index) {
      this.previewImg.index = index
    },
    closeViewer () {
      this.showViewer = false
    },
    clickImage () {
      this.showViewer = true
    },
    // 上传
    beforeUpload (file) {
      const isJPEG = file.type === 'image/jpeg'
      const isPNG = file.type === 'image/png'
      const isJPG = file.type === 'image/jpg'
      const isGIF = file.type === 'image/gif'
      const isLt5M = file.size / 1024 / 1024 < 5
      if (!isJPEG && !isGIF && !isPNG && !isJPG && !isGIF) {
        this.$message.error('请选择jpeg/png/jpg/bmp/gif格式的图片后重试')
        this.loading = false
      }
      if (!isLt5M) {
        this.$message.error('上传图片大小不能超过 5MB!')
        this.loading = false
      }
      this.imgHidden = (isJPG || isJPEG || isPNG || isGIF) && isLt5M
      return this.imgHidden
    },
    async handleChange (file) {
      let data = _.cloneDeep(this.previewImg.imgList[this.previewImg.index])
    },
    handleDelete () {
      let _this = this
      this.$confirm('此操作将永久该附件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let bsmFileList = []
        bsmFileList[0] = this.previewImg.imgList[this.previewImg.index].bsmFile
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  }
}
</script>

<style lang="scss" scoped>
// 查看大图
.rlPopup {
  position: relative;
  width: 100%;
  text-align: center;
  height: 100%;

  .handle-btn {
    position: absolute;
    top: 50%;
    transform: translateY(-100%);
    width: 66px;
    height: 66px;
    line-height: 75px;
    color: #fff;
    background-color: rgb(198, 198, 198);
    border-radius: 4px;
    cursor: pointer;
    text-align: center;

    i {
      font-size: 24px;
    }
  }

  .prev {
    left: 0%;
  }

  .next {
    right: 0%;
  }

  .img-list-wrap {
    width: 100%;
    display: flex;
    justify-content: center;
    height: calc(100% - 80px);
    align-items: center;
    background: rgba(194, 190, 190, 0.1);

    img {
      display: block;
      object-fit: scale-down;
      transition: all 0.3s;
      max-width: 100%;
    }
  }

  .thumb-wrap {
    &-button {
      display: flex;
      justify-content: center;

      .fileUpdate {
        margin: 0 10px;
      }
    }

    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%);
      }
    }

    .active {
      border-color: #409eff;
    }
  }
}
</style>
<style>
.zoom-on-hover {
  position: relative;
  overflow: hidden;
}

.zoom-on-hover .normal {
  width: 100%;
}

.zoom-on-hover .zoom {
  position: absolute;
  opacity: 0;
  transform-origin: top left;
}

.zoom-on-hover.zoomed .zoom {
  opacity: 1;
}

.zoom-on-hover.zoomed .normal {
  opacity: 0;
}
</style>