feat: 文件长传

This commit is contained in:
2026-03-02 13:40:34 +08:00
parent 50782b1e9c
commit c0fa621ecc
2 changed files with 28 additions and 18 deletions

View File

@@ -1,2 +1,9 @@
import request from '@/utils/request' import request from '@/utils/request'
export const uploadImage = (data: FormData) => {
return request({
url: '/api/file/upload',
method: 'POST',
data
})
}

View File

@@ -188,6 +188,7 @@
import restoreCloseIcon from '@/assets/images/tag-close.png' import restoreCloseIcon from '@/assets/images/tag-close.png'
import { createProject } from '@/api/agent' import { createProject } from '@/api/agent'
import { getStyleImage } from './style' import { getStyleImage } from './style'
import { uploadImage } from '@/api/upload'
// import Tag from './Tag.vue' // import Tag from './Tag.vue'
const router = useRouter() const router = useRouter()
@@ -221,18 +222,26 @@
// 处理文件选择 // 处理文件选择
const handleFileChange = (event: Event) => { const handleFileChange = (event: Event) => {
const input = event.target as HTMLInputElement const input = event.target as HTMLInputElement
if (input.files) { if (input.files) {
Array.from(input.files).forEach((file) => { Array.from(input.files).forEach((file) => {
// 只处理图片文件 // 只处理图片文件
if (file.type.startsWith('image/')) { if (file.type.startsWith('image/')) {
const reader = new FileReader() const formData = new FormData()
reader.onload = (e) => { formData.append('file', file)
uploadedImages.value.push({ uploadImage(formData).then((res) => {
url: e.target?.result as string, console.log(res)
name: file.name
}) const reader = new FileReader()
} reader.onload = (e) => {
reader.readAsDataURL(file) uploadedImages.value.push({
url: e.target?.result as string,
name: file.name,
path: res
})
}
reader.readAsDataURL(file)
})
} }
}) })
} }
@@ -293,7 +302,6 @@
imgClose.src = closeIcon as unknown as string imgClose.src = closeIcon as unknown as string
} }
textSpan.innerText = tagText textSpan.innerText = tagText
imgClose.addEventListener('click', (ev) => { imgClose.addEventListener('click', (ev) => {
@@ -467,19 +475,15 @@
} }
const handleSendAgent = async () => { const handleSendAgent = async () => {
console.log('发送信息--------')
if (props.generating) { if (props.generating) {
emits('pause') emits('pause')
return return
} }
if (!inputValue.value.trim()) return if (!inputValue.value.trim()) return
console.log('222222') const imageUrlList = uploadedImages.value.map((item) => item.path)
const payload = { text: inputValue.value.trim(), images: uploadedImages.value } const payload = { text: inputValue.value.trim(), images: imageUrlList }
console.log('准备发送 send 事件', payload)
emits('send', payload) emits('send', payload)
console.log('send 事件已发送')
// 发送后清空输入框 // 发送后清空输入框
if (editorRef.value) { if (editorRef.value) {
@@ -1056,15 +1060,14 @@
height: 1rem; height: 1rem;
cursor: pointer; cursor: pointer;
flex-shrink: 0; flex-shrink: 0;
&.restore{ &.restore {
width: 0.5rem; width: 0.5rem;
height: 0.5rem; height: 0.5rem;
} }
} }
.restore-icon{ .restore-icon {
width: 1.2rem; width: 1.2rem;
height: 1.2rem; height: 1.2rem;
} }
} }
</style> </style>