Skip to content

Commit db42934

Browse files
authored
feat: 使用fetch替换axios.get (#441)
1 parent d745930 commit db42934

File tree

2 files changed

+134
-133
lines changed

2 files changed

+134
-133
lines changed

web/src/common/request.js

+133-126
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,153 @@
1-
import axios from 'axios'
2-
import {server} from "./env";
3-
import {message} from 'antd';
4-
import {getHeaders} from "../utils/utils";
1+
import axios from 'axios';
2+
import { server } from './env';
3+
import { message } from 'antd';
4+
import { getHeaders } from '../utils/utils';
55

66
// 测试地址
77
// axios.defaults.baseURL = server;
88
// 线上地址
99
axios.defaults.baseURL = server;
1010

1111
const handleError = (error) => {
12-
if ("Network Error" === error.toString()) {
13-
message.error('网络异常');
14-
return false;
15-
}
16-
if (error.response !== undefined && error.response.status === 401) {
17-
window.location.href = '#/login';
18-
return false;
19-
}
20-
if (error.response !== undefined) {
21-
message.error(error.response.data.message);
22-
return false;
23-
}
24-
return true;
12+
if ('Network Error' === error.toString()) {
13+
message.error('网络异常');
14+
return false;
15+
}
16+
if (error.response !== undefined && error.response.status === 401) {
17+
window.location.href = '#/login';
18+
return false;
19+
}
20+
if (error.response !== undefined) {
21+
message.error(error.response.data.message);
22+
return false;
23+
}
24+
return true;
2525
};
2626

2727
const handleResult = (result) => {
28-
if (result['code'] === 401) {
29-
window.location.href = '#/login';
30-
return false;
31-
}if (result['code'] === 403) {
32-
window.location.href = '#/permission-denied';
33-
return false;
34-
} else if (result['code'] === 100) {
35-
return true;
36-
} else if (result['code'] !== 1) {
37-
message.error(result['message']);
38-
return false;
39-
}
28+
if (result['code'] === 401) {
29+
window.location.href = '#/login';
30+
return false;
31+
}
32+
if (result['code'] === 403) {
33+
window.location.href = '#/permission-denied';
34+
return false;
35+
} else if (result['code'] === 100) {
4036
return true;
41-
}
37+
} else if (result['code'] !== 1) {
38+
message.error(result['message']);
39+
return false;
40+
}
41+
return true;
42+
};
4243

