BasicLayout.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * Ant Design Pro v4 use `@ant-design/pro-layout` to handle Layout.
  3. * You can view component api by:
  4. * https://github.com/ant-design/ant-design-pro-layout
  5. */
  6. import type {
  7. MenuDataItem,
  8. BasicLayoutProps as ProLayoutProps,
  9. Settings,
  10. } from '@ant-design/pro-layout';
  11. import ProLayout, { SettingDrawer } from '@ant-design/pro-layout';
  12. import React, { useEffect, useMemo, useRef } from 'react';
  13. import type { Dispatch } from 'umi';
  14. import { Link, useIntl, connect, history } from 'umi';
  15. import { Result, Button } from 'antd';
  16. import Authorized from '@/utils/Authorized';
  17. import RightContent from '@/components/GlobalHeader/RightContent';
  18. import type { ConnectState } from '@/models/connect';
  19. import { getMatchMenu } from '@umijs/route-utils';
  20. import { BASE_URL } from '@/utils/eventkey';
  21. const noMatch = (
  22. <Result
  23. status={403}
  24. title="403"
  25. subTitle="Sorry, you are not authorized to access this page."
  26. extra={
  27. <Button type="primary">
  28. <Link to="/user/login">Go Login</Link>
  29. </Button>
  30. }
  31. />
  32. );
  33. export type BasicLayoutProps = {
  34. breadcrumbNameMap: Record<string, MenuDataItem>;
  35. route: ProLayoutProps['route'] & {
  36. authority: string[];
  37. };
  38. settings: Settings;
  39. dispatch: Dispatch;
  40. } & ProLayoutProps;
  41. export type BasicLayoutContext = { [K in 'location']: BasicLayoutProps[K] } & {
  42. breadcrumbNameMap: Record<string, MenuDataItem>;
  43. };
  44. /**
  45. * use Authorized check all menu item
  46. */
  47. const menuDataRender = (menuList: MenuDataItem[]): MenuDataItem[] =>
  48. menuList.map((item) => {
  49. const localItem = {
  50. ...item,
  51. children: item.children ? menuDataRender(item.children) : undefined,
  52. };
  53. return Authorized.check(item.authority, localItem, null) as MenuDataItem;
  54. });
  55. const defaultFooterDom = (
  56. // <DefaultFooter
  57. // copyright={`${new Date().getFullYear()} 蚂蚁集团体验技术部出品`}
  58. // links={[
  59. // {
  60. // key: 'Ant Design Pro',
  61. // title: 'Ant Design Pro',
  62. // href: 'https://pro.ant.design',
  63. // blankTarget: true,
  64. // },
  65. // {
  66. // key: 'github',
  67. // title: <GithubOutlined />,
  68. // href: 'https://github.com/ant-design/ant-design-pro',
  69. // blankTarget: true,
  70. // },
  71. // {
  72. // key: 'Ant Design',
  73. // title: 'Ant Design',
  74. // href: 'https://ant.design',
  75. // blankTarget: true,
  76. // },
  77. // ]}
  78. // />
  79. <span></span>
  80. );
  81. const BasicLayout: React.FC<BasicLayoutProps> = (props) => {
  82. const {
  83. dispatch,
  84. children,
  85. settings,
  86. location = {
  87. pathname: '/',
  88. },
  89. } = props;
  90. const menuDataRef = useRef<MenuDataItem[]>([]);
  91. useEffect(() => {
  92. console.log('hello');
  93. if (!window.localStorage.getItem('token')) {
  94. if (process.env.NODE_ENV === 'development') {
  95. window.location.href = `${BASE_URL}/#/user/login`;
  96. } else {
  97. const { origin } = window.location;
  98. window.location.href = `${origin}/tieba-admin/#/user/login`;
  99. }
  100. }
  101. // if (dispatch) {
  102. // dispatch({
  103. // type: 'user/fetchCurrent',
  104. // });
  105. // }
  106. }, []);
  107. /**
  108. * init variables
  109. */
  110. const handleMenuCollapse = (payload: boolean): void => {
  111. if (dispatch) {
  112. dispatch({
  113. type: 'global/changeLayoutCollapsed',
  114. payload,
  115. });
  116. }
  117. }; // get children authority
  118. const authorized = useMemo(
  119. () =>
  120. getMatchMenu(location.pathname || '/', menuDataRef.current).pop() || {
  121. authority: undefined,
  122. },
  123. [location.pathname],
  124. );
  125. const { formatMessage } = useIntl();
  126. return (
  127. <>
  128. <ProLayout
  129. menu={{ locale: false }}
  130. // logo={logo}
  131. formatMessage={formatMessage}
  132. {...props}
  133. {...settings}
  134. onCollapse={handleMenuCollapse}
  135. onMenuHeaderClick={() => history.push('/')}
  136. menuItemRender={(menuItemProps, defaultDom) => {
  137. if (
  138. menuItemProps.isUrl ||
  139. !menuItemProps.path ||
  140. location.pathname === menuItemProps.path
  141. ) {
  142. return defaultDom;
  143. }
  144. return <Link to={menuItemProps.path}>{defaultDom}</Link>;
  145. }}
  146. breadcrumbRender={(routers = []) => [
  147. {
  148. path: '/',
  149. breadcrumbName: formatMessage({
  150. id: 'menu.home',
  151. }),
  152. },
  153. ...routers,
  154. ]}
  155. itemRender={(route, params, routes, paths) => {
  156. const first = routes.indexOf(route) === 0;
  157. return first ? (
  158. <Link to={paths.join('/')}>{route.breadcrumbName}</Link>
  159. ) : (
  160. <span>{route.breadcrumbName}</span>
  161. );
  162. }}
  163. footerRender={() => defaultFooterDom}
  164. menuDataRender={menuDataRender}
  165. rightContentRender={() => <RightContent />}
  166. postMenuData={(menuData) => {
  167. menuDataRef.current = menuData || [];
  168. return menuData || [];
  169. }}
  170. >
  171. <Authorized authority={authorized!.authority} noMatch={noMatch}>
  172. {children}
  173. </Authorized>
  174. </ProLayout>
  175. <SettingDrawer
  176. settings={settings}
  177. onSettingChange={(config) =>
  178. dispatch({
  179. type: 'settings/changeSetting',
  180. payload: config,
  181. })
  182. }
  183. />
  184. </>
  185. );
  186. };
  187. export default connect(({ global, settings }: ConnectState) => ({
  188. collapsed: global.collapsed,
  189. settings,
  190. }))(BasicLayout);