c293da23
刘淇
新园林init
|
1
|
<template>
|
9b30ab8c
刘淇
新增快速工单,原版
|
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
|
<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>
|
c293da23
刘淇
新园林init
|
29
|
</view>
|
9b30ab8c
刘淇
新增快速工单,原版
|
30
|
</up-upload>
|
c293da23
刘淇
新园林init
|
31
32
33
|
</view>
</template>
|
9b30ab8c
刘淇
新增快速工单,原版
|
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
|
<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
}
);
|
c293da23
刘淇
新园林init
|
65
|
|
9b30ab8c
刘淇
新增快速工单,原版
|
66
67
68
69
70
71
72
73
74
75
76
77
78
|
// 定义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;
}>();
|
c293da23
刘淇
新园林init
|
79
|
|
9b30ab8c
刘淇
新增快速工单,原版
|
80
81
|
// 内部维护的图片列表
const imageList = ref([...props.value]);
|
c293da23
刘淇
新园林init
|
82
|
|
9b30ab8c
刘淇
新增快速工单,原版
|
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
// 监听父组件传入的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);
|
c293da23
刘淇
新园林init
|
107
|
|
c293da23
刘淇
新园林init
|
108
|
try {
|
9b30ab8c
刘淇
新增快速工单,原版
|
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
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' });
}
|
c293da23
刘淇
新园林init
|
125
|
} catch (err) {
|
9b30ab8c
刘淇
新增快速工单,原版
|
126
127
128
129
|
emit('upload-error', err);
console.error('图片上传失败:', err);
} finally {
uni.hideLoading();
|
c293da23
刘淇
新园林init
|
130
131
132
|
}
};
|
9b30ab8c
刘淇
新增快速工单,原版
|
133
134
135
136
|
/**
* 删除图片
*/
const handleDelete = (index: number) => {
|
c293da23
刘淇
新园林init
|
137
|
imageList.value.splice(index, 1);
|
9b30ab8c
刘淇
新增快速工单,原版
|
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
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]
});
|
c293da23
刘淇
新园林init
|
152
153
154
|
};
</script>
|
9b30ab8c
刘淇
新增快速工单,原版
|
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
|
<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;
}
|
c293da23
刘淇
新园林init
|
200
|
</style>
|