Browse Source

Merge branch '文件上传和下载' of huabei/admin-vue into develop

luguangshan 11 months ago
parent
commit
656eed503a
2 changed files with 135 additions and 255 deletions
  1. 44 38
      src/utils/request.js
  2. 91 217
      src/views/system/businessInfo.vue

+ 44 - 38
src/utils/request.js

@@ -45,46 +45,52 @@ const debounce = (() => {
 // 响应拦截器
 service.interceptors.response.use(async res => {
         // 未设置状态码则默认成功状态
-        const code = res.data.code;
-        // 获取错误信息
-        const msg = res.data.msg;
-        if (code === 401) {
-            try {
-                const res = await refreshToken()
-                // 保存新的token
-                localStorage.setItem('token', res.data.token)
-                // 有新token后再重新请求
-                response.config.headers.token = localStorage.getItem('token') // 新token
-                const resp = await service.request(response.config)
-                return resp.data
-                // return service(response.config)
-            } catch {
-                localStorage.clear() // 清除token
-                MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', {
-                    confirmButtonText: '重新登录',
-                    cancelButtonText: '取消',
-                    type: 'warning'
-                }).then(() => {
-                    router.replace('/login')
-                }).catch(() => {
-                });
-                return Promise.reject('令牌验证失败')
+        const aa = res.data instanceof Blob;
+        if(res.data instanceof Blob){
+            return res;
+        }else{
+            const code = res.data.code;
+            // 获取错误信息
+            const msg = res.data.msg;
+            if (code === 401) {
+                try {
+                    const res = await refreshToken()
+                    // 保存新的token
+                    localStorage.setItem('token', res.data.token)
+                    // 有新token后再重新请求
+                    response.config.headers.token = localStorage.getItem('token') // 新token
+                    const resp = await service.request(response.config)
+                    return resp.data
+                    // return service(response.config)
+                } catch {
+                    localStorage.clear() // 清除token
+                    MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', {
+                        confirmButtonText: '重新登录',
+                        cancelButtonText: '取消',
+                        type: 'warning'
+                    }).then(() => {
+                        router.replace('/login')
+                    }).catch(() => {
+                    });
+                    return Promise.reject('令牌验证失败')
+                }
+    
+            } else if (code === 500) {
+                Message({
+                    message: msg,
+                    type: 'error'
+                })
+                return Promise.reject(new Error(msg))
+            } else if (code !== 0) {
+                Notification.error({
+                    title: msg
+                })
+                return Promise.reject('error')
+            } else {
+                return Promise.resolve(res.data)
             }
-
-        } else if (code === 500) {
-            Message({
-                message: msg,
-                type: 'error'
-            })
-            return Promise.reject(new Error(msg))
-        } else if (code !== 0) {
-            Notification.error({
-                title: msg
-            })
-            return Promise.reject('error')
-        } else {
-            return Promise.resolve(res.data)
         }
+
     },
     error => {
         console.log('err' + error)

+ 91 - 217
src/views/system/businessInfo.vue

@@ -111,19 +111,18 @@
   <!--            <el-divider></el-divider>-->
   
               <div class="top_show" style="margin-top: 20px">
-                <span> 解析文件:</span>
+                <span> 文件列表:</span>
                 <el-button size="small" class="el-icon-upload" type="primary" round style="position: absolute; right: 10%;" @click="fileUp()">上传</el-button>
                 <el-button size="small" class="el-icon-download" type="primary" round style="position: absolute; right: 1%;" @click="fileDown()">下载</el-button>
   
               </div>
   
-              <el-table :data="reportFileData1" style="width: 100%; margin-top: 20px"
+              <el-table :data="reportFileData" style="width: 100%; margin-top: 20px"
                         @select-all="selectAll"
-  
                         @select="handleSelectionChange" :row-key="rowKey" stripe>
                 <el-table-column type="selection" :reserve-selection=true  width="55" />
   
-                <el-table-column align="center" label="文件id" width="230">
+                <el-table-column align="center" label="文件id" width="220">
                   <template v-slot="{row}">
                   <span
                   >{{ row.id }}</span>
@@ -134,57 +133,35 @@
                 <el-table-column align="center" label="文件名" width="230">
                   <template v-slot="{row}">
                   <span
-                  >{{ row.fileNameXml }}</span>
+                  >{{ row.fileName }}</span>
                     <!-- <span>{{ row.name }}</span> -->
                   </template>
                 </el-table-column>
-  
-                <el-table-column align="center" label="上传时间" width="330">
-                  <template v-slot="{row}">
-                    <span>{{ row.updateTime}}</span>
-                  </template>
-                </el-table-column>
-  
-                <el-table-column label="操作" width="250">
-                  <template  v-slot="{row}" >
-                    <el-button size="mini" type="primary" @click="fileDelete(row,1)">删除</el-button>
-                  </template>
-                </el-table-column>
-  
-              </el-table>
-              <el-divider></el-divider>
-  
-              <div class="top_show">
-                <span> 模型文件:</span>
-              </div>
-              <el-table :data="reportFileData2" style="width: 100%;"
-                        @select-all="selectAll2"
-  
-                        @select="handleSelectionChange2" :row-key="rowKey" stripe>
-                <el-table-column type="selection" :reserve-selection=true  width="55" />
-                <el-table-column align="center" label="文件id" width="230">
+
+                <el-table-column align="center" label="文件类型" width="150">
                   <template v-slot="{row}">
                   <span
-                  >{{ row.id }}</span>
+                  >{{ row.fileTypeDesc }}</span>
                     <!-- <span>{{ row.name }}</span> -->
                   </template>
                 </el-table-column>
-                <el-table-column align="center" label="文件名" width="230">
+
+                <el-table-column align="center" label="文件是否有效" width="150">
                   <template v-slot="{row}">
                   <span
-                  >{{ row.fileNameXml }}</span>
+                  >{{ row.fileOnOff==1?'有效':'作废' }}</span>
                     <!-- <span>{{ row.name }}</span> -->
                   </template>
                 </el-table-column>
-                <el-table-column align="center" label="上传时间" width="330">
+                <el-table-column align="center" label="上传时间" width="300">
                   <template v-slot="{row}">
                     <span>{{ row.updateTime}}</span>
                   </template>
                 </el-table-column>
   
-                <el-table-column label="操作" width="350">
-                  <template  v-slot="{row}">
-                    <el-button size="mini" type="primary" @click="fileDelete(row,2)">删除</el-button>
+                <el-table-column label="操作" width="250">
+                  <template  v-slot="{row}" >
+                    <el-button size="mini" type="primary" @click="fileDelete(row,1)">删除</el-button>
                   </template>
                 </el-table-column>
   
@@ -210,10 +187,9 @@
         <div class="result_view" >
           <div style="margin-top:10px">
             <span> 文件类型:</span>
-            <el-select v-model="model_type" placeholder="请选择文件类型">
-  
-              <el-option v-for='model_item in file_model' :key="model_item" :label="model_item"
-                         :value="model_item"
+            <el-select v-model="model_type">
+              <el-option v-for='model_item in file_model' :key="model_item.fileType" :label="model_item.fileTypeDesc"
+                         :value="model_item.fileType"
               />
             </el-select>
           </div>
@@ -236,7 +212,7 @@
             <el-upload
             ref="upload"
             class="upload-demo"
-            action="/admin/up/uploadModelFile"
+            action="/admin/report/uploadFile"
             :on-success="handleSuccess"
             :before-upload="beforeUpload"
             :headers="headers"
@@ -305,8 +281,21 @@
         reportFileData : [],
         reportFileData1 : [],
         reportFileData2 : [],
-        file_model:["解析文件","模型文件"],
-        model_type:"解析文件",
+        file_model:[
+          {
+          fileType :'1',
+          fileTypeDesc : '解析结果xml'
+          },
+          {
+          fileType :'2',
+          fileTypeDesc : '解析结果json'
+          },
+          {
+          fileType :'3',
+          fileTypeDesc : '模型文件'
+          },
+        ],
+        model_type:"1",
         file_name:"",
         fileNameModel:"",
         fileNameXml:"",
@@ -368,32 +357,6 @@
           res=>
           {
             this.reportFileData=res.data.report_file_list;
-            console.log(this.reportFileData)
-            for (let item of this.reportFileData){
-              if(item.fileNameXml){
-                this.reportFileData1.push(item)
-
-              }
-              if(item.fileNameTxt) {
-                let litem = {
-                  id:item.id,
-                  fileNameXml:item.fileNameTxt,
-                  updateTime:item.updateTime
-                }
-                this.reportFileData1.push(litem)
-              }
-              if(item.fileNameModel){
-                let aitem = {
-                  id:item.id,
-                  fileNameXml:item.fileNameModel,
-                  updateTime:item.updateTime
-                }
-                this.reportFileData2.push(aitem)
-              }
-            }
-            this.parseLen = this.reportFileData1.length
-            this.modelLen = this.reportFileData2.length
-  
           }
       )
       let name = sessionStorage.getItem('currentTab')
@@ -423,7 +386,8 @@
       const _vm = this;
       _vm.pdfData.id = this.id;
       _vm.pdfData.headInfId = this.headInfId;
-      _vm.pdfData.modelType = this.model_type=='模型文件'?'1':'0';
+      _vm.pdfData.modelType = this.model_type;
+      _vm.pdfData.businessNum = this.businessInfoData.businessNum;
       return true;
     },
       handleMenuSelect(index) {
@@ -440,30 +404,13 @@
       toggleCollapse(){
         this.isCollapse = !this.isCollapse;
       },
+      //多选进行下载
       handleSelectionChange(selection) {
-        this.downSelected = selection.map(item => item.fileNameXml)
-        console.log(this.downSelected)
-        if(this.downSelected.length!=this.parseLen){
-          this.selectedAll = false
-        }
+        this.downSelected = selection.map(item => item.id)
+      
       },
-      selectAll() {
-        if(this.downSelected.length!=this.parseLen){
-          this.selectedAll = true
-        }
-        else this.selectedAll = false
-        if(this.selectedAll) {
-          for (let i = 0; i < this.reportFileData1.length; i++) {
-            if (!this.downSelected.includes(this.reportFileData1[i].fileNameXml)) {
-              // 回显数据里没有本条,把这条加进来(选中)
-              this.downSelected.push(this.reportFileData1[i].fileNameXml);
-            }
-          }}
-        else {
-           this.reportFileData1.forEach((item, index) => {
-                this.downSelected.splice(index.fileNameXml, 1);
-                });
-      }
+      selectAll(selection) {
+        this.downSelected = selection.map(item => item.id)
       },
   
       handleSelectionChange2(selection) {
@@ -472,25 +419,6 @@
           this.selectedAll2 = false
         }
       },
-      selectAll2() {
-        if(this.downSelected2.length!=this.modelLen){
-          this.selectedAll2 = true
-        }
-        else this.selectedAll2 = false
-        if(this.selectedAll2) {
-          for (let i = 0; i < this.reportFileData2.length; i++) {
-            if (!this.downSelected2.includes(this.reportFileData2[i].fileNameXml)) {
-              // 回显数据里没有本条,把这条加进来(选中)
-              this.downSelected2.push(this.reportFileData2[i].fileNameXml);
-            }
-          }}
-        else {
-          this.reportFileData2.forEach((item, index) => {
-            this.downSelected2.splice(index.fileNameXml, 1);
-          });
-        }
-      },
-  
       handleClick(tab, event) {
         // 点击tab后触发事件,修改显示页面,将状态保存在sessionStorage里面
         sessionStorage.setItem('currentTab', tab.name)
@@ -502,23 +430,6 @@
         location.reload();
   
       },
-      // gotolink1()
-      // {
-      //   this.$router.replace('/business');
-      // },
-  
-      // gotolink2()
-      // {
-      //   //指定跳转地址
-      //   this.$router.replace('/business');
-
-      // },
-      // gotolink3()
-      // {
-      //   //指定跳转地址
-      //   this.$router.replace('/business');
-
-      // },
       onButtonClickReturn()
       {
         this.$router.push({
@@ -547,62 +458,45 @@
 
       // 文件下载
       fileDown: function (){
-        if(!this.headInfId) {
-          this.$alert("失败业务不进行下载文件操作");
+        if(this.downSelected ==''){
+          this.$alert("请选择要下载的文件!");
           return;
         }
-          if (this.downSelected2.length+this.downSelected.length==0) {
-          this.$alert("请选择下载文件");
+        if(!this.headInfId) {
+          this.$alert("失败业务不进行下载文件操作");
           return;
         }
-        for (let i = 0; i < this.downSelected2.length; i++) {
-          const modname = this.downSelected2[i]; // 要下载的文件名
-          request.get(`/report/downloadMod/${modname}`).then(res => {
-            console.log("success");
-            const base64Data = res.data.file;
-            const byteCharacters = atob(base64Data); // 使用atob解码Base64字符串
-            const byteNumbers = new Array(byteCharacters.length);
-            for (let i = 0; i < byteCharacters.length; i++) {
-              byteNumbers[i] = byteCharacters.charCodeAt(i);
-            }
-            const byteArray = new Uint8Array(byteNumbers)
-            const blob = new Blob([byteArray], {type: 'application/octet-stream'});
-            const downloadUrl = URL.createObjectURL(blob);
-            const link = document.createElement('a');
-            link.href = downloadUrl;
-            link.download = modname; // 替换为你的文件名及扩展名
-            link.click();
+        request({
+          url: '/report/file/downLoad',
+          responseType: 'blob', // 表明返回服务器返回的数据类型
+          method: 'post',
+          data: this.downSelected
+        }).then(result => {
+          console.log(result);
+          //创建一个Blob对象接收后端传来的文件流
+          const blob = new Blob([result.data], {
+            type: 'application/octet-stream'
           })
-              .catch(error => {
-                console.error('Error downloading file:', error);
-              });      }
-        for (let i = 0; i < this.downSelected.length; i++) {
-          const filename = this.downSelected[i]; // 要下载的文件名
-          request.get(`/report/download/${filename}`).then(res => {
-            console.log("success");
-            const base64Data = res.data.file;
-            const byteCharacters = atob(base64Data); // 使用atob解码Base64字符串
-            const byteNumbers = new Array(byteCharacters.length);
-            for (let i = 0; i < byteCharacters.length; i++) {
-              byteNumbers[i] = byteCharacters.charCodeAt(i);
-            }
-            const byteArray = new Uint8Array(byteNumbers)
-            const blob = new Blob([byteArray], {type: 'application/octet-stream'});
-            const downloadUrl = URL.createObjectURL(blob);
-            const link = document.createElement('a');
-            link.href = downloadUrl;
-            link.download = filename; // 替换为你的文件名及扩展名
-            link.click();
-          })
-              .catch(error => {
-                console.error('Error downloading file:', error);
-              });}
-        setTimeout(() => {
-          setTimeout(this.$message({
-            message: '下载成功',
-            type: 'success'
-          }, 100));
-        }, 100)
+          const downloadElement = document.createElement('a')
+          // 创建下载的链接
+          const href = window.URL.createObjectURL(blob)
+          downloadElement.href = href
+          // 下载后文件名
+          const contentDis =result.headers['content-disposition'];
+          const filename = decodeURI(contentDis.split('=')[1]);
+          downloadElement.download = filename
+          document.body.appendChild(downloadElement)
+          // 点击下载
+          downloadElement.click()
+          // 下载完成移除元素
+          document.body.removeChild(downloadElement)
+          // 释放掉blob对象
+          window.URL.revokeObjectURL(href)
+        }).catch(err => {
+          console.log("出错误啦"+err.code)
+          this.$message.error(err.message)
+        })
+
       },
 
       //1.2 上传方法具体实现重写
@@ -642,8 +536,6 @@
 
 
         this.handleUpload();
-
-        // let fd = new FormData();
         // this.fileList.forEach(item=>{
         //   //文件信息中raw才是真的文件
         //   fd.append("files",item.raw);
@@ -791,45 +683,27 @@
       }
 ,
       fileDelete: function (row,type){
-        console.log("asssssdfas")
-  
-        console.log(type)
-        // console.log(row.fileNameXml)
-  
-        request.post('/report/del',{
-          file_name : row.fileNameXml,
+        this.$confirm('此操作将删除该文件, 是否继续?', '提示', {
+          confirmButtonText: '确定',
+          cancelButtonText: '取消',
+          type: 'warning'
+        }).then(() => {
+          request.post('/report/del',{
           id:row.id,
-          type:type
-        })
-        this.$message({
-            message: '删除成功',
-            type: 'success'
+          type:row.type
+          })
+          this.$message({
+            type: 'success',
+            message: '删除成功!'
           });
           location.reload();
-  
-          // this.roleList();
-  
-  
+        }).catch(() => {
+          this.$message({
+            type: 'info',
+            message: '已取消删除'
+          });          
+        });
       },
-      // fileDelete(scope,row) {
-      //   //删除文件
-      //   reportFileDelete(row.id).then(
-      //       res=>
-      //       {
-      //         this.$router.push({
-      //           path: '/businessinfoFail',
-      //           query: {
-      //
-      //           }
-      //         })
-      //       },
-      //   ),
-      //       this.$message("已删除")
-      //
-      //
-      //   // location.reload();
-      // },
-  
     },
   
   }