| 14 | * 从 API 中加载,需要接口返回 MicroApplicationProps 的数据 |
| 15 | */ |
| 16 | export class MicroApplicationAPILoader implements MicroApplicationLoader { |
| 17 | private url: string; |
| 18 | constructor() { |
| 19 | this.url = MICRO_FRONTEND_API; |
| 20 | } |
| 21 | |
| 22 | public async enabled(): Promise<boolean> { |
| 23 | const enable = !isEmpty(this.url); |
| 24 | if (enable) { |
| 25 | debug('MicroApplicationAPILoader is enabled'); |
| 26 | } |
| 27 | return Promise.resolve(enable); |
| 28 | } |
| 29 | |
| 30 | public async loadMeta(): Promise<MicroApplication[]> { |
| 31 | try { |
| 32 | info('API: 正在加载微前端资源配置信息 ...'); |
| 33 | const response = await window.fetch(`${this.url}?time=${new Date().getTime()}`, { method: 'GET' }); |
| 34 | const app = await response.json(); |
| 35 | const meta = app.map((props: any) => new MicroApplication(props)); |
| 36 | info('API: 微前端资源配置信息完成加载 ^ _ ^'); |
| 37 | return meta; |
| 38 | } catch (e) { |
| 39 | // 增加 reload,首次获取失败时默认刷新页面进行一次重试 |
| 40 | const loadMicroFrontendFailed = 'loadMicroFrontendFailed'; |
| 41 | const firstRecord = sessionStorage.getItem(loadMicroFrontendFailed); |
| 42 | if (!firstRecord) { |
| 43 | sessionStorage.setItem(loadMicroFrontendFailed, 'true'); |
| 44 | window.location.reload(); |
| 45 | return; |
| 46 | } |
| 47 | sessionStorage.removeItem(loadMicroFrontendFailed); |
| 48 | message.error('微前端资源配置信息加载失败,请刷新页面重试或联系系统管理员', 0); |
| 49 | warn('API: 微前端资源配置信息加载失败,请检查 MICRO_FRONTEND_API ... (。ì _ í。)'); |
| 50 | throw new Error(e); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | const loader = new MicroApplicationAPILoader(); |
| 56 |
nothing calls this directly
no outgoing calls
no test coverage detected