app.jsx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. import { PageLoading } from '@ant-design/pro-layout';
  2. import { notification, Modal, message } from 'antd';
  3. import { history, Link, useModel } from 'umi';
  4. import RightContent from '@/components/RightContent';
  5. import Footer from '@/components/Footer';
  6. import { BookOutlined, LinkOutlined } from '@ant-design/icons';
  7. import { getQueryString } from './utils';
  8. import { stringify } from 'querystring';
  9. import { getSubHosp, getMenus } from './services/user';
  10. import iconEnum from './menuIcons';
  11. import { createRef } from 'react';
  12. const layoutActionRef = createRef();
  13. const defaultMenus = [
  14. {
  15. path: '/',
  16. name: 'welcome',
  17. icon: 'smile',
  18. children: [
  19. {
  20. path: '/welcome',
  21. name: 'one',
  22. icon: 'smile',
  23. children: [
  24. {
  25. path: '/welcome/welcome',
  26. name: 'two',
  27. icon: 'smile',
  28. exact: true,
  29. },
  30. ],
  31. },
  32. ],
  33. },
  34. {
  35. path: '/responsibilityCenter',
  36. name: 'demo',
  37. icon: 'heart',
  38. },
  39. ];
  40. const { query = {}, pathname } = history.location;
  41. const { redirect, hospSign } = query;
  42. const isDev = process.env.NODE_ENV === 'development';
  43. const loginParams = localStorage.getItem('loginParams');
  44. //医院标识
  45. let sign = '', isExpired = false;
  46. if (loginParams) {
  47. const { hospSign, hospId } = JSON.parse(loginParams);
  48. sign = hospSign;
  49. }
  50. const loginPath = `/user/login?hospSign=${hospSign}`;
  51. let requestMethod = 'get';
  52. /** 获取用户信息比较慢的时候会展示一个 loading */
  53. export const initialStateConfig = {
  54. loading: <PageLoading />,
  55. };
  56. /**
  57. * @see https://umijs.org/zh-CN/plugins/plugin-initial-state
  58. * */
  59. export async function getInitialState() {
  60. const fetchUserInfo = async () => {
  61. const userData = localStorage.getItem('userData');
  62. if (userData) {
  63. return JSON.parse(userData);
  64. }
  65. return undefined;
  66. }; // 如果是登录页面,不执行
  67. const getMenuDataFromServer = async () => {
  68. const resp = await getMenus();
  69. const { status, data } = resp;
  70. if (status == 200) {
  71. return data;
  72. } else {
  73. return [];
  74. }
  75. };
  76. //医院标识
  77. const sign = getQueryString('hospSign');
  78. const fetchSubHosp = async () => {
  79. // try {
  80. // const msg = await getSubHosp(sign);
  81. // } catch (error) {
  82. // }
  83. return null
  84. }
  85. if (history.location.pathname !== loginPath) {
  86. const currentUser = await fetchUserInfo();
  87. return {
  88. fetchUserInfo,
  89. currentUser,
  90. settings: {},
  91. isExpired: false,
  92. fetchSubHosp,
  93. sign,
  94. getMenuDataFromServer
  95. };
  96. }
  97. return {
  98. fetchUserInfo,
  99. settings: {},
  100. fetchSubHosp,
  101. sign,
  102. getMenuDataFromServer
  103. };
  104. }
  105. /**
  106. * 退出登录,并且将当前的 url 保存
  107. */
  108. const loginOut = async () => {
  109. // await outLogin();
  110. // Note: There may be security issues, please not
  111. localStorage.removeItem('userData');
  112. localStorage.removeItem('menus');
  113. if (window.location.pathname !== `/user/login` && !redirect) {
  114. history.replace({
  115. pathname: `/user/login`,
  116. search: stringify({
  117. redirect: pathname,
  118. hospSign: sign
  119. }),
  120. });
  121. }
  122. };
  123. /**
  124. * 异常处理程序
  125. 200: '服务器成功返回请求的数据。',
  126. 201: '新建或修改数据成功。',
  127. 202: '一个请求已经进入后台排队(异步任务)。',
  128. 204: '删除数据成功。',
  129. 400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
  130. 401: '用户没有权限(令牌、用户名、密码错误)。',
  131. 403: '用户得到授权,但是访问是被禁止的。',
  132. 404: '发出的请求针对的是不存在的记录,服务器没有进行操作。',
  133. 405: '请求方法不被允许。',
  134. 406: '请求的格式不可得。',
  135. 410: '请求的资源被永久删除,且不会再得到的。',
  136. 422: '当创建一个对象时,发生一个验证错误。',
  137. 500: '服务器发生错误,请检查服务器。',
  138. 502: '网关错误。',
  139. 503: '服务不可用,服务器暂时过载或维护。',
  140. 504: '网关超时。',
  141. //-----English
  142. 200: The server successfully returned the requested data. ',
  143. 201: New or modified data is successful. ',
  144. 202: A request has entered the background queue (asynchronous task). ',
  145. 204: Data deleted successfully. ',
  146. 400: 'There was an error in the request sent, and the server did not create or modify data. ',
  147. 401: The user does not have permission (token, username, password error). ',
  148. 403: The user is authorized, but access is forbidden. ',
  149. 404: The request sent was for a record that did not exist. ',
  150. 405: The request method is not allowed. ',
  151. 406: The requested format is not available. ',
  152. 410':
  153. 'The requested resource is permanently deleted and will no longer be available. ',
  154. 422: When creating an object, a validation error occurred. ',
  155. 500: An error occurred on the server, please check the server. ',
  156. 502: Gateway error. ',
  157. 503: The service is unavailable. ',
  158. 504: The gateway timed out. ',
  159. * @see cd
  160. */
  161. //请求拦截器
  162. const authHeaderInterceptor = (url, options) => {
  163. const { method } = options;
  164. if (method == 'post') {
  165. requestMethod = 'post'
  166. } else {
  167. requestMethod = 'get'
  168. }
  169. const userData = localStorage.getItem('userData');
  170. let authHeader = {};
  171. if (userData) {
  172. const { token } = JSON.parse(userData);
  173. authHeader.token = token;
  174. }
  175. return {
  176. url: `${url}`,
  177. options: { ...options, interceptors: true, headers: authHeader },
  178. };
  179. };
  180. //响应拦截器
  181. const demoResponseInterceptors = async (response, options) => {
  182. // response.headers.append('interceptors', 'yes yo');
  183. // console.log({response});
  184. try {
  185. const { status } = response;
  186. // console.log({response});
  187. if (status != 200) {
  188. const { errorCode, } = response;
  189. if (errorCode == 500) {
  190. notification.error({
  191. message: '抱歉,服务器出现错误!'
  192. });
  193. }
  194. if (errorCode == 404) {
  195. notification.error({
  196. message: '抱歉,服务器未找到请求!'
  197. });
  198. } else {
  199. notification.error({
  200. message:`错误 ${status}`
  201. });
  202. }
  203. }
  204. const _response = await response.clone().json();
  205. const { status: dataStatus, msg, errorMessage, errorCode } = _response;
  206. const { url } = options;
  207. //success返回
  208. if (dataStatus == 200) {
  209. if (requestMethod == 'post' && url != '/api/costAccount/login') {
  210. message.success('操作成功!');
  211. }
  212. return response;
  213. } else {
  214. // console.log({ isExpired });
  215. if (errorCode == 499 && !isExpired) {
  216. isExpired = true;
  217. Modal.confirm({
  218. title: '抱歉,登录已过期请重新登录!',
  219. onOk: () => {
  220. loginOut();
  221. }
  222. });
  223. } else {
  224. notification.error({
  225. message: errorMessage,
  226. duration: 5,
  227. });
  228. }
  229. }
  230. } catch (err) {
  231. console.log('catch error:', err)
  232. }
  233. }
  234. export const request = {
  235. errorHandler: (error) => {
  236. console.log({ error });
  237. throw error;
  238. },
  239. // 新增自动添加AccessToken的请求前拦截器
  240. requestInterceptors: [authHeaderInterceptor],
  241. responseInterceptors: [demoResponseInterceptors],
  242. }; // ProLayout 支持的api https://procomponents.ant.design/components/layout
  243. //将服务端获取的菜单 icon 字符串映射为对应的 icon Dom
  244. const mappingIcon = menuData => {
  245. const mappingMenu = menuData.map(item => ({
  246. ...item,
  247. icon: iconEnum[item.icon],
  248. children: item.children ? mappingIcon(item.children) : [],
  249. }));
  250. return mappingMenu;
  251. };
  252. export const layout = async ({ initialState }) => {
  253. // const localMenus = localStorage.getItem('menus');
  254. // console.log({pathname,localMenus});
  255. // let menus = [];
  256. // if(pathname != '/user/login'){
  257. // const {getMenuDataFromServer} = initialState;
  258. // const menu = await getMenuDataFromServer();
  259. // menus = mappingIcon(menu);
  260. // localStorage.setItem('menus',menus);
  261. // }
  262. return {
  263. rightContentRender: () => <RightContent />,
  264. disableContentMargin: false,
  265. waterMarkProps: {
  266. content: '',
  267. },
  268. actionRef: layoutActionRef,
  269. footerRender: () => <Footer />,
  270. onPageChange: () => {
  271. const { location } = history; // 如果没有登录,重定向到 login
  272. // console.log('location.pathname',location.pathname);
  273. // console.log('layoutActionRef.current',layoutActionRef.current);
  274. if (layoutActionRef.current) {
  275. layoutActionRef.current.reload();
  276. }
  277. if (!initialState?.currentUser && location.pathname !== '/user/login') {
  278. history.push(loginPath);
  279. }
  280. },
  281. menu: {
  282. // request: async (params, defaultMenuData) => {
  283. // // initialState.currentUser 中包含了所有用户信息
  284. // const menuData = await getMenus();
  285. // return menuData;
  286. // },
  287. // params: {
  288. // userId: initialState?.currentUser?.userid,
  289. // },
  290. // request: async (params, defaultMenuData) => {
  291. // // initialState.currentUser 中包含了所有用户信息
  292. // const menuData = await getMenus();
  293. // // console.log({menuData});
  294. // return menuData.data;
  295. // },
  296. },
  297. links: isDev
  298. ? [
  299. <Link to="/umi/plugin/openapi" target="_blank">
  300. <LinkOutlined />
  301. <span>OpenAPI 文档</span>
  302. </Link>,
  303. <Link to="/~docs">
  304. <BookOutlined />
  305. <span>业务组件文档</span>
  306. </Link>,
  307. ]
  308. : [],
  309. // menuDataRender:(menuData)=> {
  310. // return menus;
  311. // },
  312. menuHeaderRender: undefined,
  313. // 自定义 403 页面
  314. // unAccessible: <div>unAccessible</div>,
  315. ...initialState?.settings,
  316. };
  317. };