-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrequest.tsx
211 lines (195 loc) · 4.66 KB
/
request.tsx
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
import React, { Component } from 'react';
import { Icon, Progress } from 'antd';
import Notification from 'rc-notification';
import 'rc-notification/assets/index.css';
import _ from 'lodash';
import { Response } from './interface';
import auth from './Auth/auth';
let notification: any;
Notification.newInstance(
{
style: {
top: 24,
right: 0,
zIndex: 1001,
},
},
(n: any) => {
notification = n;
},
);
/**
* 后端接口非 5xx 都会返回 2xx
* 异常都是通过 res.err 来判断,res.err 有值则请求失败。res.err 是具体的错误信息
* res.err 为 'unauthorized' 约定的未授权状态
*/
interface Props {
duration: number;
msg: string;
onClose: () => void;
}
class ErrNotifyContent extends Component<Props> {
timerId = 0;
state = {
percent: 0,
};
componentDidMount = () => {
this.setUpTimer();
};
componentWillUnmount() {
if (this.timerId) {
window.clearInterval(this.timerId);
}
}
setUpTimer() {
const { duration, onClose } = this.props;
let { percent } = this.state;
this.timerId = window.setInterval(() => {
if (percent < 100) {
percent += 10 / duration;
this.setState({ percent });
} else {
window.clearInterval(this.timerId);
onClose();
}
}, 100);
}
render() {
const { msg } = this.props;
const { percent } = this.state;
return (
<div
style={{
width: 350,
padding: '16px 24px',
}}
onMouseOver={() => {
if (this.timerId) {
window.clearInterval(this.timerId);
this.setState({ percent: 0 });
}
}}
onMouseOut={() => {
this.setUpTimer();
}}
onFocus={() => {}}
onBlur={() => {}}
>
<Progress
className="ecmc-errNotify-progress"
percent={percent}
showInfo={false}
style={{
position: 'absolute',
bottom: 0,
left: 0,
opacity: 0.2,
}}
/>
<Icon
type="close-circle"
style={{
color: '#f5222d',
fontSize: 24,
}}
/>
<div
style={{
display: 'inline-block',
fontSize: 16,
lineHeight: '24px',
verticalAlign: 'top',
marginLeft: 10,
}}
>
请求错误
</div>
<div
style={{
marginLeft: 35,
}}
>
{msg}
</div>
</div>
);
}
}
export function errNotify(errMsg: string) {
const notifyId = _.uniqueId('notifyId_');
notification.notice({
key: notifyId,
duration: 0,
closable: true,
style: {
right: '20px',
},
content: (
<ErrNotifyContent
msg={errMsg}
duration={5}
onClose={() => {
notification.removeNotice(notifyId);
}}
/>
),
});
}
export async function processResponse(response: any, isUseDefaultErrNotify: boolean, redirectToLogin: boolean) {
const data: Response = await response.json();
if (response.status < 200 || response.status >= 300) {
if (response.status === 401) {
window.location.href = '/login';
}
if (response.status === 403) {
window.location.href = '/403';
}
if (data.err.indexOf('can not found') === -1) {
errNotify(response.statusText);
}
throw new Error(response.statusText);
}
if (typeof data === 'object' && data.err !== '') {
if (data.err === 'unauthorized') {
if (redirectToLogin && window.location.pathname !== '/login') {
try {
const redirect = window.location.pathname;
await auth.authorize({ redirect });
} catch (e) {
console.log(e);
}
}
throw new Error('unauthorized');
} else if (data.err === 'forbidden' || data.err === 'permission deny') {
window.location.href = '/403';
} else {
if (isUseDefaultErrNotify && data.err.indexOf('can not found') === -1) {
errNotify(data.err);
}
throw new Error(data.err);
}
}
return data;
}
export default async function request(
url: any,
options?: any,
isUseDefaultErrNotify = true,
redirectToLogin = true,
) {
let realUrl = url;
let realOptions = options;
if (typeof url === 'object' && url.url) {
realUrl = url.url;
realOptions = url;
delete realOptions.url;
}
const response = await fetch(realUrl, {
headers: {
'content-type': 'application/json',
},
...realOptions,
});
const data = await processResponse(response, isUseDefaultErrNotify, redirectToLogin);
return data.dat;
}