import.vue
6.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<template>
<div class="imort_page">
<div class="choose_box">
<div class="upload_wrap">
<el-input placeholder="请选择需要导入的车辆表格" v-model="uploadFile.name" readonly>
<el-button slot="append">选择文件</el-button>
</el-input>
<!-- 隐藏上传文件按钮 开始 -->
<el-upload ref="uploadBox" class="upload_box" :auto-upload="false" :headers="uploadHead" :accept="acceptFile" :data="uploadParams" :action="uploadPath" multiple :show-file-list="false" :on-change="onChange" :on-success="onSuccess" :before-upload="beforeUpload" :on-error="onError" :file-list="fileList">
<el-button type="primary">点击上传</el-button>
</el-upload>
<!-- 隐藏上传文件按钮 end -->
</div>
<el-link type="primary" class="el_link" @click="downTemplate">下载模版</el-link>
</div>
<!-- <div class="btn_group">
<el-button type="default" size="small">取消</el-button>
<el-button type="primary" size="small">确定</el-button>
</div> -->
<!-- 导入结果 开始 -->
<div class="upload_result" v-if="uploadResult && (uploadResult.total || uploadResult.total == 0)">
<h2 class="title">导入结果</h2>
<div class="result_box">
<div class="cell">
<div class="name">
<span>成功记录</span>
</div>
<span class="txt">{{uploadResult.succNum||0}}</span>
</div>
<div class="cell">
<div class="name">
<span>失败记录</span>
<span class="btn" v-if="uploadResult.failFile" @click="downloadFail()">导出记录</span>
</div>
<span class="txt">{{uploadResult.failNum||0}}</span>
</div>
</div>
</div>
<!-- 导入结果 end -->
</div>
</template>
<script>
import {
IMPORT_VEHICLE_TEMPLATE,
DOWNLOAD_ASSIGNPERMIT_TEMPLATE_FILE,
} from "@/services/user";
import { showToast, broadcast } from "@/utils/util";
export default {
name: "ImportVehicle",
props: {
id: {
type: [String, Number],
default: "",
},
},
data() {
return {
fileInfo: "",
uploadPath: "",
uploadHead: {},
uploadFile: {},
uploadParams: {},
uploadResult: {},
acceptFile: ".xlsx,.xls",
fileList: []
};
},
mounted() {
let id = this.id;
let token = localStorage.getItem("token");
this.uploadPath =
this.$root.parentVuex.getters.baseURL + IMPORT_VEHICLE_TEMPLATE;
this.uploadHead = { Authorization: token };
this.uploadParams = { receiverId: id };
},
methods: {
// 监听文件选择
onChange(file, fileList) {
console.log(file);
console.log(fileList);
if (!file.response) {
this.uploadFile = file;
this.fileList = [file];
}
this.$emit('monitorChangeFlag', true)
},
// 下载失败文件
downloadFail() {
let o = this.uploadResult;
window.open(o.failFile);
},
// 文件上传前
beforeUpload() {
this.uploadResult = {};
},
// 文件上传失败
onError(err, file, fileList) {
console.log(err);
},
// 监听文件上传成功
onSuccess(response, file, fileList) {
console.log(response);
this.$emit('monitorFlag', true)
let _this = this;
broadcast("cancel-loading");
_this.$refs.uploadBox.clearFiles();
if (response.code != 200) {
showToast({
message: response.message,
});
_this.uploadFile = {};
_this.fileList = [];
return false;
}
this.uploadResult = response.data;
broadcast("import-success");
},
// 上传文档
saveInfo() {
let o = this.uploadFile;
console.log(this.fileList);
console.log(o);
if (!o || !o.name) {
showToast({
message: "请选择文件",
});
broadcast("cancel-loading");
return false;
}
let list = o.name.split('.')
let acceptFile = this.acceptFile;
let _type = list[list.length-1];
let idx = acceptFile.includes(_type)
if(!idx){
showToast({
message: `导入文件只支持${acceptFile}`,
});
broadcast("cancel-loading");
return false;
}
this.$refs.uploadBox.submit();
},
// 下载模板
downTemplate() {
DOWNLOAD_ASSIGNPERMIT_TEMPLATE_FILE().then((res) => {
let { data } = res;
window.open(data.dictValue);
});
},
},
};
</script>
<style lang="scss" scoped>
.choose_box {
display: flex;
align-items: center;
padding: 10px 72px;
.el_link {
flex-shrink: 0;
text-align: left;
margin: 0 10px;
}
.upload_wrap {
flex: 1;
position: relative;
.upload_box {
position: absolute;
right: 0;
bottom: 0;
top: 0;
opacity: 0;
}
}
}
.btn_group {
padding: 20px 0 10px 0;
text-align: center;
}
.upload_result {
border-top: 1px solid #eee;
margin-top: 30px;
.title {
font-family: PingFangSC-Medium;
font-size: 20px;
color: #333333;
font-weight: 500;
}
.result_box {
display: flex;
align-items: center;
justify-content: center;
padding-bottom: 30px;
.cell {
width: 248px;
height: 108px;
background: #fbfbfb;
border: 1px solid rgba(215, 215, 215, 1);
border-radius: 4px;
margin: 0 20px;
display: flex;
flex-direction: column;
justify-content: center;
padding: 20px;
box-sizing: border-box;
.name {
font-family: PingFangSC-Medium;
font-size: 14px;
color: #666666;
font-weight: 500;
display: flex;
align-items: center;
.btn {
width: 76px;
height: 30px;
border: 1px solid rgba(33, 150, 243, 1);
border-radius: 4px;
text-align: center;
line-height: 28px;
font-family: PingFangSC-Regular;
font-size: 14px;
color: #409eff;
font-weight: 400;
cursor: pointer;
margin-left: 10px;
}
.btn:hover {
opacity: 0.8;
}
}
.txt {
font-family: PingFangSC-Medium;
font-size: 30px;
color: #333333;
font-weight: 500;
line-height: 1.5;
}
}
}
}
</style>