4344
const request = {
44-
45-
get: function (url) {
46-
const headers = getHeaders();
47-
48-
return new Promise((resolve, reject) => {
49-
axios.get(url, {headers: headers})
50-
.then((response) => {
51-
let contentType = response.headers['content-type'];
52-
if (contentType !== '' && contentType.includes('application/json')) {
53-
handleResult(response.data);
54-
}
55-
resolve(response.data);
56-
})
57-
.catch((error) => {
58-
if (!handleError(error)) {
59-
return;
60-
}
61-
reject(error);
62-
});
45+
get: function (url) {
46+
const headers = getHeaders();
47+
48+
return new Promise((resolve, reject) => {
49+
fetch(url, { headers })
50+
.then((response) => {
51+
let contentType = response.headers.get('content-type');
52+
if (contentType !== '' && contentType.includes('application/json')) {
53+
return response.json();
54+
}
55+
return response.text();
6356
})
64-
},
65-
66-
post: function (url, params, header) {
67-
68-
const headers = getHeaders();
69-
if (header) {
70-
for (const k in header) {
71-
headers[k] = header[k];
72-
}
73-
}
74-
75-
76-
return new Promise((resolve, reject) => {
77-
axios.post(url, params, {headers: headers})
78-
.then((response) => {
79-
handleResult(response.data);
80-
resolve(response.data);
81-
})
82-
.catch((error) => {
83-
if (!handleError(error)) {
84-
return;
85-
}
86-
reject(error);
87-
});
57+
.then((result) => {
58+
if (typeof result === 'object') {
59+
handleResult(result);
60+
}
61+
resolve(result);
8862
})
89-
},
90-
91-
put: function (url, params) {
92-
93-
const headers = getHeaders();
63+
.catch((error) => {
64+
if (!handleError(error)) {
65+
return;
66+
}
67+
reject(error);
68+
});
69+
});
70+
},
71+
72+
post: function (url, params, header) {
73+
const headers = getHeaders();
74+
if (header) {
75+
for (const k in header) {
76+
headers[k] = header[k];
77+
}
78+
}
9479

95-
return new Promise((resolve, reject) => {
96-
axios.put(url, params, {headers: headers})
97-
.then((response) => {
98-
handleResult(response.data);
99-
resolve(response.data);
100-
})
101-
.catch((error) => {
102-
if (!handleError(error)) {
103-
return;
104-
}
105-
reject(error);
106-
});
80+
return new Promise((resolve, reject) => {
81+
axios
82+
.post(url, params, { headers: headers })
83+
.then((response) => {
84+
handleResult(response.data);
85+
resolve(response.data);
10786
})
108-
},
109-
110-
delete: function (url) {
111-
const headers = getHeaders();
112-
113-
return new Promise((resolve, reject) => {
114-
axios.delete(url, {headers: headers})
115-
.then((response) => {
116-
handleResult(response.data);
117-
resolve(response.data);
118-
})
119-
.catch((error) => {
120-
if (!handleError(error)) {
121-
return;
122-
}
123-
reject(error);
124-
});
87+
.catch((error) => {
88+
if (!handleError(error)) {
89+
return;
90+
}
91+
reject(error);
92+
});
93+
});
94+
},
95+
96+
put: function (url, params) {
97+
const headers = getHeaders();
98+
99+
return new Promise((resolve, reject) => {
100+
axios
101+
.put(url, params, { headers: headers })
102+
.then((response) => {
103+
handleResult(response.data);
104+
resolve(response.data);
125105
})
126-
},
127-
128-
patch: function (url, params) {
129-
const headers = getHeaders();
130-
131-
return new Promise((resolve, reject) => {
132-
axios.patch(url, params, {headers: headers})
133-
.then((response) => {
134-
handleResult(response.data);
135-
resolve(response.data);
136-
})
137-
.catch((error) => {
138-
if (!handleError(error)) {
139-
return;
140-
}
141-
reject(error);
142-
});
106+
.catch((error) => {
107+
if (!handleError(error)) {
108+
return;
109+
}
110+
reject(error);
111+
});
112+
});
113+
},
114+
115+
delete: function (url) {
116+
const headers = getHeaders();
117+
118+
return new Promise((resolve, reject) => {
119+
axios
120+
.delete(url, { headers: headers })
121+
.then((response) => {
122+
handleResult(response.data);
123+
resolve(response.data);
124+
})
125+
.catch((error) => {
126+
if (!handleError(error)) {
127+
return;
128+
}
129+
reject(error);
130+
});
131+
});
132+
},
133+
134+
patch: function (url, params) {
135+
const headers = getHeaders();
136+
137+
return new Promise((resolve, reject) => {
138+
axios
139+
.patch(url, params, { headers: headers })
140+
.then((response) => {
141+
handleResult(response.data);
142+
resolve(response.data);
143143
})
144-
},
144+
.catch((error) => {
145+
if (!handleError(error)) {
146+
return;
147+
}
148+
reject(error);
149+
});
150+
});
151+
},
145152
};
146-
export default request
153+
export default request;

web/src/components/devops/FileSystem.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -370,16 +370,10 @@ class FileSystem extends Component {
370370
showEditor = async (name, key) => {
371371
message.loading({key: key, content: 'Loading'})
372372
let fileContent = await request.get(`${server}/${this.state.storageType}/${this.state.storageId}/download?file=${window.encodeURIComponent(key)}&t=${new Date().getTime()}`);
373-
let content = ""
374-
try {
375-
content = JSON.stringify(fileContent);
376-
}catch (e) {
377-
content = fileContent+"";
378-
}
379373
this.setState({
380374
currentFileKey: key,
381375
fileName: name,
382-
fileContent: content,
376+
fileContent: fileContent,
383377
editorVisible: true
384378
})
385379
message.destroy(key);

0 commit comments

Comments
 (0)