@skyfox2000/webui 1.4.2 → 1.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +1,13 @@
1
1
  import { defineComponent as F, defineAsyncComponent as M, ref as m, watch as V, onMounted as J, createElementBlock as x, openBlock as p, Fragment as K, createElementVNode as v, createVNode as _, toDisplayString as W, unref as i, createBlock as P, withKeys as f, normalizeClass as N, withModifiers as w, withCtx as z, createCommentVNode as L } from "vue";
2
2
  import "ant-design-vue";
3
- import { _ as q } from "../../assets/modules/toolIcon-BHWHHyOp.js";
3
+ import { _ as q } from "../../assets/modules/toolIcon-C6QsaAZ8.js";
4
4
  import "@skyfox2000/fapi";
5
- import "../../assets/modules/uploadList-EbG8acej.js";
5
+ import "../../assets/modules/uploadList-C4BvnORD.js";
6
6
  import "@skyfox2000/microbase";
7
7
  import "vue-m-message";
8
8
  import "async-validator";
9
9
  import "dayjs";
10
- import { _ as D } from "../../assets/modules/index-C5X-1A4m.js";
10
+ import { _ as D } from "../../assets/modules/index-RrFsG5IK.js";
11
11
  import "vue-draggable-next";
12
12
  const h = async (s) => window.ace ? window.ace : new Promise((l, t) => {
13
13
  const o = document.createElement("script");
@@ -1,6 +1,6 @@
1
1
  import { defineComponent as f, ref as d, watch as _, onMounted as v, resolveComponent as h, createBlock as o, openBlock as t, withCtx as s, createElementVNode as C, normalizeClass as x, createVNode as w, KeepAlive as y, unref as n, resolveDynamicComponent as I } from "vue";
2
- import { _ as g } from "../../assets/modules/baseLayout-CeyN__cz.js";
3
- import { af as A, a as S } from "../../assets/modules/uploadList-EbG8acej.js";
2
+ import { _ as g } from "../../assets/modules/baseLayout-CYzXPj7i.js";
3
+ import { af as A, a as S } from "../../assets/modules/uploadList-C4BvnORD.js";
4
4
  import "@skyfox2000/microbase";
5
5
  import "@skyfox2000/fapi";
6
6
  import b from "vue-m-message";
@@ -1,5 +1,5 @@
1
1
  import { defineComponent as i, createBlock as p, openBlock as m, unref as r, withCtx as e, createVNode as n, createTextVNode as a } from "vue";
2
- import { r as s } from "../../assets/modules/uploadList-EbG8acej.js";
2
+ import { r as s } from "../../assets/modules/uploadList-C4BvnORD.js";
3
3
  import "@skyfox2000/microbase";
4
4
  import "@skyfox2000/fapi";
5
5
  import "vue-m-message";
@@ -1,5 +1,5 @@
1
1
  import { defineComponent as i, createBlock as p, openBlock as m, unref as r, withCtx as e, createVNode as n, createTextVNode as a } from "vue";
2
- import { r as s } from "../../assets/modules/uploadList-EbG8acej.js";
2
+ import { r as s } from "../../assets/modules/uploadList-C4BvnORD.js";
3
3
  import "@skyfox2000/microbase";
4
4
  import "@skyfox2000/fapi";
5
5
  import "vue-m-message";
@@ -1,12 +1,12 @@
1
1
  import { defineComponent as we, ref as f, computed as T, watch as W, onMounted as be, createBlock as H, openBlock as d, unref as n, withCtx as S, renderSlot as J, createElementBlock as g, createCommentVNode as K, createElementVNode as h, createVNode as C, createTextVNode as X, toDisplayString as E, normalizeClass as Ce, Fragment as ie, renderList as ne } from "vue";
2
- import { _ as Y } from "../../assets/modules/index-DbgSOuIs.js";
2
+ import { _ as Y } from "../../assets/modules/index-QDkmhaJS.js";
3
3
  import { Modal as Fe, Upload as ue, Space as _e } from "ant-design-vue";
4
4
  import { httpGet as Ue, ResStatus as fe } from "@skyfox2000/fapi";
5
- import { E as Te, a2 as Se, a8 as Ee } from "../../assets/modules/uploadList-EbG8acej.js";
6
- import { A as Re, p as De, U as ce } from "../../assets/modules/file-upload-DiUcAspb.js";
5
+ import { E as Te, a2 as Se, a8 as Ee } from "../../assets/modules/uploadList-C4BvnORD.js";
6
+ import { A as Re, p as De, U as ce } from "../../assets/modules/file-upload-Dmi2Kiqo.js";
7
7
  import "@skyfox2000/microbase";
8
8
  import p from "vue-m-message";
9
- import { c as ke, _ as Z, h as Le, p as Ne, i as Ke } from "../../assets/modules/index-YIIA7qm8.js";
9
+ import { c as ke, _ as Z, h as Le, p as Ne, i as Ke } from "../../assets/modules/index-1Qt9-aFa.js";
10
10
  import "dayjs";
11
11
  import "vue-draggable-next";
12
12
  import "async-validator";
@@ -306,7 +306,7 @@ const Be = (l) => l.includes(",") || l.includes(`
306
306
  return p.error(v), ue.LIST_IGNORE;
307
307
  try {
308
308
  if (A(), y.value = t.name, q.value = u ? "text/csv" : "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", u) {
309
- const w = await t.arrayBuffer(), O = new TextDecoder("utf-8").decode(w), { csvToExcelView: G } = await import("../../assets/modules/index-YIIA7qm8.js").then((he) => he.j), M = await G(O, t.name);
309
+ const w = await t.arrayBuffer(), O = new TextDecoder("utf-8").decode(w), { csvToExcelView: G } = await import("../../assets/modules/index-1Qt9-aFa.js").then((he) => he.j), M = await G(O, t.name);
310
310
  if (!M.success)
311
311
  throw new Error(M.error || "CSV文件处理失败");
312
312
  const oe = await (await fetch(M.blobUrl)).arrayBuffer();
@@ -1,6 +1,6 @@
1
1
  import { defineComponent as f, ref as d, watch as _, onMounted as v, resolveComponent as h, createBlock as o, openBlock as t, withCtx as s, createElementVNode as C, normalizeClass as x, createVNode as w, KeepAlive as I, unref as n, resolveDynamicComponent as g } from "vue";
2
- import { _ as y } from "../../assets/modules/baseLayout-CeyN__cz.js";
3
- import { af as A, a as S } from "../../assets/modules/uploadList-EbG8acej.js";
2
+ import { _ as y } from "../../assets/modules/baseLayout-CYzXPj7i.js";
3
+ import { af as A, a as S } from "../../assets/modules/uploadList-C4BvnORD.js";
4
4
  import "@skyfox2000/microbase";
5
5
  import "@skyfox2000/fapi";
6
6
  import k from "vue-m-message";
@@ -1,15 +1,15 @@
1
1
  import { defineComponent as y, ref as f, watch as S, onMounted as z, createBlock as D, openBlock as F, unref as u, withCtx as s, createVNode as m, createTextVNode as C } from "vue";
2
- import { _ as c } from "../../assets/modules/index-DbgSOuIs.js";
2
+ import { _ as c } from "../../assets/modules/index-QDkmhaJS.js";
3
3
  import { Modal as _, Space as k } from "ant-design-vue";
4
4
  import { ResStatus as E } from "@skyfox2000/fapi";
5
- import { a4 as h } from "../../assets/modules/uploadList-EbG8acej.js";
5
+ import { a4 as h } from "../../assets/modules/uploadList-C4BvnORD.js";
6
6
  import "@skyfox2000/microbase";
7
7
  import L from "vue-m-message";
8
8
  import "async-validator";
9
9
  import "dayjs";
10
- import { A as N } from "../../assets/modules/file-upload-DiUcAspb.js";
10
+ import { A as N } from "../../assets/modules/file-upload-Dmi2Kiqo.js";
11
11
  import "vue-draggable-next";
12
- import { U as B } from "../../assets/modules/uploadList-1qFfdb6_.js";
12
+ import { U as B } from "../../assets/modules/uploadList-Bh1TJ-XO.js";
13
13
  const K = /* @__PURE__ */ y({
14
14
  __name: "uploadForm",
15
15
  props: {
@@ -1,10 +1,10 @@
1
- import { HostInfo, MainOpenApis } from '../../../502424_MicroBase';
1
+ import { HostInfo } from '../../../502424_MicroBase';
2
2
  import { StoreDefinition } from 'pinia';
3
3
  export declare const useHostInfo: StoreDefinition<"hostInfo", {
4
4
  hostInfo: HostInfo;
5
5
  }, {}, {
6
6
  getSiteHostApi(): string;
7
- loadHostInfo(hostApi: string, hostKey?: string, mainOpenApis?: MainOpenApis): Promise<void>;
7
+ loadHostInfo(hostApi: string, hostKey?: string): Promise<void>;
8
8
  clear(): void;
9
9
  _setHostInfo(hostInfo: HostInfo | null): void;
10
10
  getHostInfo(): HostInfo;
@@ -1,11 +1,25 @@
1
- declare const MicroOpenApis: {
2
- getAppInfo: () => Promise<any>;
3
- getHostInfo: () => Promise<any>;
4
- getUserInfo: () => Promise<any>;
5
- getToken: () => Promise<any>;
6
- userLogin: (params: any) => Promise<any>;
7
- userLogout: () => Promise<any>;
8
- mainAppPush: (params: any) => Promise<any>;
9
- isInMicroApp: () => boolean;
1
+ import { AppInfo, HostInfo, UserInfo } from '../../../502424_MicroBase';
2
+ declare class MicroAppSDK {
3
+ private static messageId;
4
+ private static callbacks;
5
+ static isInMicroApp(): boolean;
6
+ private static callMainAppMethod;
7
+ static handleResponseData(data: any): void;
8
+ static init(): void;
9
+ static getAppInfo(): Promise<AppInfo>;
10
+ static getHostInfo(): Promise<HostInfo>;
11
+ static getUserInfo(): Promise<UserInfo>;
12
+ static getToken(): Promise<string>;
13
+ static userLogout(): Promise<void>;
14
+ static mainAppPush(path: string): Promise<void>;
15
+ }
16
+ export declare const MicroOpenApis: {
17
+ getAppInfo: typeof MicroAppSDK.getAppInfo;
18
+ getHostInfo: typeof MicroAppSDK.getHostInfo;
19
+ getUserInfo: typeof MicroAppSDK.getUserInfo;
20
+ getToken: typeof MicroAppSDK.getToken;
21
+ userLogout: typeof MicroAppSDK.userLogout;
22
+ mainAppPush: typeof MicroAppSDK.mainAppPush;
23
+ isInMicroApp: typeof MicroAppSDK.isInMicroApp;
10
24
  };
11
- export { MicroOpenApis };
25
+ export {};
package/lib/webui.es.js CHANGED
@@ -1,6 +1,6 @@
1
- import { u as Ee, i as Z, a as me, L as Xe, c as Ze, P as j, o as Pe, b as ea, d as aa, e as Fe, r as ce, f as ta, g as la, h as Ne, j as sa, k as ke, l as ra, _ as oa, m as na, O as Me, q as Be, n as ia, p as ca, s as L, t as ua, v as da, w as pa, x as fa, y as va, z as ma, A as Y, B as ha, C as ga, D as ba, E as Re } from "./assets/modules/uploadList-EbG8acej.js";
2
- import { aa as $l, Q as Il, ac as Dl, R as El, V as Pl, a9 as Fl, a7 as Nl, a8 as Ml, ab as Bl, Y as Rl, T as Ol, a5 as Al, a2 as Ll, M as zl, a4 as Ul, U as Vl, G as Yl, $ as Kl, a3 as Wl, a6 as Hl, N as jl, S as Ql, ad as ql, F as Gl, a1 as Jl, ae as Xl, X as Zl, Z as es, W as as, af as ts, H as ls, I as ss, ag as rs, K as os, ah as ns, J as is, a0 as cs } from "./assets/modules/uploadList-EbG8acej.js";
3
- import { A as ds, U as ps, p as fs } from "./assets/modules/file-upload-DiUcAspb.js";
1
+ import { u as Ee, i as Z, a as me, L as Xe, c as Ze, P as j, o as Pe, b as ea, d as aa, e as Fe, r as ce, f as ta, g as la, h as Ne, j as sa, k as ke, l as ra, _ as oa, m as na, O as Me, q as Be, n as ia, p as ca, s as L, t as ua, v as da, w as pa, x as fa, y as va, z as ma, A as Y, B as ha, C as ga, D as ba, E as Re } from "./assets/modules/uploadList-C4BvnORD.js";
2
+ import { aa as $l, Q as Il, ac as Dl, R as El, V as Pl, a9 as Fl, a7 as Nl, a8 as Ml, ab as Bl, Y as Rl, T as Ol, a5 as Al, a2 as Ll, M as zl, a4 as Ul, U as Vl, G as Yl, $ as Kl, a3 as Wl, a6 as Hl, N as jl, S as Ql, ad as ql, F as Gl, a1 as Jl, ae as Xl, X as Zl, Z as es, W as as, af as ts, H as ls, I as ss, ag as rs, K as os, ah as ns, J as is, a0 as cs } from "./assets/modules/uploadList-C4BvnORD.js";
3
+ import { A as ds, U as ps, p as fs } from "./assets/modules/file-upload-Dmi2Kiqo.js";
4
4
  import { EnvConfig as we } from "@skyfox2000/microbase";
5
5
  import { FrontCache as re, httpPost as Oe, ResStatus as Ae, SERVER_HOST as _a, fieldMapping as oe } from "@skyfox2000/fapi";
6
6
  import he from "vue-m-message";
@@ -9,15 +9,15 @@ import "async-validator";
9
9
  import fe from "dayjs";
10
10
  import { Popover as Ca, theme as ze, Drawer as Ta, Space as te, Form as Ue, message as $a, FormItem as Ve, ConfigProvider as Ce, Popconfirm as Ia, Button as Ye, DropdownButton as Da, Menu as _e, MenuItem as xe, Dropdown as ve, Checkbox as Ea, Table as Ke, Tree as Pa, Cascader as Fa, DatePicker as Na, Input as ee, InputPassword as Ma, InputNumber as Ba, RangePicker as Ra, Textarea as Oa, TimePicker as Aa, Transfer as La, TreeSelect as za, LayoutContent as Ua } from "ant-design-vue";
11
11
  import { VueDraggableNext as Va } from "vue-draggable-next";
12
- import { d as We } from "./assets/modules/uploadList-1qFfdb6_.js";
13
- import { C as ms, U as hs, a as gs, p as bs } from "./assets/modules/uploadList-1qFfdb6_.js";
14
- import { c as He } from "./assets/modules/index-YIIA7qm8.js";
15
- import { _ as xs, h as ys, b as ks, a as ws, d as Ss, e as Cs, g as Ts, f as $s, n as Is, p as Ds, t as Es, v as Ps } from "./assets/modules/index-YIIA7qm8.js";
16
- import { _ as A } from "./assets/modules/index-DbgSOuIs.js";
17
- import { _ as U } from "./assets/modules/toolIcon-BHWHHyOp.js";
18
- import { a as Ns } from "./assets/modules/toolIcon-BHWHHyOp.js";
19
- import { _ as Bs, b as Rs, c as Os, d as As, e as Ls, I as zs, f as Us, g as Vs, a as Ys } from "./assets/modules/menuTabs-hVwE4xrx.js";
20
- import { _ as Ws } from "./assets/modules/index-C5X-1A4m.js";
12
+ import { d as We } from "./assets/modules/uploadList-Bh1TJ-XO.js";
13
+ import { C as ms, U as hs, a as gs, p as bs } from "./assets/modules/uploadList-Bh1TJ-XO.js";
14
+ import { c as He } from "./assets/modules/index-1Qt9-aFa.js";
15
+ import { _ as xs, h as ys, b as ks, a as ws, d as Ss, e as Cs, g as Ts, f as $s, n as Is, p as Ds, t as Es, v as Ps } from "./assets/modules/index-1Qt9-aFa.js";
16
+ import { _ as A } from "./assets/modules/index-QDkmhaJS.js";
17
+ import { _ as U } from "./assets/modules/toolIcon-C6QsaAZ8.js";
18
+ import { a as Ns } from "./assets/modules/toolIcon-C6QsaAZ8.js";
19
+ import { _ as Bs, b as Rs, c as Os, d as As, e as Ls, I as zs, f as Us, g as Vs, a as Ys } from "./assets/modules/menuTabs-sfALzkA5.js";
20
+ import { _ as Ws } from "./assets/modules/index-RrFsG5IK.js";
21
21
  import { _ as q } from "./assets/modules/_plugin-vue_export-helper-CHgC5LLL.js";
22
22
  const Te = (s, a, e, t = "remove") => {
23
23
  if (!e)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skyfox2000/webui",
3
- "version": "1.4.2",
3
+ "version": "1.4.4",
4
4
  "description": "后台前端通用组件定义",
5
5
  "type": "module",
6
6
  "keywords": [],
@@ -65,4 +65,4 @@
65
65
  "vite-plugin-dts": "^4.5.4",
66
66
  "vue-tsc": "^2.2.0"
67
67
  }
68
- }
68
+ }
@@ -1,16 +1,16 @@
1
1
  <script setup lang="ts">
2
2
  import { ref } from 'vue';
3
3
  import { Modal, Flex } from 'ant-design-vue';
4
- import { mainAppApis } from '@skyfox2000/microbase';
5
4
  import { ToolIcon, Tooltip } from '../../common';
6
- import { useUserInfo } from '@/index';
5
+ import { MicroOpenApis, useUserInfo } from '@/index';
6
+ import { isMicroApp } from '@skyfox2000/microbase';
7
7
 
8
8
  const userInfoStore = useUserInfo();
9
9
  const open = ref(false);
10
10
  const confirmExit = () => {
11
11
  open.value = false;
12
- if (mainAppApis.value) {
13
- mainAppApis.value.userLogout!();
12
+ if (isMicroApp()) {
13
+ MicroOpenApis.userLogout!();
14
14
  } else {
15
15
  userInfoStore.logout(true);
16
16
  }
@@ -6,7 +6,6 @@ import {
6
6
  AppSource,
7
7
  isBaseMicroApp,
8
8
  isMicroApp,
9
- mainAppApis,
10
9
  RouteRecord,
11
10
  EnvConfig,
12
11
  } from '@skyfox2000/microbase';
@@ -18,6 +17,7 @@ import { Component, h, nextTick } from 'vue';
18
17
  import { LoginExpiredError, useUserInfo } from './userInfo';
19
18
  import { ApiResponse, httpPost, IUrlInfo, ResStatus } from '@skyfox2000/fapi';
20
19
  import message from 'vue-m-message';
20
+ import { MicroOpenApis } from '@/utils/micro-openapis';
21
21
 
22
22
  // const APPINFO_STORE_KEY = 'appInfoStore';
23
23
 
@@ -353,9 +353,9 @@ export const useAppInfo = defineStore('appInfo', {
353
353
  this.appInfo.defaultPage = findFirstPage(filteredRoutes) || '';
354
354
  if (this.appInfo.defaultPage) {
355
355
  this.menuLoaded = true;
356
- if (mainAppApis.value && mainAppApis.value.mainAppPush) {
356
+ if (isMicroApp()) {
357
357
  const fullPath = this.formatRoute(this.appInfo.defaultPage);
358
- mainAppApis.value.mainAppPush(fullPath);
358
+ MicroOpenApis.mainAppPush(fullPath);
359
359
  }
360
360
  continueNavigation(this.appInfo.defaultPage);
361
361
  }
@@ -393,9 +393,9 @@ export const useAppInfo = defineStore('appInfo', {
393
393
  nextTick(() => {
394
394
  // 直接使用当前应用路由AppRouter
395
395
  AppRouter.push({ path });
396
- if (mainAppApis.value && mainAppApis.value.mainAppPush) {
396
+ if (isMicroApp()) {
397
397
  const fullPath = this.formatRoute(path);
398
- mainAppApis.value.mainAppPush(fullPath);
398
+ MicroOpenApis.mainAppPush(fullPath);
399
399
  }
400
400
  });
401
401
  },
@@ -1,6 +1,7 @@
1
+ import { MicroOpenApis } from '@/utils/micro-openapis';
1
2
  import { API_HOST, ApiResponse, httpPost, IUrlInfo, ResStatus, SERVER_HOST } from '@skyfox2000/fapi';
2
3
 
3
- import { HostInfo, MainOpenApis } from '@skyfox2000/microbase';
4
+ import { HostInfo, isMicroApp } from '@skyfox2000/microbase';
4
5
 
5
6
  import { defineStore } from 'pinia';
6
7
 
@@ -57,11 +58,11 @@ export const useHostInfo = defineStore('hostInfo', {
57
58
  * 加载站点信息
58
59
  * @param hostApi 启动配置主机地址
59
60
  * @param hostKey 站点信息,默认为当前主域名
60
- * @param mainOpenApis 宿主机开放接口
61
61
  */
62
- async loadHostInfo(hostApi: string, hostKey?: string, mainOpenApis?: MainOpenApis): Promise<void> {
63
- if (mainOpenApis) {
64
- this._setHostInfo(mainOpenApis.getHostInfo());
62
+ async loadHostInfo(hostApi: string, hostKey?: string): Promise<void> {
63
+ if (isMicroApp()) {
64
+ const hostInfo = await MicroOpenApis.getHostInfo();
65
+ this._setHostInfo(hostInfo);
65
66
  return;
66
67
  }
67
68
 
@@ -1,10 +1,12 @@
1
1
  import { hostUrl, IUrlInfo } from '@skyfox2000/fapi';
2
2
 
3
- import { getToken } from '@/utils/main-openapis';
4
3
  import { UploadFile, UploadStatus } from '@/typings/upload.d';
5
4
  import dayjs from 'dayjs';
6
5
  import message from 'vue-m-message';
7
6
  import { Ref } from 'vue';
7
+ import { MicroOpenApis } from './micro-openapis';
8
+ import { isMicroApp } from '@skyfox2000/microbase';
9
+ import { useUserInfo } from '@/stores/userInfo';
8
10
 
9
11
  export class path {
10
12
  /**
@@ -233,7 +235,7 @@ export class AsyncUploader {
233
235
 
234
236
  // 添加授权 Token(如果需要)
235
237
  if (this.urlInfo.authorize) {
236
- const token = getToken(); // 假设 getToken 是获取 Token 的方法
238
+ const token = isMicroApp() ? MicroOpenApis.getToken() : useUserInfo().getToken(); // 假设 getToken 是获取 Token 的方法
237
239
  if (!token) {
238
240
  reject(new Error('未授权或授权过期'));
239
241
  return;
@@ -360,4 +362,3 @@ export class AsyncUploader {
360
362
  // console.error('上传错误:', error);
361
363
  // }
362
364
  // })();
363
-
@@ -1,145 +1,169 @@
1
1
  /**
2
- * 子应用SDK - 提供统一的主应用API调用接口
2
+ * 微前端通信工具
3
+ * 提供与主应用的通信接口,基于micro-app框架
3
4
  */
4
5
 
5
- // 定义API方法类型
6
- interface MainApis {
7
- getAppInfo: () => Promise<any>;
8
- getHostInfo: () => Promise<any>;
9
- getUserInfo: () => Promise<any>;
10
- getToken: () => Promise<any>;
11
- userLogin: (params: any) => Promise<any>;
12
- userLogout: () => Promise<any>;
13
- mainAppPush: (params: any) => Promise<any>;
6
+ import { AppInfo, HostInfo, UserInfo } from '@skyfox2000/microbase';
7
+
8
+ // 定义请求数据格式
9
+ interface ApiRequestData {
10
+ type: 'API_REQUEST';
11
+ id: number;
12
+ method: string;
13
+ params?: any;
14
14
  }
15
15
 
16
- // 定义可用的API方法名称
17
- type MainApiMethod = keyof MainApis;
16
+ // 定义响应数据格式
17
+ interface ApiResponseData {
18
+ type: 'API_RESPONSE';
19
+ id: number;
20
+ success: boolean;
21
+ result?: any;
22
+ error?: string;
23
+ }
18
24
 
19
- /**
20
- * MicroAppSDK类 - 管理与主应用的通信
21
- */
22
25
  class MicroAppSDK {
23
- constructor() {
24
- this.ensureMicroAppReady();
25
- }
26
+ private static messageId = 0;
27
+ private static callbacks = new Map<number, { resolve: Function; reject: Function }>();
26
28
 
27
29
  /**
28
- * 确保micro-app环境就绪
30
+ * 检查是否在微前端环境中运行
31
+ * @returns boolean
29
32
  */
30
- ensureMicroAppReady() {
31
- if (typeof window === 'undefined') {
32
- throw new Error('MicroAppSDK can only be used in browser environment');
33
- }
33
+ static isInMicroApp(): boolean {
34
+ return typeof window !== 'undefined' && !!(window as any).microApp;
34
35
  }
35
36
 
36
37
  /**
37
- * 获取基座API
38
+ * 调用主应用方法的通用方法
39
+ * @param method 方法名
40
+ * @param params 参数
41
+ * @returns Promise
38
42
  */
39
- getMainApis(): Partial<MainApis> {
40
- if (this.isInMicroApp() && (window as any).microApp && (window as any).microApp.getData) {
41
- const data = (window as any).microApp.getData();
42
- return data?.MainApis || {};
43
+ private static async callMainAppMethod(method: string, params?: any): Promise<any> {
44
+ // 检查是否在微前端环境中
45
+ if (!this.isInMicroApp()) {
46
+ throw new Error(`未配置${method}接口!`);
43
47
  }
44
- return {};
48
+
49
+ // 生成唯一消息ID
50
+ const id = ++this.messageId;
51
+
52
+ // 创建Promise并保存回调
53
+ const promise = new Promise((resolve, reject) => {
54
+ this.callbacks.set(id, { resolve, reject });
55
+
56
+ // 设置超时
57
+ setTimeout(() => {
58
+ if (this.callbacks.has(id)) {
59
+ this.callbacks.delete(id);
60
+ reject(new Error(`API调用超时: ${method}`));
61
+ }
62
+ }, 10000); // 10秒超时
63
+ });
64
+
65
+ // 构造请求数据
66
+ const requestData: ApiRequestData = {
67
+ type: 'API_REQUEST',
68
+ id,
69
+ method,
70
+ params,
71
+ };
72
+
73
+ // 发送数据到主应用(使用micro-app的dispatch方法)
74
+ (window as any).microApp.dispatch(requestData);
75
+
76
+ // 等待结果
77
+ return promise;
45
78
  }
46
79
 
47
80
  /**
48
- * 检查是否在微前端环境中
81
+ * 处理来自主应用的响应数据
82
+ * @param data 响应数据
49
83
  */
50
- isInMicroApp(): boolean {
51
- return !!(window as any).microApp;
84
+ static handleResponseData(data: any) {
85
+ // 检查是否为API响应
86
+ if (data && data.type === 'API_RESPONSE' && typeof data.id === 'number') {
87
+ const { id, success, result, error } = data as ApiResponseData;
88
+ const callback = this.callbacks.get(id);
89
+
90
+ if (callback) {
91
+ this.callbacks.delete(id);
92
+ if (success) {
93
+ callback.resolve(result);
94
+ } else {
95
+ callback.reject(new Error(error || '未知错误'));
96
+ }
97
+ }
98
+ }
52
99
  }
53
100
 
54
101
  /**
55
- * API调用包装器
102
+ * 初始化通信监听器
56
103
  */
57
- async callMainApi<T extends MainApiMethod>(methodName: T, params?: any): Promise<ReturnType<MainApis[T]>> {
58
- const mainApis = this.getMainApis();
59
-
60
- // 使用类型断言解决TypeScript类型检查问题
61
- const method = mainApis[methodName] as Function | undefined;
62
-
63
- if (!method) {
64
- throw new Error(`Main API method '${methodName}' is not available`);
65
- }
66
-
67
- try {
68
- if (params !== undefined) {
69
- return await method(params);
70
- } else {
71
- return await method();
72
- }
73
- } catch (error) {
74
- console.error(`Failed to call main API '${methodName}':`, error);
75
- throw error;
104
+ static init() {
105
+ if (this.isInMicroApp()) {
106
+ // 添加监听器
107
+ (window as any).microApp.addDataListener((data: any) => {
108
+ console.log('收到来自主应用的数据:', data);
109
+ this.handleResponseData(data);
110
+ });
76
111
  }
77
112
  }
78
113
 
79
114
  /**
80
115
  * 获取应用信息
81
116
  */
82
- async getAppInfo(): Promise<any> {
83
- return this.callMainApi('getAppInfo');
117
+ static async getAppInfo(): Promise<AppInfo> {
118
+ return this.callMainAppMethod('getAppInfo');
84
119
  }
85
120
 
86
121
  /**
87
122
  * 获取主机信息
88
123
  */
89
- async getHostInfo(): Promise<any> {
90
- return this.callMainApi('getHostInfo');
124
+ static async getHostInfo(): Promise<HostInfo> {
125
+ return this.callMainAppMethod('getHostInfo');
91
126
  }
92
127
 
93
128
  /**
94
129
  * 获取用户信息
95
130
  */
96
- async getUserInfo(): Promise<any> {
97
- return this.callMainApi('getUserInfo');
131
+ static async getUserInfo(): Promise<UserInfo> {
132
+ return this.callMainAppMethod('getUserInfo');
98
133
  }
99
134
 
100
135
  /**
101
136
  * 获取授权令牌
102
137
  */
103
- async getToken(): Promise<any> {
104
- return this.callMainApi('getToken');
105
- }
106
-
107
- /**
108
- * 用户登录
109
- */
110
- async userLogin(params: any): Promise<any> {
111
- return this.callMainApi('userLogin', params);
138
+ static async getToken(): Promise<string> {
139
+ return this.callMainAppMethod('getToken');
112
140
  }
113
141
 
114
142
  /**
115
143
  * 用户登出
116
144
  */
117
- async userLogout(): Promise<any> {
118
- return this.callMainApi('userLogout');
145
+ static async userLogout(): Promise<void> {
146
+ return this.callMainAppMethod('userLogout');
119
147
  }
120
148
 
121
149
  /**
122
150
  * 主应用推送
123
151
  */
124
- async mainAppPush(params: any): Promise<any> {
125
- return this.callMainApi('mainAppPush', params);
152
+ static async mainAppPush(path: string): Promise<void> {
153
+ return this.callMainAppMethod('mainAppPush', path);
126
154
  }
127
155
  }
128
156
 
129
- // 创建单例实例
130
- const microAppSDKInstance = new MicroAppSDK();
131
-
132
- // 统一导出对象
133
- const MicroOpenApis = {
134
- getAppInfo: microAppSDKInstance.getAppInfo.bind(microAppSDKInstance),
135
- getHostInfo: microAppSDKInstance.getHostInfo.bind(microAppSDKInstance),
136
- getUserInfo: microAppSDKInstance.getUserInfo.bind(microAppSDKInstance),
137
- getToken: microAppSDKInstance.getToken.bind(microAppSDKInstance),
138
- userLogin: microAppSDKInstance.userLogin.bind(microAppSDKInstance),
139
- userLogout: microAppSDKInstance.userLogout.bind(microAppSDKInstance),
140
- mainAppPush: microAppSDKInstance.mainAppPush.bind(microAppSDKInstance),
141
- isInMicroApp: microAppSDKInstance.isInMicroApp.bind(microAppSDKInstance),
142
- };
157
+ // 初始化通信监听器
158
+ MicroAppSDK.init();
143
159
 
144
160
  // 导出统一对象
145
- export { MicroOpenApis };
161
+ export const MicroOpenApis = {
162
+ getAppInfo: MicroAppSDK.getAppInfo.bind(MicroAppSDK),
163
+ getHostInfo: MicroAppSDK.getHostInfo.bind(MicroAppSDK),
164
+ getUserInfo: MicroAppSDK.getUserInfo.bind(MicroAppSDK),
165
+ getToken: MicroAppSDK.getToken.bind(MicroAppSDK),
166
+ userLogout: MicroAppSDK.userLogout.bind(MicroAppSDK),
167
+ mainAppPush: MicroAppSDK.mainAppPush.bind(MicroAppSDK),
168
+ isInMicroApp: MicroAppSDK.isInMicroApp.bind(MicroAppSDK),
169
+ };