Blame view

src/components/machine/importEquipment.vue 3.79 KB
439c1b56   wuxw   开发完成设备台账功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  <template>
    <el-dialog
      :title="$t('equipmentAccount.importEquipment')"
      :visible.sync="dialogVisible"
      width="50%"
      @close="handleClose">
      <el-form ref="form" :model="formData" label-width="120px">
        <el-form-item :label="$t('equipmentAccount.selectFile')" prop="excelFile" required>
          <el-upload
            ref="upload"
            action=""
            :auto-upload="false"
            :on-change="handleFileChange"
            :file-list="fileList"
            :limit="1"
            accept=".xls,.xlsx">
            <el-button slot="trigger" size="small" type="primary">{{ $t('common.selectFile') }}</el-button>
            <div slot="tip" class="el-upload__tip">
              {{ $t('equipmentAccount.fileTip') }}
            </div>
          </el-upload>
        </el-form-item>
        
        <el-form-item :label="$t('equipmentAccount.downloadTemplate')">
          <div>
            {{ $t('equipmentAccount.downloadTip1') }}
            <el-link type="primary" @click="downloadTemplate">{{ $t('equipmentAccount.downloadTemplate') }}</el-link>
            {{ $t('equipmentAccount.downloadTip2') }}
          </div>
        </el-form-item>
      </el-form>
      
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">{{ $t('common.cancel') }}</el-button>
        <el-button type="primary" @click="handleImport" :loading="loading">{{ $t('common.import') }}</el-button>
      </span>
    </el-dialog>
  </template>
  
  <script>
  import { getCommunityId } from '@/api/community/communityApi'
  import { importEquipmentData, exportImportTemplate } from '@/api/machine/equipmentAccountApi'
  
  export default {
    name: 'ImportEquipment',
    data() {
      return {
        dialogVisible: false,
        loading: false,
        communityId: '',
        formData: {
          excelFile: null,
          typeId: ''
        },
        fileList: []
      }
    },
    created() {
      this.communityId = getCommunityId()
    },
    methods: {
      open(params) {
        this.formData.typeId = params.typeId
        this.dialogVisible = true
      },
      handleClose() {
        this.fileList = []
        this.formData.excelFile = null
        this.$refs.upload.clearFiles()
      },
      handleFileChange(file) {
        this.formData.excelFile = file.raw
      },
      async handleImport() {
        if (!this.formData.excelFile) {
          this.$message.warning(this.$t('equipmentAccount.selectFileFirst'))
          return
        }
  
        if (!this.checkFileType(this.formData.excelFile.name)) {
          this.$message.warning(this.$t('equipmentAccount.invalidFileType'))
          return
        }
  
        if (!this.checkFileSize(this.formData.excelFile.size)) {
          this.$message.warning(this.$t('equipmentAccount.fileSizeExceed'))
          return
        }
  
        try {
          this.loading = true
          const formData = new FormData()
          formData.append('uploadFile', this.formData.excelFile)
          formData.append('communityId', this.communityId)
          formData.append('typeId', this.formData.typeId)
  
          await importEquipmentData(formData)
          this.$message.success(this.$t('common.importSuccess'))
          this.dialogVisible = false
          this.$emit('success')
        } catch (error) {
          console.error('导入设备失败:', error)
        } finally {
          this.loading = false
        }
      },
      downloadTemplate() {
        if (!this.formData.typeId) {
          this.$message.warning(this.$t('equipmentAccount.selectTypeFirst'))
          return
        }
        exportImportTemplate({
          typeId: this.formData.typeId,
          communityId: this.communityId
        })
      },
      checkFileType(filename) {
        const ext = filename.split('.').pop().toLowerCase()
        return ['xls', 'xlsx'].includes(ext)
      },
      checkFileSize(size) {
        return size <= 2 * 1024 * 1024 // 2MB
      }
    }
  }
  </script>
  
  <style scoped>
  .el-upload__tip {
    margin-top: 7px;
    color: #606266;
    font-size: 12px;
  }
  </style>