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.

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