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.

171 lines
5.4 KiB

4 months ago
3 months ago
4 months ago
3 months ago
4 months ago
3 months ago
4 months ago
3 months ago
4 months ago
3 months ago
4 months ago
3 months ago
4 months ago
3 months ago
4 months ago
3 months ago
4 months ago
3 months ago
4 months 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" @close="handleDialogClose" :show-close="!uploading">
  4. <el-form :inline="true" label-position="top" label-width="80px">
  5. <!-- <el-row>
  6. <el-form-item label=" ">
  7. <a href="/static/downLoad/NotifyUploadModel.xlsx" download="领料申请单导入模板.xlsx">
  8. 下载 Excel 模板
  9. </a>
  10. </el-form-item>
  11. </el-row> -->
  12. <el-row>
  13. <el-col :span="24">
  14. <el-upload class="customer-upload" drag action="javascript:void(0);" ref="uploadFile" :limit="1" accept=".xlsx,.xls"
  15. :before-upload="beforeUploadHandle" :on-change="onChange" :auto-upload="false" :disabled="uploading" style="text-align: left;" :file-list="fileList">
  16. <i class="el-icon-upload"></i>
  17. <div class="el-upload__text">
  18. {{ uploading ? '正在上传中,请稍候...' : '将文件拖到此处,或点击上传' }}
  19. </div>
  20. </el-upload>
  21. </el-col>
  22. </el-row>
  23. </el-form>
  24. <span slot="footer" class="dialog-footer">
  25. <el-button type="primary" @click="saveUploadFile" :loading="uploading" :disabled="uploading">
  26. {{ uploading ? '上传中...' : '保存' }}
  27. </el-button>
  28. <el-button type="primary" @click="closeDialog" :disabled="uploading">关闭</el-button>
  29. </span>
  30. </el-dialog>
  31. </div>
  32. </template>
  33. <script>
  34. import { uploadNoorderNotifyExcel } from '@/api/orderIssure/noOrderIssueNotify.js'
  35. export default {
  36. name: 'bomComponentUpload',
  37. data() {
  38. return {
  39. buList: [],
  40. titleCon: '文件导入',
  41. visible: false,
  42. fileList: [],
  43. pageData: {
  44. site: '',
  45. buNo: '',
  46. createBy: this.$store.state.user.name,
  47. },
  48. notifyData:{
  49. site: '',
  50. notifyNo: '',
  51. },
  52. uploading: false, // 上传状态标志
  53. returnData: '', // 返回的数据
  54. }
  55. },
  56. methods: {
  57. // 初始化组件的参数
  58. init (data) {
  59. this.fileList = []
  60. this.uploading = false // 重置上传状态
  61. console.log("init data",this.fileList);
  62. // 打开页面
  63. this.visible = true
  64. this.notifyData=JSON.parse(JSON.stringify(data))
  65. },
  66. // 上传之前
  67. beforeUploadHandle (file) {
  68. let extName = file.name.substring(file.name.lastIndexOf('.')).toLowerCase()
  69. if (!(extName === '.xlsx' || extName === '.xls')) {
  70. this.$message.error('数据导入失败,请选择正确的xlsx或xls格式的Excel文件')
  71. return false
  72. }
  73. return true
  74. },
  75. // 选择上传文件时
  76. onChange (file) {
  77. // 清空之前的文件列表,确保只有一个文件
  78. this.fileList = []
  79. this.fileList.push(file)
  80. },
  81. // 关闭modal
  82. closeDialog () {
  83. // 如果正在上传,不允许关闭
  84. if (this.uploading) {
  85. this.$message.warning('正在上传中,请稍候...')
  86. return false
  87. }
  88. this.deleteFile()
  89. // 关闭当前的页面
  90. this.visible = false
  91. },
  92. // 处理弹窗关闭事件(包括右上角X按钮)
  93. handleDialogClose () {
  94. if (this.uploading) {
  95. this.$message.warning('正在上传中,请稍候...')
  96. return
  97. }
  98. this.deleteFile()
  99. },
  100. deleteFile(){
  101. this.fileList = []
  102. this.visible = false
  103. this.uploading = false // 重置上传状态
  104. // 清空文件上传记录
  105. this.$refs.uploadFile.clearFiles()
  106. // 刷新报工的页面
  107. },
  108. successCallback(){
  109. this.fileList = []
  110. this.visible = false
  111. this.uploading = false // 重置上传状态
  112. this.$emit('refreshTable',this.returnData)
  113. },
  114. // 保存当前的数据
  115. saveUploadFile () {
  116. // 判断文件是否上传
  117. if (null == this.fileList || 0 === this.fileList.length) {
  118. this.$message.error("请先上传文件!")
  119. return false
  120. }
  121. // 再次校验文件格式
  122. const file = this.fileList[0].raw
  123. const fileName = file.name
  124. const fileExtension = fileName.substring(fileName.lastIndexOf('.')).toLowerCase()
  125. if (!('.xls' === fileExtension || '.xlsx' === fileExtension)) {
  126. this.$message.error('文件格式不正确,只支持.xls和.xlsx格式的Excel文件')
  127. return false
  128. }
  129. // 设置上传状态
  130. this.uploading = true
  131. // 创建FormData对象
  132. const formData = new FormData()
  133. formData.append("file", file)
  134. formData.append("site", this.notifyData.site)
  135. formData.append("notifyNo", this.notifyData.notifyNo)
  136. // 调用新的上传接口
  137. uploadNoorderNotifyExcel(formData).then(({ data }) => {
  138. if (data.code === 0) {
  139. this.returnData = data.rows
  140. this.$message.success(data.msg || 'Excel文件上传成功')
  141. // 关闭窗口并刷新页面
  142. this.successCallback()
  143. } else {
  144. this.$message.error(data.msg || 'Excel文件上传失败')
  145. }
  146. }).catch(error => {
  147. console.error('Excel上传异常:', error)
  148. this.$message.error('Excel文件上传异常,请检查文件格式和内容')
  149. }).finally(() => {
  150. // 无论成功还是失败,都重置上传状态
  151. this.uploading = false
  152. })
  153. },
  154. }
  155. }
  156. </script>