lsc 3 天之前
父节点
当前提交
cf2ffaccc4
共有 1 个文件被更改,包括 46 次插入1 次删除
  1. 46 1
      src/views/Student/index.vue

+ 46 - 1
src/views/Student/index.vue

@@ -1116,12 +1116,27 @@ const handleHomeworkSubmit = async () => {
             throw new Error('未能获取到iframe元素,无法截图')
           }
 
+
+
           // 获取iframe内部的body元素(同源)
           if (
             iframeElement.contentWindow &&
             iframeElement.contentWindow.document &&
             iframeElement.contentWindow.document.body
           ) {
+            // 获取页面的所有注释节点
+            const comments = []
+            const childNodes = iframeElement.contentWindow.document.createTreeWalker(iframeElement.contentWindow.document.body, NodeFilter.SHOW_COMMENT, null)
+
+            while (childNodes.nextNode()) {
+              comments.push(childNodes.currentNode)
+            }
+
+            // 移除所有注释节点
+            comments.forEach(comment => {
+              comment?.parentNode?.removeChild(comment)
+            })
+            
             iframeBody = iframeElement.contentWindow.document.body as HTMLElement
           }
           else {
@@ -1163,7 +1178,9 @@ const handleHomeworkSubmit = async () => {
             }
             catch (htmlToImageError) {
               console.log('html-to-image也失败了,使用canvas绘制方案:', htmlToImageError)
-              
+              message.error('截图提交失败')
+              return
+              /*
               // 最后的备用方案:使用canvas绘制
               const canvas = document.createElement('canvas')
               const ctx = canvas.getContext('2d')
@@ -1225,8 +1242,36 @@ const handleHomeworkSubmit = async () => {
               }
               else {
                 throw new Error('无法创建canvas上下文')
+              }*/
+            }
+
+            // 将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', imageUrl, '1') // 73表示截图工具,21表示图片类型
+            message.success('页面截图提交成功')
+            hasSubmitWork = true
+          
+            // 发送作业提交成功的socket消息
+            sendMessage({
+              type: 'homework_submitted',
+              courseid: props.courseid,
+              slideIndex: slideIndex.value,
+              userid: props.userid
+            })
           }
         }
         catch (error) {