<template>
|
<div>
|
<a-upload
|
v-model:fileList="fileList"
|
:limit="limit"
|
@change="handleChange"
|
@before-remove="beforeRemove"
|
image-preview
|
>
|
<template #upload-button>
|
<a-button
|
type="text"
|
style="border-radius: 24px"
|
@click="visibleChange"
|
>
|
<icon-attachment size="28" style="color: #0960bd" />
|
</a-button>
|
</template>
|
</a-upload>
|
</div>
|
</template>
|
|
<script setup>
|
import { computed, ref, onMounted, watch, watchEffect } from 'vue';
|
import { uploadAndParse } from '@/api/session';
|
import { useUserStore } from '@/store';
|
|
const userStore = useUserStore();
|
const props = defineProps({
|
limit: {
|
type: Number,
|
default: 3,
|
},
|
sessionId: String,
|
action: String, // 上传的服务器地址
|
url: String, //回显的文件地址
|
});
|
|
const emit = defineEmits([
|
'update:fileList',
|
'success',
|
'handleRemove',
|
'selectFileCallback',
|
]);
|
const urls = computed(() => props.url);
|
const fileList = ref([]);
|
|
watch(
|
() => [props.url, props.sessionId],
|
([newVal, newSessionId], [oldVal, oldSessionId]) => {
|
// if (newVal) {
|
// fileList.value = newVal.split(',').map((item) => ({
|
// uid: item,
|
// name: item,
|
// status: 'done',
|
// url: item,
|
// }));
|
// }
|
},
|
{
|
deep: true, // 开启深度监听
|
}
|
);
|
|
onMounted(() => {
|
if (urls.value) {
|
fileList.value = urls.value.split(',').map((item) => ({
|
uid: item,
|
name: item,
|
status: 'done',
|
url: item,
|
}));
|
}
|
});
|
|
// console.log(urls.value, 8988);
|
const beforeRemove = (file) => {
|
emit('handleRemove');
|
fileList.value = [];
|
};
|
|
const handleChange = (fileList) => {
|
// emit('update:fileList', fileList);
|
|
// const successFiles = fileList.filter((item) => item.status === 'done');
|
// if (successFiles.length > 0) {
|
// emit(
|
// 'success',
|
// successFiles.map((item) => item.response.data)
|
// );
|
// emit(
|
// 'selectFileCallback',
|
// successFiles.map((item) => item.response.data)
|
// );
|
const formData = new FormData();
|
for (let i = 0; i < fileList.length; i++) {
|
formData.append('file', fileList[i].file);
|
formData.append('conversation_id', props.sessionId);
|
// formData.append('parser_config', '');
|
// if (!parser_id.value) {
|
// formData.append(
|
// 'parser_id',
|
// getIconByExtension(successFiles[i].name)
|
// );
|
// } else {
|
// formData.append('parser_id', parser_id.value);
|
// }
|
}
|
uploadAndParse(formData).then((res) => {
|
onFileSelectedLoading.value = false;
|
if (res.code == 200) {
|
cancel();
|
// uploaditemList.value = [];
|
emit('selectFileCallback', uploaditemList.value);
|
Message.success('上传成功');
|
} else {
|
Message.error('上传失败');
|
}
|
});
|
// }
|
};
|
</script>
|