Showing
4 changed files
with
38 additions
and
26 deletions
| ... | @@ -202,6 +202,10 @@ const _sfc_main = create({ | ... | @@ -202,6 +202,10 @@ const _sfc_main = create({ |
| 202 | type: Array, | 202 | type: Array, |
| 203 | default: () => ['image', 'video', 'mix'], | 203 | default: () => ['image', 'video', 'mix'], |
| 204 | }, | 204 | }, |
| 205 | + imageType: { | ||
| 206 | + type: Array, | ||
| 207 | + default: () => [], | ||
| 208 | + }, | ||
| 205 | camera: { | 209 | camera: { |
| 206 | type: String, | 210 | type: String, |
| 207 | default: 'back', | 211 | default: 'back', |
| ... | @@ -248,6 +252,7 @@ const _sfc_main = create({ | ... | @@ -248,6 +252,7 @@ const _sfc_main = create({ |
| 248 | 'delete', | 252 | 'delete', |
| 249 | 'update:fileList', | 253 | 'update:fileList', |
| 250 | 'file-item-click', | 254 | 'file-item-click', |
| 255 | + 'image-type-error', | ||
| 251 | ], | 256 | ], |
| 252 | setup(props, { emit }) { | 257 | setup(props, { emit }) { |
| 253 | const fileList = reactive(props.fileList) | 258 | const fileList = reactive(props.fileList) |
| ... | @@ -475,9 +480,18 @@ const _sfc_main = create({ | ... | @@ -475,9 +480,18 @@ const _sfc_main = create({ |
| 475 | const maximize = props.maximize * 1 | 480 | const maximize = props.maximize * 1 |
| 476 | const oversizes = new Array() | 481 | const oversizes = new Array() |
| 477 | files = files.filter((file) => { | 482 | files = files.filter((file) => { |
| 483 | + if (Taro.getEnv() != 'WEAPP') { | ||
| 484 | + file.type = file.originalFileObj.name.split('.').pop() | ||
| 485 | + } else { | ||
| 486 | + file.type = file.tempFilePath.split('.').pop() | ||
| 487 | + } | ||
| 478 | if (file.size > maximize) { | 488 | if (file.size > maximize) { |
| 479 | oversizes.push(file) | 489 | oversizes.push(file) |
| 480 | return false | 490 | return false |
| 491 | + } else if (!props.imageType.includes(file.type)) { | ||
| 492 | + // 控制文件类型上传 | ||
| 493 | + emit('image-type-error', file.type) | ||
| 494 | + return false | ||
| 481 | } else { | 495 | } else { |
| 482 | return true | 496 | return true |
| 483 | } | 497 | } | ... | ... |
| 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: 2023-04-12 10:59:48 | 4 | + * @LastEditTime: 2023-04-12 14:19:07 |
| 5 | * @FilePath: /custom_form/src/components/FileUploaderField/index.vue | 5 | * @FilePath: /custom_form/src/components/FileUploaderField/index.vue |
| 6 | * @Description: 文件上传控件 | 6 | * @Description: 文件上传控件 |
| 7 | --> | 7 | --> |
| ... | @@ -20,19 +20,6 @@ | ... | @@ -20,19 +20,6 @@ |
| 20 | v-html="item.component_props.note" | 20 | v-html="item.component_props.note" |
| 21 | style="font-size: 13px; margin-left: 1rem; color: gray; padding-bottom: 0.5rem; padding-top: 0.25rem; white-space: pre-wrap;" | 21 | style="font-size: 13px; margin-left: 1rem; color: gray; padding-bottom: 0.5rem; padding-top: 0.25rem; white-space: pre-wrap;" |
| 22 | /> | 22 | /> |
| 23 | - <!-- <div> | ||
| 24 | - <p | ||
| 25 | - v-for="(file, index) in fileList" | ||
| 26 | - :key="index" | ||
| 27 | - style="padding-left: 1rem; margin-bottom: 0.5rem" | ||
| 28 | - > | ||
| 29 | - <p style="font-size: 1rem; word-break: break-all; margin-right: 0.75rem;"> | ||
| 30 | - <span>{{ index + 1 }}. {{ file.name }} {{ (file.size / 1024 / 1024).toFixed(2) }}MB</span> | ||
| 31 | - | ||
| 32 | - <span style="color: #e32525; font-size: 0.85rem" @click="beforeDelete(file)">移除</span> | ||
| 33 | - </p> | ||
| 34 | - </p> | ||
| 35 | - </div> --> | ||
| 36 | <div style="padding: 1rem; padding-top: 0.5rem;"> | 23 | <div style="padding: 1rem; padding-top: 0.5rem;"> |
| 37 | <nut-uploader | 24 | <nut-uploader |
| 38 | :name="item.name" | 25 | :name="item.name" |
| ... | @@ -49,7 +36,7 @@ | ... | @@ -49,7 +36,7 @@ |
| 49 | @failure="uploadFailure" | 36 | @failure="uploadFailure" |
| 50 | @delete="onDelete" | 37 | @delete="onDelete" |
| 51 | @change="onChange" | 38 | @change="onChange" |
| 52 | - @file-item-click="fileItemClick" | 39 | + @image-type-error="imageTypeError" |
| 53 | > | 40 | > |
| 54 | <nut-button shape="square" type="primary"> | 41 | <nut-button shape="square" type="primary"> |
| 55 | <template #icon> | 42 | <template #icon> |
| ... | @@ -96,9 +83,12 @@ const HideShow = computed(() => { | ... | @@ -96,9 +83,12 @@ const HideShow = computed(() => { |
| 96 | 83 | ||
| 97 | const emit = defineEmits(["active"]); | 84 | const emit = defineEmits(["active"]); |
| 98 | 85 | ||
| 86 | +// // 固定类型限制 | ||
| 87 | +// const imageTypes = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'psd', 'tif']; | ||
| 88 | + | ||
| 99 | // // 文件类型中文页面显示 | 89 | // // 文件类型中文页面显示 |
| 100 | // const type_text = computed(() => { | 90 | // const type_text = computed(() => { |
| 101 | -// return props.item.component_props.file_type; | 91 | +// return imageTypes.join('/'); |
| 102 | // }); | 92 | // }); |
| 103 | 93 | ||
| 104 | // 上传文件集合 | 94 | // 上传文件集合 |
| ... | @@ -121,12 +111,18 @@ const onOversize = (files) => { | ... | @@ -121,12 +111,18 @@ const onOversize = (files) => { |
| 121 | toast_type.value = 'warn'; | 111 | toast_type.value = 'warn'; |
| 122 | }; | 112 | }; |
| 123 | 113 | ||
| 114 | +const onStart = (options) => { | ||
| 115 | + // console.warn(options); | ||
| 116 | +} | ||
| 117 | + | ||
| 124 | // 自定义上传逻辑 | 118 | // 自定义上传逻辑 |
| 125 | const beforeXhrUpload = async (xhr, options) => { | 119 | const beforeXhrUpload = async (xhr, options) => { |
| 120 | + const imgObj = defaultFileList.value[defaultFileList.value.length - 1]; | ||
| 121 | + // 判断上传文件格式 | ||
| 122 | + imgObj.type = imgObj.type ? imgObj.type : imgObj.name.split(".").pop(); | ||
| 126 | // H5环境 | 123 | // H5环境 |
| 127 | if (process.env.TARO_ENV === 'h5') { | 124 | if (process.env.TARO_ENV === 'h5') { |
| 128 | // 把本地路径转换成file实体 | 125 | // 把本地路径转换成file实体 |
| 129 | - const imgObj = defaultFileList.value[defaultFileList.value.length - 1]; | ||
| 130 | const imgBlob = await fetch(imgObj.url).then(r => r.blob()); | 126 | const imgBlob = await fetch(imgObj.url).then(r => r.blob()); |
| 131 | const imgFile = new File([imgBlob], imgObj.name , { type: imgObj.type }); | 127 | const imgFile = new File([imgBlob], imgObj.name , { type: imgObj.type }); |
| 132 | // 上传返回file数据结构 | 128 | // 上传返回file数据结构 |
| ... | @@ -146,7 +142,6 @@ const beforeXhrUpload = async (xhr, options) => { | ... | @@ -146,7 +142,6 @@ const beforeXhrUpload = async (xhr, options) => { |
| 146 | loading.value = false; | 142 | loading.value = false; |
| 147 | } | 143 | } |
| 148 | } else { | 144 | } else { |
| 149 | - const imgObj = defaultFileList.value[defaultFileList.value.length - 1]; | ||
| 150 | const fs = Taro.getFileSystemManager() | 145 | const fs = Taro.getFileSystemManager() |
| 151 | fs.getFileInfo({ | 146 | fs.getFileInfo({ |
| 152 | filePath: imgObj.url, | 147 | filePath: imgObj.url, |
| ... | @@ -250,9 +245,12 @@ const onDelete = ({ file }) => { | ... | @@ -250,9 +245,12 @@ const onDelete = ({ file }) => { |
| 250 | // 完整数据回调到表单上 | 245 | // 完整数据回调到表单上 |
| 251 | emit("active", props.item.value); | 246 | emit("active", props.item.value); |
| 252 | } | 247 | } |
| 253 | -// 上传成功,点击队列项回调 | 248 | + |
| 254 | -const fileItemClick = (fileItem) => { | 249 | +// |
| 255 | - console.warn(fileItem); | 250 | +const imageTypeError = (file) => { |
| 251 | + toast_msg.value = '请上传指定格式' | ||
| 252 | + toast_show.value = true; | ||
| 253 | + toast_type.value = 'warn'; | ||
| 256 | } | 254 | } |
| 257 | 255 | ||
| 258 | const onChange = ({ fileList }) => { | 256 | const onChange = ({ fileList }) => { |
| ... | @@ -379,7 +377,7 @@ defineExpose({ validFileUploader }); | ... | @@ -379,7 +377,7 @@ defineExpose({ validFileUploader }); |
| 379 | } | 377 | } |
| 380 | 378 | ||
| 381 | .type-text { | 379 | .type-text { |
| 382 | - font-size: 0.9rem; | 380 | + font-size: 13px; |
| 383 | margin-left: 1rem; | 381 | margin-left: 1rem; |
| 384 | padding-bottom: 1rem; | 382 | padding-bottom: 1rem; |
| 385 | color: gray; | 383 | color: gray; | ... | ... |
This diff is collapsed. Click to expand it.
| ... | @@ -8,7 +8,7 @@ import AreaPickerField from '@/components/AreaPickerField/index.vue' | ... | @@ -8,7 +8,7 @@ import AreaPickerField from '@/components/AreaPickerField/index.vue' |
| 8 | import DatePickerField from '@/components/DatePickerField/index.vue' | 8 | import DatePickerField from '@/components/DatePickerField/index.vue' |
| 9 | import TimePickerField from '@/components/TimePickerField/index.vue' | 9 | import TimePickerField from '@/components/TimePickerField/index.vue' |
| 10 | import DateTimePickerField from '@/components/DateTimePickerField/index.vue' | 10 | import DateTimePickerField from '@/components/DateTimePickerField/index.vue' |
| 11 | -// import ImageUploaderField from '@/components/ImageUploaderField/index.vue' | 11 | +import ImageUploaderField from '@/components/ImageUploaderField/index.vue' |
| 12 | import FileUploaderField from '@/components/FileUploaderField/index.vue' | 12 | import FileUploaderField from '@/components/FileUploaderField/index.vue' |
| 13 | import PhoneField from '@/components/PhoneField/index.vue' | 13 | import PhoneField from '@/components/PhoneField/index.vue' |
| 14 | import EmailField from '@/components/EmailField/index.vue' | 14 | import EmailField from '@/components/EmailField/index.vue' |
| ... | @@ -105,9 +105,9 @@ export function createComponentType(data) { | ... | @@ -105,9 +105,9 @@ export function createComponentType(data) { |
| 105 | if (item.component_props.tag === 'datetime') { | 105 | if (item.component_props.tag === 'datetime') { |
| 106 | item.component = DateTimePickerField | 106 | item.component = DateTimePickerField |
| 107 | } | 107 | } |
| 108 | - // if (item.component_props.tag === 'image_uploader') { | 108 | + if (item.component_props.tag === 'image_uploader') { |
| 109 | - // item.component = ImageUploaderField | 109 | + item.component = ImageUploaderField |
| 110 | - // } | 110 | + } |
| 111 | if (item.component_props.tag === 'file_uploader') { | 111 | if (item.component_props.tag === 'file_uploader') { |
| 112 | item.component = FileUploaderField | 112 | item.component = FileUploaderField |
| 113 | } | 113 | } | ... | ... |
-
Please register or login to post a comment