Эх сурвалжийг харах

故障字典的导出和下载模板、菜单增加文件管理

Rmengdi 4 сар өмнө
parent
commit
71aaac6771

+ 11 - 1
src/api/als/faultCase.js

@@ -1,4 +1,4 @@
-import { get, put, post, deletes } from '@/http/index'
+import { get, put, post, deletes, postFile } from '@/http/index'
 
 // 查询故障案例列表
 export const getFaultCase = async (data) => {
@@ -19,3 +19,13 @@ export const updateFaultCase = async (data) => {
 export const removeFaultCase = async (id) => {
   return await deletes('/als/faultCase/' + id)
 }
+
+// 导出下载模板
+export const importTemplateApi = async () => {
+  return await postFile('/als/faultCase/importTemplate', {})
+}
+
+// 导出数据
+export const exportApi = async (data) => {
+  return await postFile('/als/faultCase/export', data)
+}

+ 9 - 0
src/router/modules/system.js

@@ -245,6 +245,15 @@ export default [
           title: '判故逻辑管理'
         }
       },
+      {
+        name: 'AtlasFile',
+        path: '/knowledgeGraph/atlasFile',
+        component: () => import('@/views/als/atlasFile/index.vue'),
+        meta: {
+          parent: 'BasicData',
+          title: '文件管理'
+        }
+      },
       // {
       //   name: 'TrainingData',
       //   path: '/dataManage/trainingData',

+ 5 - 11
src/views/als/faultCase/index.vue

@@ -95,14 +95,14 @@
 </template>
 
 <script>
-import { getFaultCase, addFaultCase, updateFaultCase, removeFaultCase } from '@/api/als/faultCase'
+import { getFaultCase, addFaultCase, updateFaultCase, removeFaultCase, importTemplateApi, exportApi } from '@/api/als/faultCase'
 import { deepClone, debounce } from '@/utils/index'
 import { getListByIdsApi } from '@/api/als/oss'
 import { getItem } from '@/utils/index'
 import { getAircaftTypeAndModelTree } from '@/api/als/sideTree'
 import Treeselect from '@riophae/vue-treeselect'
 import '@riophae/vue-treeselect/dist/vue-treeselect.css'
-import { flattenTree, download } from '../utils/common'
+import { flattenTree, useDownload, localResource } from '../utils/common'
 // import FileUpload from '@/views/als/components/FileUpload'
 
 export default {
@@ -475,7 +475,7 @@ export default {
     },
     /** 下载模板操作 */
     importTemplate() {
-      download('/faultCase/importTemplate', {}, `faultCase_template_${new Date().getTime()}.xlsx`)
+      useDownload(importTemplateApi, 'faultCase_template', {}, '.xlsx')
     },
     // 文件上传中处理
     handleFileUploadProgress(event, file, fileList) {
@@ -494,15 +494,9 @@ export default {
     submitFileForm() {
       this.$refs.upload.submit()
     },
-    // 导出
+    // 导出   faultPhenomenon
     handleExport() {
-      download(
-        '/faultCase/export',
-        {
-          ...this.form
-        },
-        `faultCase_${new Date().getTime()}.xlsx`
-      )
+      useDownload(exportApi, '故障字典数据_', { faultPhenomenon: this.keyWordData }, '.xlsx')
     }
   }
 }

+ 48 - 30
src/views/als/utils/common.js

@@ -2,6 +2,7 @@ import { Message, Loading } from 'element-ui'
 import { saveAs } from 'file-saver'
 import errorCode from '@/views/als/utils/errorCode'
 import axios from 'axios'
+import { getItem } from '@/utils/index'
 
 /**
  * 构造树型结构数据
@@ -77,43 +78,42 @@ export function flattenTree(treeNodes) {
 // 通用下载方法
 let downloadLoadingInstance
 const service = axios.create({
-  // axios中请求配置有baseURL选项,表示请求URL公共部分
   baseURL: '/api/als',
   // 超时
-  timeout: 10000
+  timeout: 100000
 })
-export function download(url, params, filename, config) {
-  downloadLoadingInstance = Loading.service({ text: '正在下载数据,请稍候', spinner: 'el-icon-loading', background: 'rgba(0, 0, 0, 0.7)' })
+
+/**
+ * @description 接收数据流生成 blob,创建链接,下载文件
+ * @param {Function} api 导出表格的api方法 (必传)
+ * @param {String} tempName 导出的文件名 (必传)
+ * @param {Object} params 导出的参数 (默认{})
+ * @param {String} fileType 导出的文件格式 (默认为.xlsx)
+ * */
+export const useDownload = async (api, tempName, params, fileType, fileName) => {
   try {
-    const data = service.post(url, params, {
-      transformRequest: [
-        (params_1) => {
-          return tansParams(params_1)
-        }
-      ],
-      headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
-      responseType: 'blob',
-      ...config
-    })
-    console.log('data', data)
-    const isBlob = blobValidate(data)
-    console.log('isBlob', isBlob)
+    const res = await api(params)
+    const isBlob = blobValidate(res.data)
     if (isBlob) {
-      saveAs(new Blob([data]), filename)
+      const blob = fileType == 'zip' ? new Blob([res.data], { type: 'application/zip' }) : new Blob([res.data])
+      const name = fileType == 'zip' ? fileName : `${tempName}_${new Date().getTime()}${fileType}`
+      saveAs(blob, name)
     } else {
-      const resText = data.text()
-      const rspObj = JSON.parse(resText)
-      const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default']
-      Message.error(errMsg)
+      printErrMsg(res)
     }
-    downloadLoadingInstance.close()
-  } catch (r) {
-    console.error(r)
+  } catch (error) {
+    console.log(error)
     Message.error('下载文件出现错误,请联系管理员!')
-    downloadLoadingInstance.close()
   }
 }
 
+async function printErrMsg(data) {
+  const resText = await data.text()
+  const rspObj = JSON.parse(resText)
+  const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default']
+  Message.error(errMsg)
+}
+
 /**
  * 参数处理
  * @param {*} params  参数
@@ -123,18 +123,18 @@ export function tansParams(params) {
   let result = ''
   for (const propName of Object.keys(params)) {
     const value = params[propName]
-    var part = encodeURIComponent(propName) + '='
+    var part = encodeURI(propName) + '='
     if (value !== null && value !== '' && typeof value !== 'undefined') {
       if (typeof value === 'object') {
         for (const key of Object.keys(value)) {
           if (value[key] !== null && value[key] !== '' && typeof value[key] !== 'undefined') {
             let params = propName + '[' + key + ']'
-            var subPart = encodeURIComponent(params) + '='
-            result += subPart + encodeURIComponent(value[key]) + '&'
+            var subPart = encodeURI(params) + '='
+            result += subPart + encodeURI(value[key]) + '&'
           }
         }
       } else {
-        result += part + encodeURIComponent(value) + '&'
+        result += part + encodeURI(value) + '&'
       }
     }
   }
@@ -146,3 +146,21 @@ export function tansParams(params) {
 export function blobValidate(data) {
   return data.type !== 'application/json'
 }
+
+export function localResource(resource) {
+  let url = '/common/download/resource?resource=' + encodeURI(resource)
+  service({
+    method: 'get',
+    url: url,
+    responseType: 'blob',
+    headers: { Authorization: getItem('token') }
+  }).then((res) => {
+    const isBlob = blobValidate(res.data)
+    if (isBlob) {
+      const blob = new Blob([res.data])
+      saveAs(blob, decodeURI(res.headers['download-filename']))
+    } else {
+      printErrMsg(res.data)
+    }
+  })
+}