qlfy-postmate 1.1.7 → 1.2.0

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
@@ -1,184 +1,58 @@
1
- ### 子系统认证通信插件使用文档
1
+ ### 子系统认证通信插件使用文档1.2.0
2
2
 
3
- #### 1、安装
3
+ ### 1.2.0优化升级说明】
4
4
 
5
- ```
6
- npm i qlfy-postmate@1.1.3
7
- ```
8
-
9
- #### 2、导入
10
-
11
- ```js
12
- import { ChildPostmate } from 'qlfy-postmate'
13
- ```
5
+ ```txt
6
+ 1、优化了同IP链接切换时的系统资源重复加载问题
7
+ 增加了系统间的通信处理方式
8
+ 新版本中在new ChildPostmate时需传入第二个参数【vue-router的实例对象】router;
9
+ 通过通信支持 + ChildPostmate内部对router的操作,实现主系统操控集成系统页面跳转的功能;
14
10
 
15
- #### 3、使用
11
+ 2、老版本的兼容性支持
12
+ 如已完成集成的系统仍使用旧版本插件,集成效果不变,仅性能和切换效果差于新版
16
13
 
14
+ 3、建议
15
+ 已完成集成系统也建议使用最新版本,使用变化极小
16
+ 仅需以下两步操作:
17
+ (1)下载新版本插件:npm i qlfy-postmate@1.2.0
18
+ (2)使用处添加新参数:router
19
+ new ChildPostmate({...保持不变}, router)
17
20
  ```
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
- #### 4、参数对象说明
32
-
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
-
39
- #### 5、方法
40
-
41
- | 名称 | 解释 | 使用 |
42
- | ------- | ------------------------------------------------------------ | ------------------------- |
43
- | getData | 链接成功后<br />可通过ChildPostmate实例对象,主动向父页面获取数据 | getData((dataInfo)=>void) |
44
-
45
- #### 注意:
46
-
47
- 1、ChildPostmate 链接的创建是异步过程,需等待error或success函数执行后才可进行登陆状态的判断
48
-
49
- 2、getData方法,仅能在链接创建完成后再调用
50
-
51
- 3、error、success函数仅作为链接状态的标注函数,不传递数据
52
-
53
-
54
-
55
- ### 接入步骤
56
-
57
- 1. 对系统登录状态进行改造,系统加载时先将登陆状态判断屏蔽
58
-
59
- 2. 系统公共部分(app.vue)对该插件进行导入和 new,创建实例,并传入相应回调
60
-
61
- 3. 等待回调被执行,并做出以下操作:
62
-
63
- ------- 如果error回调执行:解除对登录状态的屏蔽,走系统本身登陆逻辑;
64
-
65
- ------- 如果success回调执行:
66
-
67
- ​ 1、保持对登录状态的屏蔽,并等待receive回调执行;
68
-
69
- ​ 2、receive回调执行:
70
-
71
- ​ (1)使用接收数据中的token、loginInfo数据,将本地的登陆状态设置为已登录,后续接口请求时携带本回调传入的token;
72
-
73
- ​ (2)使用接收数据中的isHideHeader字段,对系统页头的显隐进行控制
74
21
 
75
- ```vue
76
- <my-header v-if="!isHideHeader" />
77
- ```
78
22
 
79
- ​ (3)解除对登陆状态判断的屏蔽(因已使用接收的数据配置了登录状态,不会触发本系统的登陆);
80
23
 
81
- 接入步骤(动态路由)
82
-
83
- ### 样例一(app.vue):
84
-
85
- App.vue
86
-
87
- ```vue
88
- <template>
89
- <div class="system-content" v-loading="loading">
90
- <!-- 系统登录状态的判断,需等待postmate链接的响应后再执行 -->
91
-
92
- <!-- 页头 -->
93
- <my-header v-if="!isHideHeader"></my-header>
24
+ #### 1、安装
94
25
 
95
- <!-- 侧面导航 -->
96
- <my-aside v-if="!isHideHeader"></my-aside>
26
+ ```
27
+ npm i qlfy-postmate
28
+ ```
97
29
 
98
- <!-- 内容区 -->
99
- <RouterView v-if="!loading" />
100
- </div>
101
- </template>
30
+ #### 2、导入
102
31
 
103
- <script setup lang="ts">
104
- import { ref } from 'vue'
105
- import { useSystemStore } from '@/store/modules/system'
32
+ ```js
106
33
  import { ChildPostmate } from 'qlfy-postmate'
