TransferImageUpload.vue
11 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<template>
<div class="transfer-image-upload">
<!-- 隐藏的input -->
<input
ref="fileInputRef"
type="file"
accept="image/jpeg,image/jpg,image/png,image/jpeg"
:multiple="!slotMode"
class="hidden-input"
@change="onFileChange"
/>
<!-- 槽位模式:每个槽位独立上传一张,按钮文案为「上传+标题」 -->
<div v-if="slotMode" class="slot-list">
<div
v-for="(title, slotIndex) in slotTitles"
:key="slotIndex"
class="slot-item"
>
<div v-if="slottedImages[slotIndex]" class="upload-item slot-upload-item">
<a-image :src="slottedImages[slotIndex]" class="image-preview" />
<div class="delete-icon" @click="handleSlotDelete(slotIndex)">
<CloseCircleOutlined />
</div>
</div>
<div
v-else-if="!readonly"
class="upload-btn slot-upload-btn"
@click="handleSlotUploadClick(slotIndex)"
>
<PlusOutlined class="plus-icon" />
<div class="upload-text">{{ title ? `上传${title}` : (uploadText || '上传图片') }}</div>
</div>
<div v-else class="upload-placeholder">
暂无图片
</div>
</div>
</div>
<!-- 普通模式:保持原有样式 -->
<div v-else class="upload-list">
<div
v-for="(url, index) in imageList"
:key="index"
class="upload-item"
>
<a-image :src="url" class="image-preview" />
<div class="delete-icon" @click="handleDelete(index)">
<CloseCircleOutlined />
</div>
</div>
<div
v-if="imageList.length < maxCount && !readonly"
class="upload-btn"
@click="handleUploadClick"
>
<PlusOutlined class="plus-icon" />
<div class="upload-text">{{ uploadText || '上传图片' }}</div>
</div>
</div>
<div v-if="hint" class="upload-hint">{{ hint }}</div>
</div>
</template>
<script lang="ts">
import { Component, Prop, Vue, Watch } from "vue-facing-decorator";
import { PlusOutlined, CloseCircleOutlined } from "@ant-design/icons-vue";
import { message } from "ant-design-vue";
import { uploadFile, isLikelyImageFile } from "@/utils/upload.js";
@Component({
name: "TransferImageUpload",
components: { PlusOutlined, CloseCircleOutlined }
})
export default class TransferImageUpload extends Vue {
@Prop({ type: Array, default: () => [] })
value!: any[];
@Prop({ type: Number, default: 10 })
maxCount!: number;
@Prop({ type: Number, default: 10 })
maxSize!: number;
@Prop({ type: String, default: "" })
uploadText!: string;
@Prop({ type: String, default: "" })
hint!: string;
@Prop({ type: Boolean, default: false })
readonly!: boolean;
@Prop({ type: String, default: "auction/transfer" })
directory!: string;
@Prop({ type: Boolean, default: false })
slotMode!: boolean;
@Prop({ type: Array, default: () => [] })
slotTitles!: string[];
imageList: string[] = [];
slottedImages: (string | null)[] = [];
uploading = false;
currentSlotIndex: number = -1;
@Watch("slotTitles", { immediate: true })
onSlotTitlesChange(titles: string[]) {
if (!this.slotMode) return;
const len = (titles || []).length;
const next: (string | null)[] = new Array(len).fill(null);
for (let i = 0; i < Math.min(len, this.slottedImages.length); i++) {
next[i] = this.slottedImages[i] || null;
}
this.slottedImages = next;
}
@Watch("value", { deep: true, immediate: true })
onValueChange(val: any[]) {
const urls: string[] = (!val || val.length === 0)
? []
: val.map((item: any) => {
if (typeof item === "string") return item;
return item.url || item.aliyunAddress || "";
}).filter(Boolean);
if (this.slotMode) {
const len = (this.slotTitles || []).length || urls.length;
const next: (string | null)[] = new Array(Math.max(len, urls.length)).fill(null);
urls.forEach((u, i) => { next[i] = u; });
this.slottedImages = next;
this.imageList = this.slottedImages.filter((x): x is string => !!x);
} else {
this.imageList = urls;
}
}
handleUploadClick() {
if (this.readonly || this.uploading) return;
(this.$refs.fileInputRef as HTMLInputElement)?.click();
}
handleSlotUploadClick(slotIndex: number) {
if (this.readonly || this.uploading) return;
this.currentSlotIndex = slotIndex;
(this.$refs.fileInputRef as HTMLInputElement)?.click();
}
async onFileChange(ev: Event) {
const input = ev.target as HTMLInputElement;
const files = Array.from(input.files || []);
input.value = "";
if (files.length === 0) return;
if (this.slotMode) {
await this.handleSlotFile(files[0]);
this.currentSlotIndex = -1;
return;
}
if (this.imageList.length + files.length > this.maxCount) {
message.warning(`最多上传${this.maxCount}张图片`);
return;
}
for (const file of files) {
if (!isLikelyImageFile(file)) {
message.error(`${file.name} 不是有效的图片文件`);
return;
}
if (file.size > this.maxSize * 1024 * 1024) {
message.error(`${file.name} 大小超过${this.maxSize}MB`);
return;
}
}
try {
this.uploading = true;
const uploadPromises = files.map(file => uploadFile(file, { dir: this.directory }));
const results = await Promise.all(uploadPromises);
const newUrls = results.map(res => res.aliyunAddress || res.url || "").filter(Boolean);
this.imageList = [...this.imageList, ...newUrls];
this.emitChange();
message.success("上传成功");
} catch (error: any) {
message.error(`上传失败:${error.message || error}`);
} finally {
this.uploading = false;
}
}
async handleSlotFile(file: File) {
if (!file) return;
const slotIndex = this.currentSlotIndex;
if (slotIndex < 0) return;
if (!isLikelyImageFile(file)) {
message.error(`${file.name} 不是有效的图片文件`);
return;
}
if (file.size > this.maxSize * 1024 * 1024) {
message.error(`${file.name} 大小超过${this.maxSize}MB`);
return;
}
try {
this.uploading = true;
const res = await uploadFile(file, { dir: this.directory });
const url = res.aliyunAddress || res.url || "";
if (!url) throw new Error("上传返回地址为空");
const next = [...this.slottedImages];
next[slotIndex] = url;
this.slottedImages = next;
this.imageList = next.filter((x): x is string => !!x);
this.emitChange();
message.success("上传成功");
} catch (error: any) {
message.error(`上传失败:${error.message || error}`);
} finally {
this.uploading = false;
}
}
handleDelete(index: number) {
if (this.readonly) return;
this.imageList.splice(index, 1);
this.emitChange();
}
handleSlotDelete(slotIndex: number) {
if (this.readonly) return;
const next = [...this.slottedImages];
next[slotIndex] = null;
this.slottedImages = next;
this.imageList = next.filter((x): x is string => !!x);
this.emitChange();
}
emitChange() {
const urls = this.slotMode
? this.slottedImages.filter((x): x is string => !!x)
: this.imageList;
const fileList = urls.map(url => ({
url,
uid: `${Date.now()}_${Math.random()}`,
name: url.split("/").pop() || "",
status: "done"
}));
this.$emit("update:value", fileList);
this.$emit("change", fileList);
}
}
</script>
<style scoped lang="less">
.transfer-image-upload {
width: 100%;
.hidden-input {
position: absolute;
width: 0;
height: 0;
pointer-events: none;
opacity: 0;
}
.upload-list {
display: flex;
flex-wrap: wrap;
gap: 12px;
}
.slot-list {
display: flex;
flex-wrap: wrap;
gap: 16px;
}
.slot-item {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 8px;
.slot-label {
font-size: 13px;
color: #333;
font-weight: 500;
line-height: 1;
}
}
.slot-upload-item,
.slot-upload-btn,
.upload-placeholder {
width: 120px;
height: 120px;
}
.upload-placeholder {
display: flex;
align-items: center;
justify-content: center;
color: #bfbfbf;
font-size: 12px;
border: 1px dashed #e1e3e6;
border-radius: 6px;
background: #fafafa;
}
.upload-item {
position: relative;
width: 88px;
height: 88px;
border: 1px solid #e1e3e6;
border-radius: 6px;
overflow: hidden;
background: #f5f7fa;
.image-preview {
width: 100%;
height: 100%;
object-fit: cover;
}
.delete-icon {
position: absolute;
top: 2px;
right: 2px;
width: 20px;
height: 20px;
display: flex;
align-items: center;
justify-content: center;
background: rgba(255, 255, 255, 0.9);
border-radius: 50%;
cursor: pointer;
z-index: 10;
&:hover {
color: #ff4d4f;
}
}
}
.upload-btn {
width: 88px;
height: 88px;
border: 1px dashed #d9d9d9;
border-radius: 6px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
cursor: pointer;
background: #fafafa;
transition: all 0.3s;
&:hover {
border-color: #1890ff;
}
.plus-icon {
font-size: 20px;
color: #8c8c8c;
margin-bottom: 4px;
}
.upload-text {
font-size: 12px;
color: #8c8c8c;
}
}
.upload-hint {
color: #999;
font-size: 12px;
margin-top: 8px;
}
}
</style>