You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

app.tsx 8.0 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import RightContent from '@/components/RightContent';
  2. import themes from '@/styles/theme.less';
  3. import type { Settings as LayoutSettings } from '@ant-design/pro-components';
  4. import { RuntimeConfig, history } from '@umijs/max';
  5. import { RuntimeAntdConfig } from 'umi';
  6. import defaultSettings from '../config/defaultSettings';
  7. import '../public/fonts/font.css';
  8. import { getAccessToken } from './access';
  9. import './dayjsConfig';
  10. import { PageEnum } from './enums/pagesEnums';
  11. import './global.less';
  12. import { removeAllPageCacheState } from './hooks/pageCacheState';
  13. import {
  14. getRemoteMenu,
  15. getRoutersInfo,
  16. getUserInfo,
  17. patchRouteWithRemoteMenus,
  18. setRemoteMenu,
  19. } from './services/session';
  20. import './styles/menu.less';
  21. export { requestConfig as request } from './requestConfig';
  22. // const isDev = process.env.NODE_ENV === 'development';
  23. import { type GlobalInitialState } from '@/types';
  24. import { menuItemRender } from '@/utils/menuRender';
  25. import ErrorBoundary from './components/ErrorBoundary';
  26. import { gotoLoginPage } from './utils/ui';
  27. /**
  28. * @see https://umijs.org/zh-CN/plugins/plugin-initial-state
  29. * */
  30. export async function getInitialState(): Promise<GlobalInitialState> {
  31. const fetchUserInfo = async () => {
  32. try {
  33. const response = await getUserInfo();
  34. return {
  35. ...response.user,
  36. avatar: response.user.avatar || require('@/assets/img/avatar-default.png'),
  37. permissions: response.permissions,
  38. roles: response.roles,
  39. roleNames: response.user.roles,
  40. } as API.CurrentUser;
  41. } catch (error) {
  42. console.error(error);
  43. gotoLoginPage();
  44. }
  45. return undefined;
  46. };
  47. // 如果不是登录页面,执行
  48. const { location } = history;
  49. if (location.pathname !== PageEnum.LOGIN) {
  50. const currentUser = await fetchUserInfo();
  51. return {
  52. fetchUserInfo,
  53. currentUser,
  54. settings: defaultSettings as Partial<LayoutSettings>,
  55. collapsed: false,
  56. };
  57. }
  58. return {
  59. fetchUserInfo,
  60. settings: defaultSettings as Partial<LayoutSettings>,
  61. collapsed: false,
  62. };
  63. }
  64. // ProLayout 支持的api https://procomponents.ant.design/components/layout
  65. export const layout: RuntimeConfig['layout'] = ({ initialState }) => {
  66. return {
  67. ErrorBoundary: ErrorBoundary,
  68. rightContentRender: false,
  69. waterMarkProps: {
  70. // content: initialState?.currentUser?.nickName,
  71. },
  72. menu: {
  73. locale: false,
  74. // 每当 initialState?.currentUser?.userid 发生修改时重新执行 request
  75. params: {
  76. userId: initialState?.currentUser?.userId,
  77. },
  78. request: async () => {
  79. if (!initialState?.currentUser?.userId) {
  80. return [];
  81. }
  82. // console.log('get menus')
  83. // initialState.currentUser 中包含了所有用户信息
  84. // console.log('get routers')
  85. // setInitialState((preInitialState) => ({
  86. // ...preInitialState,
  87. // menus,
  88. // }));
  89. return getRemoteMenu();
  90. },
  91. },
  92. onPageChange: () => {
  93. const { location } = history;
  94. // 如果没有登录,重定向到 login
  95. if (!initialState?.currentUser && location.pathname !== PageEnum.LOGIN) {
  96. gotoLoginPage();
  97. }
  98. },
  99. layoutBgImgList: [
  100. {
  101. src: 'https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/D2LWSqNny4sAAAAAAAAAAAAAFl94AQBr',
  102. left: 85,
  103. bottom: 100,
  104. height: '303px',
  105. },
  106. {
  107. src: 'https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/C2TWRpJpiC0AAAAAAAAAAAAAFl94AQBr',
  108. bottom: -68,
  109. right: -45,
  110. height: '303px',
  111. },
  112. {
  113. src: 'https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/F6vSTbj8KpYAAAAAAAAAAAAAFl94AQBr',
  114. bottom: 0,
  115. left: 0,
  116. width: '331px',
  117. },
  118. ],
  119. // links: isDev
  120. // ? [
  121. // <Link key="openapi" to="/umi/plugin/openapi" target="_blank">
  122. // <LinkOutlined />
  123. // <span>OpenAPI 文档</span>
  124. // </Link>,
  125. // ]
  126. // : [],
  127. // 自定义 403 页面
  128. // unAccessible: <div>unAccessible</div>,
  129. // 增加一个 loading 的状态
  130. childrenRender: (children) => {
  131. // if (initialState?.loading) return <PageLoading />;
  132. return (
  133. <div className="kf-page-container">
  134. <RightContent></RightContent>
  135. <div className="kf-page-container__content">{children}</div>
  136. </div>
  137. );
  138. },
  139. collapsedButtonRender: false,
  140. collapsed: initialState?.collapsed,
  141. menuProps: {
  142. onClick: () => {
  143. // 点击菜单项,删除所有的页面 state 缓存
  144. removeAllPageCacheState();
  145. // console.log('click menu');
  146. },
  147. },
  148. ...initialState?.settings,
  149. logo: require('@/assets/img/logo.png'),
  150. token: {
  151. sider: {
  152. colorTextMenu: themes['textColor'],
  153. colorTextMenuSelected: themes['primaryColor'],
  154. colorTextMenuActive: themes['primaryColor'],
  155. colorTextMenuItemHover: themes['primaryColor'],
  156. colorBgMenuItemSelected: 'rgba(197, 232, 255, 0.8)',
  157. colorMenuBackground: themes['siderBGColor'],
  158. },
  159. },
  160. menuItemRender: menuItemRender(false),
  161. subMenuItemRender: menuItemRender(true),
  162. };
  163. };
  164. export const onRouteChange: RuntimeConfig['onRouteChange'] = async (e) => {
  165. const { location } = e;
  166. const menus = getRemoteMenu();
  167. // console.log('onRouteChange', e);
  168. if (menus === null && location.pathname !== PageEnum.LOGIN) {
  169. history.go(0);
  170. }
  171. };
  172. export const patchRoutes: RuntimeConfig['patchRoutes'] = (e) => {
  173. //console.log('patchRoutes', e);
  174. };
  175. export const patchClientRoutes: RuntimeConfig['patchClientRoutes'] = (e) => {
  176. //console.log('patchClientRoutes', e);
  177. patchRouteWithRemoteMenus(e.routes);
  178. };
  179. export function render(oldRender: () => void) {
  180. // console.log('render');
  181. const token = getAccessToken();
  182. if (!token || token?.length === 0) {
  183. oldRender();
  184. return;
  185. }
  186. getRoutersInfo()
  187. .then((res) => {
  188. setRemoteMenu(res);
  189. oldRender();
  190. })
  191. .catch(() => {
  192. oldRender();
  193. });
  194. }
  195. // 主题修改
  196. export const antd: RuntimeAntdConfig = (memo) => {
  197. memo.theme ??= {};
  198. memo.theme.token = {
  199. colorPrimary: themes['primaryColor'],
  200. colorSuccess: themes['successColor'],
  201. colorError: themes['errorColor'],
  202. colorWarning: themes['warningColor'],
  203. colorLink: themes['primaryColor'],
  204. colorText: themes['textColor'],
  205. controlHeightLG: 46,
  206. };
  207. memo.theme.components ??= {};
  208. memo.theme.components.Tabs = {};
  209. memo.theme.components.Button = {
  210. defaultBg: 'rgba(22, 100, 255, 0.06)',
  211. defaultBorderColor: 'rgba(22, 100, 255, 0.11)',
  212. defaultColor: themes['textColor'],
  213. defaultHoverBg: 'rgba(22, 100, 255, 0.06)',
  214. defaultHoverBorderColor: 'rgba(22, 100, 255, 0.5)',
  215. defaultHoverColor: '#3F7FFF ',
  216. defaultActiveBg: 'rgba(22, 100, 255, 0.12)',
  217. defaultActiveBorderColor: 'rgba(22, 100, 255, 0.75)',
  218. defaultActiveColor: themes['primaryColor'],
  219. contentFontSize: parseInt(themes['fontSize']),
  220. };
  221. memo.theme.components.Input = {
  222. inputFontSize: parseInt(themes['fontSizeInput']),
  223. inputFontSizeLG: parseInt(themes['fontSizeInputLg']),
  224. paddingBlockLG: 10,
  225. };
  226. memo.theme.components.Select = {
  227. singleItemHeightLG: 46,
  228. };
  229. memo.theme.components.Table = {
  230. headerBg: 'rgba(242, 244, 247, 0.36)',
  231. headerBorderRadius: 4,
  232. };
  233. memo.theme.components.Tabs = {
  234. titleFontSize: 16,
  235. };
  236. memo.theme.components.Form = {
  237. labelColor: 'rgba(29, 29, 32, 0.8);',
  238. };
  239. memo.theme.components.Breadcrumb = {
  240. iconFontSize: parseInt(themes['fontSize']),
  241. linkColor: 'rgba(29, 29, 32, 0.7)',
  242. separatorColor: 'rgba(29, 29, 32, 0.7)',
  243. };
  244. memo.theme.cssVar = true;
  245. // memo.theme.hashed = false;
  246. memo.appConfig = {
  247. message: {
  248. // 配置 message 最大显示数,超过限制时,最早的消息会被自动关闭
  249. maxCount: 3,
  250. },
  251. };
  252. return memo;
  253. };