plm前端
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.

150 lines
4.6 KiB

  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. uploadProjectPartExcel, // 导入项目物料文件
  26. queryFileId, // 查询文件ID
  27. downLoadFile, //下载文件
  28. } from '@/api/part/partInformation.js'
  29. export default {
  30. name:'projectPartUpload',
  31. data() {
  32. return {
  33. titleCon: '文件导入',
  34. visible: false,
  35. fileList: [],
  36. pageData: {
  37. projectId: '',
  38. createBy: '',
  39. site: '',
  40. customerNo: ''
  41. },
  42. }
  43. },
  44. methods: {
  45. //初始化组件的参数
  46. init(currentRow) {
  47. // 获得类别
  48. this.pageData = JSON.parse(JSON.stringify(currentRow))
  49. //打开页面
  50. this.visible = true
  51. },
  52. // 上传之前
  53. beforeUploadHandle(file) {
  54. let extName = file[0].name.substring(file[0].name.lastIndexOf('.')).toLowerCase()
  55. if (!(extName === '.xlsx' || extName === '.xls')) {
  56. this.$message.error('数据导入失败,请选择正确的xlsx模板文件')
  57. return false
  58. }
  59. },
  60. // 选择上传文件时
  61. onChange (file) {
  62. this.fileList.push(file)
  63. },
  64. //关闭modal
  65. closeDialog () {
  66. this.fileList = []
  67. // 清空文件上传记录
  68. this.$refs.uploadFile.clearFiles()
  69. // 刷新报工的页面
  70. this.$emit('refreshPageTables')
  71. // 关闭当前的页面
  72. this.visible = false
  73. },
  74. // 保修当前的数据
  75. saveUploadFile () {
  76. // 判断文件是否上传
  77. if (null == this.fileList || 0 === this.fileList.length) {
  78. this.$message.error("请先上传文件!")
  79. return false
  80. }
  81. const formData = new FormData()
  82. formData.append("file", this.fileList[0].raw)
  83. formData.append("createBy", this.pageData.createBy)
  84. formData.append("orderRef1", this.pageData.site)
  85. formData.append("orderRef2", this.pageData.projectId)
  86. formData.append("orderRef3", this.pageData.customerNo)
  87. uploadProjectPartExcel(formData).then(({data}) => {
  88. if (data.code === 0) {
  89. this.$message.success(data.msg)
  90. // 清空文件上传记录
  91. this.$refs.uploadFile.clearFiles()
  92. // 关闭窗口并刷新页面
  93. this.closeDialog()
  94. } else {
  95. this.$message.warning(data.msg)
  96. }
  97. })
  98. },
  99. // 下载
  100. async downloadFile () {
  101. let file = {
  102. id: 0,
  103. fileName: ''
  104. }
  105. let tempData = {
  106. orderRef1: this.pageData.site,
  107. orderRef2: 'projectPart'
  108. }
  109. await queryFileId(tempData).then(({data}) => {
  110. if (data && data.code === 0) {
  111. file.id = data.data.id
  112. file.fileName = data.data.fileName
  113. } else {
  114. this.$alert(data.msg, '错误', {
  115. confirmButtonText: '确定'
  116. })
  117. }
  118. })
  119. await downLoadFile(file).then(({data}) => {
  120. // 不限制文件下载类型
  121. const blob = new Blob([data], {type: "application/octet-stream"})
  122. // 下载文件名称
  123. const fileName = file.fileName
  124. // a标签下载
  125. const linkNode = document.createElement('a')
  126. // a标签的download属性规定下载文件的名称
  127. linkNode.download = fileName
  128. linkNode.style.display = 'none'
  129. // 生成一个Blob URL
  130. linkNode.href = URL.createObjectURL(blob)
  131. document.body.appendChild(linkNode)
  132. // 模拟在按钮上的一次鼠标单击
  133. linkNode.click()
  134. // 释放URL 对象
  135. URL.revokeObjectURL(linkNode.href)
  136. document.body.removeChild(linkNode)
  137. })
  138. },
  139. }
  140. }
  141. </script>
  142. <style scoped lang="scss">
  143. </style>