app.tsx 2.9 KB

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