Przeglądaj źródła

fix:下载功能完善

廖彦舟 1 rok temu
rodzic
commit
87d5d52d82

+ 0 - 1
src/apis/axios.config.ts

@@ -11,7 +11,6 @@ const axiosInstance = axios.create({
         'Content-Type': 'application/json',
     },
 })
-
 //请求拦截器
 axiosInstance.interceptors.request.use(
     config => {

+ 2 - 0
src/apis/behavior.ts

@@ -48,6 +48,7 @@ const bevaviorApis = {
     },
     downLoadBehaviorData:(studentId:number,assignmentId :number)=>{
         return axiosInstance.get(`/api/export/behaviorOverview/excel`, {
+            responseType: 'blob',
             params: {
                 studentId,
                 assignmentId
@@ -56,6 +57,7 @@ const bevaviorApis = {
     },
     downLoadAllBehaviorData:(assignmentId :number)=>{
         return axiosInstance.get(`/api/export/behaviorOverview/excel/assignment`, {
+            responseType: 'blob',
             params: {
                 assignmentId
             }

+ 40 - 0
src/views/BehaviorPage/BehaviorBoard.vue

@@ -251,10 +251,50 @@ function downLoadBehaviorData(){
     apis.downLoadBehaviorData(currentEngagement.value.studentId,currentEngagement.value.assignmentId)
         .then((res:any) => {
           console.log(res)
+          if (res && res.status === 200){
+            let blob = res.data as Blob;
+            const fileName = '行为数据.xlsx';
+            if ('download' in document.createElement('a')) {
+              const link = document.createElement('a');
+              link.download = fileName;
+              link.style.display = 'none';
+              link.href = URL.createObjectURL(blob);
+              document.body.appendChild(link);
+              link.click();
+              ElMessage.success('下载成功')
+              URL.revokeObjectURL(link.href); // 释放URL对象
+              document.body.removeChild(link);
+            }
+          }
+        })
+        .catch((err:any) => {
+          console.log(err)
+          ElMessage.success('下载失败')
         })
   }
   else if (selectedType.value === 'all'){
     apis.downLoadAllBehaviorData(selectedAssignment.value.assignmentId)
+        .then((res:any) => {
+          if (res && res.status === 200){
+            let blob = res.data as Blob;
+            const fileName = '行为数据.xlsx';
+            if ('download' in document.createElement('a')) {
+              const link = document.createElement('a');
+              link.download = fileName;
+              link.style.display = 'none';
+              link.href = URL.createObjectURL(blob);
+              document.body.appendChild(link);
+              link.click();
+              ElMessage.success('下载成功')
+              URL.revokeObjectURL(link.href); // 释放URL对象
+              document.body.removeChild(link);
+            }
+          }
+        })
+        .catch((err:any) => {
+          console.log(err)
+          ElMessage.success('下载失败')
+        })
   }
   else{
     ElMessage.error("请选择下载类型")