request.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import SyncStorage from 'sync-storage';
  2. import {Alert} from 'react-native';
  3. const codeMessage = {
  4. 200: '服务器成功返回请求的数据。',
  5. 201: '新建或修改数据成功。',
  6. 202: '一个请求已经进入后台排队(异步任务)。',
  7. 204: '删除数据成功。',
  8. 400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
  9. 401: '用户没有权限(令牌、用户名、密码错误)。',
  10. 403: '用户得到授权,但是访问是被禁止的。',
  11. 404: '发出的请求针对的是不存在的记录,服务器没有进行操作。',
  12. 406: '请求的格式不可得。',
  13. 410: '请求的资源被永久删除,且不会再得到的。',
  14. 422: '当创建一个对象时,发生一个验证错误。',
  15. 500: '服务器发生错误,请检查服务器。',
  16. 502: '网关错误。',
  17. 503: '服务不可用,服务器暂时过载或维护。',
  18. 504: '网关超时。',
  19. };
  20. const checkStatus = response => {
  21. //console.log('************', response, response.status)
  22. if (response.status >= 200 && response.status < 300) {
  23. return response;
  24. } else if (response.status === 400) {
  25. console.log('************', response.text())
  26. response.json().then(data => {
  27. const error = new Error();
  28. error.name = response.status;
  29. error.message = data.message;
  30. throw error;
  31. });
  32. return response;
  33. }
  34. const errortext = codeMessage[response.status] || response.statusText;
  35. const error = new Error(errortext);
  36. error.name = response.status;
  37. error.response = response;
  38. throw error;
  39. };
  40. /**
  41. * Requests a URL, returning a promise.
  42. *
  43. * @param {string} url The URL we want to request
  44. * @param {object} [option] The options we want to pass to "fetch"
  45. * @return {object} An object containing either "data" or "err"
  46. */
  47. export default function request(url, option,) {
  48. const options = {
  49. //expirys: true,
  50. ...option,
  51. };
  52. const defaultOptions = {
  53. //credentials: 'include',
  54. headers: {},
  55. };
  56. const newOptions = {...defaultOptions, ...options};
  57. if (
  58. newOptions.method === 'POST' ||
  59. newOptions.method === 'PUT' ||
  60. newOptions.method === 'PATCH' ||
  61. newOptions.method === 'DELETE'
  62. ) {
  63. if (!(newOptions.body instanceof FormData)) {
  64. newOptions.headers = {
  65. Accept: 'application/json',
  66. 'Content-Type': 'application/x-www-form-urlencoded',
  67. ...newOptions.headers,
  68. };
  69. newOptions.body = JSON.stringify(newOptions.body);
  70. } else {
  71. // newOptions.body is FormData
  72. newOptions.headers = {
  73. Accept: 'application/json',
  74. ...newOptions.headers,
  75. };
  76. }
  77. }
  78. let baseURL = SyncStorage.get('baseURL');
  79. url = baseURL + url;
  80. console.log(777777777777777,url)
  81. let credential = SyncStorage.get('credential');
  82. if (credential) {
  83. newOptions.headers = {
  84. ...newOptions.headers,
  85. Authorization:`JWT ${credential.token}`,
  86. };
  87. }
  88. return fetch(url, newOptions)
  89. //.then(checkStatus)
  90. //.then(response => cachedSave(response, hashcode))
  91. .then(response => {
  92. // console.log(99999999,response)
  93. if (response.status === 400) {
  94. response.json().then(data => {
  95. if (data.message) {
  96. Alert.alert('友情提醒', data.message,
  97. [
  98. {text: '我知道了', style: 'cancel'},
  99. ],);
  100. }
  101. });
  102. return {code: 1};
  103. }
  104. else if (response.status == 460) {
  105. SyncStorage.remove('credential');
  106. Alert.alert(
  107. '友情提醒',
  108. '登录已过期,请重新登录!',
  109. [
  110. {text: '我知道了', style: 'cancel'},
  111. ],
  112. );
  113. return {code: 1};
  114. }
  115. else if (response.status == 404) {
  116. // Alert.alert(
  117. // '友情提醒',
  118. // '当前页面出错了!',
  119. // [
  120. // {text: '我知道了', style: 'cancel'},
  121. // ],
  122. // );
  123. return {code: 1};
  124. }
  125. else if (response.status == 500) {
  126. Alert.alert(
  127. '友情提醒',
  128. '当前页面出错了!',
  129. [
  130. {text: '我知道了', style: 'cancel'},
  131. ],
  132. );
  133. return {code: 1};
  134. }
  135. // DELETE and 204 do not return data by default
  136. // using .json will report an error.
  137. if (response.status === 204) {
  138. return response.text();
  139. }
  140. const resp = response.json();
  141. // if (resp.code === 1001){
  142. // // resp.code是未定义,不会执行此处
  143. // this.props.dispatch(NavigationActions.navigate({ routeName: 'Login' }));
  144. // return;
  145. // }
  146. return resp;
  147. })
  148. .catch(e => {
  149. // console.log(88888888,e)
  150. const status = e.name;
  151. if (status == 'TypeError') {
  152. // Alert.alert(
  153. // '友情提醒',
  154. // '服务器走丢了,请稍后重试!',
  155. // [
  156. // {text: '我知道了', style: 'cancel'},
  157. // ],
  158. // );
  159. }
  160. else if (status == 400) {
  161. Alert.alert(
  162. '友情提醒',
  163. e.message,
  164. [
  165. {text: '我知道了', style: 'cancel'},
  166. ],
  167. );
  168. }
  169. else if (status == 404) {
  170. Alert.alert(
  171. '友情提醒',
  172. '请求的操作不存在!',
  173. [
  174. {text: '我知道了', style: 'cancel'},
  175. ],
  176. );
  177. }
  178. else if (status == 500) {
  179. Alert.alert(
  180. '友情提醒',
  181. '远程错误!',
  182. [
  183. {text: '我知道了', style: 'cancel'},
  184. ],
  185. );
  186. }
  187. return {code: 1};
  188. });
  189. }