importOwnerRoom.vue
3.26 KB
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
<template>
<el-dialog :title="$t('room.importOwnerRoom.title')" :visible.sync="visible" width="40%" :before-close="handleClose">
<el-form ref="form" :model="form" label-width="120px" class="text-left">
<el-form-item :label="$t('room.importOwnerRoom.selectFile')">
<el-upload ref="upload" action="" :auto-upload="false" :on-change="handleFileChange" accept=".xls,.xlsx"
class="margin-left">
<el-button size="small" type="primary">{{ $t('room.importOwnerRoom.clickUpload') }}</el-button>
<div slot="tip" class="el-upload__tip">
{{ fileName || $t('room.importOwnerRoom.requiredFile') }}
</div>
</el-upload>
</el-form-item>
<el-form-item :label="$t('room.importOwnerRoom.downloadTemplate')">
<span class="margin-left">{{ $t('room.importOwnerRoom.downloadFirst') }}</span>
<el-link type="primary" :href="templateUrl" target="_blank">
{{ $t('room.importOwnerRoom.propertyTemplate') }}
</el-link>
<span>{{ $t('room.importOwnerRoom.prepareData') }}</span>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="handleClose">{{ $t('common.cancel') }}</el-button>
<el-button type="primary" @click="handleImport">{{ $t('common.import') }}</el-button>
</span>
</el-dialog>
</template>
<script>
import { importOwnerRoom } from '@/api/room/importOwnerRoomApi'
export default {
name: 'ImportOwnerRoom',
data() {
return {
visible: false,
form: {
communityId: this.getCommunityId(),
file: null
},
fileName: '',
templateUrl: '/import/importRoom.xlsx'
}
},
methods: {
open() {
this.visible = true
this.resetForm()
},
handleClose() {
this.visible = false
this.resetForm()
},
resetForm() {
this.form.file = null
this.fileName = ''
if (this.$refs.upload) {
this.$refs.upload.clearFiles()
}
},
handleFileChange(file) {
this.form.file = file.raw
this.fileName = file.name
},
validate() {
if (!this.form.file) {
this.$message.error(this.$t('room.importOwnerRoom.fileRequired'))
return false
}
const fileType = this.fileName.split('.').pop().toLowerCase()
if (!['xls', 'xlsx'].includes(fileType)) {
this.$message.error(this.$t('room.importOwnerRoom.invalidFormat'))
return false
}
if (this.form.file.size > 2 * 1024 * 1024) {
this.$message.error(this.$t('room.importOwnerRoom.fileSizeExceeded'))
return false
}
return true
},
async handleImport() {
if (!this.validate()) return
try {
const formData = new FormData()
formData.append('uploadFile', this.form.file)
formData.append('communityId', this.form.communityId)
formData.append('importAdapt', 'importRoomOwner')
const response = await importOwnerRoom(formData)
this.handleClose()
this.$router.push({
path: '/views/system/assetImportLogDetail',
query: {
logId: response.logId,
logType: 'importRoomOwner'
}
})
} catch (error) {
this.$message.error(error)
}
},
}
}
</script>