app.tsx 12 KB

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