4
0
Quellcode durchsuchen

文件导出方法

jianglw vor 4 Jahren
Ursprung
Commit
91e3835323

+ 68 - 29
src/dashoo.cn/frontend_animal/src/pages/biobank/source/animal.vue

@@ -17,7 +17,7 @@
               <el-dropdown-item style="color:black;" command="exportchosen">导出所选样本源</el-dropdown-item>
               <el-dropdown-item style="color:black;" command="exportpage">导出当前页样本源</el-dropdown-item>
               <el-dropdown-item style="color:black;" command="exportall">导出所有样本源</el-dropdown-item>
-               <el-dropdown-item style="color:black;" command="exportassociation">导出关联的样本</el-dropdown-item>
+               <el-dropdown-item style="color:black;" command="exportrelated">导出关联的样本</el-dropdown-item>
             </el-dropdown-menu>
           </el-dropdown>
           <router-link :to="'/biobank/source/addsource/operationb'">
@@ -486,6 +486,14 @@ export default {
       minute = minute < 10 ? ('0' + minute) : minute
       return y + '-' + m + '-' + d + ' ' + h + ':' + minute
     },
+    formatExportDate (date) {
+      var y = date.getFullYear()
+      var m = date.getMonth() + 1
+      m = m < 10 ? ('0' + m) : m
+      var d = date.getDate()
+      d = d < 10 ? ('0' + d) : d
+      return y + m + d
+    },
     // 导出样本
     handleExportsamplesCommand (command) {
       let _this = this
@@ -498,7 +506,7 @@ export default {
           })
           return
         }
-        _this.exportsamples('exportchosen')
+        _this.exportSampleSource('exportchosen')
       } else if (command === 'exportpage') { // 导出当前页数据
         if (_this.$refs.tableData.data.length < 1) {
           _this.$message({
@@ -507,21 +515,35 @@ export default {
           })
           return
         }
-        _this.exportsamples('exportpage')
+        _this.exportSampleSource('exportpage')
       } else if (command === 'exportall') { // 导出所有数据
         _this.$confirm('此操作将导出所有满足条件的数据!是否继续导出?', '提示', {
           confirmButtonText: '确定',
           cancelButtonText: '取消',
           type: 'info'
         }).then(() => {
-          _this.exportsamples()
+          _this.exportSampleSource('exportall')
+        }).catch(() => {
+        })
+      } else if (command === 'exportrelated') {
+        if (this.multipleSelection.length === 0) {
+          _this.$message({
+            type: 'warning',
+            message: '请选择需要导出的样本来源'
+          })
+          return
+        }
+        _this.$confirm('是否导出当前样本来源关联样本?', '提示', {
+          confirmButtonText: '确定',
+          cancelButtonText: '取消',
+          type: 'info'
+        }).then(() => {
+          _this.exportSamples()
         }).catch(() => {
         })
-      } else if (command === 'exporassociation') {
-
       }
     },
