feat(projects): Add handling for no permission codes in the backend request (#508)

This commit is contained in:
Ma.Jinkai 2024-06-18 09:53:36 +08:00
parent a13251efd0
commit f9efd7dfc4
6 changed files with 23 additions and 2 deletions

3
.env
View File

@ -29,6 +29,9 @@ VITE_ROUTER_HISTORY_MODE=history
# success code of backend service, when the code is received, the request is successful
VITE_SERVICE_SUCCESS_CODE=0000
# no permission codes of backend service, when the code is received, it will displaying a warning message
VITE_SERVICE_NO_PERMISSION_CODES=4444,4445
# logout codes of backend service, when the code is received, the user will be logged out and redirected to login page
VITE_SERVICE_LOGOUT_CODES=8888,8889

View File

@ -60,7 +60,8 @@ const local: App.I18n.Schema = {
logoutWithModal: 'Pop up modal after request failed and then log out user',
logoutWithModalMsg: 'User status is invalid, please log in again',
refreshToken: 'The requested token has expired, refresh the token',
tokenExpired: 'The requested token has expired'
tokenExpired: 'The requested token has expired',
noPermission: 'The request has no permission'
},
theme: {
themeSchema: {

View File

@ -60,7 +60,8 @@ const local: App.I18n.Schema = {
logoutWithModal: '请求失败后弹出模态框再登出用户',
logoutWithModalMsg: '用户状态失效,请重新登录',
refreshToken: '请求的token已过期刷新token',
tokenExpired: 'token已过期'
tokenExpired: 'token已过期',
noPermission: '无接口访问权限'
},
theme: {
themeSchema: {

View File

@ -50,6 +50,13 @@ export const request = createFlatRequest<App.Service.Response, RequestInstanceSt
await handleLogout();
}
// when the backend response code is in 'noPermissionCodes', displaying warning message.
const noPermissionCodes = import.meta.env.VITE_SERVICE_NO_PERMISSION_CODES?.split(',');
if (noPermissionCodes?.includes(response.data.code)) {
window.$message?.warning(response.data.msg || $t('request.noPermission'));
return null;
}
// when the backend response code is in `logoutCodes`, it means the user will be logged out and redirected to login page
const logoutCodes = import.meta.env.VITE_SERVICE_LOGOUT_CODES?.split(',');
if (logoutCodes?.includes(response.data.code)) {

View File

@ -311,6 +311,7 @@ declare namespace App {
logoutWithModalMsg: string;
refreshToken: string;
tokenExpired: string;
noPermission: string;
};
theme: {
themeSchema: { title: string } & Record<UnionKey.ThemeScheme, string>;

View File

@ -33,6 +33,14 @@ declare namespace Env {
* when the code is received, the request is successful
*/
readonly VITE_SERVICE_SUCCESS_CODE: string;
/**
* no permission codes of backend service
*
* when the code is received, it will displaying a warning message
*
* use "," to separate multiple codes
*/
readonly VITE_SERVICE_NO_PERMISSION_CODES: string;
/**
* logout codes of backend service
*