index.vue
7.07 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
<template>
<view class="min-h-screen bg-gray-50">
<!-- Header -->
<!-- <view class="bg-white px-4 py-3 flex items-center justify-between border-b border-gray-100">
<view class="flex items-center" @click="goBack">
<Left size="20" class="text-gray-600" />
<text class="ml-2 text-lg font-medium">家庭相册</text>
</view>
</view> -->
<!-- Album Grid -->
<view class="p-4">
<view class="grid grid-cols-2 gap-3">
<view
v-for="(item, index) in albumList"
:key="index"
class="relative aspect-square rounded-lg overflow-hidden bg-white shadow-sm"
@click="handleItemClick(item, index)"
>
<!-- 图片封面 -->
<image
:src="item.type === 'video' ? item.thumbnail : item.url"
class="w-full h-full object-cover"
mode="aspectFill"
/>
<!-- 视频播放标识 -->
<view
v-if="item.type === 'video'"
class="absolute top-2 left-2 px-2 py-1 bg-black bg-opacity-70 rounded text-white text-xs"
>
视频
</view>
<!-- 视频时长 -->
<!-- <view
v-if="item.type === 'video'"
class="absolute bottom-2 right-2 bg-black bg-opacity-60 text-white text-xs px-2 py-1 rounded"
>
{{ formatDuration(item.duration) }}
</view> -->
</view>
</view>
<!-- 空状态 -->
<view v-if="albumList.length === 0" class="flex flex-col items-center justify-center py-20">
<Photograph size="48" class="text-gray-300 mb-4" />
<text class="text-gray-400">暂无相册内容</text>
</view>
</view>
<!-- 图片预览 -->
<nut-image-preview
v-model:show="previewVisible"
:images="previewImages"
:init-no="previewIndex"
@close="closePreview"
/>
<!-- 视频播放器 -->
<view
v-if="videoVisible"
class="fixed inset-0 bg-black"
style="z-index: 9999;"
@click="closeVideo"
>
<!-- 关闭按钮 -->
<view
@click.stop="closeVideo"
class="absolute top-4 right-4 w-10 h-10 bg-black bg-opacity-50 rounded-full flex items-center justify-center"
style="z-index: 10000;"
>
<Close size="24" class="text-white" />
</view>
<!-- 视频播放器 -->
<video
v-if="currentVideo"
:id="'album-video-' + videoId"
:src="currentVideo.url"
:poster="currentVideo.thumbnail"
:controls="true"
:autoplay="false"
:show-center-play-btn="true"
:show-play-btn="true"
:object-fit="'contain'"
:show-fullscreen-btn="true"
style="width: 100vw; height: 100vh; position: absolute; top: 0; left: 0;"
@click.stop
@play="handleVideoPlay"
@pause="handleVideoPause"
@error="handleVideoError"
@fullscreenchange="handleFullscreenChange"
/>
</view>
</view>
</template>
<script setup>
import { ref, onMounted } from 'vue';
import Taro from '@tarojs/taro';
import { Left, Service, Photograph, Close } from '@nutui/icons-vue-taro';
// 响应式数据
const albumList = ref([]);
const previewVisible = ref(false);
const previewImages = ref([]);
const previewIndex = ref(0);
const videoVisible = ref(false);
const currentVideo = ref(null);
const videoId = ref(Date.now());
/**
* 页面加载时设置标题和初始化数据
*/
onMounted(() => {
Taro.setNavigationBarTitle({ title: '家庭相册' });
initMockData();
});
/**
* 初始化模拟数据
*/
const initMockData = () => {
albumList.value = [
{
type: 'image',
url: 'https://img.yzcdn.cn/vant/cat.jpeg',
createTime: '2024-01-15 10:30'
},
{
type: 'video',
url: 'https://vjs.zencdn.net/v/oceans.mp4',
thumbnail: 'https://img.yzcdn.cn/vant/apple-1.jpg',
duration: 125, // 秒
createTime: '2024-01-14 16:20'
},
{
type: 'image',
url: 'https://img.yzcdn.cn/vant/apple-2.jpg',
createTime: '2024-01-13 14:15'
},
{
type: 'image',
url: 'https://img.yzcdn.cn/vant/apple-3.jpg',
createTime: '2024-01-12 09:45'
},
{
type: 'video',
url: 'https://vjs.zencdn.net/v/oceans.mp4',
thumbnail: 'https://img.yzcdn.cn/vant/apple-4.jpg',
duration: 30,
createTime: '2024-01-11 18:30'
},
{
type: 'image',
url: 'https://img.yzcdn.cn/vant/tree.jpg',
createTime: '2024-01-10 12:00'
},
{
type: 'video',
url: 'https://vjs.zencdn.net/v/oceans.mp4',
thumbnail: 'https://img.yzcdn.cn/vant/leaf.jpg',
duration: 60,
createTime: '2024-01-09 15:20'
},
{
type: 'image',
url: 'https://img.yzcdn.cn/vant/sand.jpg',
createTime: '2024-01-08 11:10'
},
{
type: 'image',
url: 'https://img.yzcdn.cn/vant/sand.jpg',
createTime: '2024-01-08 11:10'
},
];
};
/**
* 处理项目点击事件
* @param {Object} item - 相册项目
* @param {number} index - 项目索引
*/
const handleItemClick = (item, index) => {
if (item.type === 'image') {
// 图片预览
const imageItems = albumList.value.filter(item => item.type === 'image');
previewImages.value = imageItems.map(img => ({ src: img.url }));
// 计算当前图片在图片列表中的索引
const imageIndex = imageItems.findIndex(img => img.url === item.url);
previewIndex.value = imageIndex;
previewVisible.value = true;
} else if (item.type === 'video') {
// 视频播放
currentVideo.value = item;
videoId.value = Date.now(); // 生成新的视频ID
videoVisible.value = true;
}
};
/**
* 格式化视频时长
* @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')}`;
};
/**
* 关闭图片预览
*/
const closePreview = () => {
previewVisible.value = false;
};
/**
* 关闭视频播放
*/
const closeVideo = () => {
videoVisible.value = false;
currentVideo.value = null;
};
/**
* 处理视频播放
*/
const handleVideoPlay = () => {
console.log('视频开始播放');
};
/**
* 处理视频暂停
*/
const handleVideoPause = () => {
console.log('视频暂停播放');
};
/**
* 处理全屏状态变化
* @param {Event} event - 全屏事件
*/
const handleFullscreenChange = (event) => {
console.log('全屏状态变化:', event.detail);
};
/**
* 处理视频播放错误
* @param {Event} error - 错误事件
*/
const handleVideoError = (error) => {
console.error('视频播放错误:', error);
Taro.showToast({
title: '视频播放失败',
icon: 'error',
duration: 2000
});
// 关闭视频弹框
closeVideo();
};
/**
* 返回上一页
*/
const goBack = () => {
Taro.navigateBack();
};
</script>
<style scoped>
.grid {
display: grid;
}
.grid-cols-2 {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
.gap-3 {
gap: 0.75rem;
}
.aspect-square {
aspect-ratio: 1 / 1;
}
</style>