107
-
108
- // 系统loading
109
- const loading = ref(false)
110
- // 页头是否展示
111
- const isHideHeader = ref(false)
112
- // pinia系统数据管理对象
113
- const systemStore = useSystemStore()
114
-
115
- initPostMate()
116
-
117
- // 方法 -- 初始化postmate链接
118
- function initPostMate() {
119
- loading.value = true
120
- const childrenComm = new ChildPostmate({
121
- error: (err: any) => {
122
- console.log('-----------------链接失败-----------------')
123
- console.log(err)
124
- loading.value = false
125
- },
126
-
127
- success: (success: any) => {
128
- console.log('-----------------链接成功-----------------')
129
- console.log(success)
130
- loading.value = false
131
- },
132
-
133
- receive: (dataInfo: any) => {
134
- console.log('-----------------接收数据-----------------')
135
- console.log(dataInfo)
136
- if (dataInfo.status !== 200) return
137
- /**
138
- * 接收到推送数据后做如下操作
139
- * 1、根据isHideHeader控制页头显隐
140
- * 2、将登录装填变更为已登录,并将token保存在正常登录所在位置进行取用
141
- */
142
-
143
- // 控制导航、页头的展示
144
- isHideHeader.value = dataInfo.data.isHideHeader
145
- // 保存登陆信息
146
- systemStore.setLoginInfo(dataInfo.data.loginInfo)
147
- // 保存token
148
- systemStore.setTooken(dataInfo.data.token)
149
- },
150
- })
151
- }
152
- </script>
153
-
154
- <style scoped></style>
155
-
156
34
  ```
157
35
 
158
- ### 样例二(main.js)【推荐】:
159
-
160
- ```
36
+ #### 3、使用(main.js
161
37
 
38
+ ```js
162
39
  import { createApp } from "vue";
163
40
  import App from "./App.vue";
164
- import { ChildPostmate } from 'qlfy-postmate'
165
41
  import router from "./router";
166
-
167
42
  import { createPinia } from "pinia";
43
+ import { ChildPostmate } from 'qlfy-postmate'
168
44
  const store = createPinia(); //pinia实例
169
- export const app = createApp(App);
45
+ const app = createApp(App);
170
46
 
171
47
  // pinia
172
48
  app.use(store);
173
- // 路由拦截器
174
- import '@/permission.js'
175
49
 
176
50
  // 标识,是否为首次加载项目
177
51
  let flag = true
178
52
  // pinia模块
179
53
  import useCounterStore from "@/store/index.js";
180
54
  const $store = useCounterStore();
181
- // 创建链接
55
+ // 创建链接,传入router实例以支持路由更新功能
182
56
  new ChildPostmate({
183
57
  error: (err) => {
184
58
  console.log('-----------------链接失败-----------------')
@@ -198,10 +72,10 @@ new ChildPostmate({
198
72
 
199
73
  // 数据更新 -- 是否隐藏页头
200
74
  $store.setIsHideHeader(dataInfo.data.isHideHeader);
75
+ // 数据更新 -- token
76
+ $store.setIsHideHeader(dataInfo.data.token);
201
77
  // 数据更新 -- 登陆信息
202
78
  $store.setLoginInfo(dataInfo.data.loginInfo);
203
- localStorage.setItem('info', JSON.stringify(dataInfo.data.loginInfo));
204
-
205
79
  // 项目挂载,仅首次加载项目时执行,后续receive触发不执行
206
80
  if (flag) {
207
81
  app.use(router)
@@ -209,10 +83,45 @@ new ChildPostmate({
209
83
  }
210
84
  flag = false
211
85
  },
212
- })
86
+ }, router) // 传入router实例,支持路由更新功能
213
87
  ```
214
88
 
89
+ #### 4、参数说明
90
+
91
+ | 参数名 | 说明 | 类型 |
92
+ | -------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
93
+ | eventObj | 事件对象集,对外暴露链接状态<br />详细说明见下方eventObj说明 | {<br />error:()=>void<br />success:(childPostmate)=>viod<br />receive:(res:any)=>void<br />} |
94
+ | router | 当前系统的vue-router实例 | Router |
95
+
96
+ #### eventObj说明
97
+
98
+ | 属性(支持的事件回调) | 说明 | 类型 |
99
+ | ---------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
100
+ | error | 链接失败、未链接时执行 | ()=>void |
101
+ | success | 链接成功时执行 | (chldPostmate)=>void |
102
+ | receive | 父页面推送数据时执行,初次链接成功会触发一次<br />可将本回调函数看做为websoket服务,<br />后续父页面可能会不定时向子系统推送数据 | (<br /> {<br /> isHideHeader:boolean,<br /> token:string,<br /> loginInfo:object,<br /> ......<br /> }<br />)=>void |
103
+
104
+
105
+
106
+ #### 5、方法
107
+
108
+ | 名称 | 说明 | 使用 |
109
+ | ---------------- | ------------------------------------------------------------ | ---------------------------------------- |
110
+ | getData | 链接成功后<br />可通过ChildPostmate实例对象,主动向父页面获取数据 | getData((dataInfo)=>void) |
111
+ | setRouter | 设置Vue Router实例和自定义路由更新回调函数<br />用于在实例创建后设置或更新路由实例 | setRouter(router, [routeUpdateCallback]) |
112
+ | sendOtherRequest | 向父页面发送自定义请求 | sendOtherRequest(params) |
113
+
114
+ #### 注意:
115
+
116
+ 1、ChildPostmate 链接的创建是异步过程,需等待error或success函数执行后才可进行登陆状态的判断
117
+
118
+ 2、getData方法,仅能在链接创建完成后再调用
119
+
120
+ 3、路由更新功能需要传入Vue Router实例才能使用
121
+
122
+ 5、如果需要自定义路由更新逻辑,可以传入routeUpdateCallback回调函数
215
123
 
124
+ 6、error、success函数仅作为链接状态的标注函数,不传递数据
216
125
 
217
126
  ### 集成调试
218
127
 
