app.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { queryCurrentUser, queryPermData } from './services/user'
  2. import type { RequestConfig } from 'umi'
  3. import { ErrorShowType } from 'umi'
  4. import { history } from 'umi'
  5. import RightContent from '@/components/RightContent'
  6. import { message, notification } from 'antd'
  7. import logo from '../public/logo.svg'
  8. import { getToken } from '@/utils/auth'
  9. import { tryChangeWorkStatus } from './components/RightContent/Book'
  10. import { getAuthCache, setAuthCache } from './utils/auth'
  11. import { LAYOUT_COLLAPSED_KEY } from './utils/cache/cacheEnum'
  12. import consts from './consts'
  13. import ModalStore from './components/ModalStore/import'
  14. import { stringify } from 'qs'
  15. import MessageDetail from './pages/Hr/Notification/components/MessageDetail'
  16. const loginPath = '/user/login'
  17. export async function getInitialState() {
  18. // 如果是登录页面,不执行
  19. const fetchUserInfo = async () => {
  20. try {
  21. const res = await queryCurrentUser()
  22. return res.data
  23. } catch (error) {
  24. history.push(loginPath)
  25. }
  26. return undefined
  27. }
  28. const fetchPermData = async () => {
  29. try {
  30. const res = await queryPermData()
  31. return res.data
  32. } catch (error) {
  33. message.error('获取数据权限失败,请联系管理员')
  34. }
  35. return {}
  36. }
  37. // 如果是登录页面,不执行
  38. if (history.location.pathname !== loginPath) {
  39. const userInfo = (await fetchUserInfo()) || {}
  40. const permData = (await fetchPermData()) || {}
  41. const collapsed = getAuthCache(LAYOUT_COLLAPSED_KEY) || false
  42. return {
  43. fetchUserInfo,
  44. fetchPermData,
  45. permData,
  46. ...userInfo,
  47. settings: { collapsed, defaultCollapsed: collapsed }
  48. }
  49. }
  50. return {
  51. fetchUserInfo,
  52. fetchPermData,
  53. settings: {}
  54. }
  55. }
  56. export const layout = ({ initialState, setInitialState }) => {
  57. const onCollapse = collapsed => {
  58. setInitialState({ ...initialState, settings: { ...initialState.settings, collapsed } })
  59. setAuthCache(LAYOUT_COLLAPSED_KEY, collapsed)
  60. }
  61. return {
  62. logo,
  63. rightContentRender: () => (
  64. <ModalStore _modalMap={{ D_NOTICE_DETAIL: MessageDetail }}>
  65. <RightContent />
  66. </ModalStore>
  67. ),
  68. disableContentMargin: false,
  69. waterMarkProps: {
  70. content: initialState?.currentUser?.username
  71. },
  72. onPageChange: async location => {
  73. // 如果没有登录,重定向到 login
  74. if (!initialState?.currentUser?.staffId && location.pathname !== loginPath) {
  75. history.replace({
  76. pathname: loginPath,
  77. search: stringify({
  78. redirect: history.location.pathname
  79. })
  80. })
  81. } else {
  82. location.pathname !== loginPath &&
  83. tryChangeWorkStatus(initialState?.currentUser?.staffId, location.pathname)
  84. }
  85. },
  86. childrenRender: children => <ModalStore>{children}</ModalStore>,
  87. ...initialState?.settings,
  88. breakpoint: false,
  89. onCollapse
  90. }
  91. }
  92. const errorHandler = error => {
  93. const { data, info } = error
  94. if (info) {
  95. const { errorMessage = '请求失败', showType, errorCode } = info
  96. if (consts.TOKEN_INVALID_CODE.includes(errorCode)) {
  97. history.replace({
  98. pathname: loginPath,
  99. search: stringify({
  100. redirect: window.location.pathname
  101. })
  102. })
  103. }
  104. switch (showType) {
  105. case ErrorShowType.ERROR_MESSAGE:
  106. message.error(errorMessage)
  107. break
  108. case ErrorShowType.WARN_MESSAGE:
  109. message.warn(errorMessage)
  110. break
  111. case ErrorShowType.NOTIFICATION:
  112. const title = consts.TOKEN_INVALID_CODE.includes(errorCode) ? '用户信息过期' : '请求失败'
  113. notification.error({
  114. message: title,
  115. description: errorMessage
  116. })
  117. break
  118. default:
  119. break
  120. }
  121. } else {
  122. notification.error({
  123. description: '您的网络发生异常,无法连接服务器',
  124. message: '网络异常'
  125. })
  126. }
  127. return data
  128. }
  129. const authHeaderInterceptor = (url, options) => {
  130. const token = getToken()
  131. if (token) {
  132. // 在白名单里的请求放过
  133. if (consts.TOKEN_WHITE_LIST.includes(url)) {
  134. return { url, options }
  135. }
  136. // eslint-disable-next-line no-param-reassign
  137. options.headers[consts.TOKEN_HEADER] = `bearer ${token}`
  138. return { url, options }
  139. }
  140. return { url, options }
  141. }
  142. export const request: RequestConfig = {
  143. errorHandler,
  144. prefix: '/api',
  145. // 默认错误处理
  146. credentials: 'include', // 默认请求是否带上cookie
  147. prefix: consts.PREFIX_URL,
  148. cache: 'no-cache',
  149. errorConfig: {
  150. adaptor: resData => {
  151. return {
  152. success: resData.code === consts.RET_CODE.SUCCESS,
  153. errorMessage: resData.msg,
  154. errorCode: resData.code,
  155. showType: resData.showType
  156. }
  157. }
  158. },
  159. requestInterceptors: [authHeaderInterceptor]
  160. }