app.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // import ws, { onMessage } from '@/utils/ws'
  8. import { tryChangeWorkStatus } from './components/RightContent/Book'
  9. import { getAuthCache, setAuthCache } from './utils/auth'
  10. import { LAYOUT_COLLAPSED_KEY } from './utils/cache/cacheEnum'
  11. const loginPath = '/user/login'
  12. export async function getInitialState() {
  13. // 如果是登录页面,不执行
  14. const fetchUserInfo = async () => {
  15. try {
  16. const res = await queryCurrentUser()
  17. return res.data
  18. } catch (error) {
  19. history.push(loginPath)
  20. }
  21. return undefined
  22. }
  23. const fetchPermData = async () => {
  24. try {
  25. const res = await queryPermData()
  26. return res.data
  27. } catch (error) {
  28. message.error('获取数据权限失败,请联系管理员')
  29. }
  30. return {}
  31. }
  32. // 如果是登录页面,不执行
  33. if (history.location.pathname !== loginPath) {
  34. const userInfo = (await fetchUserInfo()) || {}
  35. const permData = (await fetchPermData()) || {}
  36. // if (userInfo?.currentUser?.staffId) {
  37. // const { username, staffId: id, wsToken: token } = userInfo?.currentUser
  38. // ws.init(`ws://cld2qa.com/summon/v1/chat/link?username=${username}&id=${id}&token=${token}`, {
  39. // onMessage
  40. // })
  41. // }
  42. const collapsed = getAuthCache(LAYOUT_COLLAPSED_KEY) || false
  43. return {
  44. fetchUserInfo,
  45. fetchPermData,
  46. permData,
  47. ...userInfo,
  48. settings: { collapsed, defaultCollapsed: collapsed }
  49. }
  50. }
  51. return {
  52. fetchUserInfo,
  53. fetchPermData,
  54. settings: {}
  55. }
  56. }
  57. export const layout = ({ initialState, setInitialState }) => {
  58. const onCollapse = collapsed => {
  59. setInitialState({ ...initialState, settings: { ...initialState.settings, collapsed } })
  60. setAuthCache(LAYOUT_COLLAPSED_KEY, collapsed)
  61. }
  62. return {
  63. logo,
  64. rightContentRender: () => <RightContent />,
  65. disableContentMargin: false,
  66. waterMarkProps: {
  67. content: initialState?.currentUser?.username
  68. },
  69. onPageChange: async location => {
  70. // 如果没有登录,重定向到 login
  71. if (!initialState?.currentUser?.staffId && location.pathname !== loginPath) {
  72. history.replcae('/user/login')
  73. } else {
  74. location.pathname !== loginPath &&
  75. tryChangeWorkStatus(initialState?.currentUser?.staffId, location.pathname)
  76. }
  77. // if (
  78. // initialState?.currentUser?.staffId &&
  79. // location.pathname !== loginPath &&
  80. // PERMISSION_NEED_PATH.includes(location.pathname) &&
  81. // !initialState?.permData
  82. // ) {
  83. // const permData = await initialState.fetchPermData()
  84. // permData && setInitialState({ ...initialState, permData })
  85. // }
  86. },
  87. childrenRender: children => (
  88. <>
  89. {children}
  90. <BasicDrawer />
  91. <BasicModal />
  92. </>
  93. ),
  94. // links: [
  95. // <div className="ant-menu-title-content">
  96. // <AddressBook theme="outline" className="anticon" />
  97. // <span className="ant-pro-menu-item-title">通讯录</span>
  98. // </div>
  99. // ],
  100. // menuHeaderRender: false,
  101. ...initialState?.settings,
  102. breakpoint: false,
  103. onCollapse
  104. }
  105. }