123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- /**
- * 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
|