app.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import { AvatarDropdown, AvatarName, Question, SelectLang } from '@/components';
  2. import { LinkOutlined } from '@ant-design/icons';
  3. import type { Settings as LayoutSettings } from '@ant-design/pro-components';
  4. import { SettingDrawer } from '@ant-design/pro-components';
  5. import { history, Link, RequestConfig, RunTimeLayoutConfig } from '@umijs/max';
  6. import defaultSettings from '../config/defaultSettings';
  7. import { AxiosResponse } from 'axios';
  8. import { message } from 'antd';
  9. import { Response } from '@/core/network';
  10. import { treeFormatBySymmetry } from './utils/tree';
  11. import { getStaffMeApi } from './services/swagger/staffAdmin';
  12. const isDev = process.env.NODE_ENV === 'development';
  13. const loginPath = '/user/login';
  14. /**
  15. * @see https://umijs.org/zh-CN/plugins/plugin-initial-state
  16. * */
  17. export async function getInitialState(): Promise<{
  18. settings?: Partial<LayoutSettings>;
  19. currentUser?: SectApi.StaffVO;
  20. loading?: boolean;
  21. fetchUserInfo?: (...arg: any[]) => Promise<SectApi.StaffVO | undefined>;
  22. }> {
  23. const fetchUserInfo = async () => {
  24. try {
  25. const rs = await getStaffMeApi({
  26. skipErrorHandler: true
  27. });
  28. if (!rs.success) {
  29. message.warning(rs.errorMessage);
  30. history.push(loginPath);
  31. return undefined;
  32. }
  33. return rs.data
  34. } catch (error) {
  35. history.push(loginPath);
  36. }
  37. return undefined;
  38. };
  39. // 如果不是登录页面,执行
  40. const { location } = history;
  41. if (location.pathname !== loginPath) {
  42. const currentUser = await fetchUserInfo();
  43. return {
  44. fetchUserInfo,
  45. currentUser,
  46. settings: defaultSettings as Partial<LayoutSettings>,
  47. };
  48. }
  49. return {
  50. fetchUserInfo,
  51. settings: defaultSettings as Partial<LayoutSettings>,
  52. };
  53. }
  54. // ProLayout 支持的api https://procomponents.ant.design/components/layout
  55. export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) => {
  56. return {
  57. actionsRender: () => [<Question key="doc" />, <SelectLang key="SelectLang" />],
  58. avatarProps: {
  59. src: initialState?.currentUser?.thumbAvatar,
  60. title: <AvatarName />,
  61. render: (_, avatarChildren) => {
  62. return <AvatarDropdown>{avatarChildren}</AvatarDropdown>;
  63. },
  64. },
  65. menu: {
  66. params:{
  67. },
  68. request: async (params, defaultMenuData) => {
  69. const authMenus = initialState?.currentUser?.menus ?? []
  70. const routes = treeFormatBySymmetry(defaultMenuData,authMenus, (treeNode, referTreeNode) => {
  71. const treeNodePath = treeNode.path?.toLocaleLowerCase()
  72. const referTreeNodePath = (referTreeNode?.url || '').toLocaleLowerCase()
  73. const isHasAuth = treeNodePath && referTreeNodePath && treeNodePath === referTreeNodePath
  74. if(isHasAuth) {
  75. return {
  76. ...treeNode,
  77. }
  78. }
  79. } )
  80. return routes
  81. },
  82. },
  83. // waterMarkProps: {
  84. // content: initialState?.currentUser?.name,
  85. // },
  86. //footerRender: () => <Footer />,
  87. onPageChange: () => {
  88. const { location } = history;
  89. // 如果没有登录,重定向到 login
  90. if (!initialState?.currentUser && location.pathname !== loginPath) {
  91. history.push(loginPath);
  92. }
  93. },
  94. bgLayoutImgList: [
  95. {
  96. src: 'https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/D2LWSqNny4sAAAAAAAAAAAAAFl94AQBr',
  97. left: 85,
  98. bottom: 100,
  99. height: '303px',
  100. },
  101. {
  102. src: 'https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/C2TWRpJpiC0AAAAAAAAAAAAAFl94AQBr',
  103. bottom: -68,
  104. right: -45,
  105. height: '303px',
  106. },
  107. {
  108. src: 'https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/F6vSTbj8KpYAAAAAAAAAAAAAFl94AQBr',
  109. bottom: 0,
  110. left: 0,
  111. width: '331px',
  112. },
  113. ],
  114. links: isDev
  115. ? [
  116. <Link key="openapi" to="/umi/plugin/openapi" target="_blank">
  117. <LinkOutlined />
  118. <span>OpenAPI 文档</span>
  119. </Link>,
  120. ]
  121. : [],
  122. menuHeaderRender: undefined,
  123. // 自定义 403 页面
  124. // unAccessible: <div>unAccessible</div>,
  125. // 增加一个 loading 的状态
  126. childrenRender: (children) => {
  127. // if (initialState?.loading) return <PageLoading />;
  128. return (
  129. <>
  130. {children}
  131. {isDev && (
  132. <SettingDrawer
  133. disableUrlParams
  134. enableDarkTheme
  135. settings={initialState?.settings}
  136. onSettingChange={(settings) => {
  137. setInitialState((preInitialState) => ({
  138. ...preInitialState,
  139. settings,
  140. }));
  141. }}
  142. />
  143. )}
  144. </>
  145. );
  146. },
  147. ...initialState?.settings,
  148. };
  149. };
  150. /**
  151. * @name request 配置,可以配置错误处理
  152. * 它基于 axios 和 ahooks 的 useRequest 提供了一套统一的网络请求和错误处理方案。
  153. * @doc https://umijs.org/docs/max/request#配置
  154. *
  155. *
  156. */
  157. export const request: RequestConfig = {
  158. errorConfig: {},
  159. requestInterceptors: [
  160. (url, options) => {
  161. const token = localStorage.getItem('token');
  162. if (token) {
  163. options.headers = {
  164. ...options.headers,
  165. Authorization: token,
  166. };
  167. }
  168. return {
  169. url,
  170. options,
  171. };
  172. },
  173. ],
  174. responseInterceptors: [
  175. // @ts-ignore
  176. <T extends any>(response: AxiosResponse<Response<T>>) => {
  177. console.log('response', response);
  178. console.log('res', response.status);
  179. if (response.status === 200) {
  180. const data = response.data as any;
  181. if (data.code !== undefined && data.msg !== undefined) {
  182. if (data.code === 0) {
  183. response.data = {
  184. success: true,
  185. data: data.data,
  186. errorCode: data.code,
  187. errorMessage: data.msg,
  188. };
  189. } else {
  190. response.data = {
  191. success: false,
  192. data: data,
  193. errorCode: data.code,
  194. errorMessage: data.msg,
  195. };
  196. }
  197. } else {
  198. response.data = {
  199. success: false,
  200. errorCode: -1,
  201. errorMessage: '返回结构错误!',
  202. data: undefined as T,
  203. };
  204. }
  205. } else {
  206. response.data = {
  207. success: false,
  208. errorCode: response.status,
  209. errorMessage: response.statusText,
  210. data: undefined as T,
  211. };
  212. }
  213. return response;
  214. },
  215. ],
  216. };