boke-devops 0.0.30
Sign up to get free protection for your applications and to get access to all the features.
- package/.vscode/settings.json +2 -0
- package/lerna.json +4 -0
- package/package.json +43 -0
- package/packages/boke-devops-vue/.eslintrc.cjs +22 -0
- package/packages/boke-devops-vue/README.md +45 -0
- package/packages/boke-devops-vue/demo/App.vue +62 -0
- package/packages/boke-devops-vue/demo/main.ts +7 -0
- package/packages/boke-devops-vue/package-lock.json +13513 -0
- package/packages/boke-devops-vue/package.json +59 -0
- package/packages/boke-devops-vue/pnpm-lock.yaml +6063 -0
- package/packages/boke-devops-vue/src/AegisUser/__tests__/Avatar.test.js +18 -0
- package/packages/boke-devops-vue/src/AegisUser/__tests__/__snapshots__/Avatar.test.js.snap +60 -0
- package/packages/boke-devops-vue/src/AegisUser/multiple-user-select.vue +190 -0
- package/packages/boke-devops-vue/src/AegisUser/store.ts +23 -0
- package/packages/boke-devops-vue/src/AegisUser/user-avatar-group.vue +76 -0
- package/packages/boke-devops-vue/src/AegisUser/user-avatar.vue +347 -0
- package/packages/boke-devops-vue/src/Microapp/MicroAppProvider.vue +50 -0
- package/packages/boke-devops-vue/src/Provider/Provider.vue +22 -0
- package/packages/boke-devops-vue/src/Provider/store.ts +23 -0
- package/packages/boke-devops-vue/src/global.d.ts +43 -0
- package/packages/boke-devops-vue/src/index.ts +39 -0
- package/packages/boke-devops-vue/src/style/variables.less +79 -0
- package/packages/boke-devops-vue/tsconfig.json +34 -0
- package/packages/boke-devops-vue/tsconfig.web.json +20 -0
- package/packages/boke-devops-vue/types/AegisUser/multiple-user-select.vue.d.ts +97 -0
- package/packages/boke-devops-vue/types/AegisUser/store.d.ts +7 -0
- package/packages/boke-devops-vue/types/AegisUser/user-avatar-group.vue.d.ts +44 -0
- package/packages/boke-devops-vue/types/AegisUser/user-avatar.vue.d.ts +217 -0
- package/packages/boke-devops-vue/types/Microapp/MicroAppProvider.vue.d.ts +39 -0
- package/packages/boke-devops-vue/types/Provider/Provider.vue.d.ts +63 -0
- package/packages/boke-devops-vue/types/Provider/store.d.ts +15 -0
- package/packages/boke-devops-vue/types/index.d.ts +13 -0
- package/packages/boke-devops-vue/types/store.d.ts +12 -0
- package/packages/boke-devops-vue/vite.config.ts +90 -0
- package/packages/boke-docs/.dumirc.ts +64 -0
- package/packages/boke-docs/.eslintrc.js +14 -0
- package/packages/boke-docs/.fatherrc.ts +7 -0
- package/packages/boke-docs/README.md +1 -0
- package/packages/boke-docs/docs/components/Avatar/demo/normal.vue +25 -0
- package/packages/boke-docs/docs/components/Avatar/demo/with_provider.vue +38 -0
- package/packages/boke-docs/docs/components/Avatar/index.md +10 -0
- package/packages/boke-docs/docs/components/UserSelect/demo/normal.vue +34 -0
- package/packages/boke-docs/docs/components/UserSelect/demo/with_provider.vue +40 -0
- package/packages/boke-docs/docs/components/UserSelect/index.md +12 -0
- package/packages/boke-docs/docs/components/index.md +126 -0
- package/packages/boke-docs/docs/framework-test/external/App.vue +303 -0
- package/packages/boke-docs/docs/framework-test/index.md +96 -0
- package/packages/boke-docs/docs/index.md +50 -0
- package/packages/boke-docs/node_modules/.bin/acorn +17 -0
- package/packages/boke-docs/node_modules/.bin/acorn.CMD +12 -0
- package/packages/boke-docs/node_modules/.bin/acorn.ps1 +41 -0
- package/packages/boke-docs/node_modules/.bin/browserslist +17 -0
- package/packages/boke-docs/node_modules/.bin/browserslist.CMD +12 -0
- package/packages/boke-docs/node_modules/.bin/browserslist.ps1 +41 -0
- package/packages/boke-docs/node_modules/.bin/dumi +17 -0
- package/packages/boke-docs/node_modules/.bin/dumi.CMD +12 -0
- package/packages/boke-docs/node_modules/.bin/dumi.ps1 +41 -0
- package/packages/boke-docs/node_modules/.bin/eslint +17 -0
- package/packages/boke-docs/node_modules/.bin/eslint.CMD +12 -0
- package/packages/boke-docs/node_modules/.bin/eslint.ps1 +41 -0
- package/packages/boke-docs/node_modules/.bin/prettier +17 -0
- package/packages/boke-docs/node_modules/.bin/prettier.CMD +12 -0
- package/packages/boke-docs/node_modules/.bin/prettier.ps1 +41 -0
- package/packages/boke-docs/node_modules/.bin/stylelint +17 -0
- package/packages/boke-docs/node_modules/.bin/stylelint.CMD +12 -0
- package/packages/boke-docs/node_modules/.bin/stylelint.ps1 +41 -0
- package/packages/boke-docs/node_modules/.bin/tsc +17 -0
- package/packages/boke-docs/node_modules/.bin/tsc.CMD +12 -0
- package/packages/boke-docs/node_modules/.bin/tsc.ps1 +41 -0
- package/packages/boke-docs/node_modules/.bin/tsserver +17 -0
- package/packages/boke-docs/node_modules/.bin/tsserver.CMD +12 -0
- package/packages/boke-docs/node_modules/.bin/tsserver.ps1 +41 -0
- package/packages/boke-docs/node_modules/.bin/vite +17 -0
- package/packages/boke-docs/node_modules/.bin/vite.CMD +12 -0
- package/packages/boke-docs/node_modules/.bin/vite.ps1 +41 -0
- package/packages/boke-docs/node_modules/.bin/webpack +17 -0
- package/packages/boke-docs/node_modules/.bin/webpack.CMD +12 -0
- package/packages/boke-docs/node_modules/.bin/webpack.ps1 +41 -0
- package/packages/boke-docs/package.json +38 -0
- package/packages/boke-docs/pnpm-lock.yaml +11506 -0
- package/packages/boke-docs/tsconfig.json +14 -0
- package/packages/boke-docs/tsconfig.vue.json +10 -0
- package/packages/boke-js-bridge/es/child.d.ts +27 -0
- package/packages/boke-js-bridge/es/child.js +144 -0
- package/packages/boke-js-bridge/es/index.d.ts +3 -0
- package/packages/boke-js-bridge/es/index.js +3 -0
- package/packages/boke-js-bridge/es/parent.d.ts +24 -0
- package/packages/boke-js-bridge/es/parent.js +71 -0
- package/packages/boke-js-bridge/es/utils.d.ts +2 -0
- package/packages/boke-js-bridge/es/utils.js +6 -0
- package/packages/boke-js-bridge/package-lock.json +2028 -0
- package/packages/boke-js-bridge/package.json +35 -0
- package/packages/boke-js-bridge/readme.md +11 -0
- package/packages/boke-js-bridge/src/child.ts +174 -0
- package/packages/boke-js-bridge/src/index.ts +3 -0
- package/packages/boke-js-bridge/src/parent.ts +92 -0
- package/packages/boke-js-bridge/src/utils.ts +15 -0
- package/packages/boke-js-bridge/tsconfig.json +26 -0
- package/packages/boke-js-bridge/typings.d.ts +4 -0
- package/packages/boke-js-bridge/umd/child.js +156 -0
- package/packages/boke-js-bridge/umd/index.js +18 -0
- package/packages/boke-js-bridge/umd/parent.js +84 -0
- package/packages/boke-js-bridge/umd/utils.js +18 -0
- package/packages/test/Provider-test.vue +85 -0
- package/pnpm-workspace.yaml +2 -0
- package/scripts/configs.mjs +52 -0
- package/scripts/index.mjs +43 -0
- package/scripts/release.mjs +16 -0
- package/test-setup.js +24 -0
- package/vite.config.ts +21 -0
- package/vitest.config.js +22 -0
@@ -0,0 +1,35 @@
|
|
1
|
+
{
|
2
|
+
"name": "@devops/boke-js-bridge",
|
3
|
+
"version": "0.0.30",
|
4
|
+
"description": "boke-JS-BRIDGE",
|
5
|
+
"keywords": [
|
6
|
+
"modules",
|
7
|
+
"stdlib",
|
8
|
+
"util"
|
9
|
+
],
|
10
|
+
"repository": {
|
11
|
+
"type": "git",
|
12
|
+
"url": "boke-js-bridge"
|
13
|
+
},
|
14
|
+
"license": "MIT",
|
15
|
+
"main": "./es",
|
16
|
+
"author": "Julone Lee <julone@qq.com>",
|
17
|
+
"contributors": [
|
18
|
+
"Julone Lee <julone@qq.com>"
|
19
|
+
],
|
20
|
+
"scripts": {
|
21
|
+
"build": "run-p esm-build umd-build",
|
22
|
+
"esm-build": "tsc -m EsNext --outDir es",
|
23
|
+
"umd-build": "tsc -m UMD --outDir umd"
|
24
|
+
},
|
25
|
+
"types": "./es/index.d.ts",
|
26
|
+
"devDependencies": {
|
27
|
+
"npm-run-all": "^4.1.5"
|
28
|
+
},
|
29
|
+
"homepage": "",
|
30
|
+
"files": [
|
31
|
+
"es",
|
32
|
+
"src",
|
33
|
+
"umd"
|
34
|
+
]
|
35
|
+
}
|
@@ -0,0 +1,174 @@
|
|
1
|
+
import {childLogger} from './utils'
|
2
|
+
interface ChildMethods {
|
3
|
+
/*** 获取授权 */
|
4
|
+
getAuthorization: () => Promise<any>,
|
5
|
+
/*** 获取用户信息 */
|
6
|
+
getUserInfo: () => Promise<any>,
|
7
|
+
/*** 获取自定义事件的数据 */
|
8
|
+
fetchCustomData: (tokenName /*** 获取授权 */: any) => Promise<any>;
|
9
|
+
/*** 触发parent的事件 */
|
10
|
+
triggerEvent: (methodName:string, methodData?: any) => any;
|
11
|
+
/**事件挂载函数到Child */
|
12
|
+
$on: (methodName: string, cb: (data: string) => void) => void,
|
13
|
+
/**事件移除函数到Child */
|
14
|
+
$off: (methodName: string) => void,
|
15
|
+
/**事件挂载函数到Child, 包含Parent回调函数 */
|
16
|
+
$onCallback: (methodName: string, cb: (data: string) => void) => void,
|
17
|
+
/**事件移除函数到Child, 包含Parent回调函数 */
|
18
|
+
$offCallback: (methodName: string) => void,
|
19
|
+
goPage: (path: string, params: any) => void,
|
20
|
+
refreshTag: () => void,
|
21
|
+
/*** 关闭弹窗 */
|
22
|
+
closeModal: () => void,
|
23
|
+
toast: (msg: string) => void,
|
24
|
+
cacheData: any,
|
25
|
+
version: string
|
26
|
+
}
|
27
|
+
|
28
|
+
const ChildHanlder: () => ChildMethods = () => {
|
29
|
+
let methods = {} as ChildMethods;
|
30
|
+
const triggerAppEvent = (eventName, eventData) => {
|
31
|
+
const webviewBridge = window || null;
|
32
|
+
if (!webviewBridge && window.bokeJsBridge) {
|
33
|
+
alert('请在 boke 中运行此页面!');
|
34
|
+
} else {
|
35
|
+
const postdata = {
|
36
|
+
eventName: eventName,
|
37
|
+
eventData: eventData,
|
38
|
+
msgScoped: "boke-JS-BRIDGE"
|
39
|
+
}
|
40
|
+
childLogger("boke-JS-BRIDGE", `postMessage to ParentSdk: ${postdata}`)
|
41
|
+
webviewBridge.parent.postMessage(
|
42
|
+
JSON.stringify(postdata),
|
43
|
+
'*',
|
44
|
+
);
|
45
|
+
}
|
46
|
+
};
|
47
|
+
methods.goPage = (path = '', params = {}) => {
|
48
|
+
triggerAppEvent('goPage', { path, params });
|
49
|
+
};
|
50
|
+
methods.refreshTag = () => {
|
51
|
+
triggerAppEvent('refreshTag', {});
|
52
|
+
};
|
53
|
+
methods.closeModal = () => {
|
54
|
+
triggerAppEvent('closeModal', {});
|
55
|
+
};
|
56
|
+
methods.toast = (text = '你触发了一个提示!') => {
|
57
|
+
triggerAppEvent('toast', text);
|
58
|
+
};
|
59
|
+
methods.$on = (eventName, callback) => {
|
60
|
+
window.messageArray.set(eventName, (eventData) => {
|
61
|
+
callback(eventData);
|
62
|
+
});
|
63
|
+
};
|
64
|
+
|
65
|
+
methods.$off = (eventName) => {
|
66
|
+
window.messageArray.delete(eventName);
|
67
|
+
};
|
68
|
+
methods.$onCallback = (eventName, callback) => {
|
69
|
+
methods.$on(eventName + '@Callback', callback);
|
70
|
+
};
|
71
|
+
methods.$offCallback = (eventName) => {
|
72
|
+
methods.$off(eventName + '@Callback');
|
73
|
+
};
|
74
|
+
methods.cacheData = {};
|
75
|
+
methods.version = '0.0.1'
|
76
|
+
|
77
|
+
const cratePromise = (asyncName, timeout = 1200) => {
|
78
|
+
let result = {
|
79
|
+
status: "pending",
|
80
|
+
data: null,
|
81
|
+
err: null
|
82
|
+
}
|
83
|
+
|
84
|
+
let pro = new Promise((resolve, rejected) => {
|
85
|
+
let autoTimer
|
86
|
+
const callback = (eventData) => {
|
87
|
+
autoTimer && clearTimeout(autoTimer);
|
88
|
+
methods.$offCallback(asyncName);
|
89
|
+
resolve(eventData);
|
90
|
+
}
|
91
|
+
autoTimer = setTimeout(()=> {
|
92
|
+
callback(null)
|
93
|
+
}, timeout) // 1.2秒内没有应答 直接结束.
|
94
|
+
methods.$onCallback(asyncName, callback);
|
95
|
+
triggerAppEvent(asyncName, null);
|
96
|
+
}).then(res => {
|
97
|
+
result.data = res;
|
98
|
+
result.status = "fulfiled";
|
99
|
+
methods.cacheData[asyncName] = result;
|
100
|
+
return result
|
101
|
+
}).catch(e => {
|
102
|
+
result.err = e;
|
103
|
+
result.status = "rejected";
|
104
|
+
methods.cacheData[asyncName] = result;
|
105
|
+
return result
|
106
|
+
})
|
107
|
+
return pro
|
108
|
+
}
|
109
|
+
|
110
|
+
function withAsyncContext(asyncName) {
|
111
|
+
let run = null;
|
112
|
+
try {
|
113
|
+
run = () => {
|
114
|
+
if (methods.cacheData[asyncName]) {
|
115
|
+
if (methods.cacheData[asyncName].status === 'fulfiled') {
|
116
|
+
return methods.cacheData[asyncName].data
|
117
|
+
} else if (methods.cacheData[asyncName].status === 'rejected') {
|
118
|
+
throw methods.cacheData[asyncName].err;
|
119
|
+
}
|
120
|
+
}
|
121
|
+
throw cratePromise(asyncName).then(res => res.data)
|
122
|
+
}
|
123
|
+
return run();
|
124
|
+
} catch (e) {
|
125
|
+
if (e instanceof Promise) {
|
126
|
+
(e as any).finally(done => {
|
127
|
+
throw methods.cacheData[asyncName].data
|
128
|
+
})
|
129
|
+
}
|
130
|
+
}
|
131
|
+
}
|
132
|
+
|
133
|
+
methods.getAuthorization = async () => {
|
134
|
+
return await cratePromise("getAuthorization").then(res => res.data)
|
135
|
+
};
|
136
|
+
methods.getUserInfo = async () => {
|
137
|
+
return await cratePromise("getUserInfo").then(res => res.data)
|
138
|
+
};
|
139
|
+
methods.fetchCustomData = async (tokenName) => {
|
140
|
+
return await cratePromise(tokenName, 3000).then(res => res.data)
|
141
|
+
}
|
142
|
+
methods.triggerEvent=async (eventName, eventArgs)=> {
|
143
|
+
triggerAppEvent(eventName, eventArgs);
|
144
|
+
}
|
145
|
+
if (!window.bokeJsBridge) {
|
146
|
+
window.bokeJsBridge = new Proxy(methods, {
|
147
|
+
get(target, property) {
|
148
|
+
if (property in target) {
|
149
|
+
return target[property];
|
150
|
+
}
|
151
|
+
},
|
152
|
+
});
|
153
|
+
window.messageArray = new Map();
|
154
|
+
window.addEventListener('message', ({ data, source, origin }) => {
|
155
|
+
window.messageArray.forEach((callback, key) => {
|
156
|
+
try {
|
157
|
+
const { eventName, eventData,msgScoped } = JSON.parse(data);
|
158
|
+
if(msgScoped != "boke-JS-BRIDGE") {
|
159
|
+
return; // 非boke的信息不处理
|
160
|
+
}
|
161
|
+
if (eventName == key) {
|
162
|
+
callback(eventData);
|
163
|
+
childLogger("boke-JS-BRIDGE", `postMessage to ChildSdk: ${data}`)
|
164
|
+
}
|
165
|
+
} catch (e) {
|
166
|
+
}
|
167
|
+
});
|
168
|
+
});
|
169
|
+
childLogger("boke-JS-BRIDGE", "ChildSdk is ready")
|
170
|
+
}
|
171
|
+
return window.bokeJsBridge;
|
172
|
+
}
|
173
|
+
|
174
|
+
export default ChildHanlder()
|
@@ -0,0 +1,92 @@
|
|
1
|
+
import {parentLogger} from './utils'
|
2
|
+
(() => {
|
3
|
+
if (!(window.bokeMessageQueue)) {
|
4
|
+
window.bokeMessageQueue = new Map();
|
5
|
+
window.addEventListener("message", (data) => {
|
6
|
+
window.bokeMessageQueue.forEach((cb, key) => {
|
7
|
+
key && cb(data);
|
8
|
+
});
|
9
|
+
});
|
10
|
+
}
|
11
|
+
parentLogger("boke-JS-BRIDGE", "ParentSDK is Ready")
|
12
|
+
return;
|
13
|
+
})()
|
14
|
+
|
15
|
+
interface CommonEvent {
|
16
|
+
[props: string]: (prop: any) => any;
|
17
|
+
}
|
18
|
+
|
19
|
+
interface CallbackEvent {
|
20
|
+
getAuthorization?: (data?: any) => any
|
21
|
+
getUserInfo?: (data?: any) => any
|
22
|
+
[props: string]: (prop?: any) => any;
|
23
|
+
}
|
24
|
+
type ConfigType = {
|
25
|
+
/***自定义事件的具体实现 */
|
26
|
+
events: CommonEvent,
|
27
|
+
/***异步自定义事件的具体实现: 支持async异步方法 */
|
28
|
+
eventsCallback: CallbackEvent,
|
29
|
+
/***自动启动父iframe事件绑定, 默认: 是 */
|
30
|
+
autoStart?: boolean
|
31
|
+
}
|
32
|
+
export default function (iframeTarget: HTMLIFrameElement, configs: ConfigType) {
|
33
|
+
const frameUUID = encodeURIComponent(iframeTarget.src);
|
34
|
+
let { events = {}, eventsCallback = {}, autoStart = true } = configs as any;
|
35
|
+
|
36
|
+
const handler = function (e) {
|
37
|
+
try {
|
38
|
+
const { eventName, eventData, msgScoped } = JSON.parse(e.data);
|
39
|
+
if(msgScoped != "boke-JS-BRIDGE") {
|
40
|
+
return // 没有msgScoped, 不属于sdk传递的消息
|
41
|
+
}
|
42
|
+
Object.keys(events).forEach((key) => {
|
43
|
+
if (eventName == key) {
|
44
|
+
events[key](eventData);
|
45
|
+
parentLogger("boke-JS-BRIDGE", `postMessage to ParentSdk: ${e.data}`)
|
46
|
+
}
|
47
|
+
});
|
48
|
+
|
49
|
+
Object.keys(eventsCallback).forEach( async (key) => {
|
50
|
+
if (eventName == key) {
|
51
|
+
let cbdata = await eventsCallback[key](eventData);
|
52
|
+
let data = JSON.stringify({
|
53
|
+
eventName: eventName + "@Callback",
|
54
|
+
eventData: cbdata,
|
55
|
+
msgScoped: "boke-JS-BRIDGE"
|
56
|
+
});
|
57
|
+
parentLogger("boke-JS-BRIDGE", `callbackListener to ChildSdk: ${data}`)
|
58
|
+
iframeTarget.contentWindow.postMessage(data, "*");
|
59
|
+
}
|
60
|
+
});
|
61
|
+
} catch (error) {
|
62
|
+
}
|
63
|
+
};
|
64
|
+
const methods = {
|
65
|
+
postMessage(eventName, eventData) {
|
66
|
+
const data = JSON.stringify({
|
67
|
+
eventName: eventName,
|
68
|
+
eventData: eventData,
|
69
|
+
msgScoped: "boke-JS-BRIDGE"
|
70
|
+
});
|
71
|
+
parentLogger("boke-JS-BRIDGE", `postMessage to ChildSdk: ${data}`)
|
72
|
+
iframeTarget.contentWindow.postMessage(data, "*");
|
73
|
+
},
|
74
|
+
destory() {
|
75
|
+
window.bokeMessageQueue.delete(frameUUID);
|
76
|
+
},
|
77
|
+
start(){
|
78
|
+
window.bokeMessageQueue.set(frameUUID, handler);
|
79
|
+
},
|
80
|
+
on(eventName, cb) {
|
81
|
+
events[eventName] = cb;
|
82
|
+
},
|
83
|
+
off(key) {
|
84
|
+
delete events[key];
|
85
|
+
},
|
86
|
+
}
|
87
|
+
autoStart && methods.start();
|
88
|
+
return {
|
89
|
+
...methods
|
90
|
+
}
|
91
|
+
|
92
|
+
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
|
2
|
+
export const parentLogger = (title, msg)=> {
|
3
|
+
console.log(
|
4
|
+
` %c ${title} %c ${msg} `,
|
5
|
+
'color: #fff; background: #4B60CF; padding:5px 0;',
|
6
|
+
'background: #030307; color: #fff; padding:5px 0;word-break:break-all'
|
7
|
+
)
|
8
|
+
}
|
9
|
+
export const childLogger = (title, msg)=> {
|
10
|
+
console.log(
|
11
|
+
` %c ${title} %c ${msg}`,
|
12
|
+
'color: #fa8c16; background: #fff7e6; padding:5px 0;',
|
13
|
+
'background: #030307; color: #fff; padding:5px 0;word-break:break-all'
|
14
|
+
)
|
15
|
+
}
|
@@ -0,0 +1,26 @@
|
|
1
|
+
{
|
2
|
+
"compilerOptions": {
|
3
|
+
"target": "ES2015",
|
4
|
+
"module": "ESNext",
|
5
|
+
"moduleResolution": "node",
|
6
|
+
"importHelpers": true,
|
7
|
+
"esModuleInterop": true,
|
8
|
+
"sourceMap": false,
|
9
|
+
"baseUrl": "./",
|
10
|
+
"strict": false,
|
11
|
+
"paths": {
|
12
|
+
"@/*": ["./src/*"]
|
13
|
+
},
|
14
|
+
"allowSyntheticDefaultImports": true,
|
15
|
+
"outDir": "./es",
|
16
|
+
"declarationDir": "./es",
|
17
|
+
"declaration": true,
|
18
|
+
"rootDirs": ["."]
|
19
|
+
},
|
20
|
+
"include": [
|
21
|
+
"./src/**/*",
|
22
|
+
"typings.d.ts"
|
23
|
+
],
|
24
|
+
"exclude": ["./../../node_modules"]
|
25
|
+
}
|
26
|
+
|
@@ -0,0 +1,156 @@
|
|
1
|
+
(function (factory) {
|
2
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
3
|
+
var v = factory(require, exports);
|
4
|
+
if (v !== undefined) module.exports = v;
|
5
|
+
}
|
6
|
+
else if (typeof define === "function" && define.amd) {
|
7
|
+
define(["require", "exports", "tslib", "./utils"], factory);
|
8
|
+
}
|
9
|
+
})(function (require, exports) {
|
10
|
+
"use strict";
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
+
const tslib_1 = require("tslib");
|
13
|
+
const utils_1 = require("./utils");
|
14
|
+
const ChildHanlder = () => {
|
15
|
+
let methods = {};
|
16
|
+
const triggerAppEvent = (eventName, eventData) => {
|
17
|
+
const webviewBridge = window || null;
|
18
|
+
if (!webviewBridge && window.bokeJsBridge) {
|
19
|
+
alert('请在 boke 中运行此页面!');
|
20
|
+
}
|
21
|
+
else {
|
22
|
+
const postdata = {
|
23
|
+
eventName: eventName,
|
24
|
+
eventData: eventData,
|
25
|
+
msgScoped: "boke-JS-BRIDGE"
|
26
|
+
};
|
27
|
+
utils_1.childLogger("boke-JS-BRIDGE", `postMessage to ParentSdk: ${postdata}`);
|
28
|
+
webviewBridge.parent.postMessage(JSON.stringify(postdata), '*');
|
29
|
+
}
|
30
|
+
};
|
31
|
+
methods.goPage = (path = '', params = {}) => {
|
32
|
+
triggerAppEvent('goPage', { path, params });
|
33
|
+
};
|
34
|
+
methods.refreshTag = () => {
|
35
|
+
triggerAppEvent('refreshTag', {});
|
36
|
+
};
|
37
|
+
methods.closeModal = () => {
|
38
|
+
triggerAppEvent('closeModal', {});
|
39
|
+
};
|
40
|
+
methods.toast = (text = '你触发了一个提示!') => {
|
41
|
+
triggerAppEvent('toast', text);
|
42
|
+
};
|
43
|
+
methods.$on = (eventName, callback) => {
|
44
|
+
window.messageArray.set(eventName, (eventData) => {
|
45
|
+
callback(eventData);
|
46
|
+
});
|
47
|
+
};
|
48
|
+
methods.$off = (eventName) => {
|
49
|
+
window.messageArray.delete(eventName);
|
50
|
+
};
|
51
|
+
methods.$onCallback = (eventName, callback) => {
|
52
|
+
methods.$on(eventName + '@Callback', callback);
|
53
|
+
};
|
54
|
+
methods.$offCallback = (eventName) => {
|
55
|
+
methods.$off(eventName + '@Callback');
|
56
|
+
};
|
57
|
+
methods.cacheData = {};
|
58
|
+
methods.version = '0.0.1';
|
59
|
+
const cratePromise = (asyncName, timeout = 1200) => {
|
60
|
+
let result = {
|
61
|
+
status: "pending",
|
62
|
+
data: null,
|
63
|
+
err: null
|
64
|
+
};
|
65
|
+
let pro = new Promise((resolve, rejected) => {
|
66
|
+
let autoTimer;
|
67
|
+
const callback = (eventData) => {
|
68
|
+
autoTimer && clearTimeout(autoTimer);
|
69
|
+
methods.$offCallback(asyncName);
|
70
|
+
resolve(eventData);
|
71
|
+
};
|
72
|
+
autoTimer = setTimeout(() => {
|
73
|
+
callback(null);
|
74
|
+
}, timeout); // 1.2秒内没有应答 直接结束.
|
75
|
+
methods.$onCallback(asyncName, callback);
|
76
|
+
triggerAppEvent(asyncName, null);
|
77
|
+
}).then(res => {
|
78
|
+
result.data = res;
|
79
|
+
result.status = "fulfiled";
|
80
|
+
methods.cacheData[asyncName] = result;
|
81
|
+
return result;
|
82
|
+
}).catch(e => {
|
83
|
+
result.err = e;
|
84
|
+
result.status = "rejected";
|
85
|
+
methods.cacheData[asyncName] = result;
|
86
|
+
return result;
|
87
|
+
});
|
88
|
+
return pro;
|
89
|
+
};
|
90
|
+
function withAsyncContext(asyncName) {
|
91
|
+
let run = null;
|
92
|
+
try {
|
93
|
+
run = () => {
|
94
|
+
if (methods.cacheData[asyncName]) {
|
95
|
+
if (methods.cacheData[asyncName].status === 'fulfiled') {
|
96
|
+
return methods.cacheData[asyncName].data;
|
97
|
+
}
|
98
|
+
else if (methods.cacheData[asyncName].status === 'rejected') {
|
99
|
+
throw methods.cacheData[asyncName].err;
|
100
|
+
}
|
101
|
+
}
|
102
|
+
throw cratePromise(asyncName).then(res => res.data);
|
103
|
+
};
|
104
|
+
return run();
|
105
|
+
}
|
106
|
+
catch (e) {
|
107
|
+
if (e instanceof Promise) {
|
108
|
+
e.finally(done => {
|
109
|
+
throw methods.cacheData[asyncName].data;
|
110
|
+
});
|
111
|
+
}
|
112
|
+
}
|
113
|
+
}
|
114
|
+
methods.getAuthorization = () => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
115
|
+
return yield cratePromise("getAuthorization").then(res => res.data);
|
116
|
+
});
|
117
|
+
methods.getUserInfo = () => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
118
|
+
return yield cratePromise("getUserInfo").then(res => res.data);
|
119
|
+
});
|
120
|
+
methods.fetchCustomData = (tokenName) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
121
|
+
return yield cratePromise(tokenName, 3000).then(res => res.data);
|
122
|
+
});
|
123
|
+
methods.triggerEvent = (eventName, eventArgs) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
124
|
+
triggerAppEvent(eventName, eventArgs);
|
125
|
+
});
|
126
|
+
if (!window.bokeJsBridge) {
|
127
|
+
window.bokeJsBridge = new Proxy(methods, {
|
128
|
+
get(target, property) {
|
129
|
+
if (property in target) {
|
130
|
+
return target[property];
|
131
|
+
}
|
132
|
+
},
|
133
|
+
});
|
134
|
+
window.messageArray = new Map();
|
135
|
+
window.addEventListener('message', ({ data, source, origin }) => {
|
136
|
+
window.messageArray.forEach((callback, key) => {
|
137
|
+
try {
|
138
|
+
const { eventName, eventData, msgScoped } = JSON.parse(data);
|
139
|
+
if (msgScoped != "boke-JS-BRIDGE") {
|
140
|
+
return; // 非boke的信息不处理
|
141
|
+
}
|
142
|
+
if (eventName == key) {
|
143
|
+
callback(eventData);
|
144
|
+
utils_1.childLogger("boke-JS-BRIDGE", `postMessage to ChildSdk: ${data}`);
|
145
|
+
}
|
146
|
+
}
|
147
|
+
catch (e) {
|
148
|
+
}
|
149
|
+
});
|
150
|
+
});
|
151
|
+
utils_1.childLogger("boke-JS-BRIDGE", "ChildSdk is ready");
|
152
|
+
}
|
153
|
+
return window.bokeJsBridge;
|
154
|
+
};
|
155
|
+
exports.default = ChildHanlder();
|
156
|
+
});
|
@@ -0,0 +1,18 @@
|
|
1
|
+
(function (factory) {
|
2
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
3
|
+
var v = factory(require, exports);
|
4
|
+
if (v !== undefined) module.exports = v;
|
5
|
+
}
|
6
|
+
else if (typeof define === "function" && define.amd) {
|
7
|
+
define(["require", "exports", "tslib", "./parent", "./child", "./utils"], factory);
|
8
|
+
}
|
9
|
+
})(function (require, exports) {
|
10
|
+
"use strict";
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
+
const tslib_1 = require("tslib");
|
13
|
+
var parent_1 = require("./parent");
|
14
|
+
exports.bokeJsBridgeParent = parent_1.default;
|
15
|
+
var child_1 = require("./child");
|
16
|
+
exports.bokeJsBridgeChild = child_1.default;
|
17
|
+
tslib_1.__exportStar(require("./utils"), exports);
|
18
|
+
});
|
@@ -0,0 +1,84 @@
|
|
1
|
+
(function (factory) {
|
2
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
3
|
+
var v = factory(require, exports);
|
4
|
+
if (v !== undefined) module.exports = v;
|
5
|
+
}
|
6
|
+
else if (typeof define === "function" && define.amd) {
|
7
|
+
define(["require", "exports", "tslib", "./utils"], factory);
|
8
|
+
}
|
9
|
+
})(function (require, exports) {
|
10
|
+
"use strict";
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
+
const tslib_1 = require("tslib");
|
13
|
+
const utils_1 = require("./utils");
|
14
|
+
(() => {
|
15
|
+
if (!(window.bokeMessageQueue)) {
|
16
|
+
window.bokeMessageQueue = new Map();
|
17
|
+
window.addEventListener("message", (data) => {
|
18
|
+
window.bokeMessageQueue.forEach((cb, key) => {
|
19
|
+
key && cb(data);
|
20
|
+
});
|
21
|
+
});
|
22
|
+
}
|
23
|
+
utils_1.parentLogger("boke-JS-BRIDGE", "ParentSDK is Ready");
|
24
|
+
return;
|
25
|
+
})();
|
26
|
+
function default_1(iframeTarget, configs) {
|
27
|
+
const frameUUID = encodeURIComponent(iframeTarget.src);
|
28
|
+
let { events = {}, eventsCallback = {}, autoStart = true } = configs;
|
29
|
+
const handler = function (e) {
|
30
|
+
try {
|
31
|
+
const { eventName, eventData, msgScoped } = JSON.parse(e.data);
|
32
|
+
if (msgScoped != "boke-JS-BRIDGE") {
|
33
|
+
return; // 没有msgScoped, 不属于sdk传递的消息
|
34
|
+
}
|
35
|
+
Object.keys(events).forEach((key) => {
|
36
|
+
if (eventName == key) {
|
37
|
+
events[key](eventData);
|
38
|
+
utils_1.parentLogger("boke-JS-BRIDGE", `postMessage to ParentSdk: ${e.data}`);
|
39
|
+
}
|
40
|
+
});
|
41
|
+
Object.keys(eventsCallback).forEach((key) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
42
|
+
if (eventName == key) {
|
43
|
+
let cbdata = yield eventsCallback[key](eventData);
|
44
|
+
let data = JSON.stringify({
|
45
|
+
eventName: eventName + "@Callback",
|
46
|
+
eventData: cbdata,
|
47
|
+
msgScoped: "boke-JS-BRIDGE"
|
48
|
+
});
|
49
|
+
utils_1.parentLogger("boke-JS-BRIDGE", `callbackListener to ChildSdk: ${data}`);
|
50
|
+
iframeTarget.contentWindow.postMessage(data, "*");
|
51
|
+
}
|
52
|
+
}));
|
53
|
+
}
|
54
|
+
catch (error) {
|
55
|
+
}
|
56
|
+
};
|
57
|
+
const methods = {
|
58
|
+
postMessage(eventName, eventData) {
|
59
|
+
const data = JSON.stringify({
|
60
|
+
eventName: eventName,
|
61
|
+
eventData: eventData,
|
62
|
+
msgScoped: "boke-JS-BRIDGE"
|
63
|
+
});
|
64
|
+
utils_1.parentLogger("boke-JS-BRIDGE", `postMessage to ChildSdk: ${data}`);
|
65
|
+
iframeTarget.contentWindow.postMessage(data, "*");
|
66
|
+
},
|
67
|
+
destory() {
|
68
|
+
window.bokeMessageQueue.delete(frameUUID);
|
69
|
+
},
|
70
|
+
start() {
|
71
|
+
window.bokeMessageQueue.set(frameUUID, handler);
|
72
|
+
},
|
73
|
+
on(eventName, cb) {
|
74
|
+
events[eventName] = cb;
|
75
|
+
},
|
76
|
+
off(key) {
|
77
|
+
delete events[key];
|
78
|
+
},
|
79
|
+
};
|
80
|
+
autoStart && methods.start();
|
81
|
+
return Object.assign({}, methods);
|
82
|
+
}
|
83
|
+
exports.default = default_1;
|
84
|
+
});
|
@@ -0,0 +1,18 @@
|
|
1
|
+
(function (factory) {
|
2
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
3
|
+
var v = factory(require, exports);
|
4
|
+
if (v !== undefined) module.exports = v;
|
5
|
+
}
|
6
|
+
else if (typeof define === "function" && define.amd) {
|
7
|
+
define(["require", "exports"], factory);
|
8
|
+
}
|
9
|
+
})(function (require, exports) {
|
10
|
+
"use strict";
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
+
exports.parentLogger = (title, msg) => {
|
13
|
+
console.log(` %c ${title} %c ${msg} `, 'color: #fff; background: #4B60CF; padding:5px 0;', 'background: #030307; color: #fff; padding:5px 0;word-break:break-all');
|
14
|
+
};
|
15
|
+
exports.childLogger = (title, msg) => {
|
16
|
+
console.log(` %c ${title} %c ${msg}`, 'color: #fa8c16; background: #fff7e6; padding:5px 0;', 'background: #030307; color: #fff; padding:5px 0;word-break:break-all');
|
17
|
+
};
|
18
|
+
});
|