sx_yjb_vue/src/components/video/index.vue

37 lines
1.1 KiB
Vue
Raw Normal View History

2025-09-09 09:31:50 +08:00
<template>
<el-dialog v-model="visible" title="视频" :append-to-body="appendToBody">
2025-09-24 20:09:25 +08:00
<!-- 原生video标签支持大部分基础格式如MP4 -->
<video controls :src="fnSrc(src)" autoplay></video>
2025-09-09 09:31:50 +08:00
</el-dialog>
</template>
<script setup>
import { useVModel } from "@vueuse/core";
2025-09-24 20:09:25 +08:00
import { watchEffect } from "vue";
2025-09-09 09:31:50 +08:00
const VITE_FILE_URL = import.meta.env.VITE_FILE_URL;
const props = defineProps({
2025-09-24 20:09:25 +08:00
src: { type: String, default: "" },
visible: { type: Boolean, required: true, default: false },
appendToBody: { type: Boolean, default: false },
autoplay: { type: Boolean, default: true },
2025-09-09 09:31:50 +08:00
});
const emits = defineEmits(["update:visible"]);
const visible = useVModel(props, "visible", emits);
2025-09-24 20:09:25 +08:00
2025-09-09 09:31:50 +08:00
const fnSrc = (src) => {
if (!src) return;
2025-09-24 20:09:25 +08:00
if (src.includes("http") || src.includes("https")) return src;
return VITE_FILE_URL + src;
2025-09-09 09:31:50 +08:00
};
2025-09-24 20:09:25 +08:00
2025-09-09 09:31:50 +08:00
watchEffect(() => {
2025-09-24 20:09:25 +08:00
const video = document.querySelector("video");
if (visible.value && video) {
video.play().catch((err) => console.error("自动播放失败:", err));
} else if (video) {
video.pause();
2025-09-09 09:31:50 +08:00
}
});
</script>