index.vue 8.85 KB
<template>
  <div
    class="preview_video_page"
    :style="boxStyle"
    v-if="src"
    @click="openVideo"
  >
    <!-- 封面:外部传入优先,没传则用抓到的首帧 -->
    <img v-if="realCover" class="video_cover" :src="realCover" :style="{ objectFit: coverFit }" alt="" />

    <!-- 播放按钮 -->
    <i class="el-icon-video-play video_play"></i>

    <!-- 角标:右上角 -->
    <span v-if="showFlag" class="video_flag">{{ flagText }}</span>

    <!-- 名称:左下角 -->
    <div v-if="name && showName" class="video_name">{{ name }}</div>

    <!-- 视频预览弹窗 开始 -->
    <el-dialog
      class="preview_video_dialog"
      :title="title"
      :visible.sync="videoVisible"
      :width="dialogWidth"
      append-to-body
      :close-on-click-modal="false"
      @close="closeVideo"
    >
      <div class="video_wrapper">
        <video
          v-if="videoVisible"
          ref="previewVideo"
          class="video_player"
          :src="src"
          :poster="realCover"
          controls
          autoplay
          playsinline
          preload="metadata"
        >
          您的浏览器不支持视频播放
        </video>
      </div>
    </el-dialog>
    <!-- 视频预览弹窗 end -->
  </div>

  <!-- 无视频:占位 -->
  <div v-else class="video_empty" :style="boxStyle">
    <slot name="empty">
      <i class="el-icon-video-camera"></i>
      <span class="state_text">{{ emptyText }}</span>
    </slot>
  </div>
</template>
<script>
import { isOssUrl, ossVideoCover } from "@/utils/oss";
import { capturePoster, cachePoster, getCachedPoster } from "@/utils/videoPoster";
/**
 * 公共视频组件(点击弹窗播放)
 *
 * props:
 *  src           String  视频地址
 *  cover         String  封面地址,不传则按下面优先级取封面
 *  captureCover  Boolean 无 cover 时是否自动抓帧(非 OSS 视频用),默认 true
 *  captureTime   Number  取第几秒的画面,默认 0.1(首帧常是黑场)
 *  ossProcess    Boolean OSS 链接是否用服务端截图作封面,默认 true
 *                        (优先级高于前端 canvas 抓帧,且无跨域问题)
 *  coverFit      String  封面图填充方式(CSS object-fit),默认 cover
 *                        (fill / contain / cover / none / scale-down)
 *  width/height  String  容器尺寸,默认 100%(撑满父级)
 *  name          String  名称,浮在左下角,不传则不显示
 *  showName      Boolean 是否显示名称,默认 true
 *  flagText      String  右上角角标文案,默认「视频」
 *  showFlag      Boolean 是否显示角标,默认 true
 *  title         String  弹窗标题,默认「视频预览」
 *  dialogWidth   String  弹窗宽度,默认 720px
 *  emptyText     String  无视频占位文案,默认「暂无视频」
 *
 * 用法:
 *  <preview-video :src="url" height="180px" name="开箱视频"></preview-video>
 *
 * 封面优先级:cover(外部传入) > ossCover(OSS 服务端截图) > poster(前端 canvas 首帧)
 *
 * 首帧封面注意:
 *  非 OSS 视频要抓帧需把视频画到 canvas 上导出,跨域视频必须服务端返回 CORS 头
 *  (Access-Control-Allow-Origin),否则画布被污染,toBlob 会抛 SecurityError。
 *  抓不到帧时静默降级为纯黑底 + 播放图标,不影响点击播放。
 *  OSS 视频直接用 x-oss-process 服务端截图,无跨域问题,优先走这条。
 */