package/lib/index.mjs CHANGED
@@ -1,9 +1,6 @@
1
- var p = Object.defineProperty;
2
- var g = (r, t, e) => t in r ? p(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
3
- var l = (r, t, e) => g(r, typeof t != "symbol" ? t + "" : t, e);
4
- const h = "application/x-postmate-v1+json";
5
- let y = 0;
6
- const w = {
1
+ const c = "application/x-postmate-v1+json";
2
+ let m = 0;
3
+ const f = {
7
4
  handshake: 1,
8
5
  "handshake-reply": 1,
9
6
  call: 1,
@@ -11,26 +8,26 @@ const w = {
11
8
  reply: 1,
12
9
  request: 1
13
10
  };
14
- function f(r, t) {
15
- return (typeof t != "string" || r.origin === t) && !!r.data && (typeof r.data != "object" || "postmate" in r.data) && r.data.type === h && !!w[r.data.postmate];
11
+ function u(n, t) {
12
+ return (typeof t != "string" || n.origin === t) && !!n.data && (typeof n.data != "object" || "postmate" in n.data) && n.data.type === c && !!f[n.data.postmate];
16
13
  }
17
- class P {
14
+ class g {
18
15
  constructor(t) {
19
16
  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);
17
+ if (!u(e, this.childOrigin)) return;
18
+ const { value: s = {} } = e.data, { name: a, data: r } = s;
19
+ e.data.postmate === "emit" && a in this.events && this.events[a].call(this, r);
23
20
  }, this.parent.addEventListener("message", this.listener, !1);
24
21
  }
25
22
  get(t) {
26
23
  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));
24
+ const s = ++m, a = (r) => {
25
+ r.data.uid === s && r.data.postmate === "reply" && (this.parent.removeEventListener("message", a, !1), e(r.data.value));
29
26
  };
30
- this.parent.addEventListener("message", i, !1), this.child.postMessage(
27
+ this.parent.addEventListener("message", a, !1), this.child.postMessage(
31
28
  {
32
29
  postmate: "request",
33
- type: h,
30
+ type: c,
34
31
  property: t,
35
32
  uid: s
36
33
  },
@@ -42,7 +39,7 @@ class P {
42
39
  this.child.postMessage(
43
40
  {
44
41
  postmate: "call",
45
- type: h,
42
+ type: c,
46
43
  property: t,
47
44
  data: e
48
45
  },
@@ -56,33 +53,33 @@ class P {
56
53
  window.removeEventListener("message", this.listener, !1), this.frame.parentNode.removeChild(this.frame);
57
54
  }
58
55
  }
59
- class E {
56
+ class w {
60
57
  constructor(t) {
61
58
  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];
59
+ if (!u(e, this.parentOrigin)) return;
60
+ const { postmate: s, property: a, uid: r, data: h } = e.data, i = this.model[a];
64
61
  if (s !== "call") {
65
- const c = typeof n == "function" ? n() : n;
66
- o.Promise.resolve(c).then((m) => {
62
+ const l = typeof i == "function" ? i() : i;
63
+ o.Promise.resolve(l).then((d) => {
67
64
  e.source.postMessage(
68
65
  {
69
- property: i,
66
+ property: a,
70
67
  postmate: "reply",
71
- type: h,
72
- uid: a,
73
- value: m
68
+ type: c,
69
+ uid: r,
70
+ value: d
74
71
  },
75
72
  e.origin
76
73
  );
77
74
  });
78
- } else i in this.model && typeof n == "function" && n(d);
75
+ } else a in this.model && typeof i == "function" && i(h);
79
76
  });
80
77
  }
81
78
  emit(t, e) {
82
79
  this.parent.postMessage(
83
80
  {
84
81
  postmate: "emit",
85
- type: h,
82
+ type: c,
86
83
  value: { name: t, data: e }
87
84
  },
88
85
  this.parentOrigin
@@ -90,40 +87,40 @@ class E {
90
87
  }
91
88
  }
92
89
  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);
90
+ constructor({ container: t = document.body, model: e, url: s, name: a, classListArray: r = [] }) {
91
+ return this.parent = window, this.frame = document.createElement("iframe"), this.frame.name = a || "", this.frame.classList.add(...r), t.appendChild(this.frame), this.child = this.frame.contentWindow, this.model = e || {}, this.sendHandshake(s);
95
92
  }
96
93
  sendHandshake(t) {
97
94
  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}`;
95
+ const r = document.createElement("a");
96
+ r.href = t;
97
+ const h = r.protocol.length > 4 ? r.protocol : window.location.protocol, i = r.host.length ? r.port === "80" || r.port === "443" ? r.hostname : r.host : window.location.host;
98
+ return r.origin || `${h}//${i}`;
102
99
  })();
103
- let s = 0, i;
104
- return new o.Promise((a, d) => {
105
- const n = (u) => {
106
- if (!f(u, e)) return !1;
107
- u.data.postmate === "handshake-reply" ? (clearInterval(i), this.parent.removeEventListener("message", n, !1), this.childOrigin = u.origin, a(new P(this))) : d("Failed handshake");
100
+ let s = 0, a;
101
+ return new o.Promise((r, h) => {
102
+ const i = (p) => {
103
+ if (!u(p, e)) return !1;
104
+ p.data.postmate === "handshake-reply" ? (clearInterval(a), this.parent.removeEventListener("message", i, !1), this.childOrigin = p.origin, r(new g(this))) : h("Failed handshake");
108
105
  };
109
- this.parent.addEventListener("message", n, !1);
110
- const c = () => {
106
+ this.parent.addEventListener("message", i, !1);
107
+ const l = () => {
111
108
  s++, this.child.postMessage(
112
109
  {
113
110
  postmate: "handshake",
114
- type: h,
111
+ type: c,
115
112
  model: this.model
116
113
  },
117
114
  e
118
- ), s === 5 && clearInterval(i);
119
- }, m = () => {
120
- c(), i = setInterval(c, 500);
115
+ ), s === 5 && clearInterval(a);
116
+ }, d = () => {
117
+ l(), a = setInterval(l, 500);
121
118
  };
122
- this.frame.onload = m, this.frame.attachEvent && this.frame.attachEvent("onload", m), this.frame.src = t;
119
+ this.frame.onload = d, this.frame.attachEvent && this.frame.attachEvent("onload", d), this.frame.src = t;
123
120
  });
124
121
  }
125
122
  }
126
- class I {
123
+ class P {
127
124
  constructor(t) {
128
125
  return this.child = window, this.model = t, this.parent = window.parent, this.sendHandshakeReply();
129
126
  }
@@ -135,14 +132,14 @@ class I {
135
132
  this.child.removeEventListener("message", this, !1), s.source.postMessage(
136
133
  {
137
134
  postmate: "handshake-reply",
138
- type: h
135
+ type: c
139
136
  },
140
137
  s.origin
141
138
  ), this.parentOrigin = s.origin;
142
- const i = s.data.model;
143
- i && Object.keys(i).forEach((a) => {
144
- this.model[a] = i[a];
145
- }), t(new E(this));
139
+ const a = s.data.model;
140
+ a && Object.keys(a).forEach((r) => {
141
+ this.model[r] = a[r];
142
+ }), t(new w(this));
146
143
  }
147
144
  }, !1);
148
145
  });
@@ -156,10 +153,10 @@ o.Promise = (() => {
156
153
  return Promise;
157
154
  }
158
155
  })();
159
- o.Model = I;
160
- class k {
161
- constructor(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: "未在一体化平台中运行" });
156
+ o.Model = P;
157
+ class y {
158
+ constructor(t, e, s) {
159
+ this.childPostmate = null, this.parentPostmate = null, this.isGetData = !1, this.getDataCllBack = null, this.router = null, this.routeUpdateCallback = null, this.router = e, this.routeUpdateCallback = s, window.top !== window.self ? this.init(t) : t.error({ status: 500, data: null, message: "未在一体化平台中运行" });
163
160
  }
164
161
  init(t) {
165
162
  const e = this;
@@ -175,6 +172,23 @@ class k {
175
172
  message: "success",
176
173
  data: s
177
174
  }), e.isGetData = !1;
175
+ },
176
+ // 处理路由更新
177
+ routeUpdate(s) {
178
+ try {
179
+ window.parent.postMessage(JSON.stringify({
180
+ type: "route_update_support",
181
+ supported: !0
182
+ }), "*");
183
+ const a = JSON.parse(s);
184
+ if (e.routeUpdateCallback) {
185
+ e.routeUpdateCallback(a);
186
+ return;
187
+ }
188
+ e.router ? e.router.replace(a.fullPath) : console.warn("未提供router实例,无法进行路由更新");
189
+ } catch (a) {
190
+ console.error("路由更新失败:", a);
191
+ }
178
192
  }
179
193
  }).then((s) => {
180
194
  e.parentPostmate = s, e.getData(), t.success({
@@ -200,25 +214,63 @@ class k {
200
214
  }
201
215
  t ? (this.isGetData = !0, this.getDataCllBack = t) : (this.isGetData = !1, this.getDataCllBack = null), this.parentPostmate.emit("requestBaseData");
202
216
  }
217
+ /**
218
+ * 设置路由实例和路由更新回调
219
+ * @param router Vue Router实例
220
+ * @param routeUpdateCallback 自定义路由更新回调函数
221
+ */
222
+ setRouter(t, e) {
223
+ this.router = t, e && (this.routeUpdateCallback = e);
224
+ }
225
+ /**
226
+ * 向父页面发送其他请求
227
+ * @param params 请求参数
228
+ */
229
+ sendOtherRequest(t) {
230
+ this.parentPostmate && this.parentPostmate.emit("otherRequest", t);
231
+ }
203
232
  }
204
- class v {
205
- constructor(t, e = null, s = "") {
206
- /** 连接到的子 postmate 实例 */
207
- l(this, "postmateParent", null);
208
- /** 通讯数据 */
209
- l(this, "dataInfo", {
210
- loginInfo: null,
233
+ class U {
234
+ constructor(t, e, s = "", a) {
235
+ this.postmateParent = null, this.dataInfo = {
236
+ loginInfo: {},
211
237
  token: "",
212
238
  isHideHeader: !0
213
- });
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);
239
+ }, this.iframeEle = null, this.currentUrl = "", this.currentIframe = null, this.loadFunction = null, this.handleOtherRequest = null, this.parsedCurrentUrl = null, this.childSupportsRouteUpdate = !0, this.dataInfo = t || this.dataInfo, this.iframeEle = e, this.setIframeUrl(s), a && (this.handleOtherRequest = a);
240
+ }
241
+ /** 解析URL */
242
+ parseUrl(t) {
243
+ const e = document.createElement("a");
244
+ e.href = t;
245
+ const s = e.protocol.length > 4 ? e.protocol : window.location.protocol, a = e.hostname, r = e.port || (s === "https:" ? "443" : s === "http:" ? "80" : ""), h = e.pathname.startsWith("/") ? e.pathname : `/${e.pathname}`, i = e.search, l = e.hash, d = e.origin || `${s}//${a}${r ? `:${r}` : ""}`, p = `${h}${i}${l}`;
246
+ return {
247
+ protocol: s,
248
+ host: a,
249
+ port: r,
250
+ pathname: h,
251
+ search: i,
252
+ hash: l,
253
+ origin: d,
254
+ fullPath: p
255
+ };
256
+ }
257
+ /** 比较两个URL是否相同(协议、主机、端口) */
258
+ compareUrlBase(t, e) {
259
+ if (!t || !e) return !1;
260
+ const s = this.parseUrl(t), a = this.parseUrl(e);
261
+ return s.protocol === a.protocol && s.host === a.host && s.port === a.port;
262
+ }
263
+ /** 比较两个URL的路径是否相同(不包含查询参数和哈希) */
264
+ compareUrlPath(t, e) {
265
+ if (!t || !e) return !1;
266
+ const s = this.parseUrl(t), a = this.parseUrl(e);
267
+ return s.pathname === a.pathname;
268
+ }
269
+ /** 比较两个URL是否完全相同(包括查询参数和哈希) */
270
+ compareUrlFull(t, e) {
271
+ if (!t || !e) return !1;
272
+ const s = this.parseUrl(t), a = this.parseUrl(e);
273
+ return s.fullPath === a.fullPath;
222
274
  }
223
275
  /** 清除当前的 iframe 和 postmate 实例 */
224
276
  clear() {
@@ -226,7 +278,7 @@ class v {
226
278
  }
227
279
  /** 初始化 */
228
280
  async init(t = null) {
229
- if (this.clear(), !this.iframeEle || !this.currentUrl) {
281
+ if (this.clear(), this.childSupportsRouteUpdate = !0, !this.iframeEle || !this.currentUrl) {
230
282
  console.warn("iframeEle 和 currentUrl 是必需的");
231
283
  return;
232
284
  }
@@ -237,29 +289,78 @@ class v {
237
289
  }
238
290
  const s = new o({
239
291
  container: e,
240
- url: this.currentUrl
292
+ url: this.currentUrl,
293
+ name: "",
294
+ model: ""
241
295
  });
242
296
  this.currentIframe = e.querySelector("iframe"), this.currentIframe.setAttribute("frameborder", "0"), this.currentIframe.setAttribute("allow", "fullscreen"), this.currentIframe.addEventListener("load", () => {
243
- this.loadFunction();
244
- }), s.then((i) => {
245
- this.postmateParent = i, i.on("requestBaseData", (a) => {
297
+ this.loadFunction && this.loadFunction();
298
+ }), s.then((a) => {
299
+ this.postmateParent = a, a.on("requestBaseData", () => {
246
300
  this.sendData();
301
+ }), a.on("otherRequest", (r) => {
302
+ this.handleOtherRequest && this.handleOtherRequest(r);
247
303
  }), typeof t == "function" && t();
248
- }).catch((i) => {
249
- console.error("Postmate 连接失败:", i);
304
+ }).catch((a) => {
305
+ console.error("Postmate 连接失败:", a);
250
306
  });
251
307
  }
252
308
  /** 设置load执行事件 */
253
309
  setLoadFunction(t) {
254
310
  this.loadFunction = t;
255
311
  }
256
- /** 设置 iframe Dom */
257
- setIframeEle(t) {
258
- this.iframeEle !== t && (this.iframeEle = t, this.iframeEle && this.currentUrl && this.init());
259
- }
260
312
  /** 设置 iframe url */
261
313
  setIframeUrl(t) {
262
- this.currentUrl !== t && (this.currentUrl = t, this.iframeEle && this.currentUrl && this.init());
314
+ if (this.currentUrl === t) return;
315
+ if (!this.currentUrl) {
316
+ this.currentUrl = t, this.parsedCurrentUrl = this.parseUrl(t), this.iframeEle && this.init();
317
+ return;
318
+ }
319
+ const e = this.parseUrl(t);
320
+ if (this.postmateParent && this.compareUrlBase(this.currentUrl, t) && this.childSupportsRouteUpdate)
321
+ if (this.compareUrlPath(this.currentUrl, t)) {
322
+ if (this.compareUrlFull(this.currentUrl, t))
323
+ return;
324
+ this.currentUrl = t, this.parsedCurrentUrl = e, this.sendRouteUpdate(e);
325
+ return;
326
+ } else {
327
+ this.currentUrl = t, this.parsedCurrentUrl = e, this.sendRouteUpdate(e);
328
+ return;
329
+ }
330
+ this.currentUrl = t, this.parsedCurrentUrl = e, this.iframeEle && this.init();
331
+ }
332
+ /** 向子页面发送路由更新消息 */
333
+ sendRouteUpdate(t) {
334
+ if (this.postmateParent) {
335
+ if (!this.childSupportsRouteUpdate) {
336
+ console.warn("子页面不支持路由更新,直接重新初始化iframe"), this.init();
337
+ return;
338
+ }
339
+ try {
340
+ this.postmateParent.call(
341
+ "routeUpdate",
342
+ JSON.stringify({
343
+ fullPath: t.fullPath,
344
+ pathname: t.pathname,
345
+ search: t.search,
346
+ hash: t.hash
347
+ })
348
+ );
349
+ let e = !1;
350
+ const s = (a) => {
351
+ try {
352
+ const r = JSON.parse(a.data);
353
+ r && r.type === "route_update_support" && r.supported === !0 && (e = !0, window.removeEventListener("message", s));
354
+ } catch {
355
+ }
356
+ };
357
+ window.addEventListener("message", s), setTimeout(() => {
358
+ window.removeEventListener("message", s), e || (console.warn("子页面不支持路由更新,将在下次更新时重新初始化iframe"), this.childSupportsRouteUpdate = !1, this.init());
359
+ }, 1e3);
360
+ } catch (e) {
361
+ console.warn("发送路由更新消息失败,将重新初始化iframe:", e), this.childSupportsRouteUpdate = !1, this.init();
362
+ }
363
+ }
263
364
  }
264
365
  /** 向子页面发送消息 */
265
366
  sendData() {
@@ -271,6 +372,6 @@ class v {
271
372
  }
272
373
  }
273
374
  export {
274
- k as ChildPostmate,
275
- v as ParentPostmate
375
+ y as ChildPostmate,
376
+ U as ParentPostmate
276
377
  };
Binary file
package/lib/index.umd.js CHANGED
@@ -1 +1 @@
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: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=++c,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: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:i,uid:a,data:m}=e.data,h=this.model[i];if(s!=="call"){const u=typeof h=="function"?h():h;l.Promise.resolve(u).then(f=>{e.source.postMessage({property:i,postmate:"reply",type:r,uid:a,value:f},e.origin)})}else i 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: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 m=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||`${m}//${h}`})();let s=0,i;return new l.Promise((a,m)=>{const h=g=>{if(!p(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))):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(i)},f=()=>{u(),i=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 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 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.setAttribute("frameborder","0"),this.currentIframe.setAttribute("allow","fullscreen"),this.currentIframe.addEventListener("load",()=>{this.loadFunction()}),s.then(i=>{this.postmateParent=i,i.on("requestBaseData",a=>{this.sendData()}),typeof t=="function"&&t()}).catch(i=>{console.error("Postmate 连接失败:",i)})}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"})});
1
+ (function(d,n){typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(d=typeof globalThis<"u"?globalThis:d||self,n(d["qlfy-postmate"]={}))})(this,function(d){"use strict";const n="application/x-postmate-v1+json";let f=0;const g={handshake:1,"handshake-reply":1,call:1,emit:1,reply:1,request:1};function m(o,t){return(typeof t!="string"||o.origin===t)&&!!o.data&&(typeof o.data!="object"||"postmate"in o.data)&&o.data.type===n&&!!g[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(!m(e,this.childOrigin))return;const{value:s={}}=e.data,{name:a,data:r}=s;e.data.postmate==="emit"&&a in this.events&&this.events[a].call(this,r)},this.parent.addEventListener("message",this.listener,!1)}get(t){return new h.Promise(e=>{const s=++f,a=r=>{r.data.uid===s&&r.data.postmate==="reply"&&(this.parent.removeEventListener("message",a,!1),e(r.data.value))};this.parent.addEventListener("message",a,!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(!m(e,this.parentOrigin))return;const{postmate:s,property:a,uid:r,data:l}=e.data,i=this.model[a];if(s!=="call"){const c=typeof i=="function"?i():i;h.Promise.resolve(c).then(u=>{e.source.postMessage({property:a,postmate:"reply",type:n,uid:r,value:u},e.origin)})}else a in this.model&&typeof i=="function"&&i(l)})}emit(t,e){this.parent.postMessage({postmate:"emit",type:n,value:{name:t,data:e}},this.parentOrigin)}}class h{constructor({container:t=document.body,model:e,url:s,name:a,classListArray:r=[]}){return this.parent=window,this.frame=document.createElement("iframe"),this.frame.name=a||"",this.frame.classList.add(...r),t.appendChild(this.frame),this.child=this.frame.contentWindow,this.model=e||{},this.sendHandshake(s)}sendHandshake(t){const e=(()=>{const r=document.createElement("a");r.href=t;const l=r.protocol.length>4?r.protocol:window.location.protocol,i=r.host.length?r.port==="80"||r.port==="443"?r.hostname:r.host:window.location.host;return r.origin||`${l}//${i}`})();let s=0,a;return new h.Promise((r,l)=>{const i=p=>{if(!m(p,e))return!1;p.data.postmate==="handshake-reply"?(clearInterval(a),this.parent.removeEventListener("message",i,!1),this.childOrigin=p.origin,r(new P(this))):l("Failed handshake")};this.parent.addEventListener("message",i,!1);const c=()=>{s++,this.child.postMessage({postmate:"handshake",type:n,model:this.model},e),s===5&&clearInterval(a)},u=()=>{c(),a=setInterval(c,500)};this.frame.onload=u,this.frame.attachEvent&&this.frame.attachEvent("onload",u),this.frame.src=t})}}class y{constructor(t){return this.child=window,this.model=t,this.parent=window.parent,this.sendHandshakeReply()}sendHandshakeReply(){return new h.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 a=s.data.model;a&&Object.keys(a).forEach(r=>{this.model[r]=a[r]}),t(new w(this))}},!1)})}}h.debug=!1,h.Promise=(()=>{try{return typeof window<"u"?window.Promise:Promise}catch{return Promise}})(),h.Model=y;class U{constructor(t,e,s){this.childPostmate=null,this.parentPostmate=null,this.isGetData=!1,this.getDataCllBack=null,this.router=null,this.routeUpdateCallback=null,this.router=e,this.routeUpdateCallback=s,window.top!==window.self?this.init(t):t.error({status:500,data:null,message:"未在一体化平台中运行"})}init(t){const e=this;this.childPostmate=new h.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},routeUpdate(s){try{window.parent.postMessage(JSON.stringify({type:"route_update_support",supported:!0}),"*");const a=JSON.parse(s);if(e.routeUpdateCallback){e.routeUpdateCallback(a);return}e.router?e.router.replace(a.fullPath):console.warn("未提供router实例,无法进行路由更新")}catch(a){console.error("路由更新失败:",a)}}}).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")}setRouter(t,e){this.router=t,e&&(this.routeUpdateCallback=e)}sendOtherRequest(t){this.parentPostmate&&this.parentPostmate.emit("otherRequest",t)}}class E{constructor(t,e,s="",a){this.postmateParent=null,this.dataInfo={loginInfo:{},token:"",isHideHeader:!0},this.iframeEle=null,this.currentUrl="",this.currentIframe=null,this.loadFunction=null,this.handleOtherRequest=null,this.parsedCurrentUrl=null,this.childSupportsRouteUpdate=!0,this.dataInfo=t||this.dataInfo,this.iframeEle=e,this.setIframeUrl(s),a&&(this.handleOtherRequest=a)}parseUrl(t){const e=document.createElement("a");e.href=t;const s=e.protocol.length>4?e.protocol:window.location.protocol,a=e.hostname,r=e.port||(s==="https:"?"443":s==="http:"?"80":""),l=e.pathname.startsWith("/")?e.pathname:`/${e.pathname}`,i=e.search,c=e.hash,u=e.origin||`${s}//${a}${r?`:${r}`:""}`,p=`${l}${i}${c}`;return{protocol:s,host:a,port:r,pathname:l,search:i,hash:c,origin:u,fullPath:p}}compareUrlBase(t,e){if(!t||!e)return!1;const s=this.parseUrl(t),a=this.parseUrl(e);return s.protocol===a.protocol&&s.host===a.host&&s.port===a.port}compareUrlPath(t,e){if(!t||!e)return!1;const s=this.parseUrl(t),a=this.parseUrl(e);return s.pathname===a.pathname}compareUrlFull(t,e){if(!t||!e)return!1;const s=this.parseUrl(t),a=this.parseUrl(e);return s.fullPath===a.fullPath}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.childSupportsRouteUpdate=!0,!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 h({container:e,url:this.currentUrl,name:"",model:""});this.currentIframe=e.querySelector("iframe"),this.currentIframe.setAttribute("frameborder","0"),this.currentIframe.setAttribute("allow","fullscreen"),this.currentIframe.addEventListener("load",()=>{this.loadFunction&&this.loadFunction()}),s.then(a=>{this.postmateParent=a,a.on("requestBaseData",()=>{this.sendData()}),a.on("otherRequest",r=>{this.handleOtherRequest&&this.handleOtherRequest(r)}),typeof t=="function"&&t()}).catch(a=>{console.error("Postmate 连接失败:",a)})}setLoadFunction(t){this.loadFunction=t}setIframeUrl(t){if(this.currentUrl===t)return;if(!this.currentUrl){this.currentUrl=t,this.parsedCurrentUrl=this.parseUrl(t),this.iframeEle&&this.init();return}const e=this.parseUrl(t);if(this.postmateParent&&this.compareUrlBase(this.currentUrl,t)&&this.childSupportsRouteUpdate)if(this.compareUrlPath(this.currentUrl,t)){if(this.compareUrlFull(this.currentUrl,t))return;this.currentUrl=t,this.parsedCurrentUrl=e,this.sendRouteUpdate(e);return}else{this.currentUrl=t,this.parsedCurrentUrl=e,this.sendRouteUpdate(e);return}this.currentUrl=t,this.parsedCurrentUrl=e,this.iframeEle&&this.init()}sendRouteUpdate(t){if(this.postmateParent){if(!this.childSupportsRouteUpdate){console.warn("子页面不支持路由更新,直接重新初始化iframe"),this.init();return}try{this.postmateParent.call("routeUpdate",JSON.stringify({fullPath:t.fullPath,pathname:t.pathname,search:t.search,hash:t.hash}));let e=!1;const s=a=>{try{const r=JSON.parse(a.data);r&&r.type==="route_update_support"&&r.supported===!0&&(e=!0,window.removeEventListener("message",s))}catch{}};window.addEventListener("message",s),setTimeout(()=>{window.removeEventListener("message",s),e||(console.warn("子页面不支持路由更新,将在下次更新时重新初始化iframe"),this.childSupportsRouteUpdate=!1,this.init())},1e3)}catch(e){console.warn("发送路由更新消息失败,将重新初始化iframe:",e),this.childSupportsRouteUpdate=!1,this.init()}}}sendData(){this.postmateParent&&this.postmateParent.call("baseData",JSON.stringify(this.dataInfo))}destroy(){this.clear()}}d.ChildPostmate=U,d.ParentPostmate=E,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})});
@@ -3,13 +3,32 @@ interface EventObj {
3
3
  success: (success: any) => void;
4
4
  receive: (receive: any) => void;
5
5
  }
6
+ interface RouteUpdateInfo {
7
+ fullPath: string;
8
+ pathname: string;
9
+ search: string;
10
+ hash: string;
11
+ }
6
12
  export default class ChildPostmate {
7
13
  childPostmate: any;
8
14
  parentPostmate: any;
9
15
  isGetData: boolean;
10
16
  getDataCllBack: Function | null;
11
- constructor(eventObj: EventObj);
17
+ router: any;
18
+ routeUpdateCallback: ((routeInfo: RouteUpdateInfo) => void) | null;
19
+ constructor(eventObj: EventObj, router: any, routeUpdateCallback?: (routeInfo: RouteUpdateInfo) => void);
12
20
  init(eventObj: EventObj): void;
13
21
  getData(callback?: any): void;
22
+ /**
23
+ * 设置路由实例和路由更新回调
24
+ * @param router Vue Router实例
25
+ * @param routeUpdateCallback 自定义路由更新回调函数
26
+ */
27
+ setRouter(router: any, routeUpdateCallback?: (routeInfo: RouteUpdateInfo) => void): void;
28
+ /**
29
+ * 向父页面发送其他请求
30
+ * @param params 请求参数
31
+ */
32
+ sendOtherRequest(params: any): void;
14
33
  }
15
34
  export {};
@@ -0,0 +1,61 @@
1
+ interface DataInfo {
2
+ loginInfo: {
3
+ [x: string]: any;
4
+ };
5
+ token: string;
6
+ isHideHeader: boolean;
7
+ }
8
+ interface ParsedUrl {
9
+ protocol: string;
10
+ host: string;
11
+ port: string;
12
+ pathname: string;
13
+ search: string;
14
+ hash: string;
15
+ origin: string;
16
+ fullPath: string;
17
+ }
18
+ export default class ParentPostmate {
19
+ /** 连接到的子 postmate 实例 */
20
+ postmateParent: any;
21
+ /** 通讯数据 */
22
+ dataInfo: DataInfo;
23
+ /** iframe Dom 容器 */
24
+ iframeEle: any;
25
+ /** 当前 iframe url */
26
+ currentUrl: string;
27
+ /** 存储当前的 iframe 元素 */
28
+ currentIframe: any;
29
+ /** iframe 加载完成执行函数 */
30
+ loadFunction: (() => void) | null;
31
+ /** 处理子类的其他请求方法 */
32
+ handleOtherRequest: ((params: any) => void) | null;
33
+ /** 当前URL解析结果 */
34
+ parsedCurrentUrl: ParsedUrl | null;
35
+ /** 子页面是否支持路由更新 */
36
+ childSupportsRouteUpdate: boolean;
37
+ constructor(dataInfo: DataInfo, iframeEle: HTMLElement | (() => HTMLElement), iframeUrl?: string, handleOtherRequest?: (params: any) => void);
38
+ /** 解析URL */
39
+ parseUrl(url: string): ParsedUrl;
40
+ /** 比较两个URL是否相同(协议、主机、端口) */
41
+ compareUrlBase(url1: string, url2: string): boolean;
42
+ /** 比较两个URL的路径是否相同(不包含查询参数和哈希) */
43
+ compareUrlPath(url1: string, url2: string): boolean;
44
+ /** 比较两个URL是否完全相同(包括查询参数和哈希) */
45
+ compareUrlFull(url1: string, url2: string): boolean;
46
+ /** 清除当前的 iframe 和 postmate 实例 */
47
+ clear(): void;
48
+ /** 初始化 */
49
+ init(callback?: any): Promise<void>;
50
+ /** 设置load执行事件 */
51
+ setLoadFunction(loadFunction: any): void;
52
+ /** 设置 iframe url */
53
+ setIframeUrl(iframeUrl: string): void;
54
+ /** 向子页面发送路由更新消息 */
55
+ sendRouteUpdate(parsedUrl: ParsedUrl): void;
56
+ /** 向子页面发送消息 */
57
+ sendData(): void;
58
+ /** 销毁实例 */
59
+ destroy(): void;
60
+ }
61
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qlfy-postmate",
3
- "version": "1.1.7",
3
+ "version": "1.2.0",
4
4
  "description": "",
5
5
  "types": "lib/index.d.ts",
6
6
  "module": "lib/index.umd.js",