|
@@ -33,11 +33,10 @@
|
|
|
</div>
|
|
|
</template>
|
|
|
<script lang="ts" setup>
|
|
|
-import { PropType } from 'vue'
|
|
|
-
|
|
|
import { propTypes } from '@/utils/propTypes'
|
|
|
import { getAccessToken, getTenantId } from '@/utils/auth'
|
|
|
import type { UploadInstance, UploadUserFile, UploadProps, UploadRawFile } from 'element-plus'
|
|
|
+import { isArray, isString } from '@/utils/is'
|
|
|
|
|
|
defineOptions({ name: 'UploadFile' })
|
|
|
|
|
@@ -45,10 +44,7 @@ const message = useMessage() // 消息弹窗
|
|
|
const emit = defineEmits(['update:modelValue'])
|
|
|
|
|
|
const props = defineProps({
|
|
|
- modelValue: {
|
|
|
- type: Array as PropType<UploadUserFile[]>,
|
|
|
- required: true
|
|
|
- },
|
|
|
+ modelValue: propTypes.oneOfType<string | string[]>([String, Array<String>]).isRequired,
|
|
|
title: propTypes.string.def('文件上传'),
|
|
|
updateUrl: propTypes.string.def(import.meta.env.VITE_UPLOAD_URL),
|
|
|
fileType: propTypes.array.def(['doc', 'xls', 'ppt', 'txt', 'pdf']), // 文件类型, 例如['png', 'jpg', 'jpeg']
|
|
@@ -62,7 +58,7 @@ const props = defineProps({
|
|
|
const valueRef = ref(props.modelValue)
|
|
|
const uploadRef = ref<UploadInstance>()
|
|
|
const uploadList = ref<UploadUserFile[]>([])
|
|
|
-const fileList = ref<UploadUserFile[]>(props.modelValue)
|
|
|
+const fileList = ref<UploadUserFile[]>([])
|
|
|
const uploadNumber = ref<number>(0)
|
|
|
const uploadHeaders = ref({
|
|
|
Authorization: 'Bearer ' + getAccessToken(),
|
|
@@ -109,7 +105,7 @@ const handleFileSuccess: UploadProps['onSuccess'] = (res: any): void => {
|
|
|
fileList.value = fileList.value.concat(uploadList.value)
|
|
|
uploadList.value = []
|
|
|
uploadNumber.value = 0
|
|
|
- emit('update:modelValue', listToString(fileList.value))
|
|
|
+ emitUpdateModelValue()
|
|
|
}
|
|
|
}
|
|
|
// 文件数超出提示
|
|
@@ -125,20 +121,47 @@ const handleRemove = (file) => {
|
|
|
const findex = fileList.value.map((f) => f.name).indexOf(file.name)
|
|
|
if (findex > -1) {
|
|
|
fileList.value.splice(findex, 1)
|
|
|
- emit('update:modelValue', listToString(fileList.value))
|
|
|
+ emitUpdateModelValue()
|
|
|
}
|
|
|
}
|
|
|
const handlePreview: UploadProps['onPreview'] = (uploadFile) => {
|
|
|
console.log(uploadFile)
|
|
|
}
|
|
|
-// 对象转成指定字符串分隔
|
|
|
-const listToString = (list: UploadUserFile[], separator?: string) => {
|
|
|
- let strs = ''
|
|
|
- separator = separator || ','
|
|
|
- for (let i in list) {
|
|
|
- strs += list[i].url + separator
|
|
|
+
|
|
|
+// 监听模型绑定值变动
|
|
|
+watch(
|
|
|
+ () => props.modelValue,
|
|
|
+ () => {
|
|
|
+ const files: string[] = []
|
|
|
+ // 情况1:字符串
|
|
|
+ if (isString(props.modelValue)) {
|
|
|
+ // 情况1.1:逗号分隔的多值
|
|
|
+ if (props.modelValue.includes(',')) {
|
|
|
+ files.concat(props.modelValue.split(','))
|
|
|
+ } else {
|
|
|
+ files.push(props.modelValue)
|
|
|
+ }
|
|
|
+ } else if (isArray(props.modelValue)) {
|
|
|
+ // 情况2:字符串
|
|
|
+ files.concat(props.modelValue)
|
|
|
+ } else {
|
|
|
+ throw new Error('不支持的 modelValue 类型')
|
|
|
+ }
|
|
|
+ fileList.value = files.map((url: string) => {
|
|
|
+ return { url, name: url.substring(url.lastIndexOf('/') + 1) } as UploadUserFile
|
|
|
+ })
|
|
|
+ },
|
|
|
+ { immediate: true }
|
|
|
+)
|
|
|
+// 发送文件链接列表更新
|
|
|
+const emitUpdateModelValue = () => {
|
|
|
+ // 情况1:数组结果
|
|
|
+ let result: string | string[] = fileList.value.map((file) => file.url!)
|
|
|
+ // 情况2:逗号分隔的字符串
|
|
|
+ if (isString(props.modelValue)) {
|
|
|
+ result = result.join(',')
|
|
|
}
|
|
|
- return strs != '' ? strs.substr(0, strs.length - 1) : ''
|
|
|
+ emit('update:modelValue', result)
|
|
|
}
|
|
|
</script>
|
|
|
<style scoped lang="scss">
|