Keine Beschreibung

HttpTools.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * Created by zack on 2018/3/14.
  3. */
  4. import {BaseUrl} from './API'
  5. import RNFetchBlob from 'rn-fetch-blob'
  6. const HttpTools = {
  7. post: (url, param, success, failure) => {
  8. const requestUrl = BaseUrl + url
  9. fetch(requestUrl, {
  10. credentials: 'include',
  11. method: 'POST',
  12. body: param,
  13. headers: {
  14. 'Content-Type': 'application/json',
  15. 'Authorization': "Bearer " + (global.token ? global.token : '')
  16. },
  17. }).then((response) => { //还要同时添加当前的header到下一个callback即可
  18. if (response.ok) {
  19. if (response.status === 204) {
  20. return {status: '204'}
  21. }else {
  22. return response.json()
  23. }
  24. } else if (response.status === 401) {
  25. //auth failed..
  26. }
  27. }
  28. ).then((data) => {
  29. if (data) {
  30. success(data)
  31. }else {
  32. failure(data)
  33. }
  34. })
  35. },
  36. get: (url, param, success, failure) => {
  37. let paramsString = ''
  38. if (param) {
  39. paramsString = Object.keys(param)
  40. .map(k => encodeURIComponent(k) + '=' + encodeURIComponent(param[k]))
  41. .join('&');
  42. }
  43. let requestUrl = ''
  44. if (paramsString.length) {
  45. requestUrl = BaseUrl + url + '?' + paramsString
  46. }else {
  47. requestUrl = BaseUrl + url
  48. }
  49. fetch(requestUrl, {
  50. credentials: 'include',
  51. method: 'GET',
  52. headers: {
  53. 'Content-Type': 'application/json',
  54. 'Authorization': "Bearer " + (global.token ? global.token : '')
  55. },
  56. }).then((response) => {
  57. if (response.ok) {
  58. if (response.status === 204) {
  59. return {status: '204'}
  60. }else {
  61. return response.json()
  62. }
  63. } else if (response.status === 401) {
  64. }
  65. }
  66. ).then((data) => {
  67. if (data) {
  68. success(data)
  69. }else {
  70. failure(data)
  71. }
  72. })
  73. },
  74. put: (url, param, success, failure) => {
  75. const requestUrl = BaseUrl + url
  76. fetch(requestUrl, {
  77. credentials: 'include',
  78. method: 'PUT',
  79. body: param,
  80. headers: {
  81. 'Content-Type': 'application/json',
  82. 'Authorization': "Bearer " + (global.token ? global.token : '')
  83. },
  84. }).then((response) => { //还要同时添加当前的header到下一个callback即可
  85. if (response.ok) {
  86. if (response.status === 204) {
  87. return {status: '204'}
  88. }else {
  89. return response.json()
  90. }
  91. } else if (response.status === 401) {
  92. }
  93. }
  94. ).then((data) => {
  95. if (data) {
  96. success(data)
  97. }else {
  98. failure(data)
  99. }
  100. })
  101. },
  102. delete: (url, param, success, failure) => {
  103. const requestUrl = BaseUrl + url
  104. fetch(requestUrl, {
  105. credentials: 'include',
  106. method: 'DELETE',
  107. body: param,
  108. headers: {
  109. 'Content-Type': 'application/json',
  110. 'Authorization': "Bearer " + (global.token ? global.token : '')
  111. },
  112. }).then((response) => { //还要同时添加当前的header到下一个callback即可
  113. if (response.ok) {
  114. if (response.status === 204) {
  115. return {status: '204'}
  116. }else {
  117. return response.json()
  118. }
  119. } else if (response.status === 401) {
  120. }
  121. }
  122. ).then((data) => {
  123. if (data) {
  124. success(data)
  125. }else {
  126. failure(data)
  127. }
  128. })
  129. },
  130. uploadImage: (url, base64ImageString, success, failure) => {
  131. RNFetchBlob.fetch('PUT', BaseUrl + url, {
  132. 'Content-Type' : 'multipart/form-data',
  133. 'Authorization': "Bearer " + (global.token ? global.token : '')
  134. // here's the body you're going to send, should be a BASE64 encoded string
  135. // (you can use "base64"(refer to the library 'mathiasbynens/base64') APIs to make one).
  136. // The data will be converted to "byte array"(say, blob) before request sent.
  137. }, [{ name : 'file', data: base64ImageString},])
  138. .then((res) => {
  139. console.log(res.text())
  140. success()
  141. })
  142. .catch((err) => {
  143. // error handling ..
  144. console.log(err)
  145. failure()
  146. })
  147. },
  148. postUserInfo: (url, param, success, failure) => {
  149. //因为当前没有统一URL,获取token接口暂时分离
  150. const requestUrl = 'http://api.links123.net/uc/v2/' + url
  151. fetch(requestUrl, {
  152. credentials: 'include',
  153. method: 'POST',
  154. body: param,
  155. headers: {
  156. 'Content-Type': 'application/json',
  157. 'Authorization': "Bearer " + (global.token ? global.token : '')
  158. },
  159. }).then((response) => {
  160. if (response.ok) {
  161. if (response.status === 204) {
  162. success({})
  163. }else {
  164. return response.json()
  165. }
  166. } else if (response.status === 401) {
  167. }
  168. }
  169. ).then((data) => {
  170. if (data) {
  171. success(data)
  172. }else {
  173. failure(data)
  174. }
  175. })
  176. },
  177. }
  178. export default HttpTools