request.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import fetch from 'dva/fetch';
  2. import { message, notification } from 'antd';
  3. import { routerRedux } from 'dva/router';
  4. import store from '../index';
  5. // HTTP响应状态码
  6. const httpCodeMessage = {
  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. // 自定义响应状态码
  24. const customCodeMessage = {
  25. 10004: 'Token认证失败',
  26. 10003: 'Token失效',
  27. 10002: '账号或密码错误',
  28. };
  29. /**
  30. * 检查HTTP响应状态码,>=200 && < 300正常
  31. */
  32. function httpErrorHandler(response) {
  33. if (response.status >= 200 && response.status < 300) {
  34. return response;
  35. }
  36. const errortext = httpCodeMessage[response.status] || response.statusText;
  37. notification.error({
  38. message: `HTTP错误 ${response.status}: ${response.url}`,
  39. description: errortext,
  40. });
  41. const error = new Error(errortext);
  42. error.response = response;
  43. throw error;
  44. }
  45. /**
  46. * @desc 拦截接口返回的数据状态,提示错误内容
  47. * @return {[object]} { res: data }
  48. */
  49. function apiErrorHandler(data) {
  50. if (!data.success) {
  51. const errortext = customCodeMessage[data.code] || data.message;
  52. // 登录密码错误
  53. if (data.code === 10002) {
  54. message.error(errortext);
  55. // Token失效,跳转到登录界面
  56. } else if (data.code === 10003 || data.code === 10004) {
  57. const { dispatch } = store;
  58. dispatch(routerRedux.push('/user/login'));
  59. // 其它错误打出错误代码
  60. } else {
  61. message.error(`请求错误 错误代码:${data.code} 错误信息:${errortext}`);
  62. }
  63. }
  64. return data;
  65. }
  66. /**
  67. * response为promise对象,转换为json
  68. */
  69. function promise2Json(response) {
  70. return response.json();
  71. }
  72. /**
  73. * Requests a URL, returning an object or none.
  74. *
  75. * @param {string} url The URL we want to request
  76. * @param {object} [options] The options we want to pass to "fetch"
  77. * @return {object} An object containing either "data" or "err"
  78. */
  79. export default function request(url, options) {
  80. const defaultOptions = {
  81. credentials: 'include', // with cookies(Post CORS requests)
  82. };
  83. const newOptions = { ...defaultOptions, ...options };
  84. newOptions.method = (newOptions.method || 'GET').toUpperCase();
  85. if (newOptions.method === 'POST' || newOptions.method === 'PUT' ||
  86. newOptions.method === 'PATCH' || newOptions.method === 'DELETE') {
  87. newOptions.headers = {
  88. Accept: 'application/json',
  89. 'Content-Type': 'application/json; charset=utf-8',
  90. ...newOptions.headers,
  91. };
  92. } else {
  93. newOptions.headers = {
  94. Accept: 'application/json',
  95. ...newOptions.headers,
  96. };
  97. }
  98. return fetch(url, newOptions)
  99. .then(httpErrorHandler)
  100. .then(promise2Json)
  101. .then(apiErrorHandler)
  102. .catch(e => ({ e }));
  103. }