提交 4560f80b 作者: 翁进城

api请求出错全局提示

上级 d52a3900
...@@ -36,9 +36,9 @@ export default function request(url: string, method: String = 'get', data?: any, ...@@ -36,9 +36,9 @@ export default function request(url: string, method: String = 'get', data?: any,
return fetch(config.baseUrl + url, options) return fetch(config.baseUrl + url, options)
.then((r) => { .then((r) => {
try{ try {
return r.json() return r.json()
}catch(e){ } catch (e) {
console.error(e); console.error(e);
} }
return { return {
...@@ -48,6 +48,24 @@ export default function request(url: string, method: String = 'get', data?: any, ...@@ -48,6 +48,24 @@ export default function request(url: string, method: String = 'get', data?: any,
} }
}) })
.then((data) => { .then((data) => {
if (data.errors) {
//全局消息提示
window.messageApi.error('请求出错')
if (Array.isArray(data.errors)) {
data.errors.forEach((item: any) => {
if (item.defaultMessage){
window.messageApi.error(item.defaultMessage)
}
})
}
return {
code: '-1',
message: '请求失败',
result: null
}
}
return data; return data;
}) })
.catch(error => { .catch(error => {
......
...@@ -2,7 +2,20 @@ import '../public/antd.min.css'; ...@@ -2,7 +2,20 @@ import '../public/antd.min.css';
import '../styles/index.scss'; import '../styles/index.scss';
import type { AppProps } from 'next/app'; import type { AppProps } from 'next/app';
import withTheme from '../theme'; import withTheme from '../theme';
import { message } from 'antd';
import { useEffect } from 'react';
export default function App({ Component, pageProps }: AppProps) { export default function App({ Component, pageProps }: AppProps) {
return withTheme(<Component {...pageProps} />); const [messageApi, contextHolder] = message.useMessage();
useEffect(() => {
//全局消息提示
window.messageApi = messageApi;
}, [])
return withTheme(
<>
{contextHolder}
<Component {...pageProps} />
</>
);
} }
...@@ -17,7 +17,8 @@ ...@@ -17,7 +17,8 @@
"incremental": true, "incremental": true,
"paths": { "paths": {
"~/*": ["./*"] "~/*": ["./*"]
} },
"typeRoots": ["./typings"]
}, },
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"], "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"] "exclude": ["node_modules"]
......
import { MessageInstance } from 'antd/es/message/interface';
declare global {
interface Window {
messageApi: MessageInstance;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论