Skip to content

Commit

Permalink
feat(projects): support repeated request errors occur once in a short…
Browse files Browse the repository at this point in the history
… time
  • Loading branch information
honghuangdc committed Jun 6, 2024
1 parent 90c205d commit 6e80963
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 10 deletions.
6 changes: 6 additions & 0 deletions src/locales/langs/en-us.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,12 @@ const local: App.I18n.Schema = {
superAdminVisible: 'Super Admin Visible',
adminVisible: 'Admin Visible',
adminOrUserVisible: 'Admin and User Visible'
},
request: {
repeatedErrorOccurOnce: 'Repeated Request Error Occurs Once',
repeatedError: 'Repeated Request Error',
repeatedErrorMsg1: 'Custom Request Error 1',
repeatedErrorMsg2: 'Custom Request Error 2'
}
},
manage: {
Expand Down
6 changes: 6 additions & 0 deletions src/locales/langs/zh-cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,12 @@ const local: App.I18n.Schema = {
superAdminVisible: '超级管理员可见',
adminVisible: '管理员可见',
adminOrUserVisible: '管理员和用户可见'
},
request: {
repeatedErrorOccurOnce: '重复请求错误只出现一次',
repeatedError: '重复请求错误',
repeatedErrorMsg1: '自定义请求错误 1',
repeatedErrorMsg2: '自定义请求错误 2'
}
},
manage: {
Expand Down
20 changes: 10 additions & 10 deletions src/service/request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ import { useAuthStore } from '@/store/modules/auth';
import { localStg } from '@/utils/storage';
import { getServiceBaseURL } from '@/utils/service';
import { $t } from '@/locales';
import { handleRefreshToken } from './shared';
import { handleRefreshToken, showErrorMsg } from './shared';
import type { RequestInstanceState } from './type';

const isHttpProxy = import.meta.env.DEV && import.meta.env.VITE_HTTP_PROXY === 'Y';
const { baseURL, otherBaseURL } = getServiceBaseURL(import.meta.env, isHttpProxy);

interface InstanceState {
/** whether the request is refreshing token */
isRefreshingToken: boolean;
}

export const request = createFlatRequest<App.Service.Response, InstanceState>(
export const request = createFlatRequest<App.Service.Response, RequestInstanceState>(
{
baseURL,
headers: {
Expand All @@ -35,7 +31,7 @@ export const request = createFlatRequest<App.Service.Response, InstanceState>(
isBackendSuccess(response) {
// when the backend response code is "0000"(default), it means the request is success
// to change this logic by yourself, you can modify the `VITE_SERVICE_SUCCESS_CODE` in `.env` file
return response.data.code === import.meta.env.VITE_SERVICE_SUCCESS_CODE;
return String(response.data.code) === import.meta.env.VITE_SERVICE_SUCCESS_CODE;
},
async onBackendFail(response, instance) {
const authStore = useAuthStore();
Expand All @@ -47,6 +43,8 @@ export const request = createFlatRequest<App.Service.Response, InstanceState>(
function logoutAndCleanup() {
handleLogout();
window.removeEventListener('beforeunload', handleLogout);

request.state.errMsgStack = request.state.errMsgStack.filter(msg => msg !== response.data.msg);
}

// when the backend response code is in `logoutCodes`, it means the user will be logged out and redirected to login page
Expand All @@ -58,7 +56,9 @@ export const request = createFlatRequest<App.Service.Response, InstanceState>(

// when the backend response code is in `modalLogoutCodes`, it means the user will be logged out by displaying a modal
const modalLogoutCodes = import.meta.env.VITE_SERVICE_MODAL_LOGOUT_CODES?.split(',') || [];
if (modalLogoutCodes.includes(response.data.code)) {
if (modalLogoutCodes.includes(response.data.code) && !request.state.errMsgStack?.includes(response.data.msg)) {
request.state.errMsgStack = [...(request.state.errMsgStack || []), response.data.msg];

// prevent the user from refreshing the page
window.addEventListener('beforeunload', handleLogout);

Expand Down Expand Up @@ -122,7 +122,7 @@ export const request = createFlatRequest<App.Service.Response, InstanceState>(
return;
}

window.$message?.error?.(message);
showErrorMsg(request.state, message);
}
}
);
Expand Down
21 changes: 21 additions & 0 deletions src/service/request/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { AxiosRequestConfig } from 'axios';
import { useAuthStore } from '@/store/modules/auth';
import { localStg } from '@/utils/storage';
import { fetchRefreshToken } from '../api';
import type { RequestInstanceState } from './type';

/**
* refresh token
Expand Down Expand Up @@ -29,3 +30,23 @@ export async function handleRefreshToken(axiosConfig: AxiosRequestConfig) {

return null;
}

export function showErrorMsg(state: RequestInstanceState, message: string) {
if (!state.errMsgStack?.length) {
state.errMsgStack = [];
}

const isExist = state.errMsgStack.includes(message);

if (!isExist) {
state.errMsgStack.push(message);

window.$message?.error(message, 1.5, () => {
state.errMsgStack = state.errMsgStack.filter(msg => msg !== message);

setTimeout(() => {
state.errMsgStack = [];
}, 5000);
});
}
}
6 changes: 6 additions & 0 deletions src/service/request/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface RequestInstanceState {
/** whether the request is refreshing token */
isRefreshingToken: boolean;
/** the request error message stack */
errMsgStack: string[];
}
6 changes: 6 additions & 0 deletions src/typings/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,12 @@ declare namespace App {
adminVisible: string;
adminOrUserVisible: string;
};
request: {
repeatedErrorOccurOnce: string;
repeatedError: string;
repeatedErrorMsg1: string;
repeatedErrorMsg2: string;
};
};
manage: {
common: {
Expand Down
30 changes: 30 additions & 0 deletions src/views/function/request/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ async function logoutWithModal() {
async function refreshToken() {
await fetchCustomBackendError('9999', $t('request.tokenExpired'));
}
async function handleRepeatedMessageError() {
await Promise.all([
fetchCustomBackendError('2222', $t('page.function.request.repeatedErrorMsg1')),
fetchCustomBackendError('2222', $t('page.function.request.repeatedErrorMsg1')),
fetchCustomBackendError('2222', $t('page.function.request.repeatedErrorMsg1')),
fetchCustomBackendError('3333', $t('page.function.request.repeatedErrorMsg2')),
fetchCustomBackendError('3333', $t('page.function.request.repeatedErrorMsg2')),
fetchCustomBackendError('3333', $t('page.function.request.repeatedErrorMsg2'))
]);
}
async function handleRepeatedModalError() {
await Promise.all([
fetchCustomBackendError('7777', $t('request.logoutWithModalMsg')),
fetchCustomBackendError('7777', $t('request.logoutWithModalMsg')),
fetchCustomBackendError('7777', $t('request.logoutWithModalMsg'))
]);
}
</script>

<template>
Expand All @@ -26,6 +45,17 @@ async function refreshToken() {
<ACard :title="$t('request.refreshToken')" :bordered="false" size="small" class="card-wrapper">
<AButton @click="refreshToken">{{ $t('common.trigger') }}</AButton>
</ACard>
<ACard
:title="$t('page.function.request.repeatedErrorOccurOnce')"
:bordered="false"
size="small"
class="card-wrapper"
>
<AButton @click="handleRepeatedMessageError">{{ $t('page.function.request.repeatedError') }}(Message)</AButton>
<AButton class="ml-12px" @click="handleRepeatedModalError">
{{ $t('page.function.request.repeatedError') }}(Modal)
</AButton>
</ACard>
</ASpace>
</template>

Expand Down

0 comments on commit 6e80963

Please sign in to comment.