app.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { queryCurrentUser, queryPermData } from './services/user'
  2. import { history } from 'umi'
  3. import RightContent from '@/components/RightContent'
  4. import { message } from 'antd'
  5. import logo from '../public/logo.svg'
  6. import BasicModal, { BasicDrawer } from '@/components/Modal'
  7. const loginPath = '/user/login'
  8. export async function getInitialState() {
  9. // 如果是登录页面,不执行
  10. const fetchUserInfo = async () => {
  11. try {
  12. const res = await queryCurrentUser()
  13. return res.data
  14. } catch (error) {
  15. history.push(loginPath)
  16. }
  17. return undefined
  18. }
  19. const fetchPermData = async () => {
  20. try {
  21. const res = await queryPermData()
  22. return res.data
  23. } catch (error) {
  24. message.error('获取数据权限失败,请联系管理员')
  25. }
  26. return {}
  27. }
  28. // 如果是登录页面,不执行
  29. if (history.location.pathname !== loginPath) {
  30. const currentUser = (await fetchUserInfo()) || {}
  31. const permData = (await fetchPermData()) || {}
  32. return {
  33. fetchUserInfo,
  34. fetchPermData,
  35. permData,
  36. ...currentUser,
  37. settings: {}
  38. }
  39. }
  40. return {
  41. fetchUserInfo,
  42. fetchPermData,
  43. settings: {}
  44. }
  45. }
  46. export const layout = ({ initialState }) => {
  47. return {
  48. logo,
  49. rightContentRender: () => <RightContent />,
  50. disableContentMargin: false,
  51. waterMarkProps: {
  52. content: initialState?.currentUser?.username
  53. },
  54. onPageChange: async () => {
  55. const { location } = history
  56. // 如果没有登录,重定向到 login
  57. if (!initialState?.currentUser?.staffId && location.pathname !== loginPath) {
  58. history.replcae('/user/login')
  59. }
  60. // if (
  61. // initialState?.currentUser?.staffId &&
  62. // location.pathname !== loginPath &&
  63. // PERMISSION_NEED_PATH.includes(location.pathname) &&
  64. // !initialState?.permData
  65. // ) {
  66. // const permData = await initialState.fetchPermData()
  67. // permData && setInitialState({ ...initialState, permData })
  68. // }
  69. },
  70. childrenRender: children => (
  71. <>
  72. {children}
  73. <BasicDrawer />
  74. <BasicModal />
  75. </>
  76. ),
  77. // menuHeaderRender: false,
  78. ...initialState?.settings
  79. }
  80. }