-    exportsamples (val) {
+    exportSampleSource (val) {
       let _this = this
       _this.exportloading = true
       // 显示列
@@ -593,28 +615,45 @@ export default {
         }
         params = Object.assign(params, paramsid)
       }
-      // console.log(params)
-      // this.$confirm('是否确认导?', "警告", {
-      //   confirmButtonText: '确定',
-      //   cancelButtonText: '取消',
-      //   type: "warning"
-      // }).then(function () {
-      //   exportExcel('样本来源.xlsx')
-      // }).catch(function () { })
-      // eslint-disable-next-line no-undef
-      service.downloadExcel('dashoo.biobank.bee-0.1', 'SampleSource', 'ExportExcel', '样本来源.xlsx', params)
-      // _this.$axios.get('/samplesource/exportexcel', {
-      //   params
-      // })
-      //   .then(res => {
-      //     _this.exportloading = false
-      //     window.location = 'http://' + res.data
-      //   })
-      //   .catch(err => {
-      //     _this.exportloading = false
-      //     // handle error
-      //     console.error(err)
-      //   })
+      if (val === 'exportall') {
+        params.currentPage = 0
+        params.size = 0
+      }
+      const dateInfo = this.formatExportDate(new Date())
+      const name = dateInfo + '样本来源.xlsx'
+      // 样本来源相关导出
+      service.downloadExcel('dashoo.biobank.bee-0.1', 'SampleSource', 'ExportExcel', name, params)
+    },
+    exportSamples () {
+      // TODO 后台:所有样本来源创建一个sheet表,根据不同样本来源排序
+      const dateInfo = this.formatExportDate(new Date())
+      let _this = this
+      let id = ''
+      let params = {
+        AccCode: this.authUser.Profile.AccCode,
+        Name: this.Name,
+        InnerNo: this.InnerNo,
+        CreateOn: (this.CreateOn).join(','),
+        currentPage: this.currentPage,
+        size: this.size
+      }
+      for (var i = 0; i < _this.$refs.tableData.selection.length; i++) {
+        if (i === _this.$refs.tableData.selection.length - 1) {
+          id += _this.$refs.tableData.selection[i].Id
+        } else {
+          id += _this.$refs.tableData.selection[i].Id + ','
+        }
+      }
+      let paramsid = {
+        Id: id
+      }
+      params = Object.assign(params, paramsid)
+      if (this.multipleSelection.length === 0) {
+        params.currentPage = 0
+        params.size = 0
+      }
+      const name = dateInfo + '样本来源关联样本.xlsx'
+      service.downloadExcel('dashoo.biobank.bee-0.1', 'Sample', 'ExportExcel', name, params)
     },
     jstimehandle (val) {
       if (val === '') {

+ 6 - 0
src/dashoo.cn/frontend_animal/src/utils/errorCode.js

@@ -0,0 +1,6 @@
+export default {
+  '401': '认证失败,无法访问系统资源',
+  '403': '当前操作没有权限',
+  '404': '访问资源不存在',
+  'default': '系统未知错误,请反馈给管理员'
+}

+ 107 - 12
src/dashoo.cn/frontend_animal/src/utils/micro_request.js

@@ -2,7 +2,7 @@ import axios from 'axios'
 import { Notification, MessageBox, Message } from 'element-ui'
 import store from '@/store'
 // import { getToken } from '@/utils/auth'
-// import errorCode from '@/utils/errorCode'
+import errorCode from '@/utils/errorCode'
 
 axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
 
@@ -29,7 +29,6 @@ const service = axios.create({
 //     Promise.reject(error)
 // })
 
-
 // 响应拦截器
 // service.interceptors.response.use(res => {
 //   // 未设置状态码则默认成功状态
@@ -77,6 +76,36 @@ const service = axios.create({
 //   return Promise.reject(error)
 // })
 
+service.interceptors.response.use(res => {
+  console.info(res)
+  if (res.request.responseType == 'blob'){ // 判断是否是文件流
+    let fileReader = new FileReader()
+      fileReader.readAsText(res.data)
+      fileReader.onload = function () {
+        try {
+          let jsonData = JSON.parse(this.result) // 解析成功说明是普通对象数据,后端返回的是文件上传的错误信息
+          res.data = jsonData
+          processResponse(res)
+          return
+        } catch (err) { // 解析成对象失败,说明是文件流
+          downLoadBlobFile(res)
+          return
+        }
+      };
+    }
+  // 常规响应处理
+    return processResponse(res)
+},
+  error => {
+    console.log('err' + error)
+    Message({
+      message: error.message,
+      type: 'error',
+      duration: 5 * 1000
+    })
+    return Promise.reject(error)
+})
+
 service.postRequest = function postRequest(basePath, srvName, funcName, data) {
   if (data == undefined){
     let nullParam = {"nullparam": 0}
@@ -93,7 +122,7 @@ service.postRequest = function postRequest(basePath, srvName, funcName, data) {
     },
     data: data
   })
-};
+}
 
 // Excel文件下载(服务端生成文件流)
 service.downloadExcel = function downloadExcel(basePath, srvName, funcName, fileName, data) {
@@ -115,15 +144,81 @@ service.downloadExcel = function downloadExcel(basePath, srvName, funcName, file
       'X-RPCX-ServiceMethod': funcName
     },
     data: data
-  }).then(res => {
-    const aLink = document.createElement('a')
-    var blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })
-    aLink.href = URL.createObjectURL(blob)
-    aLink.setAttribute('download', fileName) // 设置下载文件名称
-    document.body.appendChild(aLink)
-    aLink.click()
-    document.body.appendChild(aLink)
-    })
+  })
 };
 
+// .then(res => {
+//   const aLink = document.createElement('a')
+//   var blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })
+//   aLink.href = URL.createObjectURL(blob)
+//   aLink.setAttribute('download', fileName) // 设置下载文件名称
+//   document.body.appendChild(aLink)
+//   aLink.click()
+//   document.body.appendChild(aLink)
+// })
+
+function processResponse(res){
+  // 未设置状态码则默认成功状态
+  const code = res.data.code || 200;
+  // 获取错误信息
+  const message = errorCode[code] || res.data.msg || errorCode['default']
+  if (code === 401) {
+    MessageBox.confirm(
+      '登录状态已过期,您可以继续留在该页面,或者重新登录',
+      '系统提示',
+      {
+        confirmButtonText: '重新登录',
+        cancelButtonText: '取消',
+        type: 'warning'
+      }
+    ).then(() => {
+      store.dispatch('LogOut').then(() => {
+        location.reload() // 为了重新实例化vue-router对象 避免bug
+      })
+    })
+  }
+  else if (code === 500) {
+    Message({
+      message: message,
+      type: 'error'
+    })
+    return Promise.reject(new Error(message))
+  } else if (code !== 200) {
+    Notification.error({
+      title: message
+    })
+    return Promise.reject('error')
+  }
+  else {
+    return res.data
+  }
+}
+
+function downLoadBlobFile(res, mimeType){
+  if (mimeType == undefined) {
+    mimeType = 'application/vnd.ms-excel'
+  }
+  const aLink = document.createElement('a')
+  var blob = new Blob([res.data], {
+    type: mimeType
+  })
+  // //从response的headers中获取filename, 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名;
+  var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
+  var contentDisposition = decodeURI(res.headers['content-disposition'] || res.headers['Content-Disposition'])
+  console.info(res)
+  console.info(contentDisposition)
+  var result = patt.exec(contentDisposition)
+  var fileName = 'data.xlsx'
+  if (result != undefined) {
+    fileName = result[1]
+    fileName = decodeURI(escape(fileName))
+    fileName = fileName.replace(/\"/g, '')
+  }
+  aLink.href = URL.createObjectURL(blob)
+  aLink.setAttribute('download', fileName) // 设置下载文件名称
+  document.body.appendChild(aLink)
+  aLink.click()
+  document.body.appendChild(aLink)
+}
+
 export default service