You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
4.9 KiB

  1. <template>
  2. <div class="customer-css">
  3. <el-dialog title="巡检配置导入" :close-on-click-modal="false" :visible.sync="visible" width="390px" style="height: 520px;" class="customer-dialog">
  4. <el-form :inline="true" label-position="top" label-width="80px">
  5. <el-button type="primary" @click="downloadFile()">下载文件模板</el-button>
  6. <el-row>
  7. <el-col :span="24">
  8. <el-upload class="customer-upload" drag action="javascript:void(0);" ref="uploadFile" :limit="1" accept=".xlsx,.xls"
  9. :before-upload="beforeUploadHandle" :on-change="onChange" :auto-upload="false" style="text-align: left;">
  10. <i class="el-icon-upload"></i>
  11. <div class="el-upload__text">将文件拖到此处,<em>点击上传</em></div>
  12. </el-upload>
  13. </el-col>
  14. </el-row>
  15. </el-form>
  16. <span slot="footer" class="dialog-footer">
  17. <el-button type="primary" @click="saveUploadFile()">保存</el-button>
  18. <el-button type="primary" @click="closeDialog">关闭</el-button>
  19. </span>
  20. </el-dialog>
  21. </div>
  22. </template>
  23. <script>
  24. import {
  25. patrolConfigUpload, // 巡检配置批量导入
  26. queryFileId, // 查询文件ID
  27. } from "@/api/qc/qc.js"
  28. import { downLoadObjectFile } from '@/api/eam/eam_object_list.js'
  29. import axios from 'axios'
  30. import Vue from 'vue'
  31. export default {
  32. data() {
  33. return {
  34. visible: false,
  35. fileList: [],
  36. pageData: {
  37. site: '',
  38. buNo: '',
  39. partNo: ''
  40. },
  41. }
  42. },
  43. methods: {
  44. //初始化组件的参数
  45. init (currentRow) {
  46. // 获得类别
  47. this.pageData = JSON.parse(JSON.stringify(currentRow))
  48. //打开页面
  49. this.visible = true
  50. },
  51. // 上传之前
  52. beforeUploadHandle (file) {
  53. let extName = file[0].name.substring(file[0].name.lastIndexOf('.')).toLowerCase()
  54. if (!(extName === '.xlsx' || extName === '.xls')) {
  55. this.$message.error('数据导入失败,请选择正确的xlsx模板文件')
  56. return false
  57. }
  58. },
  59. // 选择上传文件时
  60. onChange (file) {
  61. this.fileList.push(file)
  62. },
  63. // 关闭modal
  64. closeDialog () {
  65. this.fileList = []
  66. // 关闭当前的页面
  67. this.visible = false
  68. },
  69. // 保存当前的数据
  70. saveUploadFile () {
  71. // 判断文件是否上传
  72. if (null == this.fileList || 0 === this.fileList.length) {
  73. this.$message.error("请先上传文件!")
  74. return false
  75. }
  76. const formData = new FormData()
  77. formData.append("file", this.fileList[0].raw)
  78. formData.append("site", this.pageData.site)
  79. formData.append("buNo", this.pageData.buNo)
  80. formData.append("partNo", this.pageData.partNo)
  81. patrolConfigUpload(formData).then(({data}) => {
  82. if (data.code === 0) {
  83. this.$message.success(data.msg)
  84. // 清空文件上传记录
  85. this.$refs.uploadFile.clearFiles()
  86. // 关闭窗口并刷新页面
  87. this.fileList = []
  88. this.visible = false
  89. // 触发父组件刷新列表
  90. this.$emit('refreshPatrolConfig')
  91. } else {
  92. let message = data.msg.split(';')
  93. this.$alert(message[0] + '<br/>' + (message[1] || ''), '导入失败', {
  94. confirmButtonText: '确定',
  95. dangerouslyUseHTMLString: true
  96. })
  97. }
  98. }).catch(error => {
  99. this.$message.error('导入失败,请检查文件格式和内容')
  100. })
  101. },
  102. // 下载
  103. async downloadFile () {
  104. let file = {
  105. id: 0,
  106. fileName: ''
  107. }
  108. let tempData = {
  109. orderRef1: 'qc',
  110. orderRef2: 'patrolConfigUpload'
  111. }
  112. await queryFileId(tempData).then(({data}) => {
  113. if (data && data.code === 0) {
  114. file.id = data.data.id
  115. file.fileName = data.data.fileName
  116. } else {
  117. this.$alert(data.msg, '错误', {
  118. confirmButtonText: '确定'
  119. })
  120. }
  121. })
  122. await downLoadObjectFile(file).then(({data}) => {
  123. // 不限制文件下载类型
  124. const blob = new Blob([data], {type: "application/octet-stream"})
  125. // 下载文件名称
  126. const fileName = file.fileName
  127. // a标签下载
  128. const linkNode = document.createElement('a')
  129. // a标签的download属性规定下载文件的名称
  130. linkNode.download = fileName
  131. linkNode.style.display = 'none'
  132. // 生成一个Blob URL
  133. linkNode.href = URL.createObjectURL(blob)
  134. document.body.appendChild(linkNode)
  135. // 模拟在按钮上的一次鼠标单击
  136. linkNode.click()
  137. // 释放URL 对象
  138. URL.revokeObjectURL(linkNode.href)
  139. document.body.removeChild(linkNode)
  140. })
  141. },
  142. /**
  143. * 点击 X 关闭对话框的回调
  144. **/
  145. handleDialogClose () {
  146. this.fileList = []
  147. }
  148. }
  149. }
  150. </script>
  151. <style scoped lang="scss">
  152. </style>