qlfy-postmate 1.1.5 → 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 CHANGED
@@ -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 u = Object.defineProperty;
2
- var g = (r, t, e) => t in r ? u(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
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 f(r, t) {
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 (!f(e, this.childOrigin)) return;
21
- const { value: s = {} } = e.data, { name: i, data: a } = s;
22
- e.data.postmate === "emit" && i in this.events && this.events[i].call(this, a);
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, i = (a) => {
28
- a.data.uid === s && a.data.postmate === "reply" && (this.parent.removeEventListener("message", i, !1), e(a.data.value));
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", i, !1), this.child.postMessage(
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 (!f(e, this.parentOrigin)) return;
63
- const { postmate: s, property: i, uid: a, data: d } = e.data, n = this.model[i];
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 m = typeof n == "function" ? n() : n;
66
- o.Promise.resolve(m).then((c) => {
65
+ const c = typeof n == "function" ? n() : n;
66
+ o.Promise.resolve(c).then((m) => {
67
67
  e.source.postMessage(
68
68
  {
69
- property: i,
69
+ property: a,
70
70
  postmate: "reply",
71
71
  type: h,
72
- uid: a,
73
- value: c
72
+ uid: i,
73
+ value: m
74
74
  },
75
75
  e.origin
76
76
  );
77
77
  });
78
- } else i in this.model && typeof n == "function" && n(d);
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: i, classListArray: a = [] }) {
94
- return this.parent = window, this.frame = document.createElement("iframe"), this.frame.name = i || "", this.frame.classList.add(...a), t.appendChild(this.frame), this.child = this.frame.contentWindow, this.model = e || {}, this.sendHandshake(s);
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 a = document.createElement("a");
99
- a.href = t;
100
- const d = a.protocol.length > 4 ? a.protocol : window.location.protocol, n = a.host.length ? a.port === "80" || a.port === "443" ? a.hostname : a.host : window.location.host;
101
- return a.origin || `${d}//${n}`;
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, i;
104
- return new o.Promise((a, d) => {
105
- const n = (p) => {
106
- if (!f(p, e)) return !1;
107
- p.data.postmate === "handshake-reply" ? (clearInterval(i), this.parent.removeEventListener("message", n, !1), this.childOrigin = p.origin, a(new w(this))) : d("Failed handshake");
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 m = () => {
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(i);
119
- }, c = () => {
120
- m(), i = setInterval(m, 500);
118
+ ), s === 5 && clearInterval(a);
119
+ }, m = () => {
120
+ c(), a = setInterval(c, 500);
121
121
  };
122
- this.frame.onload = c, this.frame.attachEvent && this.frame.attachEvent("onload", c), this.frame.src = t;
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 k {
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 i = s.data.model;
143
- i && Object.keys(i).forEach((a) => {
144
- this.model[a] = i[a];
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 = k;
160
- class C {
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 I {
205
- constructor(t, e = "", s = "") {
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, "iframeUrl", "");
218
- /** postmate 实例 */
219
- l(this, "handshake", null);
220
- this.dataInfo = t || this.dataInfo, this.iframeEle = e, this.iframeUrl = s, this.iframeEle && this.iframeUrl && this.init();
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
- this.postmateParent && (this.postmateParent.destroy(), this.postmateParent = null);
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
- new o({
234
+ if (!e) {
235
+ console.warn("无法获取 iframe 容器");
236
+ return;
237
+ }
238
+ const s = new o({
227
239
  container: e,
228
- url: this.iframeUrl
229
- }).then((i) => {
230
- this.postmateParent = i, i.on("requestBaseData", (a) => {
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
- /** 设置iframe Dom */
252
+ /** 设置load执行事件 */
253
+ setLoadFunction(t) {
254
+ this.loadFunction = t;
255
+ }
256
+ /** 设置 iframe Dom */
236
257
  setIframeEle(t) {
237
- this.iframeEle = t, this.iframeEle && this.iframeUrl && this.init();
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.iframeUrl = t, this.iframeEle && this.iframeUrl && this.init();
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.postmateParent && (this.postmateParent.destroy(), this.postmateParent = null);
270
+ this.clear();
249
271
  }
250
272
  }
251
273
  export {
252
- C as ChildPostmate,
253
- I as ParentPostmate
274
+ k as ChildPostmate,
275
+ v as ParentPostmate
254
276
  };
package/lib/index.umd.js CHANGED
@@ -1 +1 @@
1
- (function(r,n){typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(r=typeof globalThis<"u"?globalThis:r||self,n(r["qlfy-postmate"]={}))})(this,function(r){"use strict";var C=Object.defineProperty;var M=(r,n,d)=>n in r?C(r,n,{enumerable:!0,configurable:!0,writable:!0,value:d}):r[n]=d;var m=(r,n,d)=>M(r,typeof n!="symbol"?n+"":n,d);const n="application/x-postmate-v1+json";let d=0;const y={handshake:1,"handshake-reply":1,call:1,emit:1,reply:1,request:1};function u(o,t){return(typeof t!="string"||o.origin===t)&&!!o.data&&(typeof o.data!="object"||"postmate"in o.data)&&o.data.type===n&&!!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(!u(e,this.childOrigin))return;const{value:s={}}=e.data,{name:i,data:a}=s;e.data.postmate==="emit"&&i in this.events&&this.events[i].call(this,a)},this.parent.addEventListener("message",this.listener,!1)}get(t){return new l.Promise(e=>{const s=++d,i=a=>{a.data.uid===s&&a.data.postmate==="reply"&&(this.parent.removeEventListener("message",i,!1),e(a.data.value))};this.parent.addEventListener("message",i,!1),this.child.postMessage({postmate:"request",type:n,property:t,uid:s},this.childOrigin)})}call(t,e){this.child.postMessage({postmate:"call",type:n,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(!u(e,this.parentOrigin))return;const{postmate:s,property:i,uid:a,data:c}=e.data,h=this.model[i];if(s!=="call"){const p=typeof h=="function"?h():h;l.Promise.resolve(p).then(f=>{e.source.postMessage({property:i,postmate:"reply",type:n,uid:a,value:f},e.origin)})}else i in this.model&&typeof h=="function"&&h(c)})}emit(t,e){this.parent.postMessage({postmate:"emit",type:n,value:{name:t,data:e}},this.parentOrigin)}}class l{constructor({container:t=document.body,model:e,url:s,name:i,classListArray:a=[]}){return this.parent=window,this.frame=document.createElement("iframe"),this.frame.name=i||"",this.frame.classList.add(...a),t.appendChild(this.frame),this.child=this.frame.contentWindow,this.model=e||{},this.sendHandshake(s)}sendHandshake(t){const e=(()=>{const a=document.createElement("a");a.href=t;const c=a.protocol.length>4?a.protocol:window.location.protocol,h=a.host.length?a.port==="80"||a.port==="443"?a.hostname:a.host:window.location.host;return a.origin||`${c}//${h}`})();let s=0,i;return new l.Promise((a,c)=>{const h=g=>{if(!u(g,e))return!1;g.data.postmate==="handshake-reply"?(clearInterval(i),this.parent.removeEventListener("message",h,!1),this.childOrigin=g.origin,a(new P(this))):c("Failed handshake")};this.parent.addEventListener("message",h,!1);const p=()=>{s++,this.child.postMessage({postmate:"handshake",type:n,model:this.model},e),s===5&&clearInterval(i)},f=()=>{p(),i=setInterval(p,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:n},s.origin),this.parentOrigin=s.origin;const i=s.data.model;i&&Object.keys(i).forEach(a=>{this.model[a]=i[a]}),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 k{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="",s=""){m(this,"postmateParent",null);m(this,"dataInfo",{loginInfo:null,token:"",isHideHeader:!0});m(this,"iframeEle","");m(this,"iframeUrl","");m(this,"handshake",null);this.dataInfo=t||this.dataInfo,this.iframeEle=e,this.iframeUrl=s,this.iframeEle&&this.iframeUrl&&this.init()}async init(t=""){this.postmateParent&&(this.postmateParent.destroy(),this.postmateParent=null);const e=typeof this.iframeEle=="function"?this.iframeEle():this.iframeEle;new l({container:e,url:this.iframeUrl}).then(i=>{this.postmateParent=i,i.on("requestBaseData",a=>{this.sendData()}),t&&t()})}setIframeEle(t){this.iframeEle=t,this.iframeEle&&this.iframeUrl&&this.init()}setIframeUrl(t){this.iframeUrl=t,this.iframeEle&&this.iframeUrl&&this.init()}sendData(){this.postmateParent&&this.postmateParent.call("baseData",JSON.stringify(this.dataInfo))}destroy(){this.postmateParent&&(this.postmateParent.destroy(),this.postmateParent=null)}}r.ChildPostmate=k,r.ParentPostmate=D,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
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"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qlfy-postmate",
3
- "version": "1.1.5",
3
+ "version": "1.1.6",
4
4
  "description": "",
5
5
  "types": "lib/index.d.ts",
6
6
  "module": "lib/index.umd.js",