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 7.4 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
1 year 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import RightContent from '@/components/RightContent';
  2. import themes from '@/styles/theme.less';
  3. import { type GlobalInitialState } from '@/types';
  4. import { menuItemRender } from '@/utils/menuRender';
  5. import type { Settings as LayoutSettings } from '@ant-design/pro-components';
  6. import { RuntimeConfig, history } from '@umijs/max';
  7. import { useState } from 'react';
  8. import { RuntimeAntdConfig } from 'umi';
  9. import defaultSettings from '../config/defaultSettings';
  10. import '../public/fonts/font.css';
  11. import { getAccessToken } from './access';
  12. import ErrorBoundary from './components/ErrorBoundary';
  13. import './dayjsConfig';
  14. import { removeAllPageCacheState } from './hooks/useCacheState';
  15. import { globalGetSeverTime } from './hooks/useServerTime';
  16. import {
  17. getRemoteMenu,
  18. getRoutersInfo,
  19. getUserInfo,
  20. patchRouteWithRemoteMenus,
  21. setRemoteMenu,
  22. } from './services/session';
  23. import './styles/menu.less';
  24. import { isLoginPage, needAuth } from './utils';
  25. import { HomeUrl } from './utils/constant';
  26. import { closeAllModals } from './utils/modal';
  27. import { gotoHomePage } from './utils/ui';
  28. export { requestConfig as request } from './requestConfig';
  29. /**
  30. * @see https://umijs.org/zh-CN/plugins/plugin-initial-state
  31. */
  32. export async function getInitialState(): Promise<GlobalInitialState> {
  33. const fetchUserInfo = async () => {
  34. globalGetSeverTime();
  35. try {
  36. const response = await getUserInfo();
  37. return {
  38. ...response.user,
  39. avatar: response.user.avatar || require('@/assets/img/avatar-default.png'),
  40. permissions: response.permissions,
  41. roleNames: response.roles,
  42. } as API.CurrentUser;
  43. } catch (error) {
  44. console.error('getInitialState', error);
  45. // gotoLoginPage(true);
  46. gotoHomePage();
  47. }
  48. return undefined;
  49. };
  50. const token = getAccessToken();
  51. if (token) {
  52. const currentUser = await fetchUserInfo();
  53. return {
  54. fetchUserInfo,
  55. currentUser,
  56. settings: defaultSettings as Partial<LayoutSettings>,
  57. collapsed: false,
  58. };
  59. }
  60. return {
  61. fetchUserInfo,
  62. settings: defaultSettings as Partial<LayoutSettings>,
  63. collapsed: false,
  64. };
  65. }
  66. // ProLayout 支持的api https://procomponents.ant.design/components/layout
  67. export const layout: RuntimeConfig['layout'] = ({ initialState }) => {
  68. return {
  69. ErrorBoundary: ErrorBoundary,
  70. rightContentRender: false,
  71. menu: {
  72. locale: false,
  73. // 每当 initialState?.currentUser?.userid 发生修改时重新执行 request
  74. params: {
  75. userId: initialState?.currentUser?.userId,
  76. },
  77. request: async () => {
  78. if (!initialState?.currentUser?.userId) {
  79. return [];
  80. }
  81. return getRemoteMenu();
  82. },
  83. },
  84. childrenRender: (children) => {
  85. // 增加一个 loading 的状态
  86. // if (initialState?.loading) return <PageLoading />;
  87. return (
  88. <div className="kf-page-container">
  89. <RightContent></RightContent>
  90. <div className="kf-page-container__content">{children}</div>
  91. </div>
  92. );
  93. },
  94. collapsedButtonRender: false,
  95. collapsed: initialState?.collapsed,
  96. menuProps: {
  97. onClick: () => {
  98. // 点击菜单项,删除所有的页面 state 缓存
  99. removeAllPageCacheState();
  100. },
  101. },
  102. ...initialState?.settings,
  103. logo: require('@/assets/img/logo.png'),
  104. token: {
  105. sider: {
  106. colorTextMenu: themes['textColor'],
  107. colorTextMenuSelected: themes['primaryColor'],
  108. colorTextMenuActive: themes['primaryColor'],
  109. colorTextMenuItemHover: themes['primaryColor'],
  110. colorBgMenuItemSelected: 'rgba(197, 232, 255, 0.8)',
  111. colorMenuBackground: themes['siderBGColor'],
  112. },
  113. },
  114. menuItemRender: menuItemRender(false),
  115. subMenuItemRender: menuItemRender(true),
  116. };
  117. };
  118. export const onRouteChange: RuntimeConfig['onRouteChange'] = async (e) => {
  119. // console.log('onRouteChange');
  120. // 路由切换时,尤其是回退时,关闭打开的弹框
  121. closeAllModals();
  122. const { location } = e;
  123. const pathname = location.pathname;
  124. const token = getAccessToken();
  125. // 没有 token,跳转到登录页面
  126. if (!token && needAuth(pathname)) {
  127. gotoHomePage();
  128. return;
  129. }
  130. // 有 token, 登录页面直接跳转到首页
  131. if (token && isLoginPage(pathname)) {
  132. history.push(HomeUrl);
  133. }
  134. const menus = getRemoteMenu();
  135. // 没有菜单,刷新页面
  136. if (menus === null && needAuth(pathname)) {
  137. history.go(0);
  138. }
  139. };
  140. export const patchRoutes: RuntimeConfig['patchRoutes'] = () => {
  141. //console.log('patchRoutes', e);
  142. };
  143. export const patchClientRoutes: RuntimeConfig['patchClientRoutes'] = (e) => {
  144. // console.log('patchClientRoutes', e);
  145. patchRouteWithRemoteMenus(e.routes);
  146. };
  147. export function render(oldRender: () => void) {
  148. // console.log('render');
  149. const token = getAccessToken();
  150. if (!token) {
  151. oldRender();
  152. return;
  153. }
  154. // 有 token,获取路由
  155. getRoutersInfo()
  156. .then((res) => {
  157. setRemoteMenu(res);
  158. oldRender();
  159. })
  160. .catch(() => {
  161. oldRender();
  162. });
  163. }
  164. export const useQiankunStateForSlave = () => {
  165. const [globalState, setGlobalState] = useState<any>({
  166. slogan: 'Hello MicroFrontend',
  167. });
  168. return {
  169. globalState,
  170. setGlobalState,
  171. };
  172. };
  173. // 主题修改
  174. export const antd: RuntimeAntdConfig = (memo) => {
  175. memo.theme ??= {};
  176. memo.theme.token = {
  177. colorPrimary: themes['primaryColor'],
  178. colorSuccess: themes['successColor'],
  179. colorError: themes['errorColor'],
  180. colorWarning: themes['warningColor'],
  181. colorLink: themes['primaryColor'],
  182. colorText: themes['textColor'],
  183. controlHeightLG: 46,
  184. };
  185. memo.theme.components ??= {};
  186. memo.theme.components.Tabs = {};
  187. memo.theme.components.Button = {
  188. defaultBg: 'rgba(22, 100, 255, 0.06)',
  189. defaultBorderColor: 'rgba(22, 100, 255, 0.11)',
  190. defaultColor: themes['textColor'],
  191. defaultHoverBg: 'rgba(22, 100, 255, 0.06)',
  192. defaultHoverBorderColor: 'rgba(22, 100, 255, 0.5)',
  193. defaultHoverColor: '#3F7FFF',
  194. defaultActiveBg: 'rgba(22, 100, 255, 0.12)',
  195. defaultActiveBorderColor: 'rgba(22, 100, 255, 0.75)',
  196. defaultActiveColor: themes['primaryColor'],
  197. contentFontSize: parseInt(themes['fontSize']),
  198. };
  199. memo.theme.components.Input = {
  200. inputFontSize: parseInt(themes['fontSizeInput']),
  201. inputFontSizeLG: parseInt(themes['fontSizeInputLg']),
  202. paddingBlockLG: 10,
  203. };
  204. memo.theme.components.Select = {
  205. singleItemHeightLG: 46,
  206. optionSelectedColor: themes['primaryColor'],
  207. };
  208. memo.theme.components.Table = {
  209. headerBg: 'rgba(242, 244, 247, 0.36)',
  210. headerBorderRadius: 4,
  211. // rowSelectedBg: 'rgba(22, 100, 255, 0.05)', 固定列时,横向滑动导致重叠
  212. };
  213. memo.theme.components.Tabs = {
  214. titleFontSize: 16,
  215. };
  216. memo.theme.components.Form = {
  217. labelColor: 'rgba(29, 29, 32, 0.8);',
  218. };
  219. memo.theme.components.Breadcrumb = {
  220. iconFontSize: parseInt(themes['fontSize']),
  221. linkColor: 'rgba(29, 29, 32, 0.7)',
  222. separatorColor: 'rgba(29, 29, 32, 0.7)',
  223. };
  224. memo.theme.components.Tree = {
  225. directoryNodeSelectedBg: 'rgba(22, 100, 255, 0.7)',
  226. };
  227. memo.theme.cssVar = true;
  228. memo.theme.hashed = false;
  229. memo.appConfig = {
  230. message: {
  231. // 配置 message 最大显示数,超过限制时,最早的消息会被自动关闭
  232. maxCount: 3,
  233. },
  234. };
  235. return memo;
  236. };