Showing
2 changed files
with
40 additions
and
8 deletions
| 1 | <!-- | 1 | <!-- |
| 2 | * @Date: 2022-08-31 16:16:49 | 2 | * @Date: 2022-08-31 16:16:49 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2022-08-31 16:39:08 | 4 | + * @LastEditTime: 2022-09-06 14:49:08 |
| 5 | * @FilePath: /data-table/src/components/ImageUploaderField/index.vue | 5 | * @FilePath: /data-table/src/components/ImageUploaderField/index.vue |
| 6 | * @Description: 文件描述 | 6 | * @Description: 文件描述 |
| 7 | --> | 7 | --> |
| ... | @@ -9,25 +9,48 @@ | ... | @@ -9,25 +9,48 @@ |
| 9 | <div class="image-uploader-field"> | 9 | <div class="image-uploader-field"> |
| 10 | <div class="label">{{ item.label }}<span v-if="item.required"> *</span></div> | 10 | <div class="label">{{ item.label }}<span v-if="item.required"> *</span></div> |
| 11 | <div style="padding: 1rem;"> | 11 | <div style="padding: 1rem;"> |
| 12 | - <van-uploader upload-icon="add" :before-read="beforeRead" :after-read="afterRead" v-model="fileList" multiple /> | 12 | + <van-uploader upload-icon="add" :before-read="beforeRead" :after-read="afterRead" v-model="fileList" |
| 13 | + :multiple="item.component_props.multiple" /> | ||
| 13 | </div> | 14 | </div> |
| 15 | + <div class="type-text">上传格式:{{ type_text }}</div> | ||
| 14 | </div> | 16 | </div> |
| 15 | </template> | 17 | </template> |
| 16 | 18 | ||
| 17 | <script setup> | 19 | <script setup> |
| 20 | +/** | ||
| 21 | + * 图片上传 | ||
| 22 | + * @param name[String] 组件名称 | ||
| 23 | + * @param image_type[Array] 图片上传类型 | ||
| 24 | + * @param multiple[Boolean] 图片多选 | ||
| 25 | +*/ | ||
| 18 | import { Toast } from 'vant'; | 26 | import { Toast } from 'vant'; |
| 27 | +import _ from 'lodash' | ||
| 19 | 28 | ||
| 20 | const props = defineProps({ | 29 | const props = defineProps({ |
| 21 | item: Object | 30 | item: Object |
| 22 | }) | 31 | }) |
| 23 | 32 | ||
| 33 | +const type_text = computed(() => { | ||
| 34 | + return props.item.component_props.image_type.join('/') | ||
| 35 | +}) | ||
| 36 | + | ||
| 24 | // 上传前置处理 | 37 | // 上传前置处理 |
| 25 | const beforeRead = (file) => { | 38 | const beforeRead = (file) => { |
| 26 | - if (file.type !== 'image/jpeg' && file.type !== 'image/png') { | 39 | + const image_types = _.map(props.item.component_props.image_type, item => `image/${item}`) |
| 27 | - Toast('请上传 jpg/png 格式图片'); | 40 | + let flag = true; |
| 28 | - return false; | 41 | + if (_.isArray(file)) { // 多张图片 |
| 42 | + const types = _.difference(_.uniq(_.map(file, item => item.type)), image_types); // 数组返回不能上传的类型 | ||
| 43 | + if (types.length) { | ||
| 44 | + flag = false; | ||
| 45 | + Toast('请上传指定格式图片'); | ||
| 46 | + } | ||
| 47 | + } else { | ||
| 48 | + if (!_.includes(image_types, file.type)) { | ||
| 49 | + Toast('请上传指定格式图片'); | ||
| 50 | + flag = false; | ||
| 51 | + } | ||
| 29 | } | 52 | } |
| 30 | - return true; | 53 | + return flag; |
| 31 | }; | 54 | }; |
| 32 | 55 | ||
| 33 | const afterRead = (file) => { | 56 | const afterRead = (file) => { |
| ... | @@ -54,5 +77,12 @@ const fileList = ref([ | ... | @@ -54,5 +77,12 @@ const fileList = ref([ |
| 54 | color: red; | 77 | color: red; |
| 55 | } | 78 | } |
| 56 | } | 79 | } |
| 80 | + | ||
| 81 | + .type-text { | ||
| 82 | + font-size: 0.9rem; | ||
| 83 | + margin-left: 1rem; | ||
| 84 | + padding-bottom: 1rem; | ||
| 85 | + color: gray; | ||
| 86 | + } | ||
| 57 | } | 87 | } |
| 58 | </style> | 88 | </style> | ... | ... |
| 1 | <!-- | 1 | <!-- |
| 2 | * @Date: 2022-07-18 10:22:22 | 2 | * @Date: 2022-07-18 10:22:22 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2022-09-02 11:16:29 | 4 | + * @LastEditTime: 2022-09-06 14:41:04 |
| 5 | * @FilePath: /data-table/src/views/index.vue | 5 | * @FilePath: /data-table/src/views/index.vue |
| 6 | * @Description: 首页 | 6 | * @Description: 首页 |
| 7 | --> | 7 | --> |
| ... | @@ -148,7 +148,9 @@ onMounted(() => { | ... | @@ -148,7 +148,9 @@ onMounted(() => { |
| 148 | value: '', | 148 | value: '', |
| 149 | label: '图片上传', | 149 | label: '图片上传', |
| 150 | component_props: { | 150 | component_props: { |
| 151 | - name: 'image_uploader' | 151 | + name: 'image_uploader', |
| 152 | + image_type: ['jpg', 'png'], | ||
| 153 | + multiple: false | ||
| 152 | } | 154 | } |
| 153 | }]; | 155 | }]; |
| 154 | // 生成自定义组件 | 156 | // 生成自定义组件 | ... | ... |
-
Please register or login to post a comment