This commit is contained in:
@@ -94,14 +94,40 @@ 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) {
|
||||
const data = JSON.parse(res.data)
|
||||
console.log('Upload success, data:', data)
|
||||
if (data.error) {
|
||||
const error = new Error(data.error) as any
|
||||
error.response = data
|
||||
throw error
|
||||
}
|
||||
return data
|
||||
}
|
||||
console.error('Upload failed:', res)
|
||||
throw new Error(`Upload failed: ${res.statusCode}`)
|
||||
const error = new Error('Upload failed') as any
|
||||
error.statusCode = res.statusCode
|
||||
throw error
|
||||
})
|
||||
}
|
||||
|
||||
export const deleteProjectFile = (fileId: number) => {
|
||||
const BASE_URL = (typeof process !== 'undefined' && process.env && process.env.TARO_APP_API_URL) || 'https://market.quant-speed.com/api'
|
||||
return Taro.request({
|
||||
url: `${BASE_URL}/competition/files/${fileId}/`,
|
||||
method: 'DELETE',
|
||||
header: {
|
||||
'Authorization': `Bearer ${Taro.getStorageSync('token')}`
|
||||
}
|
||||
}).then(res => {
|
||||
if (res.statusCode >= 200 && res.statusCode < 300) {
|
||||
return true
|
||||
}
|
||||
const error = new Error('Delete failed') as any
|
||||
error.statusCode = res.statusCode
|
||||
try {
|
||||
const data = JSON.parse(res.data)
|
||||
error.response = data
|
||||
} catch {}
|
||||
throw error
|
||||
})
|
||||
}
|
||||
|
||||
@@ -120,9 +146,21 @@ export const uploadMedia = (filePath: string, type: 'image' | 'video') => {
|
||||
}
|
||||
}).then(res => {
|
||||
if (res.statusCode >= 200 && res.statusCode < 300) {
|
||||
return JSON.parse(res.data)
|
||||
const data = JSON.parse(res.data)
|
||||
if (data.error) {
|
||||
const error = new Error(data.error) as any
|
||||
error.response = data
|
||||
throw error
|
||||
}
|
||||
return data
|
||||
}
|
||||
throw new Error('Upload failed')
|
||||
const error = new Error('Upload failed') as any
|
||||
error.statusCode = res.statusCode
|
||||
try {
|
||||
const data = JSON.parse(res.data)
|
||||
error.response = data
|
||||
} catch {}
|
||||
throw error
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -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>}
|
||||
|
||||
@@ -79,12 +79,15 @@ const CreateTopic = () => {
|
||||
setContent(prev => prev + insertText)
|
||||
|
||||
Taro.hideLoading()
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
console.error(error)
|
||||
Taro.hideLoading()
|
||||
// Only toast if it's an error, not cancel
|
||||
if (error.errMsg && error.errMsg.indexOf('cancel') === -1) {
|
||||
Taro.showToast({ title: '上传失败', icon: 'none' })
|
||||
const errorMsg = error.response?.error || error.message || '上传失败'
|
||||
Taro.showToast({ title: errorMsg, icon: 'none' })
|
||||
} else if (!error.errMsg) {
|
||||
const errorMsg = error.response?.error || error.message || '上传失败'
|
||||
Taro.showToast({ title: errorMsg, icon: 'none' })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,10 +170,11 @@ const ForumDetail = () => {
|
||||
setReplyContent(prev => prev + insertText)
|
||||
|
||||
Taro.hideLoading()
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
console.error(error)
|
||||
Taro.hideLoading()
|
||||
Taro.showToast({ title: '上传失败', icon: 'none' })
|
||||
const errorMsg = error.response?.error || error.message || '上传失败'
|
||||
Taro.showToast({ title: errorMsg, icon: 'none' })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user