index2.vue
9.21 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
<template>
<div class="component-upload-image">
<el-row>
<draggable v-model="fileList" item-key="index" @end="draggableEnd">
<template #item="{ element }">
<div class="fileItem el-upload-list__item is-success animated">
<img
:src="element.url"
alt=""
class="el-upload-list__item-thumbnail"
>
<div class="hover-actions">
<span @click="handlePictureCardPreview(element)">
<el-icon><ZoomIn /></el-icon>
</span>
<span v-if="!disabled" @click="handleDelete(element)">
<el-icon><Delete /></el-icon>
</span>
</div>
</div>
</template>
</draggable>
<div v-if="!disabled" v-show="showUpload && fileList.length < limit" class="el-upload-list--picture-card">
<div class="el-upload--picture-card" @click="editCropper()">
<el-icon class="avatar-uploader-icon"><plus /></el-icon>
</div>
</div>
</el-row>
<!--预览-->
<el-dialog
v-model="dialogVisible"
title="预览"
width="80%"
append-to-body
>
<img
:src="dialogImageUrl"
style="display: block; width: 100%; margin: 0 auto"
>
</el-dialog>
<!-- 上传提示 -->
<div v-if="showUpload && fileList.length < limit" class="el-upload__tip">
请上传
<template v-if="fileSize">
大小不超过
<b style="color: #f56c6c">{{ fileSize }}MB</b>
</template>
<br>
<template v-if="fileType">
格式为
<b style="color: #f56c6c">{{ fileType.join("/") }}</b>
</template>
的文件
</div>
<!--裁剪-->
<el-dialog
v-model="open"
title="裁剪图片"
width="600px"
append-to-body
@opened="modalOpened"
@close="closeDialog"
>
<el-row>
<el-col :style="{ height: '350px' }">
<vue-cropper
v-if="visible"
ref="cropper"
:img="options.img"
:info="true"
:auto-crop="options.autoCrop"
:auto-crop-width="options.autoCropWidth"
:auto-crop-height="options.autoCropHeight"
:fixed-box="options.fixedBox"
output-type="png"
@realTime="realTime"
/>
</el-col>
</el-row>
<br>
<el-row>
<el-col :lg="2" :md="2">
<el-upload
action="#"
:http-request="requestUpload"
:show-file-list="false"
:before-upload="beforeUpload"
:accept="accept"
>
<el-button>
选择
<el-icon class="el-icon--right"><Upload /></el-icon>
</el-button>
</el-upload>
</el-col>
<el-col :lg="{ span: 1, offset: 3 }" :md="2">
<el-button icon="Plus" @click="changeScale(1)" />
</el-col>
<el-col :lg="{ span: 1, offset: 2 }" :md="2">
<el-button icon="Minus" @click="changeScale(-1)" />
</el-col>
<el-col :lg="{ span: 1, offset: 2 }" :md="2">
<el-button icon="RefreshLeft" @click="rotateLeft()" />
</el-col>
<el-col :lg="{ span: 1, offset: 2 }" :md="2">
<el-button icon="RefreshRight" @click="rotateRight()" />
</el-col>
<el-col :lg="{ span: 1, offset: 5 }" :md="2">
<el-button type="primary" @click="uploadImg()">提 交</el-button>
</el-col>
</el-row>
</el-dialog>
</div>
</template>
<script setup>
import 'vue-cropper/dist/index.css'
import { VueCropper } from 'vue-cropper'
import { computed, getCurrentInstance, ref, watch } from 'vue'
import { reactive } from '@vue/runtime-core'
import request from '@/utils/request'
import _ from 'lodash'
const props = defineProps({
modelValue: [String, Object, Array],
// 图片数量限制
limit: {
type: Number,
default: 5
},
// 大小限制(MB)
fileSize: {
type: Number,
default: 10
},
// 文件类型, 例如['png', 'jpg', 'jpeg']
fileType: {
type: Array,
default: () => ['png', 'jpg', 'jpeg']
},
cropWidth: {
type: Number,
default: 400
},
cropHeight: {
type: Number,
default: 300
},
disabled: {
type: Boolean,
default: false
}
})
const accept = computed(() => {
return _.map(props.fileType, (t) => {
if (t.indexOf('.') === 0) {
return t
} else {
return '.' + t
}
}).join(',')
})
const { proxy } = getCurrentInstance()
const emit = defineEmits(['update:modelValue'])
const baseUrl = import.meta.env.VITE_APP_BASE_API
const open = ref(false)
const visible = ref(false)
const dialogImageUrl = ref('')
const dialogVisible = ref(false)
const showUpload = ref(true)
// 图片裁剪数据
const options = reactive({
img: '', // 裁剪图片的地址
autoCrop: true, // 是否默认生成截图框
canScale: true, // 图片是否允许滚轮缩放
autoCropWidth: props.cropWidth, // 默认生成截图框宽度
autoCropHeight: props.cropHeight, // 默认生成截图框高度
fixedBox: false, // 固定截图框大小 不允许改变
previews: {}, // 预览数据,
visible: false
})
const fileList = ref([])
watch(
() => props.modelValue,
(val) => {
if (val) {
// 首先将值转为数组
const list = Array.isArray(val) ? val : props.modelValue.split(',')
// 然后将数组转为对象数组
fileList.value = list.map((item) => {
if (typeof item === 'string') {
if (item.indexOf('http') === -1) {
item = { url: baseUrl + item }
} else {
item = { url: item }
}
}
return item
})
} else {
fileList.value = []
return []
}
},
{ deep: true, immediate: true }
)
watch(
() => fileList.value.length,
(value) => {
showUpload.value = value !== props.limit
}, { deep: true, immediate: true }
)
/** 裁剪 */
function editCropper() {
open.value = true
}
/** 打开弹出层结束时的回调 */
function modalOpened() {
visible.value = true
}
/** 覆盖默认上传行为 */
function requestUpload() {}
/** 向左旋转 */
function rotateLeft() {
proxy.$refs.cropper.rotateLeft()
}
/** 向右旋转 */
function rotateRight() {
proxy.$refs.cropper.rotateRight()
}
/** 图片缩放 */
function changeScale(num) {
num = num || 1
proxy.$refs.cropper.changeScale(num)
}
/** 上传预处理 */
function beforeUpload(file) {
if (props.fileSize) {
const isLt = file.size / 1024 / 1024 < props.fileSize
if (!isLt) {
proxy.$modal.msgError(`上传图片大小不能超过 ${props.fileSize} MB!`)
return false
}
}
if (file.type.indexOf('image/') == -1) {
proxy.$modal.msgError(
'文件格式错误,请上传图片类型,如:JPG,PNG后缀的文件。'
)
} else {
const reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = (aa) => {
// console.log(aa)
options.img = reader.result
}
}
}
/** 上传图片 */
function uploadImg() {
proxy.$refs.cropper.getCropBlob((data) => {
const formData = new FormData()
formData.append('image', data)
request({
url: '/upload/uploadImgToLocalServer',
method: 'post',
data: formData
}).then((res) => {
open.value = false
options.img = res.msg
proxy.$modal.msgSuccess('上传成功')
visible.value = false
fileList.value.push({
url: options.img
})
emit('update:modelValue', listToString(fileList.value))
})
})
}
/** 实时预览 */
function realTime(data) {
options.previews = data
}
/** 关闭窗口 */
function closeDialog() {
options.img = ''
options.visible = false
}
// 对象转成指定字符串分隔
function listToString(list, separator = ',') {
let strs = ''
for (const i in list) {
if (undefined !== list[i].url && list[i].url.indexOf('blob:') !== 0) {
strs += list[i].url.replace(baseUrl, '') + separator
}
}
return strs != '' ? strs.substr(0, strs.length - 1) : ''
}
function draggableEnd() {
emit('update:modelValue', listToString(fileList.value))
}
// 预览
function handlePictureCardPreview(file) {
dialogImageUrl.value = file.url
dialogVisible.value = true
}
// 删除图片
function handleDelete(file) {
const findex = fileList.value.map((f) => f.url).indexOf(file.url)
fileList.value.splice(findex, 1)
emit('update:modelValue', listToString(fileList.value))
}
</script>
<style lang="scss" scoped>
// .el-upload--picture-card 控制加号部分
:deep(.hide .el-upload--picture-card) {
display: none;
}
.fileItem {
position: relative;
width: 150px;
height: 150px;
overflow: hidden;
margin-bottom: 10px;
margin-right: 10px;
}
.fileItem img {
width: 100%;
height: 100%;
object-fit: cover;
}
.fileItem .hover-actions {
background: rgba(0, 0, 0, 0.4);
width: 100%;
height: 100%;
position: absolute;
top: 100%;
left: 0;
color: #fff;
transition: top 0.2s;
text-align: center;
span {
cursor: pointer;
font-size: 26px;
padding: 10px;
margin: 50px 5px;
display: inline-block;
}
span:hover {
background: rgba(255, 255, 255, 0.4);
}
}
.fileItem:hover .hover-actions {
top: 0;
}
.component-upload-image > div > div {
display: flex;
}
</style>