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

This commit is contained in:
jeremygan2021
2026-03-22 22:21:02 +08:00
parent 94333b61b6
commit 41d6991d5c
7 changed files with 125 additions and 66 deletions

View File

@@ -1,7 +1,7 @@
import { View, Text, Button, Image, Input, Textarea, Picker } from '@tarojs/components'
import Taro, { useLoad, useShareAppMessage, useShareTimeline, useRouter } from '@tarojs/taro'
import { useState } from 'react'
import { getProjectDetail, createProject, updateProject, uploadProjectFile, submitProject, uploadMedia, getCompetitions } from '../../api'
import { getProjectDetail, createProject, updateProject, uploadProjectFile, submitProject, uploadMedia, getCompetitions, deleteProjectFile } from '../../api'
import './project.scss'
export default function ProjectEdit() {
@@ -89,21 +89,14 @@ export default function ProjectEdit() {
Taro.showLoading({ title: '上传中...' })
console.log('Uploading cover image:', tempFilePaths[0])
const res = await uploadMedia(tempFilePaths[0], 'image')
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)
handleInput('cover_image_url', res.file)
Taro.hideLoading()
Taro.showToast({ title: '上传成功', icon: 'success' })
} catch (e) {
} catch (e: any) {
Taro.hideLoading()
console.error('Cover upload error:', e)
Taro.showToast({ title: '上传失败', icon: 'none' })
const errorMsg = e.response?.error || e.message || '上传失败'
Taro.showToast({ title: errorMsg, icon: 'none' })
}
}
@@ -120,52 +113,25 @@ 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 - use file_url_display or file_url
const fileUrl = result.file_url_display || result.file_url || ''
// Update file list
setProject(prev => ({
...prev,
files: [...(prev.files || []), {
...result,
url: fileUrl
}]
files: [...(prev.files || []), result]
}))
Taro.hideLoading()
Taro.showToast({ title: '上传成功', icon: 'success' })
} catch (e) {
Taro.hideLoading()
console.error('Upload error:', e)
console.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.
@@ -295,10 +261,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: 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)}>
{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' }}>
<Text className='file-name' style={{ flex: 1, fontSize: '14px', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{file.name || '未知文件'}</Text>
<Text style={{ color: '#1890ff', fontSize: '12px', marginLeft: '10px' }}></Text>
{/* <Text className='delete' style={{ color: 'red', marginLeft: '10px' }} onClick={() => handleDeleteFile(file.id)}>删除</Text> */}
</View>
))}
{(!project.files || project.files.length === 0) && <Text style={{ color: '#999', fontSize: '12px' }}> (PDF/PPT/)</Text>}