request.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. if (response.status >= 200 && response.status < 300) {
  22. return response;
  23. }
  24. const errortext = codeMessage[response.status] || response.statusText;
  25. const error = new Error(errortext);
  26. error.name = response.status;
  27. error.response = response;
  28. throw error;
  29. };
  30. /**
  31. * Requests a URL, returning a promise.
  32. *
  33. * @param {string} url The URL we want to request
  34. * @param {object} [option] The options we want to pass to "fetch"
  35. * @return {object} An object containing either "data" or "err"
  36. */
  37. export default function request(url, option) {
  38. const options = {
  39. //expirys: true,
  40. ...option,
  41. };
  42. const defaultOptions = {
  43. //credentials: 'include',
  44. headers: {},
  45. };
  46. const newOptions = { ...defaultOptions, ...options };
  47. if (
  48. newOptions.method === 'POST' ||
  49. newOptions.method === 'PUT' ||
  50. newOptions.method === 'DELETE'
  51. ) {
  52. if (!(newOptions.body instanceof FormData)) {
  53. newOptions.headers = {
  54. Accept: 'application/json',
  55. 'Content-Type': 'application/json; charset=utf-8',
  56. ...newOptions.headers,
  57. };
  58. newOptions.body = JSON.stringify(newOptions.body);
  59. } else {
  60. // newOptions.body is FormData
  61. newOptions.headers = {
  62. Accept: 'application/json',
  63. ...newOptions.headers,
  64. };
  65. }
  66. }
  67. const baseURL = SyncStorage.get('baseURL');
  68. url = baseURL + url;
  69. console.log(url)
  70. let credential = SyncStorage.get('credential');
  71. if(credential) {
  72. newOptions.headers = {
  73. ...newOptions.headers,
  74. Authorization:`JWT ${credential.token}`,
  75. };
  76. };
  77. return fetch(url, newOptions)
  78. .then(checkStatus)
  79. //.then(response => cachedSave(response, hashcode))
  80. .then(response => {
  81. // DELETE and 204 do not return data by default
  82. // using .json will report an error.
  83. if (newOptions.method === 'DELETE' || response.status === 204) {
  84. return response.text();
  85. }
  86. const resp = response.json();
  87. if (resp.code === 1001){
  88. // resp.code是未定义,不会执行此处
  89. this.props.dispatch(NavigationActions.navigate({ routeName: 'Login' }));
  90. return;
  91. }
  92. return resp;
  93. })
  94. .catch(e => {
  95. const status = e.name;
  96. if (status == 'TypeError' || status == 400) {
  97. Alert.alert('无效的服务器地址,请重新登录!');
  98. }
  99. if (status == 404) {
  100. Alert.alert('请求的操作不存在!');
  101. }
  102. if (status == 500) {
  103. Alert.alert('远程错误!');
  104. }
  105. });
  106. }