qlfy-postmate 1.1.4 → 1.1.6
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.
- package/README.md +71 -10
- package/lib/index.mjs +85 -63
- package/lib/index.umd.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -30,11 +30,11 @@ const childrenComm = new ChildPostmate({
|
|
|
30
30
|
|
|
31
31
|
#### 4、参数对象说明
|
|
32
32
|
|
|
33
|
-
| 回调函数名称 | 解释
|
|
34
|
-
| ------------ |
|
|
35
|
-
| error | 链接失败、未链接时执行
|
|
36
|
-
| success | 链接成功时执行
|
|
37
|
-
| receive |
|
|
33
|
+
| 回调函数名称 | 解释 | 回调参数 | 返回值 |
|
|
34
|
+
| ------------ | ------------------------------------------------------------ | ------------------------------------------------------------ | ------ |
|
|
35
|
+
| error | 链接失败、未链接时执行 | null | void |
|
|
36
|
+
| success | 链接成功时执行 | chldPostmate | void |
|
|
37
|
+
| receive | 父页面推送数据时执行,初次链接成功会触发一次<br />可将本回调函数看做为websoket服务,后续父页面可能会不定时向子系统推送数据 | {<br />isHideHeader:boolean,<br />token:string,<br />loginInfo:object,<br />......<br />} | void |
|
|
38
38
|
|
|
39
39
|
#### 5、方法
|
|
40
40
|
|
|
@@ -60,17 +60,17 @@ const childrenComm = new ChildPostmate({
|
|
|
60
60
|
|
|
61
61
|
3. 等待回调被执行,并做出以下操作:
|
|
62
62
|
|
|
63
|
-
------- 如果error
|
|
63
|
+
------- 如果error回调执行:解除对登录状态的屏蔽,走系统本身登陆逻辑;
|
|
64
64
|
|
|
65
65
|
------- 如果success回调执行:
|
|
66
66
|
|
|
67
|
-
|
|
67
|
+
1、保持对登录状态的屏蔽,并等待receive回调执行;
|
|
68
68
|
|
|
69
|
-
2
|
|
69
|
+
2、receive回调执行:
|
|
70
70
|
|
|
71
71
|
(1)使用接收数据中的token、loginInfo数据,将本地的登陆状态设置为已登录,后续接口请求时携带本回调传入的token;
|
|
72
72
|
|
|
73
|
-
(2)使用接收数据中的isHideHeader
|
|
73
|
+
(2)使用接收数据中的isHideHeader字段,对系统页头的显隐进行控制
|
|
74
74
|
|
|
75
75
|
```vue
|
|
76
76
|
<my-header v-if="!isHideHeader" />
|
|
@@ -78,7 +78,9 @@ const childrenComm = new ChildPostmate({
|
|
|
78
78
|
|
|
79
79
|
(3)解除对登陆状态判断的屏蔽(因已使用接收的数据配置了登录状态,不会触发本系统的登陆);
|
|
80
80
|
|
|
81
|
-
|
|
81
|
+
接入步骤(动态路由)
|
|
82
|
+
|
|
83
|
+
### 样例一(app.vue):
|
|
82
84
|
|
|
83
85
|
App.vue
|
|
84
86
|
|
|
@@ -153,6 +155,65 @@ function initPostMate() {
|
|
|
153
155
|
|
|
154
156
|
```
|
|
155
157
|
|
|
158
|
+
### 样例二(main.js)【推荐】:
|
|
159
|
+
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
import { createApp } from "vue";
|
|
163
|
+
import App from "./App.vue";
|
|
164
|
+
import { ChildPostmate } from 'qlfy-postmate'
|
|
165
|
+
import router from "./router";
|
|
166
|
+
|
|
167
|
+
import { createPinia } from "pinia";
|
|
168
|
+
const store = createPinia(); //pinia实例
|
|
169
|
+
export const app = createApp(App);
|
|
170
|
+
|
|
171
|
+
// pinia
|
|
172
|
+
app.use(store);
|
|
173
|
+
// 路由拦截器
|
|
174
|
+
import '@/permission.js'
|
|
175
|
+
|
|
176
|
+
// 标识,是否为首次加载项目
|
|
177
|
+
let flag = true
|
|
178
|
+
// pinia模块
|
|
179
|
+
import useCounterStore from "@/store/index.js";
|
|
180
|
+
const $store = useCounterStore();
|
|
181
|
+
// 创建链接
|
|
182
|
+
new ChildPostmate({
|
|
183
|
+
error: (err) => {
|
|
184
|
+
console.log('-----------------链接失败-----------------')
|
|
185
|
+
app.use(router)
|
|
186
|
+
app.mount("#app");
|
|
187
|
+
},
|
|
188
|
+
|
|
189
|
+
success: (success) => {
|
|
190
|
+
console.log('-----------------链接成功-----------------')
|
|
191
|
+
},
|
|
192
|
+
|
|
193
|
+
// 异步方法
|
|
194
|
+
receive: async (dataInfo) => {
|
|
195
|
+
console.log('-----------------接收数据-----------------')
|
|
196
|
+
console.log(dataInfo)
|
|
197
|
+
if (dataInfo.status !== 200) return
|
|
198
|
+
|
|
199
|
+
// 数据更新 -- 是否隐藏页头
|
|
200
|
+
$store.setIsHideHeader(dataInfo.data.isHideHeader);
|
|
201
|
+
// 数据更新 -- 登陆信息
|
|
202
|
+
$store.setLoginInfo(dataInfo.data.loginInfo);
|
|
203
|
+
localStorage.setItem('info', JSON.stringify(dataInfo.data.loginInfo));
|
|
204
|
+
|
|
205
|
+
// 项目挂载,仅首次加载项目时执行,后续receive触发不执行
|
|
206
|
+
if (flag) {
|
|
207
|
+
app.use(router)
|
|
208
|
+
app.mount("#app")
|
|
209
|
+
}
|
|
210
|
+
flag = false
|
|
211
|
+
},
|
|
212
|
+
})
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
|
|
156
217
|
### 集成调试
|
|
157
218
|
|
|
158
219
|
按下面链接格式在浏览器中访问,该链接支持前端的本地调试
|
package/lib/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
var
|
|
2
|
-
var g = (r, t, e) => t in r ?
|
|
1
|
+
var f = Object.defineProperty;
|
|
2
|
+
var g = (r, t, e) => t in r ? f(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
|
|
3
3
|
var l = (r, t, e) => g(r, typeof t != "symbol" ? t + "" : t, e);
|
|
4
4
|
const h = "application/x-postmate-v1+json";
|
|
5
5
|
let y = 0;
|
|
@@ -11,23 +11,23 @@ const P = {
|
|
|
11
11
|
reply: 1,
|
|
12
12
|
request: 1
|
|
13
13
|
};
|
|
14
|
-
function
|
|
14
|
+
function p(r, t) {
|
|
15
15
|
return (typeof t != "string" || r.origin === t) && !!r.data && (typeof r.data != "object" || "postmate" in r.data) && r.data.type === h && !!P[r.data.postmate];
|
|
16
16
|
}
|
|
17
17
|
class w {
|
|
18
18
|
constructor(t) {
|
|
19
19
|
this.parent = t.parent, this.frame = t.frame, this.child = t.child, this.childOrigin = t.childOrigin, this.events = {}, this.listener = (e) => {
|
|
20
|
-
if (!
|
|
21
|
-
const { value: s = {} } = e.data, { name:
|
|
22
|
-
e.data.postmate === "emit" &&
|
|
20
|
+
if (!p(e, this.childOrigin)) return;
|
|
21
|
+
const { value: s = {} } = e.data, { name: a, data: i } = s;
|
|
22
|
+
e.data.postmate === "emit" && a in this.events && this.events[a].call(this, i);
|
|
23
23
|
}, this.parent.addEventListener("message", this.listener, !1);
|
|
24
24
|
}
|
|
25
25
|
get(t) {
|
|
26
26
|
return new o.Promise((e) => {
|
|
27
|
-
const s = ++y,
|
|
28
|
-
|
|
27
|
+
const s = ++y, a = (i) => {
|
|
28
|
+
i.data.uid === s && i.data.postmate === "reply" && (this.parent.removeEventListener("message", a, !1), e(i.data.value));
|
|
29
29
|
};
|
|
30
|
-
this.parent.addEventListener("message",
|
|
30
|
+
this.parent.addEventListener("message", a, !1), this.child.postMessage(
|
|
31
31
|
{
|
|
32
32
|
postmate: "request",
|
|
33
33
|
type: h,
|
|
@@ -59,23 +59,23 @@ class w {
|
|
|
59
59
|
class E {
|
|
60
60
|
constructor(t) {
|
|
61
61
|
this.model = t.model, this.parent = t.parent, this.parentOrigin = t.parentOrigin, this.child = t.child, this.child.addEventListener("message", (e) => {
|
|
62
|
-
if (!
|
|
63
|
-
const { postmate: s, property:
|
|
62
|
+
if (!p(e, this.parentOrigin)) return;
|
|
63
|
+
const { postmate: s, property: a, uid: i, data: d } = e.data, n = this.model[a];
|
|
64
64
|
if (s !== "call") {
|
|
65
|
-
const
|
|
66
|
-
o.Promise.resolve(
|
|
65
|
+
const c = typeof n == "function" ? n() : n;
|
|
66
|
+
o.Promise.resolve(c).then((m) => {
|
|
67
67
|
e.source.postMessage(
|
|
68
68
|
{
|
|
69
|
-
property:
|
|
69
|
+
property: a,
|
|
70
70
|
postmate: "reply",
|
|
71
71
|
type: h,
|
|
72
|
-
uid:
|
|
73
|
-
value:
|
|
72
|
+
uid: i,
|
|
73
|
+
value: m
|
|
74
74
|
},
|
|
75
75
|
e.origin
|
|
76
76
|
);
|
|
77
77
|
});
|
|
78
|
-
} else
|
|
78
|
+
} else a in this.model && typeof n == "function" && n(d);
|
|
79
79
|
});
|
|
80
80
|
}
|
|
81
81
|
emit(t, e) {
|
|
@@ -90,24 +90,24 @@ class E {
|
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
92
|
class o {
|
|
93
|
-
constructor({ container: t = document.body, model: e, url: s, name:
|
|
94
|
-
return this.parent = window, this.frame = document.createElement("iframe"), this.frame.name =
|
|
93
|
+
constructor({ container: t = document.body, model: e, url: s, name: a, classListArray: i = [] }) {
|
|
94
|
+
return this.parent = window, this.frame = document.createElement("iframe"), this.frame.name = a || "", this.frame.classList.add(...i), t.appendChild(this.frame), this.child = this.frame.contentWindow, this.model = e || {}, this.sendHandshake(s);
|
|
95
95
|
}
|
|
96
96
|
sendHandshake(t) {
|
|
97
97
|
const e = (() => {
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
const d =
|
|
101
|
-
return
|
|
98
|
+
const i = document.createElement("a");
|
|
99
|
+
i.href = t;
|
|
100
|
+
const d = i.protocol.length > 4 ? i.protocol : window.location.protocol, n = i.host.length ? i.port === "80" || i.port === "443" ? i.hostname : i.host : window.location.host;
|
|
101
|
+
return i.origin || `${d}//${n}`;
|
|
102
102
|
})();
|
|
103
|
-
let s = 0,
|
|
104
|
-
return new o.Promise((
|
|
105
|
-
const n = (
|
|
106
|
-
if (!
|
|
107
|
-
|
|
103
|
+
let s = 0, a;
|
|
104
|
+
return new o.Promise((i, d) => {
|
|
105
|
+
const n = (u) => {
|
|
106
|
+
if (!p(u, e)) return !1;
|
|
107
|
+
u.data.postmate === "handshake-reply" ? (clearInterval(a), this.parent.removeEventListener("message", n, !1), this.childOrigin = u.origin, i(new w(this))) : d("Failed handshake");
|
|
108
108
|
};
|
|
109
109
|
this.parent.addEventListener("message", n, !1);
|
|
110
|
-
const
|
|
110
|
+
const c = () => {
|
|
111
111
|
s++, this.child.postMessage(
|
|
112
112
|
{
|
|
113
113
|
postmate: "handshake",
|
|
@@ -115,15 +115,15 @@ class o {
|
|
|
115
115
|
model: this.model
|
|
116
116
|
},
|
|
117
117
|
e
|
|
118
|
-
), s === 5 && clearInterval(
|
|
119
|
-
},
|
|
120
|
-
|
|
118
|
+
), s === 5 && clearInterval(a);
|
|
119
|
+
}, m = () => {
|
|
120
|
+
c(), a = setInterval(c, 500);
|
|
121
121
|
};
|
|
122
|
-
this.frame.onload =
|
|
122
|
+
this.frame.onload = m, this.frame.attachEvent && this.frame.attachEvent("onload", m), this.frame.src = t;
|
|
123
123
|
});
|
|
124
124
|
}
|
|
125
125
|
}
|
|
126
|
-
class
|
|
126
|
+
class I {
|
|
127
127
|
constructor(t) {
|
|
128
128
|
return this.child = window, this.model = t, this.parent = window.parent, this.sendHandshakeReply();
|
|
129
129
|
}
|
|
@@ -139,9 +139,9 @@ class k {
|
|
|
139
139
|
},
|
|
140
140
|
s.origin
|
|
141
141
|
), this.parentOrigin = s.origin;
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
this.model[
|
|
142
|
+
const a = s.data.model;
|
|
143
|
+
a && Object.keys(a).forEach((i) => {
|
|
144
|
+
this.model[i] = a[i];
|
|
145
145
|
}), t(new E(this));
|
|
146
146
|
}
|
|
147
147
|
}, !1);
|
|
@@ -156,8 +156,8 @@ o.Promise = (() => {
|
|
|
156
156
|
return Promise;
|
|
157
157
|
}
|
|
158
158
|
})();
|
|
159
|
-
o.Model =
|
|
160
|
-
class
|
|
159
|
+
o.Model = I;
|
|
160
|
+
class k {
|
|
161
161
|
constructor(t) {
|
|
162
162
|
this.childPostmate = null, this.parentPostmate = null, this.isGetData = !1, this.getDataCllBack = null, window.top !== window.self ? this.init(t) : t.error({ status: 500, data: null, message: "未在一体化平台中运行" });
|
|
163
163
|
}
|
|
@@ -201,9 +201,9 @@ class C {
|
|
|
201
201
|
t ? (this.isGetData = !0, this.getDataCllBack = t) : (this.isGetData = !1, this.getDataCllBack = null), this.parentPostmate.emit("requestBaseData");
|
|
202
202
|
}
|
|
203
203
|
}
|
|
204
|
-
class
|
|
205
|
-
constructor(t, e =
|
|
206
|
-
/** 连接到的子postmate实例 */
|
|
204
|
+
class v {
|
|
205
|
+
constructor(t, e = null, s = "") {
|
|
206
|
+
/** 连接到的子 postmate 实例 */
|
|
207
207
|
l(this, "postmateParent", null);
|
|
208
208
|
/** 通讯数据 */
|
|
209
209
|
l(this, "dataInfo", {
|
|
@@ -211,44 +211,66 @@ class I {
|
|
|
211
211
|
token: "",
|
|
212
212
|
isHideHeader: !0
|
|
213
213
|
});
|
|
214
|
-
/** iframe Dom */
|
|
215
|
-
l(this, "iframeEle",
|
|
216
|
-
/** iframe url */
|
|
217
|
-
l(this, "
|
|
218
|
-
/**
|
|
219
|
-
l(this, "
|
|
220
|
-
this
|
|
214
|
+
/** iframe Dom 容器 */
|
|
215
|
+
l(this, "iframeEle", null);
|
|
216
|
+
/** 当前 iframe url */
|
|
217
|
+
l(this, "currentUrl", "");
|
|
218
|
+
/** 存储当前的 iframe 元素 */
|
|
219
|
+
l(this, "currentIframe", null);
|
|
220
|
+
l(this, "loadFunction", null);
|
|
221
|
+
this.dataInfo = t || this.dataInfo, this.setIframeEle(e), this.setIframeUrl(s);
|
|
222
|
+
}
|
|
223
|
+
/** 清除当前的 iframe 和 postmate 实例 */
|
|
224
|
+
clear() {
|
|
225
|
+
this.postmateParent && (this.postmateParent.destroy(), this.postmateParent = null), this.currentIframe && this.currentIframe.parentNode && this.currentIframe.parentNode.removeChild(this.currentIframe), this.currentIframe = null;
|
|
221
226
|
}
|
|
222
227
|
/** 初始化 */
|
|
223
|
-
async init(t =
|
|
224
|
-
|
|
228
|
+
async init(t = null) {
|
|
229
|
+
if (this.clear(), !this.iframeEle || !this.currentUrl) {
|
|
230
|
+
console.warn("iframeEle 和 currentUrl 是必需的");
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
225
233
|
const e = typeof this.iframeEle == "function" ? this.iframeEle() : this.iframeEle;
|
|
226
|
-
|
|
234
|
+
if (!e) {
|
|
235
|
+
console.warn("无法获取 iframe 容器");
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
const s = new o({
|
|
227
239
|
container: e,
|
|
228
|
-
url: this.
|
|
229
|
-
})
|
|
230
|
-
|
|
240
|
+
url: this.currentUrl
|
|
241
|
+
});
|
|
242
|
+
this.currentIframe = e.querySelector("iframe"), this.currentIframe.addEventListener("load", () => {
|
|
243
|
+
this.loadFunction();
|
|
244
|
+
}), s.then((a) => {
|
|
245
|
+
console.log("parent", a), this.postmateParent = a, a.on("requestBaseData", (i) => {
|
|
231
246
|
this.sendData();
|
|
232
|
-
}), t && t();
|
|
247
|
+
}), typeof t == "function" && t();
|
|
248
|
+
}).catch((a) => {
|
|
249
|
+
console.error("Postmate 连接失败:", a);
|
|
233
250
|
});
|
|
234
251
|
}
|
|
235
|
-
/** 设置
|
|
252
|
+
/** 设置load执行事件 */
|
|
253
|
+
setLoadFunction(t) {
|
|
254
|
+
this.loadFunction = t;
|
|
255
|
+
}
|
|
256
|
+
/** 设置 iframe Dom */
|
|
236
257
|
setIframeEle(t) {
|
|
237
|
-
this.iframeEle = t, this.iframeEle && this.
|
|
258
|
+
this.iframeEle !== t && (this.iframeEle = t, this.iframeEle && this.currentUrl && this.init());
|
|
238
259
|
}
|
|
239
|
-
/** 设置iframe url */
|
|
260
|
+
/** 设置 iframe url */
|
|
240
261
|
setIframeUrl(t) {
|
|
241
|
-
this.
|
|
262
|
+
this.currentUrl !== t && (this.currentUrl = t, this.iframeEle && this.currentUrl && this.init());
|
|
242
263
|
}
|
|
243
264
|
/** 向子页面发送消息 */
|
|
244
265
|
sendData() {
|
|
245
266
|
this.postmateParent && this.postmateParent.call("baseData", JSON.stringify(this.dataInfo));
|
|
246
267
|
}
|
|
268
|
+
/** 销毁实例 */
|
|
247
269
|
destroy() {
|
|
248
|
-
this.
|
|
270
|
+
this.clear();
|
|
249
271
|
}
|
|
250
272
|
}
|
|
251
273
|
export {
|
|
252
|
-
|
|
253
|
-
|
|
274
|
+
k as ChildPostmate,
|
|
275
|
+
v as ParentPostmate
|
|
254
276
|
};
|
package/lib/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(r
|
|
1
|
+
(function(n,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(n=typeof globalThis<"u"?globalThis:n||self,r(n["qlfy-postmate"]={}))})(this,function(n){"use strict";var k=Object.defineProperty;var v=(n,r,c)=>r in n?k(n,r,{enumerable:!0,configurable:!0,writable:!0,value:c}):n[r]=c;var d=(n,r,c)=>v(n,typeof r!="symbol"?r+"":r,c);const r="application/x-postmate-v1+json";let c=0;const y={handshake:1,"handshake-reply":1,call:1,emit:1,reply:1,request:1};function p(o,t){return(typeof t!="string"||o.origin===t)&&!!o.data&&(typeof o.data!="object"||"postmate"in o.data)&&o.data.type===r&&!!y[o.data.postmate]}class P{constructor(t){this.parent=t.parent,this.frame=t.frame,this.child=t.child,this.childOrigin=t.childOrigin,this.events={},this.listener=e=>{if(!p(e,this.childOrigin))return;const{value:s={}}=e.data,{name:a,data:i}=s;e.data.postmate==="emit"&&a in this.events&&this.events[a].call(this,i)},this.parent.addEventListener("message",this.listener,!1)}get(t){return new l.Promise(e=>{const s=++c,a=i=>{i.data.uid===s&&i.data.postmate==="reply"&&(this.parent.removeEventListener("message",a,!1),e(i.data.value))};this.parent.addEventListener("message",a,!1),this.child.postMessage({postmate:"request",type:r,property:t,uid:s},this.childOrigin)})}call(t,e){this.child.postMessage({postmate:"call",type:r,property:t,data:e},this.childOrigin)}on(t,e){this.events[t]=e}destroy(){window.removeEventListener("message",this.listener,!1),this.frame.parentNode.removeChild(this.frame)}}class w{constructor(t){this.model=t.model,this.parent=t.parent,this.parentOrigin=t.parentOrigin,this.child=t.child,this.child.addEventListener("message",e=>{if(!p(e,this.parentOrigin))return;const{postmate:s,property:a,uid:i,data:m}=e.data,h=this.model[a];if(s!=="call"){const u=typeof h=="function"?h():h;l.Promise.resolve(u).then(f=>{e.source.postMessage({property:a,postmate:"reply",type:r,uid:i,value:f},e.origin)})}else a in this.model&&typeof h=="function"&&h(m)})}emit(t,e){this.parent.postMessage({postmate:"emit",type:r,value:{name:t,data:e}},this.parentOrigin)}}class l{constructor({container:t=document.body,model:e,url:s,name:a,classListArray:i=[]}){return this.parent=window,this.frame=document.createElement("iframe"),this.frame.name=a||"",this.frame.classList.add(...i),t.appendChild(this.frame),this.child=this.frame.contentWindow,this.model=e||{},this.sendHandshake(s)}sendHandshake(t){const e=(()=>{const i=document.createElement("a");i.href=t;const m=i.protocol.length>4?i.protocol:window.location.protocol,h=i.host.length?i.port==="80"||i.port==="443"?i.hostname:i.host:window.location.host;return i.origin||`${m}//${h}`})();let s=0,a;return new l.Promise((i,m)=>{const h=g=>{if(!p(g,e))return!1;g.data.postmate==="handshake-reply"?(clearInterval(a),this.parent.removeEventListener("message",h,!1),this.childOrigin=g.origin,i(new P(this))):m("Failed handshake")};this.parent.addEventListener("message",h,!1);const u=()=>{s++,this.child.postMessage({postmate:"handshake",type:r,model:this.model},e),s===5&&clearInterval(a)},f=()=>{u(),a=setInterval(u,500)};this.frame.onload=f,this.frame.attachEvent&&this.frame.attachEvent("onload",f),this.frame.src=t})}}class E{constructor(t){return this.child=window,this.model=t,this.parent=window.parent,this.sendHandshakeReply()}sendHandshakeReply(){return new l.Promise((t,e)=>{this.child.addEventListener("message",s=>{if(s.data.postmate){if(s.data.postmate!=="handshake")return e("Handshake Reply Failed");this.child.removeEventListener("message",this,!1),s.source.postMessage({postmate:"handshake-reply",type:r},s.origin),this.parentOrigin=s.origin;const a=s.data.model;a&&Object.keys(a).forEach(i=>{this.model[i]=a[i]}),t(new w(this))}},!1)})}}l.debug=!1,l.Promise=(()=>{try{return typeof window<"u"?window.Promise:Promise}catch{return Promise}})(),l.Model=E;class I{constructor(t){this.childPostmate=null,this.parentPostmate=null,this.isGetData=!1,this.getDataCllBack=null,window.top!==window.self?this.init(t):t.error({status:500,data:null,message:"未在一体化平台中运行"})}init(t){const e=this;this.childPostmate=new l.Model({baseData(s){s=JSON.parse(s),e.isGetData?e.getDataCllBack&&e.getDataCllBack({status:200,message:"success",data:s}):t.receive({status:200,message:"success",data:s}),e.isGetData=!1}}).then(s=>{e.parentPostmate=s,e.getData(),t.success({status:200,message:"success",data:{ChildPostmate:s}})}).catch(s=>{t.error({status:500,data:null,message:s})})}getData(t){if(!this.parentPostmate){t({status:500,message:"父页面未连接",data:null});return}t?(this.isGetData=!0,this.getDataCllBack=t):(this.isGetData=!1,this.getDataCllBack=null),this.parentPostmate.emit("requestBaseData")}}class D{constructor(t,e=null,s=""){d(this,"postmateParent",null);d(this,"dataInfo",{loginInfo:null,token:"",isHideHeader:!0});d(this,"iframeEle",null);d(this,"currentUrl","");d(this,"currentIframe",null);d(this,"loadFunction",null);this.dataInfo=t||this.dataInfo,this.setIframeEle(e),this.setIframeUrl(s)}clear(){this.postmateParent&&(this.postmateParent.destroy(),this.postmateParent=null),this.currentIframe&&this.currentIframe.parentNode&&this.currentIframe.parentNode.removeChild(this.currentIframe),this.currentIframe=null}async init(t=null){if(this.clear(),!this.iframeEle||!this.currentUrl){console.warn("iframeEle 和 currentUrl 是必需的");return}const e=typeof this.iframeEle=="function"?this.iframeEle():this.iframeEle;if(!e){console.warn("无法获取 iframe 容器");return}const s=new l({container:e,url:this.currentUrl});this.currentIframe=e.querySelector("iframe"),this.currentIframe.addEventListener("load",()=>{this.loadFunction()}),s.then(a=>{console.log("parent",a),this.postmateParent=a,a.on("requestBaseData",i=>{this.sendData()}),typeof t=="function"&&t()}).catch(a=>{console.error("Postmate 连接失败:",a)})}setLoadFunction(t){this.loadFunction=t}setIframeEle(t){this.iframeEle!==t&&(this.iframeEle=t,this.iframeEle&&this.currentUrl&&this.init())}setIframeUrl(t){this.currentUrl!==t&&(this.currentUrl=t,this.iframeEle&&this.currentUrl&&this.init())}sendData(){this.postmateParent&&this.postmateParent.call("baseData",JSON.stringify(this.dataInfo))}destroy(){this.clear()}}n.ChildPostmate=I,n.ParentPostmate=D,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})});
|