ClientImportModal.tsx
3.21 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
import { RESPONSE_CODE } from '@/constants/enum';
import { downloadFile } from '@/services/order';
import { blobToJson } from '@/utils';
import { ModalForm, ProFormUploadDragger } from '@ant-design/pro-components';
import { Button, Form, message } from 'antd';
import { RcFile } from 'antd/es/upload';
import axios from 'axios';
export default () => {
const [form] = Form.useForm();
const [messageApi, contextHolder] = message.useMessage();
const downloadImportTemplate = () => {
messageApi.open({
type: 'loading',
content: '正在导入...',
duration: 0,
});
downloadFile(
'/api/admin/client/downloadImportTemplate',
'客户导入模板.xlsx',
'post',
{},
() => {
messageApi.destroy();
},
);
};
return (
<ModalForm
title="导入客户信息"
trigger={<Button type="primary">导入客户信息</Button>}
form={form}
autoFocusFirstInput
modalProps={{
destroyOnClose: true,
onCancel: () => console.log('run'),
}}
submitTimeout={2000}
onFinish={async (values) => {
const formData = new FormData();
values.file.forEach((file) => {
formData.append('file', file.originFileObj as RcFile);
});
axios({
url: '/api/admin/client/importClient',
method: 'post',
responseType: 'blob',
headers: {
Authorization: localStorage.getItem('token'),
'Content-Type':
'multipart/form-data; boundary=----WebKitFormBoundarynl6gT1BKdPWIejNq',
},
data: formData,
})
.then((response) => {
let data = response.data;
if (data.type === 'application/json') {
blobToJson(data).then((dataJson) => {
if (dataJson?.result === RESPONSE_CODE.SUCCESS) {
message.success(dataJson?.message);
} else {
message.error(dataJson?.message);
}
});
} else {
message.error('上传失败,已下载错误信息表格');
// 创建一个新的 Blob 对象,它包含了服务器响应的数据(即你的 Excel 文件)
const blob = new Blob([response.data]); // Excel 的 MIME 类型
const downloadUrl = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = downloadUrl;
a.download = '银行流水导入模板.xlsx'; // 你可以为文件命名
document.body.appendChild(a);
a.click(); // 模拟点击操作来下载文件
URL.revokeObjectURL(downloadUrl); // 释放掉 blob 对象所占用的内存
document.body.removeChild(a);
}
})
.catch((error) => {
// 处理错误
message.error('系统出现异常了,请联系管理员', error);
})
.finally(() => {});
return true;
}}
>
<ProFormUploadDragger max={1} label="上传" name="file" />
<Button type="link" onClick={downloadImportTemplate}>
下载导入模板
</Button>
{contextHolder}
</ModalForm>
);
};