useMediaPreview.js
6.96 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
import { ref } from 'vue';
import Taro from '@tarojs/taro';
/**
* 媒体预览 composable
* 提供图片和视频预览功能
*/
export function useMediaPreview() {
// 图片预览相关状态
const previewVisible = ref(false);
const previewImages = ref([]);
const previewIndex = ref(0);
// 视频预览相关状态
const videoVisible = ref(false);
const currentVideo = ref(null);
const videoId = ref(Date.now());
/**
* 处理媒体项目点击事件
* @param {Object} item - 媒体项目
* @param {number} item.id - 媒体ID
* @param {string} item.media_type - 媒体类型 IMAGE=图片, VIDEO=视频
* @param {string} item.media_url - 媒体文件URL
* @param {string} item.thumbnail - 缩略图URL
* @param {boolean} item.is_my - 是否是我的相册
* @param {Array} mediaList - 完整的媒体列表
*/
const handleMediaClick = (item, mediaList = []) => {
if (item.media_type === 'IMAGE') {
// 图片预览
const imageItems = mediaList.filter(media => media.media_type === 'IMAGE');
previewImages.value = imageItems.map(img => ({
src: img.media_url,
id: img.id,
thumbnail: img.thumbnail,
is_my: img.is_my
}));
// 计算当前图片在图片列表中的索引
const imageIndex = imageItems.findIndex(img => img.media_url === item.media_url);
previewIndex.value = imageIndex >= 0 ? imageIndex : 0;
previewVisible.value = true;
} else if (item.media_type === 'VIDEO') {
// 视频播放
currentVideo.value = {
...item,
url: item.media_url, // 兼容原有的url字段
type: 'video' // 兼容原有的type字段
};
videoId.value = Date.now(); // 生成新的视频ID
videoVisible.value = true;
}
};
/**
* 预览单张图片
* @param {string} imageUrl - 图片URL
*/
const previewSingleImage = (imageUrl) => {
previewImages.value = [{ src: imageUrl }];
previewIndex.value = 0;
previewVisible.value = true;
};
/**
* 预览多张图片
* @param {Array} images - 图片数组,可以是URL字符串数组或媒体对象数组
* @param {number} index - 初始显示的图片索引
*/
const previewMultipleImages = (images, index = 0) => {
previewImages.value = images.map(item => {
if (typeof item === 'string') {
// 兼容URL字符串数组
return { src: item };
} else {
// 处理媒体对象数组
return {
src: item.media_url || item.url,
id: item.id,
thumbnail: item.thumbnail,
is_my: item.is_my
};
}
});
previewIndex.value = index;
previewVisible.value = true;
};
/**
* 播放视频
* @param {Object} video - 视频对象
* @param {string} video.media_url - 视频URL
* @param {string} video.thumbnail - 缩略图URL
* @param {number} video.id - 视频ID
* @param {boolean} video.is_my - 是否是我的视频
*/
const playVideo = (video) => {
currentVideo.value = {
...video,
url: video.media_url || video.url, // 兼容新旧字段
type: 'video'
};
videoId.value = Date.now();
videoVisible.value = true;
};
/**
* 关闭图片预览
*/
const closePreview = () => {
previewVisible.value = false;
};
/**
* 关闭视频播放
*/
const closeVideo = () => {
videoVisible.value = false;
currentVideo.value = null;
};
/**
* 处理视频播放
*/
const handleVideoPlay = () => {
// 视频开始播放
};
/**
* 处理视频暂停
*/
const handleVideoPause = () => {
// 视频暂停播放
};
/**
* 处理全屏状态变化
*/
const handleFullscreenChange = () => {
// 全屏状态变化
};
/**
* 处理视频播放错误
* @param {Event} error - 错误事件
*/
const handleVideoError = (error) => {
console.error('视频播放错误:', error);
Taro.showToast({
title: '视频播放失败',
icon: 'error',
duration: 2000
});
// 关闭视频弹框
closeVideo();
};
/**
* 格式化视频时长
* @param {number} seconds - 秒数
* @returns {string} 格式化后的时长
*/
const formatDuration = (seconds) => {
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
return `${minutes}:${remainingSeconds.toString().padStart(2, '0')}`;
};
/**
* 判断媒体类型是否为图片
* @param {Object} media - 媒体对象
* @returns {boolean} 是否为图片
*/
const isImage = (media) => {
return media.media_type === 'IMAGE' || media.type === 'image';
};
/**
* 判断媒体类型是否为视频
* @param {Object} media - 媒体对象
* @returns {boolean} 是否为视频
*/
const isVideo = (media) => {
return media.media_type === 'VIDEO' || media.type === 'video';
};
/**
* 获取媒体URL
* @param {Object} media - 媒体对象
* @returns {string} 媒体URL
*/
const getMediaUrl = (media) => {
return media.media_url || media.url || '';
};
/**
* 获取缩略图URL
* @param {Object} media - 媒体对象
* @returns {string} 缩略图URL
*/
const getThumbnailUrl = (media) => {
return media.thumbnail || media.media_url || media.url || '';
};
/**
* 转换接口数据为预览格式
* @param {Array} mediaList - 接口返回的媒体列表
* @returns {Array} 转换后的媒体列表
*/
const transformMediaList = (mediaList = []) => {
return mediaList.map(item => ({
id: item.id,
type: item.media_type === 'IMAGE' ? 'image' : 'video',
media_type: item.media_type,
url: item.media_url,
media_url: item.media_url,
thumbnail: item.thumbnail,
is_my: item.is_my
}));
};
return {
// 状态
previewVisible,
previewImages,
previewIndex,
videoVisible,
currentVideo,
videoId,
// 方法
handleMediaClick,
previewSingleImage,
previewMultipleImages,
playVideo,
closePreview,
closeVideo,
handleVideoPlay,
handleVideoPause,
handleFullscreenChange,
handleVideoError,
formatDuration,
// 辅助方法
isImage,
isVideo,
getMediaUrl,
getThumbnailUrl,
transformMediaList
};
}