app.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 { history, Link } from 'umi';
  6. import RightContent from '@/components/RightContent';
  7. import Footer from '@/components/Footer';
  8. import { BookOutlined, LinkOutlined } from '@ant-design/icons';
  9. import Service from '@/pages/user/Login/service';
  10. import { service as SystemConfigService } from '@/pages/system/Config';
  11. import Token from '@/utils/token';
  12. import type { RequestOptionsInit } from 'umi-request';
  13. import ReconnectingWebSocket from 'reconnecting-websocket';
  14. import SystemConst from '@/utils/const';
  15. import { service as MenuService } from '@/pages/system/Menu';
  16. import getRoutes, { extraRouteArr, getMenus, handleRoutes, saveMenusCache } from '@/utils/menu';
  17. import { AIcon } from '@/components';
  18. import React from 'react';
  19. const isDev = process.env.NODE_ENV === 'development';
  20. const loginPath = '/user/login';
  21. const bindPath = '/account/center/bind';
  22. let extraRoutes: any[] = [];
  23. /** 获取用户信息比较慢的时候会展示一个 loading */
  24. export const initialStateConfig = {
  25. loading: <PageLoading />,
  26. };
  27. /**
  28. * @see https://umijs.org/zh-CN/plugins/plugin-initial-state
  29. * */
  30. export async function getInitialState(): Promise<{
  31. settings?: Partial<LayoutSettings>;
  32. currentUser?: UserInfo;
  33. fetchUserInfo?: () => Promise<UserInfo | undefined>;
  34. }> {
  35. const fetchUserInfo = async () => {
  36. try {
  37. const user = await Service.queryCurrent();
  38. return user.result;
  39. } catch (error) {
  40. history.push(loginPath);
  41. }
  42. return undefined;
  43. };
  44. const getSettings = async () => {
  45. try {
  46. const res = await Service.settingDetail(['basis']);
  47. return res.result;
  48. } catch (error) {
  49. history.push(loginPath);
  50. }
  51. return undefined;
  52. };
  53. // 如果是登录页面,不执行
  54. if (history.location.pathname !== loginPath && history.location.pathname !== bindPath) {
  55. const currentUser = await fetchUserInfo();
  56. const settings = await getSettings();
  57. return {
  58. fetchUserInfo,
  59. currentUser,
  60. settings: settings?.[0].properties,
  61. };
  62. }
  63. // 链接websocket
  64. const url = `${document.location.protocol.replace('http', 'ws')}//${document.location.host}/${
  65. SystemConst.API_BASE
  66. }/messaging/${Token.get()}?:X_Access_Token=${Token.get()}`;
  67. const ws = new ReconnectingWebSocket(url);
  68. // ws.send('sss');
  69. ws.onerror = () => {
  70. console.log('链接错误。ws');
  71. };
  72. return {
  73. fetchUserInfo,
  74. settings: {},
  75. };
  76. }
  77. /**
  78. * 异常处理程序
  79. 200: '服务器成功返回请求的数据。',
  80. 201: '新建或修改数据成功。',
  81. 202: '一个请求已经进入后台排队(异步任务)。',
  82. 204: '删除数据成功。',
  83. 400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
  84. 401: '用户没有权限(令牌、用户名、密码错误)。',
  85. 403: '用户得到授权,但是访问是被禁止的。',
  86. 404: '发出的请求针对的是不存在的记录,服务器没有进行操作。',
  87. 405: '请求方法不被允许。',
  88. 406: '请求的格式不可得。',
  89. 410: '请求的资源被永久删除,且不会再得到的。',
  90. 422: '当创建一个对象时,发生一个验证错误。',
  91. 500: '服务器发生错误,请检查服务器。',
  92. 502: '网关错误。',
  93. 503: '服务不可用,服务器暂时过载或维护。',
  94. 504: '网关超时。',
  95. //-----English
  96. 200: The server successfully returned the requested data. ',
  97. 201: New or modified data is successful. ',
  98. 202: A request has entered the background queue (asynchronous task). ',
  99. 204: Data deleted successfully. ',
  100. 400: 'There was an error in the request sent, and the server did not create or modify data. ',
  101. 401: The user does not have permission (token, username, password error). ',
  102. 403: The user is authorized, but access is forbidden. ',
  103. 404: The request sent was for a record that did not exist. ',
  104. 405: The request method is not allowed. ',
  105. 406: The requested format is not available. ',
  106. 410':
  107. 'The requested resource is permanently deleted and will no longer be available. ',
  108. 422: When creating an object, a validation error occurred. ',
  109. 500: An error occurred on the server, please check the server. ',
  110. 502: Gateway error. ',
  111. 503: The service is unavailable. ',
  112. 504: The gateway timed out. ',
  113. * @see https://beta-pro.ant.design/docs/request-cn
  114. */
  115. /**
  116. * Token 拦截器
  117. * @param url
  118. * @param options
  119. */
  120. const filterUrl = [
  121. '/authorize/captcha/config',
  122. '/authorize/login',
  123. '/sso/bind-code/',
  124. '/sso/providers',
  125. ];
  126. const requestInterceptor = (url: string, options: RequestOptionsInit) => {
  127. // const {params} = options;
  128. let authHeader = {};
  129. if (!filterUrl.some((fUrl) => url.includes(fUrl))) {
  130. authHeader = { 'X-Access-Token': Token.get() || '' };
  131. }
  132. return {
  133. url: `${url}`,
  134. options: {
  135. ...options,
  136. // 格式化成后台需要的查询参数
  137. // params: encodeQueryParam(params),
  138. interceptors: true,
  139. headers: authHeader,
  140. },
  141. };
  142. };
  143. export const request: RequestConfig = {
  144. errorHandler: (error: any) => {
  145. const { response } = error;
  146. if (response.status === 401) {
  147. history.push('/user/login');
  148. return;
  149. }
  150. if (response.status === 400 || response.status === 500) {
  151. // 添加clone() 避免后续其它地方用response.text()时报错
  152. response
  153. .clone()
  154. .text()
  155. .then((resp: string) => {
  156. if (resp) {
  157. notification.error({
  158. key: 'error',
  159. message: JSON.parse(resp).message || '服务器内部错误!',
  160. });
  161. } else {
  162. response
  163. .clone()
  164. .json()
  165. .then((res: any) => {
  166. notification.error({
  167. key: 'error',
  168. message: `请求错误:${res.message}`,
  169. });
  170. })
  171. .catch(() => {
  172. notification.error({
  173. key: 'error',
  174. message: '系统错误',
  175. });
  176. });
  177. }
  178. });
  179. return response;
  180. }
  181. if (!response) {
  182. notification.error({
  183. description: '您的网络发生异常,无法连接服务器',
  184. message: '网络异常',
  185. });
  186. }
  187. return response;
  188. },
  189. requestInterceptors: [requestInterceptor],
  190. };
  191. const MenuItemIcon = (
  192. menuItemProps: any,
  193. defaultDom: React.ReactNode,
  194. props: any,
  195. ): React.ReactNode => {
  196. if (defaultDom) {
  197. // @ts-ignore
  198. const menuItem = React.cloneElement(defaultDom, {
  199. // @ts-ignore
  200. children: [<AIcon type={menuItemProps.icon as string} />, defaultDom.props.children[1]],
  201. ...props,
  202. });
  203. return menuItem;
  204. }
  205. return defaultDom;
  206. };
  207. // ProLayout 支持的api https://procomponents.ant.design/components/layout
  208. export const layout: RunTimeLayoutConfig = ({ initialState }) => {
  209. // console.log({ ...initialState });
  210. return {
  211. navTheme: 'light',
  212. headerTheme: 'light',
  213. rightContentRender: () => <RightContent />,
  214. disableContentMargin: false,
  215. waterMarkProps: {
  216. // content: initialState?.currentUser?.name,
  217. },
  218. footerRender: () => <Footer />,
  219. onPageChange: () => {
  220. const { location } = history;
  221. // 如果没有登录,重定向到 login
  222. if (
  223. !initialState?.currentUser &&
  224. location.pathname !== loginPath &&
  225. location.pathname !== bindPath
  226. ) {
  227. history.push(loginPath);
  228. }
  229. },
  230. menuDataRender: () => {
  231. return getMenus(extraRoutes);
  232. },
  233. subMenuItemRender: MenuItemIcon,
  234. menuItemRender: (menuItemProps, defaultDom) => {
  235. return MenuItemIcon(menuItemProps, defaultDom, {
  236. onClick: () => {
  237. history.push(menuItemProps.path!);
  238. },
  239. });
  240. },
  241. links: isDev
  242. ? [
  243. <Link key={1} to="/umi/plugin/openapi" target="_blank">
  244. <LinkOutlined />
  245. <span>OpenAPI 文档</span>
  246. </Link>,
  247. <Link key={2} to="/~docs">
  248. <BookOutlined />
  249. <span>业务组件文档</span>
  250. </Link>,
  251. ]
  252. : [],
  253. menuHeaderRender: undefined,
  254. // 自定义 403 页面
  255. // unAccessible: <div>unAccessible</div>,
  256. ...initialState?.settings,
  257. // title: '',
  258. // logo:''
  259. };
  260. };
  261. export function patchRoutes(routes: any) {
  262. if (extraRoutes && extraRoutes.length) {
  263. const basePath = routes.routes.find((_route: any) => _route.path === '/')!;
  264. const _routes = getRoutes(extraRoutes);
  265. const baseRedirect = {
  266. path: '/',
  267. routes: [
  268. ..._routes,
  269. {
  270. path: '/',
  271. redirect: _routes[0].path,
  272. },
  273. ],
  274. };
  275. basePath.routes = [...basePath.routes, baseRedirect];
  276. console.log(basePath.routes);
  277. }
  278. }
  279. export function render(oldRender: any) {
  280. if (history.location.pathname !== loginPath && history.location.pathname !== bindPath) {
  281. SystemConfigService.getAMapKey().then((res) => {
  282. if (res && res.status === 200 && res.result) {
  283. localStorage.setItem(SystemConst.AMAP_KEY, res.result.apiKey);
  284. }
  285. });
  286. MenuService.queryOwnThree({ paging: false }).then((res) => {
  287. if (res && res.status === 200) {
  288. if (isDev) {
  289. res.result.push({
  290. code: 'demo',
  291. id: 'demo',
  292. name: '例子',
  293. url: '/demo',
  294. });
  295. }
  296. extraRoutes = handleRoutes([...res.result, ...extraRouteArr]);
  297. saveMenusCache(extraRoutes);
  298. }
  299. oldRender();
  300. });
  301. } else {
  302. oldRender();
  303. }
  304. }