动态菜单和动态路由的 antd pro

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import fetch from 'dva/fetch';
  2. import { notification } from 'antd';
  3. import { routerRedux } from 'dva/router';
  4. import store from '../index';
  5. const codeMessage = {
  6. 200: '服务器成功返回请求的数据。',
  7. 201: '新建或修改数据成功。',
  8. 202: '一个请求已经进入后台排队(异步任务)。',
  9. 204: '删除数据成功。',
  10. 400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
  11. 401: '用户没有权限(令牌、用户名、密码错误)。',
  12. 403: '用户得到授权,但是访问是被禁止的。',
  13. 404: '发出的请求针对的是不存在的记录,服务器没有进行操作。',
  14. 406: '请求的格式不可得。',
  15. 410: '请求的资源被永久删除,且不会再得到的。',
  16. 422: '当创建一个对象时,发生一个验证错误。',
  17. 500: '服务器发生错误,请检查服务器。',
  18. 502: '网关错误。',
  19. 503: '服务不可用,服务器暂时过载或维护。',
  20. 504: '网关超时。',
  21. };
  22. function checkStatus(response) {
  23. if (response.status >= 200 && response.status < 300) {
  24. return response;
  25. }
  26. const errortext = codeMessage[response.status] || response.statusText;
  27. notification.error({
  28. message: `请求错误 ${response.status}: ${response.url}`,
  29. description: errortext,
  30. });
  31. const error = new Error(errortext);
  32. error.name = response.status;
  33. error.response = response;
  34. throw error;
  35. }
  36. /**
  37. * Requests a URL, returning a promise.
  38. *
  39. * @param {string} url The URL we want to request
  40. * @param {object} [options] The options we want to pass to "fetch"
  41. * @return {object} An object containing either "data" or "err"
  42. */
  43. export default function request(url, options) {
  44. const defaultOptions = {
  45. credentials: 'include',
  46. };
  47. const newOptions = { ...defaultOptions, ...options };
  48. if (newOptions.method === 'POST' || newOptions.method === 'PUT') {
  49. if (!(newOptions.body instanceof FormData)) {
  50. newOptions.headers = {
  51. Accept: 'application/json',
  52. 'Content-Type': 'application/json; charset=utf-8',
  53. ...newOptions.headers,
  54. };
  55. newOptions.body = JSON.stringify(newOptions.body);
  56. } else {
  57. // newOptions.body is FormData
  58. newOptions.headers = {
  59. Accept: 'application/json',
  60. ...newOptions.headers,
  61. };
  62. }
  63. }
  64. return fetch(url, newOptions)
  65. .then(checkStatus)
  66. .then(response => {
  67. if (newOptions.method === 'DELETE' || response.status === 204) {
  68. return response.text();
  69. }
  70. return response.json();
  71. })
  72. .catch(e => {
  73. const { dispatch } = store;
  74. const status = e.name;
  75. if (status === 401) {
  76. dispatch({
  77. type: 'login/logout',
  78. });
  79. return;
  80. }
  81. if (status === 403) {
  82. dispatch(routerRedux.push('/exception/403'));
  83. return;
  84. }
  85. if (status <= 504 && status >= 500) {
  86. dispatch(routerRedux.push('/exception/500'));
  87. return;
  88. }
  89. if (status >= 404 && status < 422) {
  90. dispatch(routerRedux.push('/exception/404'));
  91. }
  92. });
  93. }