How to uniformly handle the session invalidation returned by the interface in ant-design-pro

there are many APIs in model that can only be called after login. It is normal for code to return 2000.When
returns 20020, the session expires. At this time, I want to jump back to the login page. This is how I handled it

.
    *fetchShopSaleList({ payload }, { call, put }) {
      let res = yield call(qryShopSaleGroup, payload);
      if(res.code === 200) {
        yield put({
          type: "save",
          payload: {
            shopCardList: res.data,
          },
        });
      } else if (res.code === 20020) {
        yield put(routerRedux.push("/user/cloudlogin"));
      }
    },

    *fetchShopSaleInfo({ payload }, { call, put }) {
      let res = yield call(qryShopSaleGroup, payload);
      if(res.code === 200) {
        yield put({
          type: "save",
          payload: {
            shopSaleInfoList: res.data,
          },
        });
      } else if (res.code === 20020) {
        yield put(routerRedux.push("/user/cloudlogin"));
      }
    },

in each method, determine whether the returned code is equal to 20020, and if so, jump to the login page.
I have to write one more judgment under each interface, which doesn"t feel good.

is there any way to deal with it in a unified way?


In

antd-pro, it can be handled uniformly in the request method in the src/utils/request.js file, and there is no need to add additional code in model

import { extend } from 'umi-request';

...

/**
 * request
 */
const request = extend({
  errorHandler, // 
  credentials: 'include', // cookie
});

/**
*  200 
*/
request.interceptors.response.use(async (response) => {
  const data = await response.clone().json();
  if(data && data.NOT_LOGIN) {
    location.href = 'url';
  }
  return response;
})

export default request;

for more information, please see umi-request

. In

antd-pro, it can be handled uniformly in the request method in the src/utils/request.js file.

Menu