fix(ui): 修复登录和并发请求并统一页面展示
调整登录流程和请求处理,统一各中心的表格交互、主题样式和页面展示。
This commit is contained in:
+56
-53
@@ -28,12 +28,7 @@ import {
|
||||
import { MOBILE_BREAKPOINT } from '../hooks/common/useIsMobile';
|
||||
// 官方价折扣计算(纯函数):管理端与市场页共用
|
||||
import { buildOfficialComparison, formatDiscountLabel } from './officialPrice';
|
||||
import {
|
||||
clearUserData,
|
||||
getUserData,
|
||||
isSafePostLoginRedirect,
|
||||
savePostLoginRedirect,
|
||||
} from './data';
|
||||
import { clearUserData, getUserData, isSafePostLoginRedirect } from './data';
|
||||
|
||||
const HTMLToastContent = ({ htmlContent }) => {
|
||||
return <div dangerouslySetInnerHTML={{ __html: htmlContent }} />;
|
||||
@@ -128,56 +123,64 @@ if (isMobileScreen) {
|
||||
// showNoticeOptions.transition = 'flip';
|
||||
}
|
||||
|
||||
export function showError(error) {
|
||||
console.error(error);
|
||||
if (error.message) {
|
||||
if (error.name === 'AxiosError') {
|
||||
// 检查 response 是否存在(网络错误时 response 为 undefined)
|
||||
if (!error.response) {
|
||||
Toast.error(i18next.t('错误:网络请求失败,请检查网络连接'));
|
||||
return;
|
||||
}
|
||||
switch (error.response.status) {
|
||||
case 401:
|
||||
// 清除用户状态
|
||||
clearUserData();
|
||||
const currentPath = `${window.location.pathname}${window.location.search}${window.location.hash}`;
|
||||
const redirect = isSafePostLoginRedirect(currentPath)
|
||||
? `&redirect=${encodeURIComponent(currentPath)}`
|
||||
: '';
|
||||
if (redirect) {
|
||||
savePostLoginRedirect(currentPath);
|
||||
}
|
||||
// toast.error('错误:未登录或登录已过期,请重新登录!', showErrorOptions);
|
||||
// 会话过期统一回企业登录(默认登录入口),OrgLogin 会识别 expired/redirect 参数
|
||||
window.location.href = `/org/login?expired=true${redirect}`;
|
||||
break;
|
||||
case 429:
|
||||
Toast.error(i18next.t('错误:请求次数过多,请稍后再试!'));
|
||||
break;
|
||||
case 500:
|
||||
Toast.error(i18next.t('错误:服务器内部错误,请联系管理员!'));
|
||||
break;
|
||||
case 405:
|
||||
Toast.info(i18next.t('本站仅作演示之用,无服务端!'));
|
||||
break;
|
||||
default: {
|
||||
// 后端业务错误(4xx/5xx)带有面向用户的 message 字段;
|
||||
// 优先展示它,而不是 axios 的 "Request failed with status code XXX"。
|
||||
const backendMessage = error.response?.data?.message;
|
||||
Toast.error(
|
||||
i18next.t('错误:{{message}}', {
|
||||
message: backendMessage || error.message,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
let redirectingToLogin = false;
|
||||
const reportedRequestErrors = new WeakSet();
|
||||
|
||||
export function redirectToExpiredLogin() {
|
||||
if (redirectingToLogin) return;
|
||||
redirectingToLogin = true;
|
||||
clearUserData();
|
||||
const { pathname, search, hash } = window.location;
|
||||
const currentPath = `${pathname}${search}${hash}`;
|
||||
const redirect = isSafePostLoginRedirect(currentPath)
|
||||
? `&redirect=${encodeURIComponent(currentPath)}`
|
||||
: '';
|
||||
window.location.replace(`/org/login?expired=true${redirect}`);
|
||||
}
|
||||
|
||||
export function showError(error, { redirectOnUnauthorized = true } = {}) {
|
||||
if (
|
||||
error?.code === 'ERR_CANCELED' ||
|
||||
error === 'canceled' ||
|
||||
(redirectingToLogin && error === 'Request failed with status code 401')
|
||||
)
|
||||
return;
|
||||
|
||||
if (error?.isAxiosError || error?.name === 'AxiosError') {
|
||||
if (reportedRequestErrors.has(error)) return;
|
||||
reportedRequestErrors.add(error);
|
||||
|
||||
const status = error.response?.status;
|
||||
if (status === 401 && redirectOnUnauthorized) {
|
||||
redirectToExpiredLogin();
|
||||
return;
|
||||
}
|
||||
Toast.error(i18next.t('错误:{{message}}', { message: error.message }));
|
||||
} else {
|
||||
Toast.error(i18next.t('错误:{{message}}', { message: error }));
|
||||
if (status === 405) {
|
||||
Toast.info({
|
||||
key: 'http-error:405',
|
||||
content: i18next.t('本站仅作演示之用,无服务端!'),
|
||||
});
|
||||
return;
|
||||
}
|
||||
let content;
|
||||
if (!error.response) {
|
||||
content = i18next.t('错误:网络请求失败,请检查网络连接');
|
||||
} else if (status === 429) {
|
||||
content = i18next.t('错误:请求次数过多,请稍后再试!');
|
||||
} else if (status === 500) {
|
||||
content = i18next.t('错误:服务器内部错误,请联系管理员!');
|
||||
} else {
|
||||
content = i18next.t('错误:{{message}}', {
|
||||
message: error.response.data?.message || error.message,
|
||||
});
|
||||
}
|
||||
Toast.error({ key: `http-error:${content}`, content });
|
||||
return;
|
||||
}
|
||||
console.error(error);
|
||||
Toast.error(
|
||||
i18next.t('错误:{{message}}', { message: error?.message || error }),
|
||||
);
|
||||
}
|
||||
|
||||
export function showWarning(message) {
|
||||
|
||||
Reference in New Issue
Block a user