request.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import fetch from 'dva/fetch';
  2. import { notification } from 'antd';
  3. import router from 'umi/router';
  4. import hash from 'hash.js';
  5. import { isAntdPro } from './utils';
  6. const codeMessage = {
  7. 200: '服务器成功返回请求的数据。',
  8. 201: '新建或修改数据成功。',
  9. 202: '一个请求已经进入后台排队(异步任务)。',
  10. 204: '删除数据成功。',
  11. 400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
  12. 401: '用户没有权限(令牌、用户名、密码错误)。',
  13. 403: '用户得到授权,但是访问是被禁止的。',
  14. 404: '发出的请求针对的是不存在的记录,服务器没有进行操作。',
  15. 406: '请求的格式不可得。',
  16. 410: '请求的资源被永久删除,且不会再得到的。',
  17. 422: '当创建一个对象时,发生一个验证错误。',
  18. 500: '服务器发生错误,请检查服务器。',
  19. 502: '网关错误。',
  20. 503: '服务不可用,服务器暂时过载或维护。',
  21. 504: '网关超时。',
  22. };
  23. const checkStatus = response => {
  24. if (response.status >= 200 && response.status < 300) {
  25. return response;
  26. }
  27. const errortext = codeMessage[response.status] || response.statusText;
  28. notification.error({
  29. message: `请求错误 ${response.status}: ${response.url}`,
  30. description: errortext,
  31. });
  32. const error = new Error(errortext);
  33. error.name = response.status;
  34. error.response = response;
  35. throw error;
  36. };
  37. const cachedSave = (response, hashcode) => {
  38. /**
  39. * Clone a response data and store it in sessionStorage
  40. * Does not support data other than json, Cache only json
  41. */
  42. const contentType = response.headers.get('Content-Type');
  43. if (contentType && contentType.match(/application\/json/i)) {
  44. // All data is saved as text
  45. response
  46. .clone()
  47. .text()
  48. .then(content => {
  49. sessionStorage.setItem(hashcode, content);
  50. sessionStorage.setItem(`${hashcode}:timestamp`, Date.now());
  51. });
  52. }
  53. return response;
  54. };
  55. /**
  56. * Requests a URL, returning a promise.
  57. *
  58. * @param {string} url The URL we want to request
  59. * @param {object} [option] The options we want to pass to "fetch"
  60. * @return {object} An object containing either "data" or "err"
  61. */
  62. export default function request(url, option) {
  63. const options = {
  64. expirys: isAntdPro(),
  65. ...option,
  66. };
  67. /**
  68. * Produce fingerprints based on url and parameters
  69. * Maybe url has the same parameters
  70. */
  71. const fingerprint = url + (options.body ? JSON.stringify(options.body) : '');
  72. const hashcode = hash
  73. .sha256()
  74. .update(fingerprint)
  75. .digest('hex');
  76. const defaultOptions = {
  77. credentials: 'include',
  78. };
  79. const newOptions = { ...defaultOptions, ...options };
  80. if (
  81. newOptions.method === 'POST' ||
  82. newOptions.method === 'PUT' ||
  83. newOptions.method === 'DELETE'
  84. ) {
  85. if (!(newOptions.body instanceof FormData)) {
  86. newOptions.headers = {
  87. Accept: 'application/json',
  88. 'Content-Type': 'application/json; charset=utf-8',
  89. ...newOptions.headers,
  90. };
  91. newOptions.body = JSON.stringify(newOptions.body);
  92. } else {
  93. // newOptions.body is FormData
  94. newOptions.headers = {
  95. Accept: 'application/json',
  96. ...newOptions.headers,
  97. };
  98. }
  99. }
  100. const expirys = options.expirys && 60;
  101. // options.expirys !== false, return the cache,
  102. if (options.expirys !== false) {
  103. const cached = sessionStorage.getItem(hashcode);
  104. const whenCached = sessionStorage.getItem(`${hashcode}:timestamp`);
  105. if (cached !== null && whenCached !== null) {
  106. const age = (Date.now() - whenCached) / 1000;
  107. if (age < expirys) {
  108. const response = new Response(new Blob([cached]));
  109. return response.json();
  110. }
  111. sessionStorage.removeItem(hashcode);
  112. sessionStorage.removeItem(`${hashcode}:timestamp`);
  113. }
  114. }
  115. return fetch(url, newOptions)
  116. .then(checkStatus)
  117. .then(response => cachedSave(response, hashcode))
  118. .then(response => {
  119. // DELETE and 204 do not return data by default
  120. // using .json will report an error.
  121. if (newOptions.method === 'DELETE' || response.status === 204) {
  122. return response.text();
  123. }
  124. return response.json();
  125. })
  126. .catch(e => {
  127. const status = e.name;
  128. if (status === 401) {
  129. // @HACK
  130. /* eslint-disable no-underscore-dangle */
  131. window.g_app._store.dispatch({
  132. type: 'login/logout',
  133. });
  134. return;
  135. }
  136. // environment should not be used
  137. if (status === 403) {
  138. router.push('/exception/403');
  139. return;
  140. }
  141. if (status <= 504 && status >= 500) {
  142. router.push('/exception/500');
  143. return;
  144. }
  145. if (status >= 404 && status < 422) {
  146. router.push('/exception/404');
  147. }
  148. });
  149. }