feat: antd message注入

This commit is contained in:
Mozzie 2023-09-07 13:41:32 +08:00
parent 558ca8e7ac
commit c522de39ae
5 changed files with 49 additions and 5 deletions

View File

@ -1,4 +1,4 @@
import { message } from "antd"; import { antdUtils } from "@/AntdUtils";
import { getFingerprint, SymmetricCrypto } from "@tavi/util"; import { getFingerprint, SymmetricCrypto } from "@tavi/util";
import axios, { import axios, {
AxiosError, AxiosError,
@ -65,7 +65,8 @@ class AxiosRequestInstance {
(error: AxiosError) => { (error: AxiosError) => {
if (error.response && error.response.status === 403) { if (error.response && error.response.status === 403) {
const { msg } = error.response.data as ResponseException; const { msg } = error.response.data as ResponseException;
message.error(msg); console.log(antdUtils);
antdUtils.message?.error(msg);
} }
return Promise.reject(error); return Promise.reject(error);
} }

View File

@ -0,0 +1,29 @@
import { MessageInstance } from "antd/es/message/interface";
import { ModalStaticFunctions } from "antd/es/modal/confirm";
import { NotificationInstance } from "antd/es/notification/interface";
type ModalInstance = Omit<ModalStaticFunctions, "warn">;
class AntdUtils {
message: MessageInstance | null = null;
notification: NotificationInstance | null = null;
modal: ModalInstance | null = null;
setMessageInstance(message: MessageInstance) {
this.message = message;
this.message.success;
return this;
}
setNotificationInstance(notification: NotificationInstance) {
this.notification = notification;
return this;
}
setModalInstance(modal: ModalInstance) {
this.modal = modal;
return this;
}
}
export const antdUtils = new AntdUtils();

View File

@ -2,9 +2,11 @@ import { DomainServiceProvider } from "./context/domainService";
import { defaultLang, theme } from "./constant"; import { defaultLang, theme } from "./constant";
import { useTracker } from "./hook/useTracker"; import { useTracker } from "./hook/useTracker";
import { RouterElements } from "./router"; import { RouterElements } from "./router";
import { ConfigProvider } from "antd"; import { ConfigProvider, App as AntApp } from "antd";
import { useI18n } from "@tavi/i18n"; import { useI18n } from "@tavi/i18n";
import "./styles/app.less"; import "./styles/app.less";
import { useEffect } from "react";
import { antdUtils } from "./AntdUtils";
/** /**
* *
@ -17,6 +19,13 @@ export const { i18n } = useI18n({
export const App = () => { export const App = () => {
useTracker(); useTracker();
const { notification, message, modal } = AntApp.useApp();
useEffect(() => {
antdUtils
.setMessageInstance(message)
.setModalInstance(modal)
.setNotificationInstance(notification);
}, []);
return ( return (
<DomainServiceProvider> <DomainServiceProvider>

View File

@ -1,11 +1,15 @@
import { createRoot } from "react-dom/client"; import { createRoot } from "react-dom/client";
import { BrowserRouter } from "react-router-dom"; import { BrowserRouter } from "react-router-dom";
const root = document.querySelector("#root"); import { App as AntApp } from "antd";
import { App } from "./App"; import { App } from "./App";
const root = document.querySelector("#root");
if (root) if (root)
createRoot(root).render( createRoot(root).render(
<BrowserRouter> <BrowserRouter>
<AntApp>
<App /> <App />
</AntApp>
</BrowserRouter> </BrowserRouter>
); );

View File

@ -17,6 +17,7 @@ export class ForbiddenExceptionFilter implements ExceptionFilter {
statusCode: status, statusCode: status,
timestamp: new Date().toISOString(), timestamp: new Date().toISOString(),
path: request.url, path: request.url,
msg: exception.message,
}); });
// 你可以设置一个重定向地址 // 你可以设置一个重定向地址
// response.redirect('/'); // response.redirect('/');