fetch.js
11.6 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
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
import 'es6-promise';
import fetch from 'isomorphic-fetch';
import getLocalForage from '../storage/localForage';
import { dispatch } from './commonUtils';
import * as types from '../redux/actionTypes';
import uuidv1 from 'uuid/v1';
//请求后端环境
let SERVER_DOMAIN = '/';
// if (window.evn == 'production') {
// SERVER_DOMAIN = window.PRODUCTION_SERVER_DOMAIN;
// } else if (window.evn == 'development') {
// SERVER_DOMAIN = window.DEVELOPMENT_SERVER_DOMAIN;
// } else if (window.evn == 'test') {
// SERVER_DOMAIN = window.TEST_SERVER_DOMAIN;
// }
export const api_version = 'v1';
const checkStatus = (response) => {
switch (response.status) {
case 200:
return response;
case 409:
return response;
case 400:
return response;
case 401:
dispatch({
type: types.FETCH_FAIL,
payload: '用户认证授权失败',
meta: {
title: '用户认证授权失败',
description: '当前请求没有认证授权,请重新登录'
}
});
return response;
default:
return response;
}
};
const handleErrorStatus = (errorJSon = {}, data) => {
const { code, errors, message, title, x_request_id } = errorJSon;
const { __form_name = '' } = data, fields = [];
switch (code) {
case 400:
break;
case 401:
dispatch({
type: types.FETCH_FAIL,
payload: { message, errors, title, x_request_id }
});
break;
default:
break;
}
console.log(__form_name);
if (__form_name) {
dispatch({
type: '@@redux-form/SET_SUBMIT_FINALLY',
meta: {
form: __form_name,
fields: fields,
error: true
}
});
}
};
const parseJSON = (response, data) => {
let backJson;
try {
backJson = response.json();
handleErrorStatus(backJson, data);
} catch (error) {
backJson = {};
dispatch({
type: types.FETCH_FAIL,
payload: '解析JSON失败'
});
}
return backJson;
};
const redirectFun = (back) => {
if (back && back.redirect) {
window.location.href = back.redirect;
}
return back;
};
const getToken = () => {
return getLocalForage().getItem('tokens').then(function (value) {
const { access_token = '' } = value || {};
console.log(value);
return access_token;
}).catch(function (err) {
console.log(err);
return err;
});
};
const fetchCatch = (err) => {
console.log(err);
dispatch({
type: '@@redux-form/SET_SUBMIT_FINALLY',
meta: {
form: '',
fields: [],
error: true
}
});
return err;
};
export const get = function* (url) {
const token = yield getToken();
const defaultOpt = {
method: 'get',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
'Request-ID': uuidv1()
},
credentials: 'same-origin',
timeout: 1000 * 300
};
return yield fetch(`${SERVER_DOMAIN}${url}`, defaultOpt).then(checkStatus).then((response) => {
return parseJSON(response, unSerializeParams(url));
}).then(redirectFun).catch(fetchCatch);
};
export const post = function* (url, data = {}) {
const token = yield getToken();
const defaultOpt = {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
'Request-ID': uuidv1()
},
credentials: 'same-origin',
timeout: 1000 * 600,
body: JSON.stringify(data)
}; console.log("postpostpostpostpost", SERVER_DOMAIN, url);
return yield fetch(`${SERVER_DOMAIN}${url}`, defaultOpt).then(checkStatus).then((response) => {
return parseJSON(response, data);
}).catch(fetchCatch);
};
export const postFile = function* (url, data = {}) {
const token = yield getToken();
const defaultOpt = {
method: 'post',
headers: {
'Accept': 'application/json',
'Authorization': `Bearer ${token}`,
'Request-ID': uuidv1()
},
credentials: 'same-origin',
timeout: 1000 * 300,
body: data
};
return yield fetch(`${SERVER_DOMAIN}${url}`, defaultOpt).then(checkStatus).then(parseJSON).catch(fetchCatch);
};
export const put = function* (url, data = {}) {
const token = yield getToken();
const defaultOpt = {
method: 'put',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
'Request-ID': uuidv1()
},
credentials: 'same-origin',
timeout: 1000 * 300,
body: JSON.stringify(data)
};
return yield fetch(`${SERVER_DOMAIN}${url}`, defaultOpt).then(checkStatus).then(parseJSON).catch(fetchCatch);
};
export const del = function* (url, data = {}) {
const token = yield getToken();
const defaultOpt = {
method: 'delete',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
'Request-ID': uuidv1()
},
credentials: 'same-origin',
timeout: 1000 * 300
};
return yield fetch(`${SERVER_DOMAIN}${url}`, defaultOpt).then(checkStatus).then(parseJSON).catch(fetchCatch);
};
export const head = function* (url, data = {}) {
const token = yield getToken();
const defaultOpt = {
method: 'head',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
'Request-ID': uuidv1()
},
credentials: 'same-origin',
timeout: 1000 * 300,
body: JSON.stringify(data)
};
return yield fetch(`${SERVER_DOMAIN}${url}`, defaultOpt).then(checkStatus).then(parseJSON).catch(fetchCatch);
};
export const patch = function* (url, data = {}) {
const token = yield getToken();
const defaultOpt = {
method: 'patch',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': `Bearer ${token}`,
'Request-ID': uuidv1()
},
credentials: 'same-origin',
timeout: 1000 * 300,
body: JSON.stringify(data)
};
return yield fetch(`${SERVER_DOMAIN}${url}`, defaultOpt).then(checkStatus).then(parseJSON).catch(fetchCatch);
};
export const json = function* (url) {
const token = yield getToken();
const defaultOpt = {
method: 'get',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
'Request-ID': uuidv1()
},
credentials: 'same-origin',
timeout: 1000 * 300
};
return yield fetch(`${SERVER_DOMAIN}${url}`, defaultOpt).then(checkStatus).then(parseJSON).catch(fetchCatch);
};
export const postJson = function* (url, data = {}) {
const token = yield getToken();
const defaultOpt = {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
'Request-ID': uuidv1()
},
credentials: 'same-origin',
timeout: 1000 * 600,
body: JSON.stringify(data)
};
return yield fetch(`${SERVER_DOMAIN}${url}`, defaultOpt).then(checkStatus).then(parseJSON).catch(fetchCatch);
};
export const putJson = function* (url, data = {}) {
const token = yield getToken();
const defaultOpt = {
method: 'put',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
'Request-ID': uuidv1()
},
credentials: 'same-origin',
timeout: 1000 * 300,
body: JSON.stringify(data)
};
return yield fetch(`${SERVER_DOMAIN}${url}`, defaultOpt).then(checkStatus).then(parseJSON).catch(fetchCatch);
};
export const patchJson = function* (url, data = {}) {
const token = yield getToken();
const defaultOpt = {
method: 'patch',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
'Request-ID': uuidv1()
},
credentials: 'same-origin',
timeout: 1000 * 300,
body: JSON.stringify(data)
};
return yield fetch(`${SERVER_DOMAIN}${url}`, defaultOpt).then(checkStatus).then(parseJSON).catch(fetchCatch);
};
export const serializeParams = (params) => {
if (params && typeof (params) === "object") {
const keys = Object.keys(params);
let stringParams = ['?t=1'];
keys.map((key, i) => {
stringParams.push(key + "=" + encodeURIComponent(params[key]));
});
return stringParams.join("&");
} else if (params && typeof (params) === "string") {
return params;
} else {
return '';
}
};
export const unSerializeParams = (url) => {
const backParmas = {};
if (url && typeof (url) === "string" && url.indexOf("?") != -1) {
let parmas = [];
if (url.split("?")[1]) {
parmas = url.split("?")[1].split("&");
}
parmas.map((parma, i) => {
if (parma && parma.indexOf("=" != -1)) {
const temp = parma.split("=");
backParmas[temp[0]] = decodeURIComponent(temp[1]);
}
});
return backParmas;
} else {
return backParmas;
}
};
export async function ajaxGet(url) {
let token = '';
await getLocalForage().getItem('tokens').then(function (value) {
const { access_token = '' } = value || {};
token = access_token;
return access_token;
}).catch(function (err) {
console.log(err);
return err;
});
const defaultOpt = {
method: 'get',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
'Request-ID': uuidv1()
},
credentials: 'same-origin',
timeout: 1000 * 300
};
return fetch(`${SERVER_DOMAIN}${url}`, defaultOpt).then(checkStatus).then((response) => {
return parseJSON(response, unSerializeParams(url));
}).then(redirectFun).catch(fetchCatch);
}
export async function ajaxPost(url, data = {}) {
let token = '';
await getLocalForage().getItem('tokens').then(function (value) {
const { access_token = '' } = value || {};
token = access_token;
return access_token;
}).catch(function (err) {
console.log(err);
return err;
});
const defaultOpt = {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
'Request-ID': uuidv1()
},
credentials: 'same-origin',
timeout: 1000 * 600,
body: JSON.stringify(data)
};
return fetch(`${SERVER_DOMAIN}${url}`, defaultOpt).then(checkStatus).then((response) => {
return parseJSON(response, data);
}).catch(fetchCatch);
}