|
@@ -539,7 +539,75 @@ const submitWork = async (slideIndex: number, atool: string, content: string, ty
|
|
|
})
|
|
|
console.log(res)
|
|
|
}
|
|
|
+// 文件上传到AWS S3的函数
|
|
|
+const uploadFile = (file: File): Promise<string> => {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ try {
|
|
|
+ // 检查AWS SDK是否可用
|
|
|
+ if (!(window as any).AWS) {
|
|
|
+ reject(new Error('AWS SDK not loaded'))
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ const credentials = {
|
|
|
+ accessKeyId: 'AKIATLPEDU37QV5CHLMH',
|
|
|
+ secretAccessKey: 'Q2SQw37HfolS7yeaR1Ndpy9Jl4E2YZKUuuy2muZR'
|
|
|
+ }
|
|
|
+
|
|
|
+ // 配置AWS
|
|
|
+ ;(window as any).AWS.config.update(credentials)
|
|
|
+ ;(window as any).AWS.config.region = 'cn-northwest-1'
|
|
|
+
|
|
|
+ // 创建S3实例
|
|
|
+ const bucket = new (window as any).AWS.S3({ params: { Bucket: 'ccrb' } })
|
|
|
+
|
|
|
+ if (file) {
|
|
|
+ // 生成唯一的文件名
|
|
|
+ const fileExtension = file.name.split('.').pop()
|
|
|
+ const fileName = `${file.name.split('.')[0]}_${Date.now()}.${fileExtension}`
|
|
|
+
|
|
|
+ const params = {
|
|
|
+ Key: fileName,
|
|
|
+ ContentType: file.type,
|
|
|
+ Body: file,
|
|
|
+ ACL: 'public-read'
|
|
|
+ }
|
|
|
+
|
|
|
+ const options = {
|
|
|
+ partSize: 2048 * 1024 * 1024, // 2GB分片
|
|
|
+ queueSize: 2,
|
|
|
+ leavePartsOnError: true
|
|
|
+ }
|
|
|
|
|
|
+ bucket
|
|
|
+ .upload(params, options)
|
|
|
+ .on('httpUploadProgress', (evt: any) => {
|
|
|
+ // 这里可以添加进度条逻辑
|
|
|
+ const progress = Math.round((evt.loaded * 100) / evt.total)
|
|
|
+ console.log(`Uploaded: ${progress}%`)
|
|
|
+ })
|
|
|
+ .send((err: any, data: any) => {
|
|
|
+ if (err) {
|
|
|
+ console.error('Upload failed:', err)
|
|
|
+ message.error('文件上传失败')
|
|
|
+ reject(err)
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ console.log('Upload successful:', data.Location)
|
|
|
+ resolve(data.Location)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ reject(new Error('No file provided'))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (error) {
|
|
|
+ console.error('Upload error:', error)
|
|
|
+ reject(error)
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|
|
|
// 作业提交功能(优化版)
|
|
|
const handleHomeworkSubmit = async () => {
|
|
|
console.log('作业提交按钮被点击')
|
|
@@ -628,9 +696,24 @@ const handleHomeworkSubmit = async () => {
|
|
|
quality: 0.95,
|
|
|
backgroundColor: '#ffffff'
|
|
|
})
|
|
|
-
|
|
|
+
|
|
|
+ // 将base64字符串转换为File对象
|
|
|
+ const base64ToFile = (base64String: string, filename: string): File => {
|
|
|
+ const arr = base64String.split(',')
|
|
|
+ const mime = arr[0].match(/:(.*?);/)?.[1] || 'image/png'
|
|
|
+ const bstr = atob(arr[1])
|
|
|
+ let n = bstr.length
|
|
|
+ const u8arr = new Uint8Array(n)
|
|
|
+ while (n--) {
|
|
|
+ u8arr[n] = bstr.charCodeAt(n)
|
|
|
+ }
|
|
|
+ return new File([u8arr], filename, { type: mime })
|
|
|
+ }
|
|
|
+
|
|
|
+ const imageFile = base64ToFile(imageData, `screenshot_${Date.now()}.png`)
|
|
|
+ const imageUrl = await uploadFile(imageFile)
|
|
|
// 提交截图
|
|
|
- await submitWork(slideIndex.value, '73', imageData, '1') // 73表示截图工具,21表示图片类型
|
|
|
+ await submitWork(slideIndex.value, '73', imageUrl, '1') // 73表示截图工具,21表示图片类型
|
|
|
message.success('页面截图提交成功')
|
|
|
hasSubmitWork = true
|
|
|
}
|