CustomizeFormButtonSelectUploadFile.vue
4.67 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
<template>
<div>
<div class="hidden-input">
<input ref="fileInputRef" accept="image/*" type="file" @change="onFileChange">
</div>
<div class="uploadPage">
<div v-if="(getValidationRules().maxCount || 3) > pickerValue.length" class="uploadBox"
@click="handlePickFile()">
<PlusOutlined class="plusBox" />
<div class="uploadTxt">
上传图片
</div>
</div>
<div v-for="(url, index) in pickerValue" :key="index" class="uploadBox">
<!-- 删除按钮 -->
<div class="deleteBox" title="点击删除" @click="handleDelete(url)">
<CloseCircleOutlined class="closeIcon" />
</div>
<!-- 图片 -->
<a-image class="uploadBoxImg" :src="url" />
</div>
</div>
</div>
</template>
<script lang="ts">
import { Component, Prop, Vue, Watch } from "vue-facing-decorator";
import { uploadFile } from "@/utils/upload.js";
import { PlusOutlined, CloseCircleOutlined } from "@ant-design/icons-vue";
import { message } from "ant-design-vue";
@Component({ components: { PlusOutlined, CloseCircleOutlined } })
export default class CustomizeFormButtonSelectUploadFile extends Vue {
@Prop({ default: () => { } })
item: any = {};
pickerValue: any = [];
// item.resultStatus 变化时,清空选择项
@Watch('item.resultStatus', { deep: true, immediate: true })
watchItem(newValue: any, oldValue: any): void {
if (newValue) {
this.pickerValue = [];
this.updateData();
}
}
mounted() {
this.initData();
}
// 初始化数据
initData() {
this.pickerValue = this.item?.inspectionResultVO?.inspectionValueList?.filter(item => item.valueText && item.interactionType == "image_upload")?.map(item => item.valueText) || [];
this.updateData();
}
getValidationRules() {
return JSON.parse(this.item.validationRules || "{}");
}
//点击上传文件
handlePickFile() {
this.$refs.fileInputRef?.click();
}
async onFileChange(ev: Event) {
const input = ev.target as HTMLInputElement;
const file = input.files?.[0];
input.value = "";
if (!file) return;
const maxSize10MB = 10 * 1024 * 1024;
const maxSize100MB = 100 * 1024 * 1024;
if (this.item.interactionType == "image_upload" && file.size > maxSize10MB) {
return message.error("文件大小不能超过10MB");
}
if (this.item.interactionType == "video_upload" && file.size > maxSize100MB) {
return message.error("文件大小不能超过100MB");
}
try {
const res = await uploadFile(file);
const url = (typeof res.aliyunAddress === "string" && res.aliyunAddress) || (typeof res.url === "string" && res.url) || "";
message.success("上传成功");
this.pickerValue.push(url);
this.updateData();
} catch (error: unknown) {
const msg = error instanceof Error ? error.message : String(error);
message.error(`上传失败:${msg}`);
} finally {
}
}
handleDelete(item: string) {
this.pickerValue = this.pickerValue.filter((i) => i !== item);
this.updateData();
}
updateData() {
if (this.pickerValue.length > 0) {
this.$emit("update", JSON.stringify(this.pickerValue));
} else {
this.$emit("update", "");
}
}
}
</script>
<style scoped lang="less">
.hidden-input {
position: absolute;
width: 0;
height: 0;
pointer-events: none;
opacity: 0;
}
.uploadPage {
float: left;
width: calc(100% - 24px);
margin-left: 6px;
}
.uploadBox {
position: relative;
float: left;
width: 88px;
height: 88px;
margin-right: 12px;
margin-bottom: 16px;
background: #f5f7fa;
border: 1px solid #e1e3e6;
border-radius: 6px;
cursor: pointer;
}
.plusBox {
float: left;
margin-top: 22px;
margin-right: 36px;
margin-left: 36px;
font-size: 12px;
color: #9BA3AD;
}
.closeIcon {
font-size: 20px;
color: #999999;
}
.uploadTxt {
float: left;
width: 100%;
height: 20px;
margin-top: 8px;
font-size: 12px;
font-weight: 400;
line-height: 20px;
color: #969aa3;
text-align: center;
}
.uploadBoxImg {
width: 100%;
height: 100%;
z-index: 9;
border-radius: 8px;
}
.deleteBox {
position: absolute;
top: 2px;
right: 2px;
z-index: 999;
width: 20px;
height: 20px;
cursor: pointer;
}
</style>