export default {
  name: "PreviewVideo",
  props: {
    src: {
      type: String,
      default: "",
    },
    cover: {
      type: String,
      default: "",
    },
    captureCover: {
      type: Boolean,
      default: true,
    },
    captureTime: {
      type: Number,
      default: 0.1,
    },
    // OSS 链接是否用服务端截图作封面(优先级高于前端 canvas 抓帧)
    ossProcess: {
      type: Boolean,
      default: true,
    },
    // 封面图填充方式(对应 CSS object-fit),默认 cover 撑满裁切
    // 可选:fill / contain / cover / none / scale-down
    coverFit: {
      type: String,
      default: "cover",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
    },
    name: {
      type: String,
      default: "",
    },
    showName: {
      type: Boolean,
      default: true,
    },
    flagText: {
      type: String,
      default: "视频",
    },
    showFlag: {
      type: Boolean,
      default: true,
    },
    title: {
      type: String,
      default: "视频预览",
    },
    dialogWidth: {
      type: String,
      default: "720px",
    },
    emptyText: {
      type: String,
      default: "暂无视频",
    },
  },
  data() {
    return {
      videoVisible: false,
      poster: "", // 抓帧得到的封面(blob url)
    };
  },
  computed: {
    boxStyle() {
      return {
        width: this.width,
        height: this.height,
      };
    },
    // 封面优先级:外部 cover > OSS 服务端截图 > 前端 canvas 首帧
    realCover() {
      return this.cover || this.ossCover || this.poster;
    },
    // OSS 链接:用服务端截图作封面(无跨域问题,比 canvas 抓帧更稳)
    ossCover() {
      if (this.ossProcess && !this.cover && isOssUrl(this.src)) {
        return ossVideoCover(this.src);
      }
      return "";
    },
  },
  watch: {
    src: {
      handler: "updatePoster",
      immediate: true,
    },
    cover() {
      // 外部补了封面就以它为准,清掉抓帧结果
      this.poster = "";
    },
  },
  methods: {
    // 没有封面时抓视频首帧
    updatePoster() {
      this.poster = "";
      let src = this.src;
      if (!src || this.cover || !this.captureCover) {
        return;
      }
      // OSS 视频直接用服务端截图作封面,无需前端抓帧
      if (this.ossProcess && isOssUrl(src)) {
        return;
      }
      if (getCachedPoster(src)) {
        this.poster = getCachedPoster(src);
        return;
      }
      capturePoster(src, this.captureTime).then((url) => {
        // src 在这期间被换掉了就丢弃这次结果
        if (!url || this.src !== src) {
          return;
        }
        cachePoster(src, url);
        this.poster = url;
      });
    },

    // 打开弹窗播放
    openVideo() {
      this.videoVisible = true;
    },

    // 关闭弹窗,暂停播放
    closeVideo() {
      let video = this.$refs.previewVideo;
      if (video) {
        video.pause();
      }
      this.videoVisible = false;
    },
  },
};
</script>
<style lang="scss" scoped>
.preview_video_page {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  cursor: pointer;
  background: #000;
  border-radius: 4px;
  .video_cover {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
  .video_play {
    position: relative;
    z-index: 2;
    color: #fff;
    font-size: 48px;
    opacity: 0.85;
  }
  .video_flag {
    position: absolute;
    right: 8px;
    top: 8px;
    z-index: 2;
    padding: 2px 8px;
    font-size: 12px;
    color: #fff;
    background: rgba(0, 0, 0, 0.6);
    border-radius: 10px;
  }
  // 名称浮层:左下角,与角标同风格
  // pointer-events: none 让点击穿透,点名称区域也能触发播放
  .video_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;
  }
}
.preview_video_page:hover {
  .video_play {
    opacity: 1;
  }
}
// 弹窗内容会被 append-to-body 移到 body 下,这里不做嵌套,保证 scoped 命中
.video_wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000;
  .video_player {
    width: 100%;
    max-height: 460px;
    background: #000;
    outline: none;
  }
}
.video_empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #f5f7fa;
  color: #c0c4cc;
  font-size: 26px;
  border-radius: 4px;
  .state_text {
    margin-top: 6px;
    font-size: 12px;
  }
}
</style>
<style lang="scss">
.preview_video_dialog {
  // 顶部标题栏:克制的留白 + 一条细分隔线,结构清晰、稳重
  .el-dialog__header {
    position: relative;
    padding: 12px 22px;
    margin: 0;
    border-bottom: 1px solid #ebeef5;
    .el-dialog__title {
      font-size: 15px;
      font-weight: 500;
      letter-spacing: 0.5px;
      color: #1f2d3d;
    }
    // 关闭按钮与标题垂直居中对齐,留白更从容
    .el-dialog__headerbtn {
      top: 50%;
      transform: translateY(-50%);
      right: 18px;
    }
  }
  .el-dialog__body {
    padding: 0;
    background: #000;
  }
}
</style>