-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathHttpUtil.js
122 lines (105 loc) · 3.5 KB
/
HttpUtil.js
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
/**
* Created by wuyunqiang on 2017/6/12.
*/
import {DeviceEventEmitter} from 'react-native';
const HOST = 'http://172.28.111.198:887/api.php';
function toParameterString(obj) {
return obj ? Object.keys(obj).sort().map(function (key) {
var val = obj[key];
if (Array.isArray(val)) {
return val.sort().map(function (val2) {
if (!val2) val2 = "";
return encodeURIComponent(key) + '=' + encodeURIComponent(val2);
}).join('&');
}
if (!val) val = "";
return encodeURIComponent(key) + '=' + encodeURIComponent(val);
}).join('&') : '';
}
export default class HttpRequest {
constructor() {
this.handleResponse = this.handleResponse.bind(this);
}
handleResponse(response) {
console.log('response',response);
const resJson = JSON.parse(response.body);
if(resJson.status !== '000000'&&resJson.status !== 200){
return false;
}
if (response.err)
throw response.err;
return resJson;
}
async GET(url) {
let result = await fetch(HOST + url , {
method: 'GET',
credentials: 'include',
headers: {
'Cookie':'PHPSESSID=2psj93stkrg339qnkdj1qgjok7',
'Accept': 'application/json',
// 'Content-Type': 'application/json;charset=UTF-8'
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(this.handleResponse).catch((error) => {
console.log(error)
});
return result;
}
async POST(url, params,headers) {
let result = await fetch(HOST + url, {
method: 'POST',
credentials: 'include',
headers: {
'Cookie':'PHPSESSID=2psj93stkrg339qnkdj1qgjok7',
'Accept': 'application/json',
// 'Content-Type': 'application/json;charset=UTF-8'
'Content-Type': 'application/x-www-form-urlencoded'
},
body: params
}).then(this.handleResponse).catch((error) => {
Log(error);
});
return result;
}
async POSTBODY(url, pars, headers, security) {
let result = await fetch(HOST + url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest',
// ...HEADERS,
...headers,
},
body: JSON.stringify(pars)
}).then(this.handleResponse).catch((error) => {
Log(error);
});
return result;
}
// type : 'image/png' 'image/jpeg'
// async POSTIMAGE(imageSource, type, headers, security) {
//
// let formData = new FormData();
// let file = {uri: imageSource, type: type, name: 'jpg'};
// formData.append('file', file);
//
// let HEADERS = {
// 'Content-Type': 'multipart/form-data'
// };
// let result = await fetch(HOST + imageURL, {
// method: 'POST',
// headers: {
// 'Content-Type': 'multipart/form-data',
// ...HEADERS,
// ...headers,
// },
// body: formData
// }).then((response) => response.json()).catch((error) => {
// Log(error);
// });
//
// return result;
// }
}
global.HttpUtil = new HttpRequest();