qlfy-postmate 1.0.9 → 1.1.1

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
@@ -0,0 +1,31 @@
1
+ ### 子系统认证通信插件使用文档
2
+
3
+ #### 1、安装
4
+
5
+ ```
6
+ npm i qlfy-postmate@1.1.0
7
+ ```
8
+
9
+ #### 2、导入
10
+
11
+ ```js
12
+ import { ChildPostmate } from 'qlfy-postmate'
13
+ ```
14
+
15
+ #### 3、使用
16
+
17
+ ```
18
+ const childrenComm = new ChildPostmate({
19
+ error: (err: any) => {
20
+ console.log('-----------------链接失败-----------------', err)
21
+ },
22
+ success: (success: any) => {
23
+ console.log('-----------------链接成功-----------------', success)
24
+ },
25
+ receive: (dataInfo: any) => {
26
+ console.log('-----------------接收数据-----------------', dataInfo)
27
+ },
28
+ })
29
+ ```
30
+
31
+ 样例:
package/lib/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  var u = Object.defineProperty;
2
2
  var g = (r, t, e) => t in r ? u(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
3
- var o = (r, t, e) => g(r, typeof t != "symbol" ? t + "" : t, e);
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;
6
6
  const P = {
@@ -14,7 +14,7 @@ const P = {
14
14
  function f(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
- class E {
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
20
  if (!f(e, this.childOrigin)) return;
@@ -23,7 +23,7 @@ class E {
23
23
  }, this.parent.addEventListener("message", this.listener, !1);
24
24
  }
25
25
  get(t) {
26
- return new l.Promise((e) => {
26
+ return new o.Promise((e) => {
27
27
  const s = ++y, i = (a) => {
28
28
  a.data.uid === s && a.data.postmate === "reply" && (this.parent.removeEventListener("message", i, !1), e(a.data.value));
29
29
  };
@@ -56,14 +56,14 @@ class E {
56
56
  window.removeEventListener("message", this.listener, !1), this.frame.parentNode.removeChild(this.frame);
57
57
  }
58
58
  }
59
- class w {
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
62
  if (!f(e, this.parentOrigin)) return;
63
63
  const { postmate: s, property: i, uid: a, data: d } = e.data, n = this.model[i];
64
64
  if (s !== "call") {
65
65
  const m = typeof n == "function" ? n() : n;
66
- l.Promise.resolve(m).then((c) => {
66
+ o.Promise.resolve(m).then((c) => {
67
67
  e.source.postMessage(
68
68
  {
69
69
  property: i,
@@ -89,7 +89,7 @@ class w {
89
89
  );
90
90
  }
91
91
  }
92
- class l {
92
+ class o {
93
93
  constructor({ container: t = document.body, model: e, url: s, name: i, classListArray: a = [] }) {
94
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);
95
95
  }
@@ -101,10 +101,10 @@ class l {
101
101
  return a.origin || `${d}//${n}`;
102
102
  })();
103
103
  let s = 0, i;
104
- return new l.Promise((a, d) => {
104
+ return new o.Promise((a, d) => {
105
105
  const n = (p) => {
106
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 E(this))) : d("Failed handshake");
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");
108
108
  };
109
109
  this.parent.addEventListener("message", n, !1);
110
110
  const m = () => {
@@ -128,7 +128,7 @@ class k {
128
128
  return this.child = window, this.model = t, this.parent = window.parent, this.sendHandshakeReply();
129
129
  }
130
130
  sendHandshakeReply() {
131
- return new l.Promise((t, e) => {
131
+ return new o.Promise((t, e) => {
132
132
  this.child.addEventListener("message", (s) => {
133
133
  if (s.data.postmate) {
134
134
  if (s.data.postmate !== "handshake") return e("Handshake Reply Failed");
@@ -142,39 +142,35 @@ class k {
142
142
  const i = s.data.model;
143
143
  i && Object.keys(i).forEach((a) => {
144
144
  this.model[a] = i[a];
145
- }), t(new w(this));
145
+ }), t(new E(this));
146
146
  }
147
147
  }, !1);
148
148
  });
149
149
  }
150
150
  }
151
- l.debug = !1;
152
- l.Promise = (() => {
151
+ o.debug = !1;
152
+ o.Promise = (() => {
153
153
  try {
154
154
  return typeof window < "u" ? window.Promise : Promise;
155
155
  } catch {
156
156
  return Promise;
157
157
  }
158
158
  })();
159
- l.Model = k;
159
+ o.Model = k;
160
160
  class C {
161
161
  constructor(t) {
162
- o(this, "childPostmate", null);
163
- o(this, "parentPostmate", null);
164
- o(this, "isGetData", !1);
165
- o(this, "getDataCllBack", null);
166
- window.frameElement ? t.error({ status: 500, data: null, message: "未在一体化平台中运行" }) : this.init(t);
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: "未在一体化平台中运行" });
167
163
  }
168
164
  init(t) {
169
165
  const e = this;
170
- this.childPostmate = new l.Model({
166
+ this.childPostmate = new o.Model({
171
167
  // 定义子页面可以暴露给父页面的方法
172
168
  baseData(s) {
173
169
  s = JSON.parse(s), e.isGetData ? e.getDataCllBack && e.getDataCllBack({
174
170
  status: 200,
175
171
  message: "success",
176
172
  data: s
177
- }) : t.success({
173
+ }) : t.receive({
178
174
  status: 200,
179
175
  message: "success",
180
176
  data: s
@@ -193,7 +189,7 @@ class C {
193
189
  });
194
190
  }
195
191
  // 获取数据
196
- getData(t = null) {
192
+ getData(t) {
197
193
  if (!this.parentPostmate) {
198
194
  t({
199
195
  status: 500,
@@ -208,26 +204,26 @@ class C {
208
204
  class I {
209
205
  constructor(t, e = "", s = "") {
210
206
  /** 连接到的子postmate实例 */
211
- o(this, "postmateParent", null);
207
+ l(this, "postmateParent", null);
212
208
  /** 通讯数据 */
213
- o(this, "dataInfo", {
209
+ l(this, "dataInfo", {
214
210
  loginInfo: null,
215
211
  token: "",
216
212
  isHideHeader: !0
217
213
  });
218
214
  /** iframe Dom */
219
- o(this, "iframeEle", "");
215
+ l(this, "iframeEle", "");
220
216
  /** iframe url */
221
- o(this, "iframeUrl", "");
217
+ l(this, "iframeUrl", "");
222
218
  /** postmate 实例 */
223
- o(this, "handshake", null);
219
+ l(this, "handshake", null);
224
220
  this.dataInfo = t || this.dataInfo, this.iframeEle = e, this.iframeUrl = s, this.iframeEle && this.iframeUrl && this.init();
225
221
  }
226
222
  /** 初始化 */
227
223
  async init(t = "") {
228
224
  this.postmateParent && (this.postmateParent.destroy(), this.postmateParent = null);
229
225
  const e = typeof this.iframeEle == "function" ? this.iframeEle() : this.iframeEle;
230
- new l({
226
+ new o({
231
227
  container: e,
232
228
  url: this.iframeUrl
233
229
  }).then((i) => {
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,m)=>n in r?C(r,n,{enumerable:!0,configurable:!0,writable:!0,value:m}):r[n]=m;var d=(r,n,m)=>M(r,typeof n!="symbol"?n+"":n,m);const n="application/x-postmate-v1+json";let m=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=++m,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 E{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 w{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 E(this))}},!1)})}}l.debug=!1,l.Promise=(()=>{try{return typeof window<"u"?window.Promise:Promise}catch{return Promise}})(),l.Model=w;class k{constructor(t){d(this,"childPostmate",null);d(this,"parentPostmate",null);d(this,"isGetData",!1);d(this,"getDataCllBack",null);window.frameElement?t.error({status:500,data:null,message:"未在一体化平台中运行"}):this.init(t)}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.success({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=null){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=""){d(this,"postmateParent",null);d(this,"dataInfo",{loginInfo:null,token:"",isHideHeader:!0});d(this,"iframeEle","");d(this,"iframeUrl","");d(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(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"})});
@@ -0,0 +1,15 @@
1
+ interface EventObj {
2
+ error: (error: any) => {};
3
+ success: (success: any) => {};
4
+ receive: (receive: any) => {};
5
+ }
6
+ export default class ChildPostmate {
7
+ childPostmate: any;
8
+ parentPostmate: any;
9
+ isGetData: boolean;
10
+ getDataCllBack: Function | null;
11
+ constructor(eventObj: EventObj);
12
+ init(eventObj: EventObj): void;
13
+ getData(callback?: any): void;
14
+ }
15
+ export {};
@@ -0,0 +1,3 @@
1
+ import { default as ChildPostmate } from './ChildPostmate';
2
+ import { default as ParentPostmate } from './ParentPostmate';
3
+ export { ChildPostmate, ParentPostmate };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qlfy-postmate",
3
- "version": "1.0.9",
3
+ "version": "1.1.1",
4
4
  "description": "",
5
5
  "types": "lib/index.d.ts",
6
6
  "module": "lib/index.umd.js",