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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import React, { Fragment } from 'react';
import { Button, Modal, Steps, Upload, Icon, message, Tabs, Table } from 'antd';
import styles from './ImportUtil.less';
import ButtonDiy from '@/baseComponent/ButtonDiy';
import config from '@/config/config';
import { connect } from 'dva';
import { exportExcel } from 'xlsx-oc';
const Step = Steps.Step;
const TabPane = Tabs.TabPane;
@connect(({ DataObj, loading }) => ({
DataObj,
loading: loading.models.DataObj,
}))
export default class ImportUtil extends React.PureComponent {
constructor(props) {
super(props);
const exportTemplateUrl = encodeURI(
`${config.sqlFormsServer}/DataObjApi/importTemplateDownload?objId=${this.props.objId}&name=${
props.fileName
}`,
);
this.state = {
ch: false,
currentKey: '1',
visible: false,
current: 0,
isShow: true,
filekey: '',
sucData: [],
errData: [],
column: [],
exportTemplateUrl,
isNextDisabled: false,
};
}
showModal = () => {
this.setState({
visible: true,
});
};
handleOk = (e) => {
this.setState({
visible: false,
});
};
handleCancel = (e) => {
this.setState({
visible: false,
});
};
next() {
const current = this.state.current + 1;
if (current === 2) {
this.setState({
current: current,
isShow: false,
});
if (this.state.errData.lenth > 0) {
message.error('请检查数据');
return;
} else {
this.import();
}
}
if (current === 3) {
this.setState({
current: current,
isShow: true,
});
if (this.props.callback) {
this.props.callback();
}
}
}
prev() {
const current = this.state.current - 1;
this.setState({ current });
}
import() {
const { dispatch, importParams } = this.props;
dispatch({
type: 'DataObj/importExecute',
payload: {
...(importParams || {}),
cacheKey: this.state.filekey,
objId: this.props.objId,
},
callback: (res) => {
this.next();
message.success('导入成功');
this.setState({
current: 3,
isShow: true,
visible: false,
});
if (this.props.callback) {
this.props.callback();
}
},
});
}
getCachKey = (filePath) => {
const { dispatch, importParams } = this.props;
dispatch({
type: 'DataObj/importAnalyse',
payload: {
...(importParams || {}),
isLocal: false,
filePath,
objId: this.props.objId,
},
callback: (res) => {
this.setState({
filekey: res.cacheKey,
});
this.queryFile(res.cacheKey);
},
});
};
queryFile = (cacheKey) => {
const { dispatch, importParams } = this.props;
dispatch({
type: 'DataObj/importDataQuery',
payload: {
...(importParams || {}),
cacheKey,
objId: this.props.objId,
},
callback: (res) => {
this.setState({
current: 1,
isShow: false,
sucData: res.pass,
errData: res.noPass,
column: res.column,
});
if (res.noPass && res.noPass.length == 0) {
if (res.pass.length == 0) {
message.error('当前没有验证成功的数据,无法导入。');
this.setState({
currentKey: '1',
ch: !this.state.ch,
isNextDisabled: true,
});
} else {
message.success('所有数据验证通过,请确认后点击下一步。');
this.setState({
currentKey: '2',
ch: !this.state.ch,
isNextDisabled: false,
});
}
} else {
message.error('当前存在未验证通过的数据,请按错误提示检测更正导入文件');
this.setState({
currentKey: '1',
ch: !this.state.ch,
isNextDisabled: true,
});
}
},
});
};
changePane = (activeKey) => {
this.setState({ currentKey: activeKey });
};
exportError = (column, dataSource) => {
var _headers = [];
for (var i = 0; i < column.length; i++) {
_headers.push({ k: column[i].dataIndex, v: column[i].title });
}
for (var j = 0; j < dataSource.length; j++) {
dataSource[j].index = j + 1;
}
exportExcel(_headers, dataSource);
};
render() {
const props = {
name: 'file',
action: config.sqlFormsServer + '/upload',
headers: {
authorization: 'authorization-text',
},
accept: '.xlsx',
showUploadList: false,
onChange: (info) => {
if (info.file.status !== 'uploading') {
}
if (info.file.status === 'done') {
message.success(`${info.file.name} 上传成功`);
let x = info.file.response;
this.getCachKey(x);
} else if (info.file.status === 'error') {
message.error(`${info.file.name} file upload failed.`);
}
},
};
const steps = [
{
title: '上传Excel',
},
{
title: '验证数据',
},
{
title: '数据导入',
},
{
title: '导入完成',
},
];
const { current } = this.state;
const { name, loading } = this.props;
const column1 = this.state.column
? [
{
title: '序号',
dataIndex: 'index',
fixed: 'left',
width: 50,
render: (v, r, i) => i + 1,
},
...this.state.column,
]
: [];
var column2 = this.state.column ? [...this.state.column] : [];
if (column1.length > 1)
column1[1].render = (val) => <span style={{ color: 'red' }}>{val}</span>;
column2.splice(0, 1);
column2 = [{ title: '序号', fixed: 'left', width: 50, render: (v, r, i) => i + 1 }, ...column2];
return (
<span>
<ButtonDiy
name={name || '批量导入'}
type="default"
className="defaultBlue"
handleClick={this.showModal}
/>
<Modal
visible={this.state.visible}
onOk={this.handleOk}
maskClosable={false}
destroyOnClose
onCancel={this.handleCancel}
title={null}
footer={null}
width={'80%'}>
<div className={styles.nomal}>
<p className={styles.import}>EXCEL导入向导</p>
<div style={{ margin: 'auto 23px' }}>
<Steps current={current}>
{steps.map((item) => (
<Step key={item.title} title={item.title} />
))}
</Steps>
<div className="steps-content">{steps[this.state.current].content}</div>
<div className={styles.button}>
{this.state.current < steps.length - 1 &&
this.state.current !== 0 && (
<ButtonDiy
name={this.state.current == 3 ? '确认导入' : '下一步'}
type="primary"
disabled={this.state.current != 3 && this.state.isNextDisabled}
className="primaryBlue"
handleClick={() => this.next()}
/>
)}
{this.state.current > 0 && (
<ButtonDiy
style={{ marginLeft: 8 }}
name="上一步"
className="defaultBlue"
handleClick={() => this.prev()}
/>
)}
</div>
</div>
<div className={styles.tip}>
<p className={styles.tipContent}>
<span>欢迎使用EXCEL导入向导,首先请您上传需要导入的EXCEL文件。</span>
<span>
点击“选择文件”选择您要导入的EXCEL文件,点击“开始上传”将其上传到服务器上,点击“清空队列”清空当前上传文件队列。
</span>
</p>
</div>
{this.state.isShow ? (
<div className={styles.buttonDown}>
<Upload {...props}>
<Button style={{ marginRight: '20px' }}>
<Icon type="upload" />
点击上传
</Button>
</Upload>
<Button href={this.state.exportTemplateUrl} target="_blank" type="danger">
下载模板
</Button>
</div>
) : (
<Upload {...props}>
<Button>
<Icon type="upload" />
重新上传
</Button>
</Upload>
)}
{this.state.isShow ? (
<div className={styles.attentionItem}>
<p>导入事项</p>
<p>1. 导入操作一次只能上传 1 个EXCEL文件。</p>
<p>2. 导入文件最大文件大小上传 1 GB。</p>
<p>3. 只能上传EXCEL文件(XLS, XLSX) 默认支持EXCEL 2003和EXCEL 2007。</p>
<p>4. 请将EXCEL文件的所有单元格格式设置为“文本”格式</p>
</div>
) : (
<div className={styles.error} style={{ marginTop: 20 }}>
<Tabs
activeKey={this.state.currentKey}
key={this.state.ch}
onChange={this.changePane}
type="card">
{this.state.errData && this.state.errData.length == 0 ? (
''
) : (
<TabPane tab={<span style={{ color: 'red' }}>验证错误列表</span>} key="1">
<Button
style={{ marginTop: 5, marginBottom: 5 }}
type="danger"
onClick={this.exportError.bind(this, column1, this.state.errData)}>
导出错误信息
</Button>
<Table
columns={column1}
size="small"
style={{ overflow: 'auto' }}
dataSource={this.state.errData}
bordered={true}
/>
</TabPane>
)}
<TabPane tab={<span style={{ color: 'green' }}>验证成功列表</span>} key="2">
<Button
style={{ marginTop: 5, marginBottom: 5 }}
type="primary"
onClick={this.exportError.bind(this, column2, this.state.sucData)}>
导出正确信息
</Button>
<Table
columns={column2}
size="small"
style={{ overflow: 'auto' }}
dataSource={this.state.sucData}
bordered={true}
/>
</TabPane>
</Tabs>
</div>
)}
</div>
</Modal>
</span>
);
}
}