upload
All checks were successful
Deploy to Server / deploy (push) Successful in 14s

This commit is contained in:
jeremygan2021
2026-03-22 22:10:21 +08:00
parent 2104e7b7dc
commit 94333b61b6
3 changed files with 58 additions and 19 deletions

View File

@@ -94,10 +94,14 @@ export const uploadProjectFile = (filePath: string, projectId: number, fileName?
'Authorization': `Bearer ${Taro.getStorageSync('token')}`
}
}).then(res => {
console.log('Upload response:', res)
if (res.statusCode >= 200 && res.statusCode < 300) {
return JSON.parse(res.data)
const data = JSON.parse(res.data)
console.log('Upload success, data:', data)
return data
}
throw new Error('Upload failed')
console.error('Upload failed:', res)
throw new Error(`Upload failed: ${res.statusCode}`)
})
}

View File

@@ -89,12 +89,20 @@ export default function ProjectEdit() {
Taro.showLoading({ title: '上传中...' })
console.log('Uploading cover image:', tempFilePaths[0])
const res = await uploadMedia(tempFilePaths[0], 'image')
handleInput('cover_image_url', res.file) // 假设返回 { file: 'url...' }
console.log('Cover upload result:', res)
// res.file is the OSS URL
const coverUrl = res.file || res.file_url || res.url
console.log('Cover URL:', coverUrl)
handleInput('cover_image_url', coverUrl)
Taro.hideLoading()
Taro.showToast({ title: '上传成功', icon: 'success' })
} catch (e) {
Taro.hideLoading()
console.error('Cover upload error:', e)
Taro.showToast({ title: '上传失败', icon: 'none' })
}
}
@@ -112,25 +120,52 @@ export default function ProjectEdit() {
Taro.showLoading({ title: '上传中...' })
const file = tempFiles[0]
console.log('Uploading file:', file.name, 'path:', file.path)
// @ts-ignore
const result = await uploadProjectFile(file.path, project.id, file.name)
console.log('Upload result:', result)
// Update file list
// Update file list - use file_url_display or file_url
const fileUrl = result.file_url_display || result.file_url || ''
setProject(prev => ({
...prev,
files: [...(prev.files || []), result]
files: [...(prev.files || []), {
...result,
url: fileUrl
}]
}))
Taro.hideLoading()
Taro.showToast({ title: '上传成功', icon: 'success' })
} catch (e) {
Taro.hideLoading()
console.error(e)
console.error('Upload error:', e)
Taro.showToast({ title: '上传失败', icon: 'none' })
}
}
const handlePreviewFile = (file: any) => {
const url = file.url || file.file_url_display || file.file_url
if (!url) {
Taro.showToast({ title: '文件链接无效', icon: 'none' })
return
}
Taro.downloadFile({
url: url,
success: (res: any) => {
if (res.statusCode === 200) {
Taro.openDocument({ filePath: res.tempFilePath }).catch(() => {
Taro.showToast({ title: '无法打开文件', icon: 'none' })
})
}
},
fail: () => {
Taro.showToast({ title: '无法打开文件', icon: 'none' })
}
})
}
const handleDeleteFile = (fileId) => {
// API call to delete file not implemented yet? Or just remove from list?
// Usually we should call delete API. For now just remove from UI.
@@ -260,10 +295,10 @@ export default function ProjectEdit() {
<Button size='mini' style={{ margin: 0, fontSize: '12px' }} onClick={handleUploadFile}></Button>
</View>
<View className='file-list'>
{project.files && project.files.map((file, index) => (
<View key={index} className='file-item' style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', padding: '10px', background: '#f8f8f8', marginBottom: '8px', borderRadius: '4px' }}>
{project.files && project.files.map((file: any, index: number) => (
<View key={index} className='file-item' style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', padding: '10px', background: '#f8f8f8', marginBottom: '8px', borderRadius: '4px' }} onClick={() => handlePreviewFile(file)}>
<Text className='file-name' style={{ flex: 1, fontSize: '14px', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{file.name || '未知文件'}</Text>
{/* <Text className='delete' style={{ color: 'red', marginLeft: '10px' }} onClick={() => handleDeleteFile(file.id)}>删除</Text> */}
<Text style={{ color: '#1890ff', fontSize: '12px', marginLeft: '10px' }}></Text>
</View>
))}
{(!project.files || project.files.length === 0) && <Text style={{ color: '#999', fontSize: '12px' }}> (PDF/PPT/)</Text>}