/** * Created by zack on 2018/3/14. */ import {BaseUrl} from './API' import RNFetchBlob from 'rn-fetch-blob' const HttpTools = { post: (url, param, success, failure) => { const requestUrl = BaseUrl + url fetch(requestUrl, { credentials: 'include', method: 'POST', body: param, headers: { 'Content-Type': 'application/json', 'Authorization': "Bearer " + (global.token ? global.token : '') }, }).then((response) => { //还要同时添加当前的header到下一个callback即可 if (response.ok) { if (response.status === 204) { return {status: '204'} }else { return response.json() } } else if (response.status === 401) { //auth failed.. } } ).then((data) => { if (data) { success(data) }else { failure(data) } }) }, get: (url, param, success, failure) => { let paramsString = '' if (param) { paramsString = Object.keys(param) .map(k => encodeURIComponent(k) + '=' + encodeURIComponent(param[k])) .join('&'); } let requestUrl = '' if (paramsString.length) { requestUrl = BaseUrl + url + '?' + paramsString }else { requestUrl = BaseUrl + url } fetch(requestUrl, { credentials: 'include', method: 'GET', headers: { 'Content-Type': 'application/json', 'Authorization': "Bearer " + (global.token ? global.token : '') }, }).then((response) => { if (response.ok) { if (response.status === 204) { return {status: '204'} }else { return response.json() } } else if (response.status === 401) { } } ).then((data) => { if (data) { success(data) }else { failure(data) } }) }, put: (url, param, success, failure) => { const requestUrl = BaseUrl + url fetch(requestUrl, { credentials: 'include', method: 'PUT', body: param, headers: { 'Content-Type': 'application/json', 'Authorization': "Bearer " + (global.token ? global.token : '') }, }).then((response) => { //还要同时添加当前的header到下一个callback即可 if (response.ok) { if (response.status === 204) { return {status: '204'} }else { return response.json() } } else if (response.status === 401) { } } ).then((data) => { if (data) { success(data) }else { failure(data) } }) }, delete: (url, param, success, failure) => { const requestUrl = BaseUrl + url fetch(requestUrl, { credentials: 'include', method: 'DELETE', body: param, headers: { 'Content-Type': 'application/json', 'Authorization': "Bearer " + (global.token ? global.token : '') }, }).then((response) => { //还要同时添加当前的header到下一个callback即可 if (response.ok) { if (response.status === 204) { return {status: '204'} }else { return response.json() } } else if (response.status === 401) { } } ).then((data) => { if (data) { success(data) }else { failure(data) } }) }, uploadImage: (url, base64ImageString, success, failure) => { RNFetchBlob.fetch('PUT', BaseUrl + url, { 'Content-Type' : 'multipart/form-data', 'Authorization': "Bearer " + (global.token ? global.token : '') // here's the body you're going to send, should be a BASE64 encoded string // (you can use "base64"(refer to the library 'mathiasbynens/base64') APIs to make one). // The data will be converted to "byte array"(say, blob) before request sent. }, [{ name : 'file', data: base64ImageString},]) .then((res) => { console.log(res.text()) success() }) .catch((err) => { // error handling .. console.log(err) failure() }) }, postUserInfo: (url, param, success, failure) => { //因为当前没有统一URL,获取token接口暂时分离 const requestUrl = 'http://api.links123.net/uc/v2/' + url fetch(requestUrl, { credentials: 'include', method: 'POST', body: param, headers: { 'Content-Type': 'application/json', 'Authorization': "Bearer " + (global.token ? global.token : '') }, }).then((response) => { if (response.ok) { if (response.status === 204) { success({}) }else { return response.json() } } else if (response.status === 401) { } } ).then((data) => { if (data) { success(data) }else { failure(data) } }) }, } export default HttpTools