sx_yjb_vue/src/components/video/index.vue

56 lines
1.6 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 getEnvVar = (name) => {
try {
// 优先尝试 Vite 的环境变量
if (typeof import !== 'undefined' && import.meta && import.meta.env) {
return import.meta.env[name];
}
// 回退到 process.env
if (typeof process !== 'undefined' && process.env) {
return process.env[name];
}
return "";
} catch (error) {
console.warn(`获取环境变量 ${name} 时出错:`, error);
return "";
}
};
const VITE_FILE_URL = getEnvVar('VITE_FILE_URL') || "";
2025-09-09 09:31:50 +08:00
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>