app.tsx 5.2 KB

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