app.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import type { Settings as LayoutSettings } from '@ant-design/pro-layout'
  2. import { PageLoading } from '@ant-design/pro-layout'
  3. import { notification } from 'antd'
  4. import type { RequestConfig, RunTimeLayoutConfig } from 'umi'
  5. import { getIntl, getLocale, history } from 'umi'
  6. import RightContent from '@/components/RightContent'
  7. import Footer from '@/components/Footer'
  8. import type { RequestOptionsInit, ResponseError } from 'umi-request'
  9. import { currentUser as queryCurrentUser } from './services/user/api'
  10. import consts from './utils/consts'
  11. const isDev = process.env.NODE_ENV === 'development'
  12. const loginPath = '/user/login'
  13. /** 获取用户信息比较慢的时候会展示一个 loading */
  14. export const initialStateConfig = {
  15. loading: <PageLoading />
  16. }
  17. /**
  18. * @see https://umijs.org/zh-CN/plugins/plugin-initial-state
  19. * */
  20. export async function getInitialState(): Promise<{
  21. settings?: Partial<LayoutSettings>
  22. currentUser?: API.CurrentUser
  23. menuList?: API.MenuListItem[]
  24. fetchUserInfo?: () => Promise<API.CurrentUser | undefined>
  25. }> {
  26. // eslint-disable-next-line react-hooks/rules-of-hooks
  27. // 获取用户信息
  28. const fetchUserInfo = async () => {
  29. const currentUser = await queryCurrentUser()
  30. return currentUser
  31. }
  32. // 如果是登录页面,不执行
  33. if (history.location.pathname !== loginPath) {
  34. try {
  35. const { data: currentUser } = await fetchUserInfo()
  36. return {
  37. fetchUserInfo,
  38. menuList: currentUser.menus,
  39. currentUser: currentUser.staff,
  40. settings: {}
  41. }
  42. } catch (error) {
  43. history.push(loginPath)
  44. }
  45. return undefined
  46. }
  47. return {
  48. settings: {}
  49. }
  50. }
  51. const authHeaderInterceptor = (url: string, options: RequestOptionsInit) => {
  52. const token = window.localStorage.getItem('TOKEN_ID')
  53. // 如果是登录页面,不执行
  54. if (!token && history.location.pathname !== loginPath) {
  55. return history.push(loginPath)
  56. }
  57. const authHeader = { Authorization: `Bearer ${JSON.parse(token)}` }
  58. return {
  59. url: `${url}`,
  60. options: { ...options, interceptors: true, headers: authHeader }
  61. }
  62. }
  63. /**
  64. * 异常处理程序
  65. const codeMessage = {
  66. 200: '服务器成功返回请求的数据。',
  67. 201: '新建或修改数据成功。',
  68. 202: '一个请求已经进入后台排队(异步任务)。',
  69. 204: '删除数据成功。',
  70. 400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
  71. 401: '用户没有权限(令牌、用户名、密码错误)。',
  72. 403: '用户得到授权,但是访问是被禁止的。',
  73. 404: '发出的请求针对的是不存在的记录,服务器没有进行操作。',
  74. 405: '请求方法不被允许。',
  75. 406: '请求的格式不可得。',
  76. 410: '请求的资源被永久删除,且不会再得到的。',
  77. 422: '当创建一个对象时,发生一个验证错误。',
  78. 500: '服务器发生错误,请检查服务器。',
  79. 502: '网关错误。',
  80. 503: '服务不可用,服务器暂时过载或维护。',
  81. 504: '网关超时。',
  82. };
  83. * @see https://beta-pro.ant.design/docs/request-cn
  84. */
  85. export const request: RequestConfig = {
  86. errorHandler: (error: ResponseError) => {
  87. const { messages } = getIntl(getLocale())
  88. const { response } = error
  89. if (response && response.status) {
  90. const { status, statusText, url } = response
  91. const requestErrorMessage = messages['app.request.error']
  92. const errorMessage = `${requestErrorMessage} ${status}: ${url}`
  93. const errorDescription = messages[`app.request.${status}`] || statusText
  94. notification.error({
  95. message: errorMessage,
  96. description: errorDescription
  97. })
  98. }
  99. if (!response) {
  100. notification.error({
  101. description: '您的网络发生异常,无法连接服务器',
  102. message: '网络异常'
  103. })
  104. }
  105. throw error
  106. },
  107. middlewares: [
  108. async (ctx, next) => {
  109. await next()
  110. const { req, res } = ctx
  111. // @ts-ignore
  112. if (req.options?.skipErrorHandler) {
  113. return
  114. }
  115. const errorAdaptor = req.options?.errorConfig.adaptor || (resData => resData)
  116. const { options } = req
  117. const { getResponse } = options
  118. const resData = getResponse ? res.data : res
  119. const errorInfo = errorAdaptor(resData, ctx)
  120. if (resData.code !== consts.RET_CODE.SUCCESS) {
  121. // 抛出错误到 errorHandler 中处理
  122. const error: ResponseError = new Error(errorInfo.errorMessage)
  123. error.name = 'BizError'
  124. error.data = resData
  125. error.info = errorInfo
  126. error.response = res
  127. throw error
  128. }
  129. }
  130. ],
  131. prefix: '/backstage',
  132. errorConfig: {
  133. adaptor: resData => {
  134. return {
  135. // success: resData.code === consts.RET_CODE.SUCCESS,
  136. data: resData.data,
  137. errorCode: resData.code,
  138. errorMessage: resData.msg
  139. }
  140. }
  141. },
  142. responseInterceptors: [
  143. async (response, options) => {
  144. const errorAdaptor = options?.errorConfig.adaptor || (resData => resData)
  145. const res = await response.clone().json()
  146. const { getResponse } = options
  147. const resData = getResponse ? res.data : res
  148. const errorInfo = errorAdaptor(resData, { res: response, req: options })
  149. if (res?.code !== consts.RET_CODE.SUCCESS) {
  150. // 抛出错误到 errorHandler 中处理
  151. const error: ResponseError = new Error(errorInfo.errorMessage)
  152. error.name = 'BizError'
  153. error.data = resData
  154. error.info = errorInfo
  155. error.response = res
  156. throw error
  157. }
  158. return response
  159. }
  160. ],
  161. requestInterceptors: [authHeaderInterceptor]
  162. }
  163. // ProLayout 支持的api https://procomponents.ant.design/components/layout
  164. export const layout: RunTimeLayoutConfig = ({ initialState }) => {
  165. return {
  166. rightContentRender: () => <RightContent />,
  167. disableContentMargin: false,
  168. waterMarkProps: {
  169. content: initialState?.currentUser?.username
  170. },
  171. footerRender: () => <Footer />,
  172. onPageChange: () => {
  173. const { location } = history
  174. // 如果没有登录,重定向到 login
  175. if (!initialState?.currentUser && location.pathname !== loginPath) {
  176. history.push(loginPath)
  177. }
  178. },
  179. links: isDev
  180. ? [
  181. // <Link to="/umi/plugin/openapi" target="_blank">
  182. // <LinkOutlined />
  183. // <span>openAPI 文档</span>
  184. // </Link>,
  185. // <Link to="/~docs">
  186. // <BookOutlined />
  187. // <span>业务组件文档</span>
  188. // </Link>
  189. ]
  190. : [],
  191. menuHeaderRender: undefined,
  192. // 自定义 403 页面
  193. // unAccessible: <div>unAccessible</div>,
  194. ...initialState?.settings
  195. }
  196. }