upload-image.vue
4.66 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
<template>
<view class="upload-images-container">
<!-- 已上传图片预览(uView Album组件) -->
<up-album
v-if="imageList.length > 0"
:list="imageList"
:max-count="maxCount"
:show-delete="showDelete"
@delete="handleDelete"
@preview="handlePreview"
:border-radius="8"
:column="column"
:gap="15"
></up-album>
<!-- 上传按钮 -->
<up-upload
class="upload-btn"
:disabled="imageList.length >= maxCount"
:multiple="true"
:max-count="maxCount - imageList.length"
@after-read="handleAfterRead"
:previewFullImage="true"
>
<view class="upload-btn-inner">
<up-icon name="plus" color="#999" size="24"></up-icon>
<text class="upload-tips" v-if="imageList.length < maxCount">上传图片</text>
<text class="upload-tips" v-else>已达上限</text>
</view>
</up-upload>
</view>
</template>
<script setup lang="ts">
import { ref, watch, defineProps, defineEmits, withDefaults } from 'vue';
import { uploadImages } from '@/common/utils/upload.js';
// 定义Props
const props = withDefaults(
defineProps<{
// 已上传图片列表(双向绑定)
value: string[];
// 最大上传数量
maxCount?: number;
// 是否显示删除按钮
showDelete?: boolean;
// 相册列数
column?: number;
// 上传文件key名
fileKey?: string;
// 自定义请求头(覆盖默认)
header?: Record<string, string>;
// 上传失败是否继续(多图时)
ignoreError?: boolean;
}>(),
{
maxCount: 9,
showDelete: true,
column: 3,
fileKey: 'file',
ignoreError: true
}
);
// 定义Emits
const emit = defineEmits<{
// 双向绑定更新图片列表
(e: 'update:value', val: string[]): void;
// 单张/多张上传成功
(e: 'upload-success', urls: string[]): void;
// 上传失败
(e: 'upload-error', err: any): void;
// 删除图片
(e: 'delete-image', index: number): void;
// 预览图片
(e: 'preview-image', index: number): void;
}>();
// 内部维护的图片列表
const imageList = ref([...props.value]);
// 监听父组件传入的value变化
watch(
() => props.value,
(newVal) => {
imageList.value = [...newVal];
},
{ immediate: true }
);
// 监听内部列表变化,同步给父组件
watch(
() => imageList.value,
(newVal) => {
emit('update:value', [...newVal]);
},
{ deep: true }
);
/**
* 选择图片后触发(上传核心逻辑)
*/
const handleAfterRead = async (event: any) => {
// 获取选择的图片临时路径
const filePaths = event.file.map((item: any) => item.url);
try {
uni.showLoading({ title: '上传中...' });
// 调用公共上传方法
const uploadUrls = await uploadImages({
filePaths,
fileKey: props.fileKey,
header: props.header,
ignoreError: props.ignoreError
});
if (uploadUrls.length > 0) {
// 合并到已上传列表
imageList.value = [...imageList.value, ...uploadUrls];
// 通知父组件上传成功
emit('upload-success', uploadUrls);
uni.showToast({ title: `成功上传${uploadUrls.length}张图片`, icon: 'success' });
}
} catch (err) {
emit('upload-error', err);
console.error('图片上传失败:', err);
} finally {
uni.hideLoading();
}
};
/**
* 删除图片
*/
const handleDelete = (index: number) => {
imageList.value.splice(index, 1);
emit('delete-image', index);
uni.showToast({ title: '删除成功', icon: 'success' });
};
/**
* 预览图片
*/
const handlePreview = (index: number) => {
emit('preview-image', index);
// 原生预览(可选)
uni.previewImage({
urls: imageList.value,
current: imageList.value[index]
});
};
</script>
<style lang="scss" scoped>
.upload-images-container {
padding: 10rpx 0;
}
// 上传按钮样式
.upload-btn {
margin-top: 15rpx;
width: 200rpx;
height: 200rpx;
border: 1px dashed #e5e5e5;
border-radius: 8rpx;
display: flex;
align-items: center;
justify-content: center;
background-color: #f9f9f9;
.upload-btn-inner {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.upload-tips {
font-size: 24rpx;
color: #999;
margin-top: 10rpx;
}
}
// 适配Album组件样式
:deep(.u-album__item) {
border-radius: 8rpx !important;
overflow: hidden;
}
:deep(.u-album__delete) {
background-color: rgba(0, 0, 0, 0.5) !important;
border-radius: 50% !important;
width: 40rpx !important;
height: 40rpx !important;
top: 0 !important;
right: 0 !important;
}
</style>