index.vue 4.22 KB
<template>
  <div class="preview_image_page" :style="boxStyle">
    <!-- 有图:点击走 el-image 原生大图预览 -->
    <el-image
      v-if="src"
      class="preview_img"
      :src="displaySrc"
      :fit="fit"
      :preview-src-list="previewList"
    >
      <div slot="error" class="img_state">
        <i class="el-icon-picture-outline"></i>
        <span class="state_text">加载失败</span>
      </div>
    </el-image>

    <!-- 无图:占位 -->
    <div v-else class="img_state">
      <slot name="empty">
        <i class="el-icon-picture-outline"></i>
        <span class="state_text">{{ emptyText }}</span>
      </slot>
    </div>

    <!-- 名称:浮在左下角 -->
    <div v-if="name && showName" class="img_name">{{ name }}</div>
  </div>
</template>
<script>
import { ossImageThumb, isOssUrl } from "@/utils/oss";
/**
 * 公共图片组件(点击预览)
 *
 * props:
 *  src              String  图片地址
 *  previewSrcList   Array   预览列表(点开大图用),不传则只预览当前这张
 *  name             String  名称,浮在左下角,不传则不显示
 *  showName         Boolean 是否显示名称,默认 true
 *  fit              String  填充方式,默认 cover
 *  width / height   String  容器尺寸,默认 100%(撑满父级)
 *  ossProcess       Boolean OSS 链接是否自动拼缩略图参数,默认 true
 *                          (卡片显示小图,预览大图仍用原图 previewSrcList)
 *  emptyText        String  无图占位文案,默认「暂无图片」
 *
 * 用法:
 *  <preview-image :src="url" :preview-src-list="list" name="面单照片" height="180px"></preview-image>
 */
export default {
  name: "PreviewImage",
  props: {
    src: {
      type: String,
      default: "",
    },
    previewSrcList: {
      type: Array,
      default: function() {
        return [];
      },
    },
    name: {
      type: String,
      default: "",
    },
    showName: {
      type: Boolean,
      default: true,
    },
    fit: {
      type: String,
      default: "cover",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
    },
    // OSS 链接是否自动拼缩略图参数(卡片显示小图)
    ossProcess: {
      type: Boolean,
      default: true,
    },
    emptyText: {
      type: String,
      default: "暂无图片",
    },
  },
  computed: {
    boxStyle() {
      return {
        width: this.width,
        height: this.height,
      };
    },
    // 卡片显示用的图:OSS 链接默认拼缩略图参数;预览大图仍用原图(见 previewList)
    displaySrc() {
      if (this.ossProcess && isOssUrl(this.src)) {
        return ossImageThumb(this.src);
      }
      return this.src;
    },
    previewList() {
      const base =
        this.previewSrcList && this.previewSrcList.length > 0
          ? this.previewSrcList
          : this.src
          ? [this.src]
          : [];
      if (!this.src) {
        return base;
      }
      // el-image 原生预览永远从第 0 项打开:把当前这张图排到首位,
      // 点哪张默认呈现哪张,其余图按顺序跟在后面仍可左右切换。
      return [this.src, ...base.filter((u) => u !== this.src)];
    },
  },
};
</script>
<style lang="scss" scoped>
.preview_image_page {
  position: relative;
  overflow: hidden;
  background: #f5f7fa;
  .preview_img {
    width: 100%;
    height: 100%;
    border-radius: 4px;
    display: block;
  }
  // 名称浮层:左下角
  // pointer-events: none 让点击穿透到下层图片,点名称区域也能触发预览
  .img_name {
    position: absolute;
    left: 8px;
    bottom: 8px;
    z-index: 4;
    max-width: calc(100% - 16px);
    padding: 2px 8px;
    font-size: 12px;
    line-height: 18px;
    color: #fff;
    background: rgba(0, 0, 0, 0.55);
    border-radius: 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    pointer-events: none;
  }
  .img_state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    color: #c0c4cc;
    font-size: 26px;
    .state_text {
      margin-top: 6px;
      font-size: 12px;
    }
  }
}
</style>