app.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. import { useState } from 'react';
  2. import { PageLoading } from '@ant-design/pro-layout';
  3. import { notification, Modal } from 'antd';
  4. import { RequestConfig, history, useModel } from 'umi';
  5. import type { RequestOptionsInit } from 'umi-request';
  6. import { getHospSubSystemList, UserDataType } from '@/service/login';
  7. import { BasicLayoutProps } from '@ant-design/pro-layout';
  8. import { logoutHandle } from './global';
  9. import { Platforms } from './constant';
  10. import { SpacicalPageParamsType } from './typings';
  11. const loginPath = '/login';
  12. let hospSign: string = ''; //医院标识
  13. if (history && history.location.query) {
  14. hospSign = history.location.query.hospSign as string;
  15. if (!hospSign) {
  16. const localHospSign = localStorage.getItem('hospSign');
  17. hospSign = localHospSign ? localHospSign : '';
  18. }
  19. }
  20. /** 获取用户信息比较慢的时候会展示一个 loading */
  21. export const initialStateConfig = {
  22. loading: <PageLoading />,
  23. };
  24. type InitialStateType = {
  25. userData?: UserDataType;
  26. systemLists?: TopBar.Tab[]; //当前医院可选子系统列表
  27. openedSysLists?: TopBar.Tab[]; //当前已打开的系统列表
  28. currentSelectedSys?: TopBar.Tab; //当前选中的tab
  29. logout?: () => Promise<boolean>;
  30. childAppIsShowMenu?:boolean;
  31. spacicalPageParamsType?: SpacicalPageParamsType[];
  32. getHospSubSystemListFunc?: () => Promise<any[]>;
  33. };
  34. export async function getInitialState(): Promise<InitialStateType> {
  35. const fetchUserInfo = async () => {
  36. try {
  37. const userData = localStorage.getItem('userData');
  38. if (userData) {
  39. return JSON.parse(userData);
  40. }
  41. throw Error;
  42. } catch (error) {
  43. history.push(`${loginPath}?hospSign=${hospSign}`);
  44. }
  45. return undefined;
  46. };
  47. const getAppIcon = (name: string) => {
  48. return Platforms.filter((i) => i.name == name).length > 0 ? Platforms.filter((i) => i.name == name)[0].logo : '';
  49. };
  50. //获取当前账号所有子应用列表
  51. const getHospSubSystemListFunc = async () => {
  52. const data = await getHospSubSystemList();
  53. if (data) {
  54. const _data = data.map((t) => ({
  55. ...t,
  56. icon: getAppIcon(t.name),
  57. path: t.path,
  58. }));
  59. return _data;
  60. }
  61. return [];
  62. };
  63. const logout = logoutHandle;
  64. const userData = await fetchUserInfo();
  65. let systemLists: userRelationInfo.OwnAppsItem[] = [];
  66. if (userData) {
  67. systemLists = await getHospSubSystemListFunc();
  68. }
  69. const localInitData = localStorage.getItem('initialState');
  70. return {
  71. currentSelectedSys: undefined,
  72. openedSysLists: [],
  73. ...JSON.parse(localInitData ? localInitData : '{}'), //覆盖,恢复tab状态
  74. userData,
  75. logout,
  76. spacicalPageParamsType: [],
  77. getHospSubSystemListFunc,
  78. systemLists: systemLists,
  79. };
  80. }
  81. const requestInterceptorsHandle = (url: string, options: RequestOptionsInit) => {
  82. const userData = localStorage.getItem('userData');
  83. let authHeader = { token: '' };
  84. if (userData) {
  85. const { token } = JSON.parse(userData);
  86. authHeader.token = token;
  87. }
  88. return {
  89. url: `/gateway${url}`,
  90. options: { ...options, interceptors: true, headers: authHeader,timeout:100000000, },
  91. };
  92. };
  93. const responseInterceptorsHandle = async (response: Response, options: RequestOptionsInit) => {
  94. const _response: {
  95. data?: any;
  96. status: number;
  97. success?: boolean;
  98. msg?: string;
  99. } = await response.clone().json();
  100. if (_response.status == 200) {
  101. if (_response.data) {
  102. return _response.data;
  103. }
  104. notification.success({
  105. message: `操作成功!`,
  106. });
  107. return {
  108. status: _response.status,
  109. success: true,
  110. };
  111. } else {
  112. notification.error({
  113. message:`${_response.msg}`,
  114. });
  115. return false
  116. }
  117. };
  118. interface ErrorInfoStructure {
  119. success: boolean; // if request is success
  120. data?: any; // response data
  121. status?: number;
  122. errorCode: number;
  123. errorMessage: string;
  124. showType?: number;
  125. traceId?: string;
  126. host?: string;
  127. }
  128. interface ResponseErr extends Error {
  129. data?: any; // 这里是后端返回的原始数据
  130. info: ErrorInfoStructure;
  131. }
  132. const errorHandlerFunc = (error: ResponseErr) => {
  133. try {
  134. const { info } = error;
  135. const { errorCode , errorMessage } = info;
  136. if (errorCode == 499) {
  137. //token过期
  138. Modal.confirm({
  139. title: '抱歉,你的登录已过期,请重新登录!',
  140. // closable:false,
  141. maskClosable: false,
  142. // cancelButtonProps:
  143. onOk: () => {
  144. logoutHandle();
  145. return Promise.resolve(true);
  146. },
  147. });
  148. return;
  149. }
  150. if (errorMessage.length > 20) {
  151. notification.error({
  152. message: ` ${errorCode}:出现错误!`,
  153. description: errorMessage,
  154. });
  155. } else {
  156. notification.error({
  157. message: ` ${errorCode}:${errorMessage}`,
  158. });
  159. }
  160. } catch (err) {
  161. console.log({ errorHandlerFunc: err });
  162. notification.error({
  163. message: '遇到未知错误,查看控制台!',
  164. });
  165. }
  166. };
  167. export const request: RequestConfig = {
  168. timeout: 10000,
  169. errorConfig: {
  170. adaptor: (resData) => {
  171. if (!resData.success && resData.status != 200) {
  172. return {
  173. ...resData,
  174. };
  175. } else {
  176. return {
  177. success: true,
  178. status: 200,
  179. };
  180. }
  181. },
  182. },
  183. errorHandler: (err: any) => errorHandlerFunc(err),
  184. middlewares: [
  185. async function middlewareA(ctx, next) {
  186. await next();
  187. },
  188. async function middlewareB(ctx, next) {
  189. await next();
  190. },
  191. ],
  192. requestInterceptors: [requestInterceptorsHandle],
  193. responseInterceptors: [responseInterceptorsHandle],
  194. };
  195. // 从接口中获取子应用配置,export 出的 qiankun 变量是一个 promise
  196. export const qiankun = fetch('/config').then(() => ({
  197. // 注册子应用信息
  198. apps: [
  199. // {
  200. // name: 'microApp', // 唯一 id
  201. // entry: '//localhost:8808', // 开发
  202. // },
  203. {
  204. name: 'app1', // 唯一 id
  205. entry: '//112.124.59.133:8804', //测试
  206. //entry: '//118.31.245.65:8804', //线上
  207. //entry:'//192.168.50.143:8804',//本机
  208. //entry: '//localhost:8804', // 开发
  209. },
  210. {
  211. name: 'reviewMana', // 唯一 id
  212. entry: '//112.124.59.133:8807', //测试
  213. //entry: '//118.31.245.65:8807', //线上
  214. //entry:'//192.168.50.143:8804',//本机
  215. //entry: '//localhost:8804', // 开发
  216. },
  217. {
  218. name: 'budgetManaSystem', // 唯一 id
  219. //entry: '//localhost:8001'
  220. entry: '//112.124.59.133:5000/perform/', // 开发
  221. },
  222. {
  223. name: 'PFMBackC', // 唯一 id
  224. entry: '//112.124.59.133:5000/pfmManager/index.html'
  225. //entry: '//112.124.59.133:5000/perform/', // 开发
  226. },
  227. ],
  228. // 完整生命周期钩子请看 https://qiankun.umijs.org/zh/api/#registermicroapps-apps-lifecycles
  229. lifeCycles: {
  230. afterMount: (props: any) => {},
  231. },
  232. // 支持更多的其他配置,详细看这里 https://qiankun.umijs.org/zh/api/#start-opts
  233. }));
  234. //向子应用透传
  235. export function useQiankunStateForSlave() {
  236. const [masterState, setMasterState] = useState({});
  237. return {
  238. masterState,
  239. setMasterState,
  240. };
  241. }
  242. //@/pages/platform/setting/reports/index
  243. export function patchRoutes({ routes }:{routes:any}) {
  244. const paths = [...new Array(100).keys()].map((a,index)=>({
  245. path: `/platform/setting/reports/${15+(index+1)}`,
  246. exact: true,
  247. component: require('@/pages/platform/setting/reports/index.tsx').default,
  248. }));
  249. const treeLoop = (treeData:any)=>{
  250. //console.log({treeData})
  251. if(treeData.path == '/platform'){
  252. paths.forEach((a:any)=>{
  253. treeData.routes.push(a);
  254. })
  255. return;
  256. }else{
  257. if(treeData.routes&&treeData.routes.length>0){
  258. treeData.routes.forEach((a:any)=>{
  259. treeLoop(a);
  260. })
  261. }
  262. }
  263. }
  264. treeLoop(routes[0]);
  265. }
  266. export const layout = ({ initialState: { userData } }: { initialState: InitialStateType }): BasicLayoutProps => {
  267. return {
  268. headerRender: false,
  269. rightContentRender: () => <>right</>,
  270. footerRender: () => null,
  271. onPageChange: () => {
  272. //如果没有登录,重定向到 login
  273. if (!userData && location.pathname !== '/login') {
  274. // history.push('/login');
  275. }
  276. },
  277. menuHeaderRender: undefined,
  278. // ...initialState?.settings,
  279. };
  280. };