| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- import { queryCurrentUser, queryPermData } from './services/user'
- import { history } from 'umi'
- import RightContent from '@/components/RightContent'
- import { message } from 'antd'
- import logo from '../public/logo.svg'
- import BasicModal, { BasicDrawer } from '@/components/Modal'
- // import ws, { onMessage } from '@/utils/ws'
- import { tryChangeWorkStatus } from './components/RightContent/Book'
- import { getAuthCache, setAuthCache } from './utils/auth'
- import { LAYOUT_COLLAPSED_KEY } from './utils/cache/cacheEnum'
- const loginPath = '/user/login'
- export async function getInitialState() {
- // 如果是登录页面,不执行
- const fetchUserInfo = async () => {
- try {
- const res = await queryCurrentUser()
- return res.data
- } catch (error) {
- history.push(loginPath)
- }
- return undefined
- }
- const fetchPermData = async () => {
- try {
- const res = await queryPermData()
- return res.data
- } catch (error) {
- message.error('获取数据权限失败,请联系管理员')
- }
- return {}
- }
- // 如果是登录页面,不执行
- if (history.location.pathname !== loginPath) {
- const userInfo = (await fetchUserInfo()) || {}
- const permData = (await fetchPermData()) || {}
- // if (userInfo?.currentUser?.staffId) {
- // const { username, staffId: id, wsToken: token } = userInfo?.currentUser
- // ws.init(`ws://cld2qa.com/summon/v1/chat/link?username=${username}&id=${id}&token=${token}`, {
- // onMessage
- // })
- // }
- const collapsed = getAuthCache(LAYOUT_COLLAPSED_KEY) || false
- return {
- fetchUserInfo,
- fetchPermData,
- permData,
- ...userInfo,
- settings: { collapsed, defaultCollapsed: collapsed }
- }
- }
- return {
- fetchUserInfo,
- fetchPermData,
- settings: {}
- }
- }
- export const layout = ({ initialState, setInitialState }) => {
- const onCollapse = collapsed => {
- setInitialState({ ...initialState, settings: { ...initialState.settings, collapsed } })
- setAuthCache(LAYOUT_COLLAPSED_KEY, collapsed)
- }
- return {
- logo,
- rightContentRender: () => <RightContent />,
- disableContentMargin: false,
- waterMarkProps: {
- content: initialState?.currentUser?.username
- },
- onPageChange: async location => {
- // 如果没有登录,重定向到 login
- if (!initialState?.currentUser?.staffId && location.pathname !== loginPath) {
- history.replcae('/user/login')
- } else {
- location.pathname !== loginPath &&
- tryChangeWorkStatus(initialState?.currentUser?.staffId, location.pathname)
- }
- // if (
- // initialState?.currentUser?.staffId &&
- // location.pathname !== loginPath &&
- // PERMISSION_NEED_PATH.includes(location.pathname) &&
- // !initialState?.permData
- // ) {
- // const permData = await initialState.fetchPermData()
- // permData && setInitialState({ ...initialState, permData })
- // }
- },
- childrenRender: children => (
- <>
- {children}
- <BasicDrawer />
- <BasicModal />
- </>
- ),
- // links: [
- // <div className="ant-menu-title-content">
- // <AddressBook theme="outline" className="anticon" />
- // <span className="ant-pro-menu-item-title">通讯录</span>
- // </div>
- // ],
- // menuHeaderRender: false,
- ...initialState?.settings,
- breakpoint: false,
- onCollapse
- }
- }
|