iframe-tab-sdk 1.0.0 → 1.0.2
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 +51 -44
- package/index.js +186 -11
- package/package.json +14 -14
package/README.md
CHANGED
|
@@ -1,44 +1,51 @@
|
|
|
1
|
-
## iframe-tab-sdk
|
|
2
|
-
|
|
3
|
-
iframe微前端集成, 顶部tab
|
|
4
|
-
|
|
5
|
-
## API
|
|
6
|
-
|方法名|说明|参数|类型
|
|
7
|
-
|:----|:----|:----|:----
|
|
8
|
-
|
|
|
9
|
-
|
|
|
10
|
-
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
1
|
+
## iframe-tab-sdk
|
|
2
|
+
|
|
3
|
+
iframe微前端集成, 顶部tab页操作;支持独立运行
|
|
4
|
+
|
|
5
|
+
## API
|
|
6
|
+
|方法名|说明|参数|类型
|
|
7
|
+
|:----|:----|:----|:----
|
|
8
|
+
|openTab|打开tab页|urlPath,openRoute|string,object
|
|
9
|
+
|refreshTab|刷新tab页|urlPath,openRoute|string,object
|
|
10
|
+
|closeTab|关闭tab页|urlPath|string
|
|
11
|
+
|logout|退出登录|无|无
|
|
12
|
+
|reload|刷新主应用网页|无|无
|
|
13
|
+
|closeAndOpenTab|关闭当前tab并新开tab|urlPath,{ {...openRoute}, closeTabName }|string,object
|
|
14
|
+
|fullscreen|全屏/退出全屏|isFullScreen|boolean
|
|
15
|
+
|
|
16
|
+
### openRoute Attributes
|
|
17
|
+
|参数|类型|必填|说明
|
|
18
|
+
|:----|:----|:----|:----
|
|
19
|
+
|urlPath|string|是|tab页对应的路由(需加上子应用前缀)
|
|
20
|
+
|title|string|是|tab页标题
|
|
21
|
+
|params|object|否|params路由参数
|
|
22
|
+
|query|object|否|query路由参数
|
|
23
|
+
|refreshClearQuery|boolean|否|是否清空路由参数
|
|
24
|
+
|key|string|否|资源id
|
|
25
|
+
|meta|object|否|tab页元数据
|
|
26
|
+
|
|
27
|
+
## 示例(vue+elementui)
|
|
28
|
+
``` vue
|
|
29
|
+
<template>
|
|
30
|
+
<el-button type="primary" @click="closeAndOpenTab(urlPath,{...openRoute,closeTabName: '/console/user/add',message:'用户添加成功!' })">关闭当前tab并打开新的tab页</el-button>
|
|
31
|
+
</template>
|
|
32
|
+
<script>
|
|
33
|
+
import {closeAndOpenTabAsIframe} from 'iframe-tab-sdk'
|
|
34
|
+
export default {
|
|
35
|
+
data() {
|
|
36
|
+
return {
|
|
37
|
+
openRoute: {
|
|
38
|
+
urlPath: '/console/user/list',
|
|
39
|
+
title: '新tab',
|
|
40
|
+
params: {}, // 非必填
|
|
41
|
+
query: {}, // 非必填
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
methods: {
|
|
46
|
+
closeAndOpenTab
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
</script>
|
|
50
|
+
|
|
51
|
+
```
|
package/index.js
CHANGED
|
@@ -1,12 +1,187 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
//
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
//
|
|
10
|
-
|
|
11
|
-
window.
|
|
1
|
+
const origin =
|
|
2
|
+
process.env.NODE_ENV === "development" ? "*" : window.location.origin;
|
|
3
|
+
window.isIframeRun = window.self !== window.top; // 是否在iframe中运行
|
|
4
|
+
|
|
5
|
+
// 获取Vue Router实例的辅助函数
|
|
6
|
+
function getRouter() {
|
|
7
|
+
// 尝试获取Vue应用实例和路由
|
|
8
|
+
if (window.VueApp && window.VueApp.config && window.VueApp.config.globalProperties) {
|
|
9
|
+
// Vue 3
|
|
10
|
+
return window.VueApp.config.globalProperties.$router;
|
|
11
|
+
} else if (window.VueApp && window.VueApp._router) {
|
|
12
|
+
// Vue 2
|
|
13
|
+
return window.VueApp._router;
|
|
14
|
+
} else if (window.$nuxt && window.$nuxt.$router) {
|
|
15
|
+
// Nuxt.js
|
|
16
|
+
return window.$nuxt.$router;
|
|
17
|
+
} else if (window.router) {
|
|
18
|
+
// 直接访问全局router对象
|
|
19
|
+
return window.router;
|
|
20
|
+
}
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// 非iframe环境下执行路由跳转
|
|
25
|
+
function navigateTo(urlPath, query = {}) {
|
|
26
|
+
const router = getRouter();
|
|
27
|
+
if (!router) {
|
|
28
|
+
console.error('Vue Router instance not found. Please make sure to set window.VueApp to your Vue instance.');
|
|
29
|
+
// 尝试使用window.location进行跳转作为备选方案
|
|
30
|
+
const queryString = new URLSearchParams(query).toString();
|
|
31
|
+
const targetUrl = queryString ? `${urlPath}?${queryString}` : urlPath;
|
|
32
|
+
window.location.href = targetUrl;
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// 根据Vue Router版本适配不同API
|
|
37
|
+
if (typeof router.push === 'function') {
|
|
38
|
+
// 尝试获取路由对象或字符串路径
|
|
39
|
+
try {
|
|
40
|
+
// 如果是Vue Router 3.1+,支持对象路由
|
|
41
|
+
router.push({
|
|
42
|
+
path: urlPath,
|
|
43
|
+
query: query
|
|
44
|
+
}).catch(err => {
|
|
45
|
+
// 忽略导航到相同路由时的错误
|
|
46
|
+
if (err.name !== 'NavigationDuplicated' && !err.message.includes('redundant navigation')) {
|
|
47
|
+
console.error('Router navigation error:', err);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
} catch (error) {
|
|
51
|
+
// 如果对象形式失败,尝试字符串形式
|
|
52
|
+
const queryString = new URLSearchParams(query).toString();
|
|
53
|
+
const targetUrl = queryString ? `${urlPath}?${queryString}` : urlPath;
|
|
54
|
+
router.push(targetUrl).catch(err => {
|
|
55
|
+
if (err.name !== 'NavigationDuplicated' && !err.message.includes('redundant navigation')) {
|
|
56
|
+
console.error('Router navigation error:', err);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
} else {
|
|
61
|
+
console.error('Vue Router push method not available.');
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 新开或跳转 tab 标签页
|
|
67
|
+
* @param {Object} params 相关参数
|
|
68
|
+
* @param {String} params[urlPath] 页面地址
|
|
69
|
+
* @param {String} params[name] 页面名称
|
|
70
|
+
* @param {String} params[title] 页面标题
|
|
71
|
+
* @param {Object} params[query] query 参数
|
|
72
|
+
* @param {Boolean} params[refreshClearQuery] 是否清空路由参数
|
|
73
|
+
*
|
|
74
|
+
*/
|
|
75
|
+
export function openTab(params) {
|
|
76
|
+
if (window.isIframeRun) {
|
|
77
|
+
window.parent.postMessage(
|
|
78
|
+
{
|
|
79
|
+
type: "openTab",
|
|
80
|
+
params
|
|
81
|
+
},
|
|
82
|
+
origin
|
|
83
|
+
);
|
|
84
|
+
} else {
|
|
85
|
+
const { urlPath, query = {} } = params;
|
|
86
|
+
// 非iframe环境,执行路由跳转
|
|
87
|
+
navigateTo(urlPath, query);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 刷新指定 tab 标签页
|
|
93
|
+
* @param {Object} params 相关参数
|
|
94
|
+
* @param {String} params[urlPath] 指定 tab 标签页 URL
|
|
95
|
+
* @param {String} params[name] 页面名称
|
|
96
|
+
* @param {String} params[title] 页面标题
|
|
97
|
+
* @param {Object} params[query] query 参数
|
|
98
|
+
*/
|
|
99
|
+
export function refreshTab(params) {
|
|
100
|
+
const urlPath = params.urlPath;
|
|
101
|
+
if (window.isIframeRun) {
|
|
102
|
+
if (params) {
|
|
103
|
+
return openTab(params);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
window.parent.postMessage({ type: "refreshTab", params: urlPath }, origin);
|
|
107
|
+
} else {
|
|
108
|
+
// 非iframe环境下,重新导航到指定路径
|
|
109
|
+
if (params) {
|
|
110
|
+
return openTab(params);
|
|
111
|
+
}
|
|
112
|
+
navigateTo(urlPath, {});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 关闭指定 tab 标签页
|
|
118
|
+
* @param {String} urlPath 指定 tab 标签页 URL
|
|
119
|
+
*/
|
|
120
|
+
export function closeTab(urlPath) {
|
|
121
|
+
if (window.isIframeRun) {
|
|
122
|
+
window.parent.postMessage({ type: "closeTab", params: urlPath }, origin);
|
|
123
|
+
} else {
|
|
124
|
+
// 非iframe环境,通常在主应用中没有实际的tab需要关闭,可以返回上一页或跳转到首页
|
|
125
|
+
if (window.history.length > 1) {
|
|
126
|
+
window.history.back();
|
|
127
|
+
} else {
|
|
128
|
+
window.location.href = '/';
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* 调用主应用退出登录
|
|
135
|
+
*/
|
|
136
|
+
export function logout() {
|
|
137
|
+
if (window.isIframeRun) {
|
|
138
|
+
window.parent.postMessage({ type: "logout" }, origin);
|
|
139
|
+
} else {
|
|
140
|
+
// 非iframe环境,直接执行登出逻辑
|
|
141
|
+
window.location.href = '/';
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* 刷新主应用网页
|
|
147
|
+
*/
|
|
148
|
+
export function reload() {
|
|
149
|
+
if (window.isIframeRun) {
|
|
150
|
+
window.parent.postMessage({ type: "reload" }, origin);
|
|
151
|
+
} else {
|
|
152
|
+
// 非iframe环境,直接刷新页面
|
|
153
|
+
window.location.reload();
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* 关闭某个tab打开新的tab(常用于关闭自身tab并打开新tab)
|
|
159
|
+
*/
|
|
160
|
+
export function closeAndOpenTab(params) {
|
|
161
|
+
if (window.isIframeRun) {
|
|
162
|
+
window.parent.postMessage(
|
|
163
|
+
{
|
|
164
|
+
type: "closeAndOpenTab",
|
|
165
|
+
params
|
|
166
|
+
},
|
|
167
|
+
origin
|
|
168
|
+
);
|
|
169
|
+
} else {
|
|
170
|
+
const {urlPath,query} = params;
|
|
171
|
+
// 非iframe环境,直接跳转到新页面
|
|
172
|
+
navigateTo(urlPath, query);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* 全屏/退出全屏
|
|
178
|
+
* @param {Boolean} isFullScreen 是否全屏
|
|
179
|
+
*/
|
|
180
|
+
export function fullscreen(isFullScreen) {
|
|
181
|
+
if (window.isIframeRun) {
|
|
182
|
+
window.parent.postMessage({ type: "fullscreen", params: isFullScreen }, origin);
|
|
183
|
+
} else {
|
|
184
|
+
// 非iframe环境,不执行任何操作或可以实现本地全屏功能
|
|
185
|
+
console.log('Fullscreen operation is only available in iframe mode.');
|
|
186
|
+
}
|
|
12
187
|
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "iframe-tab-sdk",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "门户iframe集成子应用顶部tab页管理",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"private": false,
|
|
7
|
-
"homepage": "https://gitee.com/weixiaoyun/iframe-tab-sdk/blob/master/README.md",
|
|
8
|
-
"scripts": {
|
|
9
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
10
|
-
},
|
|
11
|
-
"keywords": ["iframe", "tab", "集成", "微前端"],
|
|
12
|
-
"author": "wy",
|
|
13
|
-
"license": "ISC"
|
|
14
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "iframe-tab-sdk",
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "门户iframe集成子应用顶部tab页管理",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"private": false,
|
|
7
|
+
"homepage": "https://gitee.com/weixiaoyun/iframe-tab-sdk/blob/master/README.md",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
10
|
+
},
|
|
11
|
+
"keywords": ["iframe", "tab", "集成", "微前端"],
|
|
12
|
+
"author": "wy",
|
|
13
|
+
"license": "ISC"
|
|
14
|
+
}
|