LOCKING盒子版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { history, RequestConfig } from 'umi';
  2. import { ResponseError } from 'umi-request';
  3. import { notification } from 'antd';
  4. import { firstCharToLowerCase, handleRequest } from './utils/tool';
  5. import { isObject } from 'lodash';
  6. import { logout, queryCurrent } from './services/user';
  7. const codeMessage = {
  8. 200: '服务器成功返回请求的数据。',
  9. 201: '新建或修改数据成功。',
  10. 202: '一个请求已经进入后台排队(异步任务)。',
  11. 204: '删除数据成功。',
  12. 400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
  13. 401: '用户没有权限(令牌、用户名、密码错误)。',
  14. 403: '用户得到授权,但是访问是被禁止的。',
  15. 404: '发出的请求针对的是不存在的记录,服务器没有进行操作。',
  16. 405: '请求方法不被允许。',
  17. 406: '请求的格式不可得。',
  18. 410: '请求的资源被永久删除,且不会再得到的。',
  19. 422: '当创建一个对象时,发生一个验证错误。',
  20. 500: '服务器发生错误,请检查服务器。',
  21. 502: '网关错误。',
  22. 503: '服务不可用,服务器暂时过载或维护。',
  23. 504: '网关超时。',
  24. };
  25. /**
  26. * 异常处理程序
  27. */
  28. const errorHandler = (error: ResponseError) => {
  29. const { response, data } = error;
  30. if (response && response.status) {
  31. const errorText = codeMessage[response.status] || response.statusText;
  32. const { status, url } = response;
  33. notification.error({
  34. message: `请求错误 ${status}: ${url}`,
  35. description: errorText,
  36. });
  37. return {
  38. code: response.status,
  39. message: errorText,
  40. }
  41. }
  42. if (data && typeof data === 'object' && ('error' in data)) return data;
  43. // todo 报错都不走error, 改为构建成data;
  44. if (!response) {
  45. notification.error({
  46. description: '您的网络发生异常,无法连接服务器',
  47. message: '网络异常',
  48. });
  49. }
  50. // throw error;
  51. return {
  52. code: 'err',
  53. message: '网络异常',
  54. }
  55. };
  56. export const request: RequestConfig = {
  57. errorConfig: {
  58. adaptor: (resData, ctx) => {
  59. const { Code, Data, Msg } = resData;
  60. if ('Code' in resData) { resData.code = Code; delete resData.Code; }
  61. if ('Data' in resData) {
  62. resData.data = Data;
  63. if (isObject(Data)) {
  64. resData.data = firstCharToLowerCase(Data);
  65. }
  66. delete resData.Data;
  67. }
  68. if ('Msg' in resData) { resData.message = Msg; delete resData.Msg; };
  69. if (!('success' in resData)) { // http请求是否成功
  70. // console.log('adpator log:', resData);
  71. resData.success = true;
  72. }
  73. if ('code' in resData) { // 业务请求是否成功
  74. resData.requestIsSuccess = resData.code === null || resData.code === 0 || resData.code === '0';
  75. }
  76. return resData;
  77. },
  78. },
  79. credentials: "same-origin",
  80. // errorHandler,
  81. errorHandler,
  82. };
  83. export async function getInitialState(): Promise<{ isLogin: boolean, fetchUserInfo: () => Promise<DATA.User>, currentUser?: DATA.User }> {
  84. async function fetchUserInfo() {
  85. const res = await queryCurrent();
  86. handleRequest(res)
  87. .error(() => logout())
  88. .httpError(() => logout());
  89. return res.data;
  90. }
  91. // 如果是登录页面,不执行
  92. if (history.location.pathname !== '/login' && history.location.pathname !== '/~docs') {
  93. const currentUser = await fetchUserInfo();
  94. if(!currentUser) {
  95. logout();
  96. }
  97. return {
  98. fetchUserInfo,
  99. isLogin: !!currentUser,
  100. currentUser,
  101. };
  102. }
  103. return {
  104. isLogin: false,
  105. fetchUserInfo,
  106. };
  107. }