ImportFormAdd.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <a-modal
  3. :title="modalTitle"
  4. :width="640"
  5. :visible="visible"
  6. :confirmLoading="confirmLoading"
  7. @cancel="handleCancel"
  8. >
  9. <a-form :form="form">
  10. <a-form-item
  11. label="上传文件"
  12. :labelCol="BaseTool.Constant.labelCol"
  13. :wrapperCol="BaseTool.Constant.wrapperCol"
  14. >
  15. <a-upload :fileList="fileList" @change="handleChange" :remove="handleRemove" :beforeUpload="beforeUpload">
  16. <a-button> <a-icon type="upload" />选择上传文件</a-button>
  17. </a-upload>
  18. </a-form-item>
  19. </a-form>
  20. <p style="color: red">注意事项:<br/>
  21. 1:请确保设备新号、名称、型号的准确填写<br/>
  22. 2:请确保数据都未导入,重新导入成功不会覆盖原有数据,导致存在多台同样设备<br/>
  23. 3:导入如出现问题,请及时联系<br/>
  24. </p>
  25. <template slot="footer">
  26. <a-button :loading="confirmLoading" type="primary" @click="save()">确定</a-button>
  27. </template>
  28. </a-modal>
  29. </template>
  30. <script>
  31. import { importSbInfo } from '@/api/sb/info'
  32. export default {
  33. name: 'SbModelBomImportForm',
  34. data () {
  35. return {
  36. confirmLoading: false,
  37. modalTitle: null,
  38. form: this.$form.createForm(this),
  39. visible: false,
  40. useCompany: null,
  41. useProject: null,
  42. type: null,
  43. fileList: []
  44. }
  45. },
  46. methods: {
  47. base (useCompany, useProject) {
  48. this.visible = true
  49. this.useCompany = useCompany
  50. this.useProject = useProject
  51. this.modalTitle = '设备新增导入'
  52. this.type = 1
  53. },
  54. handleRemove (file) {
  55. const index = this.fileList.indexOf(file)
  56. const newFileList = this.fileList.slice()
  57. newFileList.splice(index, 1)
  58. this.fileList = newFileList
  59. },
  60. beforeUpload (file) {
  61. const reg = /\.(xls|xlsx)(\?.*)?$/
  62. return new Promise((resolve, reject) => {
  63. if (reg.test(file.name)) {
  64. this.fileList = [file]
  65. return false
  66. } else {
  67. this.$message.error(`请上传正确的excel文件`)
  68. reject(new Error('请上传正确的excel文件'))
  69. return false
  70. }
  71. })
  72. },
  73. handleChange (info) {
  74. if (info.file.status !== 'uploading') {
  75. console.log(info.file, info.fileList)
  76. }
  77. if (info.file.status === 'done') {
  78. this.$message.success(`${info.file.name} file uploaded successfully`)
  79. } else if (info.file.status === 'error') {
  80. this.$message.error(`${info.file.name} file upload failed.`)
  81. }
  82. },
  83. save () {
  84. const { form: { validateFieldsAndScroll } } = this
  85. this.confirmLoading = true
  86. validateFieldsAndScroll((errors, values) => {
  87. if (errors) {
  88. this.confirmLoading = false
  89. return
  90. }
  91. const formData = new FormData()
  92. formData.append('type', this.type)
  93. formData.append('file', this.fileList[0])
  94. importSbInfo(formData)
  95. .then((res) => {
  96. this.$message.success(res.data)
  97. this.handleCancel(values)
  98. this.BaseTool.ListForm.clearOneList(this)
  99. this.BaseTool.ListForm.pushOneListAddMore(this, res.data)
  100. }).catch(() => {
  101. this.confirmLoading = false
  102. })
  103. })
  104. },
  105. handleCancel (values) {
  106. this.visible = false
  107. this.confirmLoading = false
  108. this.fileList = []
  109. this.form.resetFields()
  110. this.storeId = null
  111. this.$emit('ok', values)
  112. }
  113. }
  114. }
  115. </script>