import { Alert } from 'react-native';
//export const CTX = 'http://112.74.163.201:8085/zft_app/'; // 需要设置成ip地址,不能设置成localhost
//export const CTX = 'http://192.168.3.110:8061/zft/';
export const CTX = 'http://112.74.163.201:8061/zft/';
//export const CTX='http://47.106.22.165:8085/zft_app/';
export function processError(data) {
if (data.status === -2) { // 用户未登陆
navigation.push('Login')
} else {
Alert.alert('错误提示', data.msg);
}
}
export function httpGet(uri) {
uri = CTX + uri;
let init = {
method: 'GET',
credentials: 'include',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
};
return new Promise(function (resolve, reject) {
fetch(uri, init)
.then(response => response.json())
.then(data => {
if (data.status === 0) {
resolve(data);
} else {
processError(data);
}
}).catch(function (ex) {
reject(ex);
Alert.alert('错误提示', '网络链接出错');
});
});
}
export function httpPost(uri, params) {
uri = CTX + uri;
let init = {
method: 'POST',
credentials: 'include',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(params)
};
return new Promise(function (resolve, reject) {
fetch(uri, init)
.then(response => response.json())
.then(data => {
if (data.status === 0) {
resolve(data);
} else {
processError(data);
}
}).catch(function (ex) {
reject(ex);
Alert.alert('错误提示', '网络链接出错');
});
});
}
export function httpUpload(uri, params) {
uri = CTX + uri;
let init = {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'multipart/form-data',
},
body: params
};
return new Promise(function (resolve, reject) {
fetch(uri, init)
.then(response => response.json())
.then(data => {
if (data.status === 0) {
resolve(data);
} else {
processError(data);
}
}).catch(function (ex) {
reject(ex);
Alert.alert('错误提示', '网络链接出错');
});
});
}