import-dialog.vue
3.86 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<template>
<el-dialog
title="导入店内支出"
:visible.sync="visible"
:close-on-click-modal="false"
class="NCC-dialog NCC-dialog_center"
width="600px"
@close="handleClose"
>
<div class="form-content">
<el-upload
ref="upload"
:auto-upload="false"
:on-change="handleFileChange"
:file-list="fileList"
:limit="1"
accept=".xlsx,.xls"
drag
>
<i class="el-icon-upload"></i>
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
<div class="el-upload__tip" slot="tip">只能上传xlsx/xls文件,且不超过10MB</div>
</el-upload>
<div v-if="uploadResult" class="upload-result" :class="uploadResult.type">
<div class="result-title">{{ uploadResult.title }}</div>
<div class="result-description">{{ uploadResult.description }}</div>
<div v-if="uploadResult.failMessages && uploadResult.failMessages.length > 0" class="fail-messages">
<div v-for="(msg, index) in uploadResult.failMessages" :key="index" class="fail-message">{{ msg }}</div>
</div>
</div>
</div>
<div slot="footer" class="dialog-footer">
<el-button @click="handleClose">取消</el-button>
<el-button type="primary" @click="submitUpload" :loading="uploading" :disabled="!fileList.length">上传</el-button>
</div>
</el-dialog>
</template>
<script>
import request from '@/utils/request'
export default {
data() {
return {
visible: false,
fileList: [],
uploading: false,
uploadResult: null
}
},
methods: {
init() {
this.visible = true
this.fileList = []
this.uploading = false
this.uploadResult = null
},
handleFileChange(file, fileList) {
this.fileList = fileList
this.uploadResult = null
},
submitUpload() {
if (!this.fileList.length || !this.fileList[0]) {
this.$message.warning('请选择要上传的文件')
return
}
const file = this.fileList[0]
if (!file.raw) {
this.$message.warning('文件信息异常,请重新选择文件')
return
}
this.uploading = true
this.uploadResult = null
const formData = new FormData()
formData.append('file', file.raw)
request({
url: '/api/Extend/LqStoreExpense/Actions/Import',
method: 'POST',
data: formData,
headers: {
'Content-Type': 'multipart/form-data'
}
}).then(res => {
this.uploading = false
if (res.code === 200) {
const data = res.data || {}
this.uploadResult = {
type: 'success',
title: '导入成功',
description: `成功导入 ${data.successCount || 0} 条,失败 ${data.failCount || 0} 条`,
failMessages: data.failMessages || []
}
this.$message.success('导入成功')
setTimeout(() => {
this.handleClose()
this.$emit('refresh')
}, 2000)
} else {
this.uploadResult = {
type: 'error',
title: '导入失败',
description: res.msg || '数据导入失败,请检查文件格式'
}
}
}).catch(err => {
this.uploading = false
this.uploadResult = {
type: 'error',
title: '导入失败',
description: err.message || '网络错误,请重试'
}
this.$message.error('导入失败')
})
},
handleClose() {
this.visible = false
this.fileList = []
this.uploadResult = null
if (this.$refs.upload) {
this.$refs.upload.clearFiles()
}
}
}
}
</script>
<style lang="scss" scoped>
.dialog-footer {
text-align: right;
}
.upload-result {
margin-top: 20px;
padding: 15px;
border-radius: 4px;
&.success {
background-color: #f0f9ff;
border: 1px solid #67C23A;
}
&.error {
background-color: #fef0f0;
border: 1px solid #F56C6C;
}
.result-title {
font-weight: 600;
margin-bottom: 8px;
}
.result-description {
margin-bottom: 10px;
}
.fail-messages {
margin-top: 10px;
padding-top: 10px;
border-top: 1px solid #ddd;
.fail-message {
color: #F56C6C;
margin-bottom: 5px;
}
}
}
</style>