index.vue
8.85 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<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>