index.vue
2.24 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
<!--
* @Date: 2022-08-31 16:16:49
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2022-09-06 14:49:08
* @FilePath: /data-table/src/components/ImageUploaderField/index.vue
* @Description: 文件描述
-->
<template>
<div class="image-uploader-field">
<div class="label">{{ item.label }}<span v-if="item.required"> *</span></div>
<div style="padding: 1rem;">
<van-uploader upload-icon="add" :before-read="beforeRead" :after-read="afterRead" v-model="fileList"
:multiple="item.component_props.multiple" />
</div>
<div class="type-text">上传格式:{{ type_text }}</div>
</div>
</template>
<script setup>
/**
* 图片上传
* @param name[String] 组件名称
* @param image_type[Array] 图片上传类型
* @param multiple[Boolean] 图片多选
*/
import { Toast } from 'vant';
import _ from 'lodash'
const props = defineProps({
item: Object
})
const type_text = computed(() => {
return props.item.component_props.image_type.join('/')
})
// 上传前置处理
const beforeRead = (file) => {
const image_types = _.map(props.item.component_props.image_type, item => `image/${item}`)
let flag = true;
if (_.isArray(file)) { // 多张图片
const types = _.difference(_.uniq(_.map(file, item => item.type)), image_types); // 数组返回不能上传的类型
if (types.length) {
flag = false;
Toast('请上传指定格式图片');
}
} else {
if (!_.includes(image_types, file.type)) {
Toast('请上传指定格式图片');
flag = false;
}
}
return flag;
};
const afterRead = (file) => {
// 此时可以自行将文件上传至服务器
console.log(file);
};
const fileList = ref([
{ url: 'https://fastly.jsdelivr.net/npm/@vant/assets/leaf.jpeg' },
// Uploader 根据文件后缀来判断是否为图片文件
// 如果图片 URL 中不包含类型信息,可以添加 isImage 标记来声明
// { url: 'https://cloud-image', isImage: true },
]);
</script>
<style lang="less" scoped>
.image-uploader-field {
.label {
padding: 1rem 1rem 0 1rem;
font-size: 0.9rem;
font-weight: bold;
span {
color: red;
}
}
.type-text {
font-size: 0.9rem;
margin-left: 1rem;
padding-bottom: 1rem;
color: gray;
}
}
</style>