batchImport.vue
6.8 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<template>
<el-dialog title="批量导入" width="800px" :visible="visible" :close-on-click-modal="false" :close-on-press-escape="!importing" :show-close="!importing" :before-close="handleClose" @update:visible="handleDialogVisible">
<div class="import-content">
<div class="form-item">
<span class="label">导入文件:</span>
<div class="file-area">
<el-button size="small" icon="el-icon-plus" :disabled="importing" @click="$refs.fileInput.click()">导入文件</el-button>
<input ref="fileInput" class="file-input" type="file" accept=".xlsx,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" :disabled="importing" @change="selectFile" />
<a class="template-link" :href="templateUrl" download>模板下载</a>
<p v-if="file.name" class="file-name">
{{ file.name }}
<el-button type="text" :disabled="importing" icon="el-icon-close" @click="removeFile"></el-button>
</p>
</div>
</div>
<p v-if="importing" class="importing-tip">导入中,请稍候...</p>
<div v-if="hasResult" class="result">
<p>共导入{{ result.totalCount }}条,成功{{ result.successCount }}条,失败{{ result.failCount }}条</p>
<el-button v-if="result.failCount > 0" type="text" @click="downloadErrorFile">导出错误文件</el-button>
</div>
</div>
<span slot="footer" class="dialog-footer">
<el-button v-if="importing" disabled>导入中,请稍候...</el-button>
<template v-else>
<el-button @click="handleClose">取消</el-button>
<el-button type="primary" @click="startImport">确定</el-button>
</template>
</span>
</el-dialog>
</template>
<script>
const TEMPLATE_URL = 'https://img02.glsx.com.cn/weapp/resource/saas/scrm/xlsx/核赔订单导入模板.xlsx'
export default {
name: 'ClaimsBatchImport',
props: { visible: { type: Boolean, default: false } },
data() {
return {
file: {},
importing: false,
hasResult: false,
templateUrl: TEMPLATE_URL,
result: { totalCount: 0, successCount: 0, failCount: 0, batchId: '' }
}
},
methods: {
selectFile(event) {
const file = event.target.files[0]
if (!file) return
if (file.name.substring(file.name.lastIndexOf('.')).toLowerCase() !== '.xlsx') {
this.$message({
message: '仅支持上传xlsx格式的文件',
type: 'error',
customClass: 'message-Z-zinde'
})
this.$refs.fileInput.value = ''
return
}
this.file = file
this.clearResult()
},
removeFile() {
if (this.importing) return
this.clearFile()
this.clearResult()
},
clearFile() {
this.file = {}
if (this.$refs.fileInput) this.$refs.fileInput.value = ''
},
clearResult() {
this.hasResult = false
this.result = { totalCount: 0, successCount: 0, failCount: 0, batchId: '' }
},
startImport() {
if (!this.file.size) {
return this.$message({
message: '请上传文件',
type: 'error',
customClass: 'message-Z-zinde'
})
}
const reader = new FileReader()
this.importing = true
reader.readAsDataURL(this.file)
reader.onload = event => {
const base64 = event.target.result.split(',')[1]
const fileType = this.file.name.split('.').pop()
this.$apis.claims
.batchImportGHClaims({ file: `${fileType}@${base64}` })
.then(res => {
if (res.code !== 0 && res.code !== '0') {
this.$message({
message: res.message || '导入失败,请检查文件后重试',
type: 'error',
customClass: 'message-Z-zinde'
})
return
}
this.setResult(res.data || {})
if (this.result.successCount > 0) this.$emit('success')
})
.catch(() => {})
.finally(() => {
this.importing = false
this.clearFile()
})
}
reader.onerror = () => {
this.importing = false
this.clearFile()
this.$message({
message: '文件读取失败,请重新上传',
type: 'error',
customClass: 'message-Z-zinde'
})
}
},
setResult(res) {
const successCount = Number(res.successCount || res.successNum || res.successfulCount || 0)
const failCount = Number(res.failCount || res.failedCount || res.failNum || 0)
this.result = {
totalCount: Number(res.totalCount || res.importCount || res.total || successCount + failCount),
successCount,
failCount,
batchId: res.batchId || res.batchID || ''
}
this.hasResult = true
},
downloadErrorFile() {
if (!this.result.batchId) {
return this.$message({
message: '未获取到导入批次,请联系管理员',
type: 'error',
customClass: 'message-Z-zinde'
})
}
this.$apis.claims
.exportBatchImportError({ batchId: this.result.batchId })
.then(res => {
if (res.code !== 0 && res.code !== '0') {
return this.$message({
message: res.message || '错误文件导出失败',
type: 'error',
customClass: 'message-Z-zinde'
})
}
const data = res.data || {}
const fileUrl = typeof data === 'string' ? data : data.fileUrl || data.url || data.downloadUrl || data.exportUrl || ''
if (!fileUrl) {
return this.$message({
message: '未获取到错误文件,请联系管理员',
type: 'error',
customClass: 'message-Z-zinde'
})
}
window.location.href = fileUrl
})
.catch(() => {})
},
handleClose(done) {
if (this.importing) return
this.clearFile()
this.clearResult()
this.$emit('update:visible', false)
if (done) done()
},
handleDialogVisible(visible) {
if (!visible) this.handleClose()
}
}
}
</script>
<style lang="scss" scoped>
.import-content {
min-height: 150px;
padding: 12px 20px;
.form-item {
display: flex;
align-items: flex-start;
.label {
width: 88px;
line-height: 32px;
text-align: right;
}
.file-area {
position: relative;
.file-input {
display: none;
}
.template-link {
margin-left: 12px;
line-height: 32px;
color: #5850ff;
}
.file-name {
margin: 8px 0 0;
color: #666;
}
}
}
.importing-tip {
margin: 24px 0 0 88px;
color: #999;
}
.result {
margin: 50px 0 0 88px;
text-align: left;
p {
display: inline-block;
margin-right: 12px;
}
}
}
</style>