index.tsx
4.3 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
import React, { useState, useEffect } from 'react';
import { message, Upload, notification, Button } from 'antd';
import { uploadToken } from '@/services/upload';
import { UploadFile, UploadListType, UploadProps } from 'antd/lib/upload/interface';
import { UploadOutlined } from '@ant-design/icons';
import cx from 'classnames';
interface UploadFileProps {
accept?: string;
listType?: UploadListType;
Callback?: (e: any) => void;
num?: number;
action?: string;
name?: string | undefined;
maxSize?: number;
onChange?: (val: any) => void;
value?: any;
[prop: string]: any;
}
type Config = Record<
| 'action'
| 'OSSAccessKeyId'
| 'key'
| 'policy'
| 'signature'
| 'callback'
| 'x:access_token'
// | 'x-oss-security-token'
| 'success_action_status',
string
>;
// picture-card
const UploadFiles: React.FC<UploadFileProps> = (props) => {
const {
value,
onChange,
Callback,
accept = '*.*',
maxSize = 52428800,
children,
multiple,
object_type,
} = props;
const [fileLists, setFileList] = useState<Array<UploadFile>>([]);
const [config, setConfig] = useState<Config>();
//解决Warning: Can‘t perform a React state update on an unmounted component.问题
const [isFlag, setIsFlag] = useState(true);
useEffect(() => {
if (!isFlag) {
return;
}
uploadToken({
access_type: 'web_upload',
object_type: object_type || 'recruit',
}).then((res) => {
if (res.errors) {
notification.open({
message: '错误',
description: res.message,
duration: 2,
});
} else {
const data = {
action: `https://${res.bucket}.${res.domain}/`,
OSSAccessKeyId: res.access_key_id,
key: res.object_path + '/' + '${filename}',
policy: res.policy,
signature: res.signature,
callback: res.callback_body,
'x:access_token': res.callback_token,
// 测试环境不能使用
// 'x-oss-security-token': res.security_token,
success_action_status: '200',
};
setConfig(data);
}
});
return () => setIsFlag(false);
}, []);
const onchange = ({ fileList }: { fileList: any }) => {
if (fileList[fileList.length - 1].status === 'done') {
// setFileList([...fileLists, fileList[fileList.length - 1]]);
if (onChange) {
onChange([...fileLists, fileList[fileList.length - 1]]);
}
if (Callback) Callback([...fileLists, fileList[fileList.length - 1]]);
}
};
const onRemove = (file: UploadFile) => {
const files = value.filter((v: { uid: string }) => v.uid !== file.uid);
setFileList(files);
if (onChange) {
onChange(files);
}
if (Callback) Callback([...files]);
};
const onPreview = async (file: any) => {
let src = file.url;
if (!src) {
src = await new Promise((resolve) => {
const reader = new FileReader();
reader.readAsDataURL(file.originFileObj);
reader.onload = () => resolve(reader.result);
});
}
const image = new Image();
image.src = src;
const imgWindow = window.open(src);
imgWindow?.document.write(image.outerHTML);
};
const beforeUpload = (file: UploadFile) => {
// 如果限制了上传类型
if (accept !== '*.*') {
const { name } = file;
const accepts = accept.toLowerCase().split(',');
const names = name.toLowerCase().split('.');
const extName = names[names.length - 1];
if (!accepts.includes(`.${extName}`)) {
message.error('不支持的文件类型!');
return false;
}
}
// 如果限制了上传大小
if (maxSize) {
const { size = 0 } = file;
if (size > maxSize) {
message.error('文件太大了!');
return false;
}
}
return true;
};
const uploadProps: UploadProps = {
accept: props.accept || '*.*',
action: config?.action,
name: 'file',
data: config,
showUploadList: false,
multiple: multiple || false,
beforeUpload: beforeUpload,
onPreview: onPreview,
onChange: onchange,
onRemove: onRemove,
};
return (
<div className={cx('global_upload')}>
<Upload {...uploadProps}>
{children || <Button icon={<UploadOutlined />}>上传文件</Button>}
</Upload>
</div>
);
};
export default UploadFiles;