app.tsx 11 KB

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