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.

165 lines
5.0 KiB

1 year ago
  1. <template>
  2. <div class="customer-css">
  3. <el-dialog :title="titleCon" :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. subDetailUpload, // 子明细批量导入
  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. titleCon: '文件导入',
  35. visible: false,
  36. fileList: [],
  37. pageData: {
  38. flag: '',
  39. site: '',
  40. buNo: '',
  41. inspectionNo: ''
  42. },
  43. }
  44. },
  45. methods: {
  46. //初始化组件的参数
  47. init (currentRow) {
  48. // 获得类别
  49. this.pageData = JSON.parse(JSON.stringify(currentRow))
  50. //打开页面
  51. this.visible = true
  52. },
  53. // 上传之前
  54. beforeUploadHandle (file) {
  55. let extName = file[0].name.substring(file[0].name.lastIndexOf('.')).toLowerCase()
  56. if (!(extName === '.xlsx' || extName === '.xls')) {
  57. this.$message.error('数据导入失败,请选择正确的xlsx模板文件')
  58. return false
  59. }
  60. },
  61. // 选择上传文件时
  62. onChange (file) {
  63. this.fileList.push(file)
  64. },
  65. // 关闭modal
  66. closeDialog () {
  67. this.fileList = []
  68. // 刷新报工的页面
  69. //this.$emit('refreshPageTables')
  70. // 关闭当前的页面
  71. this.visible = false
  72. },
  73. // 保修当前的数据
  74. saveUploadFile () {
  75. // 判断文件是否上
  76. if (null == this.fileList || 0 === this.fileList.length) {
  77. this.$message.error("请先上传文件!")
  78. return false
  79. }
  80. const formData = new FormData()
  81. formData.append("file", this.fileList[0].raw)
  82. formData.append("flag", this.pageData.flag)
  83. formData.append("site", this.pageData.site)
  84. formData.append("buNo", this.pageData.buNo)
  85. formData.append("inspectionNo", this.pageData.inspectionNo)
  86. subDetailUpload(formData).then(({data}) => {
  87. if (data.code === 0) {
  88. // 返回超出上下限的数量
  89. this.$emit("changeEvent", data.countMap)
  90. this.$message.success(data.msg)
  91. // 清空文件上传记录
  92. this.$refs.uploadFile.clearFiles()
  93. // 关闭窗口并刷新页面
  94. //this.closeDialog()
  95. this.fileList = []
  96. this.visible = false
  97. } else {
  98. let message = data.msg.split(';')
  99. this.$alert(message[0] + '<br/>' + message[1], '导入失败', {
  100. confirmButtonText: '确定',
  101. dangerouslyUseHTMLString: true
  102. })
  103. }
  104. })
  105. },
  106. // 下载
  107. async downloadFile () {
  108. let file = {
  109. id: 0,
  110. fileName: ''
  111. }
  112. let tempData = {
  113. orderRef1: 'qc',
  114. orderRef2: 'subDetailUpload'
  115. }
  116. await queryFileId(tempData).then(({data}) => {
  117. if (data && data.code === 0) {
  118. file.id = data.data.id
  119. file.fileName = data.data.fileName
  120. } else {
  121. this.$alert(data.msg, '错误', {
  122. confirmButtonText: '确定'
  123. })
  124. }
  125. })
  126. await downLoadObjectFile(file).then(({data}) => {
  127. // 不限制文件下载类型
  128. const blob = new Blob([data], {type: "application/octet-stream"})
  129. // 下载文件名称
  130. const fileName = file.fileName
  131. // a标签下载
  132. const linkNode = document.createElement('a')
  133. // a标签的download属性规定下载文件的名称
  134. linkNode.download = fileName
  135. linkNode.style.display = 'none'
  136. // 生成一个Blob URL
  137. linkNode.href = URL.createObjectURL(blob)
  138. document.body.appendChild(linkNode)
  139. // 模拟在按钮上的一次鼠标单击
  140. linkNode.click()
  141. // 释放URL 对象
  142. URL.revokeObjectURL(linkNode.href)
  143. document.body.removeChild(linkNode)
  144. })
  145. },
  146. /**
  147. * 点击 X 关闭对话框的回调
  148. **/
  149. handleDialogClose () {
  150. this.fileList = []
  151. }
  152. }
  153. }
  154. </script>
  155. <style scoped lang="scss">
  156. </style>