|
- import type { Settings as LayoutSettings } from '@ant-design/pro-layout';
- import { PageLoading } from '@ant-design/pro-layout';
- import type { RunTimeLayoutConfig } from 'umi';
- import { history } from 'umi';
- import RightContent from '@/components/RightContent';
- import Footer from '@/components/Footer';
- import { currentUser as queryCurrentUser } from './services/ant-design-pro/api';
- import defaultSettings from '../config/defaultSettings';
-
- const loginPath = '/user/login';
-
- /** Show a Loading page when loading inital data such as user info slowly*/
- export const initialStateConfig = {
- loading: <PageLoading />,
- };
-
- if (window.location.hostname == 'paddlecv-sig.github.io') {
- localStorage.setItem('basePath', 'https://pplabel.herokuapp.com/api');
- } else {
- const port = window.location.port == '8000' ? '17995' : window.location.port;
- localStorage.setItem('basePath', `http://${window.location.hostname}:${port}/api`);
- // localStorage.setItem('basePath', `http://172.18.178.89:17995/api`);
- }
-
- /**
- * @see https://umijs.org/zh-CN/plugins/plugin-initial-state
- * */
- export async function getInitialState(): Promise<{
- settings?: Partial<LayoutSettings>;
- currentUser?: API.CurrentUser;
- loading?: boolean;
- fetchUserInfo?: () => Promise<API.CurrentUser | undefined>;
- }> {
- const fetchUserInfo = async () => {
- try {
- const msg = await queryCurrentUser();
- return msg.data;
- } catch (error) {
- history.push(loginPath);
- }
- return undefined;
- };
- // Do not execute on login page
- if (history.location.pathname !== loginPath) {
- const currentUser = await fetchUserInfo();
- return {
- fetchUserInfo,
- currentUser,
- settings: defaultSettings,
- };
- }
- return {
- fetchUserInfo,
- settings: defaultSettings,
- };
- }
-
- // ProLayout supports these apis: https://procomponents.ant.design/components/layout
- export const layout: RunTimeLayoutConfig = ({ initialState }) => {
- return {
- rightContentRender: () => <RightContent />,
- disableContentMargin: false,
- waterMarkProps: {
- content: initialState?.currentUser?.name,
- },
- footerRender: () => <Footer />,
- onPageChange: () => {
- const { location } = history;
- // Redirect to login when not login
- if (!initialState?.currentUser && location.pathname !== loginPath) {
- history.push(loginPath);
- }
- },
- links: [],
- title: '',
- menuHeaderRender: undefined,
- // Customize 403
- // unAccessible: <div>unAccessible</div>,
- // Add loading status
- // Can accept props as 2nd param
- childrenRender: (children) => {
- if (initialState?.loading) return <PageLoading />;
- return <>{children}</>;
- },
- ...initialState?.settings,
- };
- };
|