Prechádzať zdrojové kódy

feat:完成行为记录存储

白白 1 rok pred
rodič
commit
6c3e8efc1d

+ 25 - 0
src/apis/behavior.ts

@@ -0,0 +1,25 @@
+import axiosInstance from "@/apis/axios.config";
+
+const PREFIX = "/api/behaviorRecord"
+
+const bevaviorApis = {
+    selectBehaviorRecord: (assignmentId :number) => {
+        return axiosInstance.get(`${PREFIX}`, {
+            params: {
+                assignmentId
+            }
+        })
+    },
+    addBehaviorRecord: (assignmentId :number, file) => {
+        const formData = new FormData();
+        formData.append("assignmentId", assignmentId.toString())
+        formData.append('file', file);
+        return axiosInstance.post(`${PREFIX}`, formData, {
+            headers: {
+                'Content-Type': 'multipart/form-data'
+            }
+        })
+    }
+}
+
+export default bevaviorApis;

+ 3 - 1
src/apis/index.ts

@@ -10,6 +10,7 @@ import fileApis from "@/apis/file";
 import evaluationApis from "@/apis/evaluation";
 import emailApis from "@/apis/email";
 import authApis from "@/apis/auth";
+import bevaviorApis from "@/apis/behavior";
 
 
 const apis = {
@@ -24,7 +25,8 @@ const apis = {
     ...courseApis,
     ...fileApis,
     ...evaluationApis,
-    ...emailApis
+    ...emailApis,
+    ...bevaviorApis,
 }
 
 // hook

+ 1 - 1
src/components/QuillEditor/Editor.vue

@@ -105,7 +105,7 @@
           ...opsObj,
           docLength: quill.getLength() - 1,
           letterCount: (quill.getText().match(/[a-zA-Z\d\u4e00-\u9fa5]/g) || []).length,
-          key: args["0"].ops.find(op => 'insert' in op).insert,
+          key: args["0"].ops.find(op => 'insert' in op).insert || null
         })
       }
     });

+ 28 - 12
src/components/QuillEditor/QuillEditor.vue

@@ -17,6 +17,14 @@
   import emitter from '@/utils/emitter';
   import dayjs from "dayjs";
   import * as XLSX from 'xlsx'
+  import useApis from "@/apis";
+  import {useRoute} from "vue-router";
+
+  const apis = useApis()
+  // 获取当前路由对象
+  const route = useRoute();
+  // 从路由对象中提取 id 参数
+  const assignmentId = Number(route.params.assignmentId);
 
   let range = reactive({})
   let lastChange = reactive({})
@@ -184,14 +192,14 @@
         delete keyEventMap[key];
       }
     }
+
+    // TODO:测试片段,需删除
+    emitter.emit('record-test', changeRecordList)
   }
 
   const download = () => {
-    //   const ws = XLSX.utils.json_to_sheet(flattenOps(changeRecordList))
     const ws = XLSX.utils.json_to_sheet(changeRecordList)
-    /* 新建空workbook */
     const wb = XLSX.utils.book_new()
-    /* 添加worksheet,当然你可以添加多个,这里我只添加一个 */
     XLSX.utils.book_append_sheet(wb, ws, 'result')
 
     const wbout = XLSX.write(wb, {
@@ -200,16 +208,23 @@
       type: 'array'
     })
 
-    let url = window.URL.createObjectURL(new Blob([wbout]))
-    let link = document.createElement('a')
-    link.style.display = 'none'
-    link.href = url
-    link.setAttribute('download', '测试数据' + '.xls')
-    document.body.appendChild(link)
-    link.click()
-    document.body.removeChild(link) //下载完成移除元素
-    window.URL.revokeObjectURL(url) //释放掉blob对象
+    // 创建 Blob 对象(使用正确的 MIME 类型)
+    const blob = new Blob([wbout], {
+      type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
+    })
 
+    // 创建 File 对象并设置文件名(添加时间戳防止重复)
+    const filename = `行为记录.xlsx`
+    const file = new File([blob], filename, { type: blob.type })
+    if(assignmentId != null){
+      apis.addBehaviorRecord(assignmentId, file)
+          .then(res => {
+            console.log(res.data.data)
+          })
+          .catch(err => {
+            console.log(err)
+          })
+    }
   }
 
   // 监听 changeRecordList 数组变化
@@ -223,6 +238,7 @@
 
   onUnmounted(() => {
     emitter.off("change-record")
+    download()
   })
 
 </script>