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
<template>
<div class="doc-image-upload">
<div class="text-12 description">{{ description }}</div>
<div class="mt-2 py-3 text-center upload-btn" @click="fileSelect">
<doc-icon type="doc-upload" class="mr-2 text-16"/>
<span>{{ btText }}</span>
</div>
<div class="flex flex-wrap mt-4 image-box">
<div v-for="item in innerImage.filter(e => e.fileType != 'pdf')" :key="item.id"
class="item-img">
<DocImage :src="item.url" remove/>
</div>
<div v-for="item in innerImage.filter(e => e.fileType == 'pdf')" :key="item.id"
class="item-pdf">
<DocImage isPdf :src="item.url" name="pdf文件名称" remove/>
</div>
</div>
<input type="file"
accept=".jpg,.jpeg,.png,.pdf"
ref="fileElem"
style="display: none;"
@change="handleFiles"
:key="inputKey">
</div>
</template>
<script>
import DocImage from './DocImage.vue'
import { showToast } from 'vant'
const testUrl = 'https://beta-tumour.zmnyjk.com/chronic-admin/file-proxy/chronic/20241125/1732502450600434289.png?e=1732610606&token=yrkyCAltqk1WVrw1ZNWUl5F5gLxE0O8LJ0Vq4hzi:RYbssXLQIEX6KTcVk5u4tWaxb9o='
export default {
name: 'DocImageUpload',
components:{
DocImage
},
props: {
// 上传描述
description: { default: '温馨提示:请上传JPG、PNG格式图片,文件大小不超过5M' },
// 传入的img数据 { id, trueDownloadUrl }
imageData: { default: () => [] },
// 上传按键的文字
btText: { default: '上传图片' },
// 图片框大小
size: String,
// 上传最大数量
maxLength: { default: 1 },
lengthMessage: { default: '已达到图片上限,请移除后再上传' }
},
data() {
return {
// 内部图片对象
innerImage: [],
file: null,
spinning: false,
inputKey: '1',
// viewer显示
visible: false
}
},
computed: {
fileElem() {
return this.$refs['fileElem']
},
ids() {
return this.innerImage.map(e => e.id).join(',')
}
},
methods:{
init() {
if (this.imageData?.length) {
this.innerImage = [...this.imageData]
} else {
this.innerImage = [
{ fileType: 'pdf', id: 1, url: testUrl },
{ fileType: 'png', id: 2, url: testUrl },
{ fileType: 'png', id: 3, url: testUrl },
{ fileType: 'png', id: 4, url: testUrl },
]
}
},
fileSelect() {
console.log(this.innerImage, this.maxLength)
if (this.innerImage?.length >= this.maxLength) {
showToast(this.lengthMessage)
return
}
const dom = this.$refs['fileElem']
if (dom) {
dom.click()
}
},
upload() {
let formData = new FormData()
formData.append('file', this.file)
this.spinning = true
fileUpload(formData).then(res => {
// this.$message.success('上传成功')
let result = res.data || {}
this.$emit('upload', result)
this.addImage(result)
this.inputKey = Math.random().toString(16).substring(2, 6)
this.$emit('change', this.ids, this.innerImage)
this.formItemContext.onFieldChange()
}).finally(() => {
this.spinning = false
})
},
handleFiles() {
let files = this.$refs['fileElem'].files
if (files.length <= 0) {
showToast('未选中文件,请尝试重新选择')
return
}
if (files[0].size / 1024 / 1024 > 5) {
showToast('图片大小不能超过5M')
return
}
this.file = files[0]
this.upload()
},
addImage(data = {}) {
if (!data.id) {
console.warn('addImage 文件为空')
return
}
if (!this.innerImage.find(e => e.id === data.id)) {
this.innerImage.push(data)
}
},
removeImage(item) {
if (!item) return
this.innerImage = this.innerImage.filter(e => e.id != item.id)
this.$emit('change', this.ids, this.innerImage)
}
},
watch: {
imageData: {
handler(value) {
this.init()
},
immediate: true
}
}
}
</script>
<style lang="less" scoped>
.description {
color: #A5AEBE;
}
.upload-btn {
color: var(--van-primary-color);
border: 1px solid #eee;
background-color: #FAFAFA;
border-radius: 8px;
}
.image-box {
row-gap: 8px;
column-gap: 12px;
.item-img {
width: calc(33.3vw - 8px);
}
.item-pdf {
width: 100%;
}
>div {
width: calc(33.3vw - 8px);
position: relative;
border-radius: 2px;
img {
object-fit: contain;
}
}
}
</style>