Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Hooke
/
data-table
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Authored by
hookehuyr
2022-09-06 15:00:52 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
10251db3a6c5aa49e9c091a005f627d5ec3f4163
10251db3
1 parent
4351d326
✨ feat: 新增多图片上传模块
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
8 deletions
src/components/ImageUploaderField/index.vue
src/views/index.vue
src/components/ImageUploaderField/index.vue
View file @
10251db
<!--
* @Date: 2022-08-31 16:16:49
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2022-0
8-31 16:3
9:08
* @LastEditTime: 2022-0
9-06 14:4
9:08
* @FilePath: /data-table/src/components/ImageUploaderField/index.vue
* @Description: 文件描述
-->
...
...
@@ -9,25 +9,48 @@
<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 />
<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) => {
if (file.type !== 'image/jpeg' && file.type !== 'image/png') {
Toast('请上传 jpg/png 格式图片');
return false;
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
true
;
return
flag
;
};
const afterRead = (file) => {
...
...
@@ -54,5 +77,12 @@ const fileList = ref([
color: red;
}
}
.type-text {
font-size: 0.9rem;
margin-left: 1rem;
padding-bottom: 1rem;
color: gray;
}
}
</style>
...
...
src/views/index.vue
View file @
10251db
<!--
* @Date: 2022-07-18 10:22:22
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2022-09-0
2 11:16:29
* @LastEditTime: 2022-09-0
6 14:41:04
* @FilePath: /data-table/src/views/index.vue
* @Description: 首页
-->
...
...
@@ -148,7 +148,9 @@ onMounted(() => {
value: '',
label: '图片上传',
component_props: {
name: 'image_uploader'
name: 'image_uploader',
image_type: ['jpg', 'png'],
multiple: false
}
}];
// 生成自定义组件
...
...
Please
register
or
login
to post a comment