index.vue
7.56 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
<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.is_my"
class="absolute top-2 right-2 px-2 py-1 bg-blue-500 bg-opacity-80 rounded text-white text-xs"
>
我的
</view> -->
<!-- 删除按钮 -->
<view
v-if="item.is_my"
@click.stop="handleDeleteClick(item)"
class="absolute bottom-2 right-2 px-2 py-1 bg-red-500 bg-opacity-80 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>
<!-- 视频播放器 -->
<view
v-if="videoVisible"
class="fixed inset-0 bg-black"
style="z-index: 9999;"
@click="closeVideo"
>
<!-- 关闭按钮 -->
<view
@tap.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: 50vh; position: absolute; top: 20vh; left: 0;"
@tap.stop
@play="handleVideoPlay"
@pause="handleVideoPause"
@error="handleVideoError"
@fullscreenchange="handleFullscreenChange"
/>
</view>
</view>
</template>
<script setup>
import { ref, onMounted } from 'vue';
import Taro, { useDidShow } from '@tarojs/taro';
import { Photograph, Close } from '@nutui/icons-vue-taro';
import { getPhotoListAPI, deletePhotoAPI } from '@/api/photo';
// 响应式数据
const albumList = ref([]);
// 视频播放相关状态
const videoVisible = ref(false);
const currentVideo = ref(null);
const videoId = ref(0);
/**
* 关闭视频播放器
*/
const closeVideo = () => {
videoVisible.value = false;
currentVideo.value = null;
};
/**
* 视频播放事件处理
*/
const handleVideoPlay = () => {
console.log('视频开始播放');
};
/**
* 视频暂停事件处理
*/
const handleVideoPause = () => {
console.log('视频暂停播放');
};
/**
* 视频全屏变化事件处理
*/
const handleFullscreenChange = () => {
console.log('视频全屏状态变化');
};
/**
* 视频错误事件处理
*/
const handleVideoError = (error) => {
console.error('视频播放错误:', error);
Taro.showToast({
title: '视频播放失败',
icon: 'none'
});
};
// 删除相关状态
const currentDeleteItem = ref(null);
/**
* 页面加载时设置标题和初始化数据
*/
onMounted(() => {
Taro.setNavigationBarTitle({ title: '家庭相册' });
// fetchAlbumList();
});
useDidShow(() => {
fetchAlbumList();
});
/**
* 获取相册列表数据
*/
const fetchAlbumList = async () => {
try {
Taro.showLoading({ title: '加载中...' });
const response = await getPhotoListAPI({ page: 0, limit: 50 });
if (response.code) {
// 转换接口数据格式为组件需要的格式
albumList.value = response.data.map(item => ({
id: item.id,
type: item.media_type === 'IMAGE' ? 'image' : 'video',
url: item.media_url,
thumbnail: item.thumbnail,
is_my: item.is_my,
media_type: item.media_type,
media_url: item.media_url
}));
}
} catch (error) {
console.error('获取相册列表失败:', error);
Taro.showToast({
title: '获取相册列表失败',
icon: 'none'
});
} finally {
Taro.hideLoading();
}
};
/**
* 处理项目点击事件
* @param {Object} item - 相册项目
* @param {number} index - 项目索引
*/
const handleItemClick = (item, index) => {
if (item.type === 'video') {
// 播放视频
currentVideo.value = {
url: item.url,
thumbnail: item.thumbnail
};
videoId.value = Date.now();
videoVisible.value = true;
} else {
// 预览图片
const imageUrls = albumList.value
.filter(media => media.type === 'image')
.map(media => media.url);
Taro.previewImage({
urls: imageUrls,
current: item.url
});
}
};
/**
* 处理删除按钮点击事件
* @param {Object} item - 要删除的相册项目
*/
const handleDeleteClick = (item) => {
currentDeleteItem.value = item;
Taro.showModal({
title: '删除确认',
content: '确定要删除这张照片吗?删除后无法恢复。',
success: (res) => {
if (res.confirm) {
confirmDelete();
} else if (res.cancel) {
cancelDelete();
}
}
});
};
/**
* 确认删除相册项目
*/
const confirmDelete = async () => {
if (!currentDeleteItem.value) return;
try {
Taro.showLoading({ title: '删除中...' });
const response = await deletePhotoAPI({ ids: [currentDeleteItem.value.id] });
if (response.code) {
// 从列表中移除已删除的项目
const index = albumList.value.findIndex(item => item.id === currentDeleteItem.value.id);
if (index > -1) {
albumList.value.splice(index, 1);
}
Taro.showToast({
title: '删除成功',
icon: 'success'
});
} else {
throw new Error(response.msg || '删除失败');
}
} catch (error) {
console.error('删除相册项目失败:', error);
Taro.showToast({
title: '删除失败',
icon: 'none'
});
} finally {
Taro.hideLoading();
currentDeleteItem.value = null;
}
};
/**
* 取消删除
*/
const cancelDelete = () => {
currentDeleteItem.value = null;
};
</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>