@hylid/call 3.2.0-alpha.0 → 3.2.0-alpha.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/lib/mpWebCall.d.ts +11 -5
- package/lib/mpWebCall.js +90 -108
- package/lib/types.d.ts +24 -5
- package/lib/webviewBridge.d.ts +19 -5
- package/lib/webviewBridge.js +138 -191
- package/package.json +3 -3
package/lib/mpWebCall.d.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { MpWebJsApiConfig } from './types';
|
|
3
|
-
|
|
4
|
-
export declare
|
|
5
|
-
export {
|
|
1
|
+
import { MPApi } from '@hylid/types';
|
|
2
|
+
import { MpWebJsApiConfig, MpWebJsApiOptions } from './types';
|
|
3
|
+
import { mpWebOnMessage, mpWebPostMessage } from './mpWebBridge';
|
|
4
|
+
export declare function mpWebCall<T extends keyof MPApi>(api: T | ({} & string), options?: MpWebJsApiOptions<T>, config?: MpWebJsApiConfig): void;
|
|
5
|
+
export declare namespace mpWebCall {
|
|
6
|
+
var onMessage: typeof mpWebOnMessage;
|
|
7
|
+
var postMessage: typeof mpWebPostMessage;
|
|
8
|
+
var broadcastGlobalData: (key: string, payload: any) => void;
|
|
9
|
+
var onReceiveGlobalData: (key: string, callback: (data: any, form: string) => void) => () => void;
|
|
10
|
+
var onPageEvent: (event: ({} & string) | "onShow" | "onHide" | "onTitleClick" | "onOptionMenuClick" | "onTabItemTap", callback: (data: any) => void) => () => void;
|
|
11
|
+
}
|
package/lib/mpWebCall.js
CHANGED
|
@@ -1,118 +1,100 @@
|
|
|
1
|
-
|
|
1
|
+
import { mpWebOnMessage, mpWebPostMessage } from "./mpWebBridge";
|
|
2
|
+
// 关联 postMessage 和 onMessage,根据 serialId 执行回调
|
|
3
|
+
var callMap = {};
|
|
4
|
+
// 注册 jsapi 的 message 处理器
|
|
5
|
+
mpWebOnMessage(function (msg) {
|
|
2
6
|
var _a;
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
cb && cb("loaded" /* BridgeReady.LOADED */);
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
var head = document.getElementsByTagName('head')[0] || document.body;
|
|
24
|
-
head.appendChild(script_1);
|
|
25
|
-
} catch (e) {
|
|
26
|
-
cb && cb(null);
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
var callbackFn = function callbackFn(res, callback) {
|
|
30
|
-
var _a = callback || {},
|
|
31
|
-
success = _a.success,
|
|
32
|
-
fail = _a.fail,
|
|
33
|
-
complete = _a.complete;
|
|
34
|
-
var _b = res || {},
|
|
35
|
-
_data_ = _b._data_,
|
|
36
|
-
_type_ = _b._type_;
|
|
7
|
+
var message = msg;
|
|
8
|
+
var serialId = message.serialId;
|
|
9
|
+
// 不是 jsapi 的调用的消息
|
|
10
|
+
if (message.type !== 'apiCall') return;
|
|
11
|
+
if (!serialId) return;
|
|
12
|
+
if (!callMap[serialId]) return;
|
|
13
|
+
var callback = callMap[serialId];
|
|
14
|
+
var data = message.result;
|
|
15
|
+
var type = (_a = message.config) === null || _a === void 0 ? void 0 : _a.type; // JsApi 的类型
|
|
16
|
+
if (type === 'callback') return callback(data);
|
|
17
|
+
var _b = callback || {},
|
|
18
|
+
success = _b.success,
|
|
19
|
+
fail = _b.fail,
|
|
20
|
+
complete = _b.complete;
|
|
21
|
+
var _c = data || {},
|
|
22
|
+
_data_ = _c._data_,
|
|
23
|
+
_type_ = _c._type_;
|
|
37
24
|
// 新版本协议
|
|
38
25
|
if (_type_) {
|
|
39
26
|
if (_type_ === 'success') success === null || success === void 0 ? void 0 : success(_data_);
|
|
40
27
|
if (_type_ === 'fail') fail === null || fail === void 0 ? void 0 : fail(_data_);
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
// 下面兼容老版本(老版本消息识别不明确,导致 H5 中走 success/fail 逻辑可能岔了)
|
|
44
|
-
// @ts-ignore
|
|
45
|
-
if (res === null || res === void 0 ? void 0 : res.error) {
|
|
46
|
-
fail === null || fail === void 0 ? void 0 : fail(res);
|
|
28
|
+
complete === null || complete === void 0 ? void 0 : complete(_data_);
|
|
47
29
|
} else {
|
|
48
|
-
|
|
30
|
+
// 下面兼容老版本(老版本消息识别不明确,导致 H5 中走 success/fail 逻辑可能岔了)
|
|
31
|
+
// @ts-ignore
|
|
32
|
+
if (data === null || data === void 0 ? void 0 : data.error) {
|
|
33
|
+
fail === null || fail === void 0 ? void 0 : fail(data);
|
|
34
|
+
} else {
|
|
35
|
+
success === null || success === void 0 ? void 0 : success(data);
|
|
36
|
+
}
|
|
37
|
+
complete === null || complete === void 0 ? void 0 : complete(data);
|
|
49
38
|
}
|
|
50
|
-
|
|
51
|
-
};
|
|
52
|
-
export
|
|
53
|
-
var
|
|
54
|
-
var
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
window.my.onMessage = function (message) {
|
|
59
|
-
var _a;
|
|
60
|
-
var serialId = message.serialId;
|
|
61
|
-
var data = message.result;
|
|
62
|
-
var type = (_a = message.config) === null || _a === void 0 ? void 0 : _a.type; // JsApi 的类型
|
|
63
|
-
if (serialId) {
|
|
64
|
-
var callback = callMap[serialId];
|
|
65
|
-
if (!callback) return;
|
|
66
|
-
if (type === 'callback') {
|
|
67
|
-
callback(data);
|
|
68
|
-
} else {
|
|
69
|
-
callbackFn(data, callback);
|
|
70
|
-
delete callMap[serialId];
|
|
71
|
-
}
|
|
72
|
-
} else if (messageHandler) {
|
|
73
|
-
messageHandler(message);
|
|
74
|
-
}
|
|
75
|
-
};
|
|
39
|
+
delete callMap[serialId];
|
|
40
|
+
});
|
|
41
|
+
export function mpWebCall(api, options, config) {
|
|
42
|
+
var random = Math.floor(Math.random() * 1000000);
|
|
43
|
+
var serialId = api + '_' + random;
|
|
44
|
+
// 只调用,没有参数,不需要接收 jsapi 执行结果
|
|
45
|
+
if (options) {
|
|
46
|
+
callMap[serialId] = options;
|
|
76
47
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
48
|
+
var message = {
|
|
49
|
+
source: 'hylid',
|
|
50
|
+
type: 'apiCall',
|
|
51
|
+
api: api,
|
|
52
|
+
serialId: serialId,
|
|
53
|
+
options: options,
|
|
54
|
+
config: config
|
|
55
|
+
};
|
|
56
|
+
mpWebPostMessage(message);
|
|
57
|
+
}
|
|
58
|
+
function broadcastGlobalData(key, payload) {
|
|
59
|
+
var message = {
|
|
60
|
+
source: 'hylid',
|
|
61
|
+
type: 'broadcast',
|
|
62
|
+
from: window.location.href,
|
|
63
|
+
result: {
|
|
64
|
+
key: key,
|
|
65
|
+
payload: payload
|
|
85
66
|
}
|
|
86
|
-
var params = {
|
|
87
|
-
source: 'hylid',
|
|
88
|
-
type: 'apiCall',
|
|
89
|
-
api: api,
|
|
90
|
-
serialId: serialId,
|
|
91
|
-
options: options,
|
|
92
|
-
config: config
|
|
93
|
-
};
|
|
94
|
-
window.my.postMessage(params);
|
|
95
|
-
}
|
|
96
|
-
return function call(api, options, config) {
|
|
97
|
-
loadAppxBridge(function (bridgeReady) {
|
|
98
|
-
var params = {
|
|
99
|
-
api: api,
|
|
100
|
-
options: options,
|
|
101
|
-
config: config
|
|
102
|
-
};
|
|
103
|
-
if (bridgeReady) {
|
|
104
|
-
if (!isListening) {
|
|
105
|
-
onMessage();
|
|
106
|
-
isListening = true;
|
|
107
|
-
}
|
|
108
|
-
if (WAITING_QUEUE.length) {
|
|
109
|
-
WAITING_QUEUE.forEach(fireMessage);
|
|
110
|
-
WAITING_QUEUE = [];
|
|
111
|
-
}
|
|
112
|
-
fireMessage(params);
|
|
113
|
-
} else {
|
|
114
|
-
WAITING_QUEUE.push(params);
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
67
|
};
|
|
118
|
-
|
|
68
|
+
mpWebPostMessage(message);
|
|
69
|
+
}
|
|
70
|
+
function onReceiveGlobalData(key, callback) {
|
|
71
|
+
return mpWebOnMessage(function (msg) {
|
|
72
|
+
var _a, _b;
|
|
73
|
+
var message = msg;
|
|
74
|
+
if (message.source !== 'hylid') return;
|
|
75
|
+
if (message.type !== 'broadcast') return;
|
|
76
|
+
// 不是接收的目标消息
|
|
77
|
+
if (((_a = message.result) === null || _a === void 0 ? void 0 : _a.key) !== key) return;
|
|
78
|
+
callback((_b = message.result) === null || _b === void 0 ? void 0 : _b.payload, message.from);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
function onPageEvent(event, callback) {
|
|
82
|
+
// 告诉小程序侧,监听 event 事件
|
|
83
|
+
mpWebPostMessage({
|
|
84
|
+
source: 'hylid',
|
|
85
|
+
type: 'pageEvent',
|
|
86
|
+
event: event
|
|
87
|
+
});
|
|
88
|
+
return mpWebOnMessage(function (msg) {
|
|
89
|
+
var message = msg;
|
|
90
|
+
if (message.source !== 'hylid') return;
|
|
91
|
+
if (message.type !== 'pageEvent') return;
|
|
92
|
+
if (message.event !== event) return;
|
|
93
|
+
callback(message.result);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
mpWebCall.onMessage = mpWebOnMessage;
|
|
97
|
+
mpWebCall.postMessage = mpWebPostMessage;
|
|
98
|
+
mpWebCall.broadcastGlobalData = broadcastGlobalData;
|
|
99
|
+
mpWebCall.onReceiveGlobalData = onReceiveGlobalData;
|
|
100
|
+
mpWebCall.onPageEvent = onPageEvent;
|
package/lib/types.d.ts
CHANGED
|
@@ -1,20 +1,39 @@
|
|
|
1
|
-
import { JsApiType, MPApi } from '@hylid/types';
|
|
1
|
+
import { JsApiType, MPApi, PickMPArgs, PickMpReturns } from '@hylid/types';
|
|
2
2
|
export declare type JsApi = keyof MPApi;
|
|
3
3
|
export interface MpWebJsApiConfig {
|
|
4
4
|
type?: JsApiType;
|
|
5
5
|
}
|
|
6
6
|
export declare type Config = MpWebJsApiConfig;
|
|
7
|
-
export interface
|
|
7
|
+
export interface MpWebJsApiMessageResult {
|
|
8
8
|
_type_: 'success' | 'fail';
|
|
9
9
|
_data_: any;
|
|
10
10
|
}
|
|
11
|
-
export interface
|
|
11
|
+
export interface MpWebJsApiMessage {
|
|
12
12
|
source: 'hylid';
|
|
13
13
|
type: 'apiCall';
|
|
14
14
|
api: string;
|
|
15
15
|
serialId: string;
|
|
16
16
|
options: any;
|
|
17
17
|
config?: MpWebJsApiConfig;
|
|
18
|
-
result?:
|
|
18
|
+
result?: MpWebJsApiMessageResult;
|
|
19
19
|
}
|
|
20
|
-
export
|
|
20
|
+
export interface MpWebBroadcastMessage {
|
|
21
|
+
source: 'hylid';
|
|
22
|
+
type: 'broadcast';
|
|
23
|
+
from: string;
|
|
24
|
+
result?: {
|
|
25
|
+
key: string;
|
|
26
|
+
payload: any;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export interface MpWebPageEventMessage {
|
|
30
|
+
source: 'hylid';
|
|
31
|
+
type: 'pageEvent';
|
|
32
|
+
event: 'onShow' | 'onHide' | 'onTitleClick' | 'onOptionMenuClick' | 'onTabItemTap' | ({} & string);
|
|
33
|
+
result?: any;
|
|
34
|
+
}
|
|
35
|
+
export declare type Message = MpWebJsApiMessage | MpWebBroadcastMessage | MpWebPageEventMessage;
|
|
36
|
+
export declare type MessageHandler = (message: Message) => void;
|
|
37
|
+
declare type Callback<T> = (result?: T) => void;
|
|
38
|
+
export declare type MpWebJsApiOptions<T extends keyof MPApi> = PickMPArgs<T> | Callback<PickMpReturns<T>>;
|
|
39
|
+
export {};
|
package/lib/webviewBridge.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
/// <reference types="miniprogram" />
|
|
2
|
-
import {
|
|
2
|
+
import { Message, MessageHandler, MpWebBroadcastMessage, MpWebJsApiMessage, MpWebPageEventMessage } from './types';
|
|
3
3
|
interface Config {
|
|
4
|
+
/** 允许调用 jsApi 的白名单 */
|
|
4
5
|
whitelist?: string[];
|
|
6
|
+
/** 禁止调用 jsApi 的黑名单 */
|
|
5
7
|
blacklist?: string[];
|
|
8
|
+
/** 自定义 jsApi [ 优先级比较高,可以覆盖原生的 jsApi ] */
|
|
6
9
|
customApi?: CustomApi;
|
|
7
10
|
}
|
|
8
11
|
interface CustomApi {
|
|
@@ -14,11 +17,22 @@ interface CustomApi {
|
|
|
14
17
|
export declare class WebViewBridge {
|
|
15
18
|
webview: WebViewContext;
|
|
16
19
|
config: Config;
|
|
17
|
-
|
|
20
|
+
context: any;
|
|
21
|
+
MESSAGE_HANDLER: MessageHandler[];
|
|
22
|
+
/**
|
|
23
|
+
*
|
|
24
|
+
* @param webviewId webview 组件的 id,可传字符串,或者 my.createWebViewContext 创建出的实例
|
|
25
|
+
* @param config Config 配置
|
|
26
|
+
* @param context 页面上下文,传 this 即可。传入后,H5 侧可以使用广播、可以监听页面事件
|
|
27
|
+
*/
|
|
28
|
+
constructor(webviewId: string | WebViewContext, config?: Config, context?: any);
|
|
18
29
|
listen: (data: {
|
|
19
|
-
detail:
|
|
20
|
-
}) =>
|
|
30
|
+
detail: Message;
|
|
31
|
+
}) => void;
|
|
21
32
|
isAllowed(api: string): boolean;
|
|
22
|
-
|
|
33
|
+
jsApiHandler: (command: MpWebJsApiMessage) => void;
|
|
34
|
+
broadcastHandler: (data: MpWebBroadcastMessage) => void;
|
|
35
|
+
pageEventHandler: (data: MpWebPageEventMessage) => void;
|
|
36
|
+
bindCtxEvent(event: string, callback: (...rest: any) => void): void;
|
|
23
37
|
}
|
|
24
38
|
export {};
|
package/lib/webviewBridge.js
CHANGED
|
@@ -8,222 +8,169 @@ var __assign = this && this.__assign || function () {
|
|
|
8
8
|
};
|
|
9
9
|
return __assign.apply(this, arguments);
|
|
10
10
|
};
|
|
11
|
-
var
|
|
12
|
-
function adopt(value) {
|
|
13
|
-
return value instanceof P ? value : new P(function (resolve) {
|
|
14
|
-
resolve(value);
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
18
|
-
function fulfilled(value) {
|
|
19
|
-
try {
|
|
20
|
-
step(generator.next(value));
|
|
21
|
-
} catch (e) {
|
|
22
|
-
reject(e);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
function rejected(value) {
|
|
26
|
-
try {
|
|
27
|
-
step(generator["throw"](value));
|
|
28
|
-
} catch (e) {
|
|
29
|
-
reject(e);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
function step(result) {
|
|
33
|
-
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
|
34
|
-
}
|
|
35
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
36
|
-
});
|
|
37
|
-
};
|
|
38
|
-
var __generator = this && this.__generator || function (thisArg, body) {
|
|
39
|
-
var _ = {
|
|
40
|
-
label: 0,
|
|
41
|
-
sent: function sent() {
|
|
42
|
-
if (t[0] & 1) throw t[1];
|
|
43
|
-
return t[1];
|
|
44
|
-
},
|
|
45
|
-
trys: [],
|
|
46
|
-
ops: []
|
|
47
|
-
},
|
|
48
|
-
f,
|
|
49
|
-
y,
|
|
50
|
-
t,
|
|
51
|
-
g;
|
|
52
|
-
return g = {
|
|
53
|
-
next: verb(0),
|
|
54
|
-
"throw": verb(1),
|
|
55
|
-
"return": verb(2)
|
|
56
|
-
}, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
|
|
57
|
-
return this;
|
|
58
|
-
}), g;
|
|
59
|
-
function verb(n) {
|
|
60
|
-
return function (v) {
|
|
61
|
-
return step([n, v]);
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
function step(op) {
|
|
65
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
66
|
-
while (_) try {
|
|
67
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
68
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
69
|
-
switch (op[0]) {
|
|
70
|
-
case 0:
|
|
71
|
-
case 1:
|
|
72
|
-
t = op;
|
|
73
|
-
break;
|
|
74
|
-
case 4:
|
|
75
|
-
_.label++;
|
|
76
|
-
return {
|
|
77
|
-
value: op[1],
|
|
78
|
-
done: false
|
|
79
|
-
};
|
|
80
|
-
case 5:
|
|
81
|
-
_.label++;
|
|
82
|
-
y = op[1];
|
|
83
|
-
op = [0];
|
|
84
|
-
continue;
|
|
85
|
-
case 7:
|
|
86
|
-
op = _.ops.pop();
|
|
87
|
-
_.trys.pop();
|
|
88
|
-
continue;
|
|
89
|
-
default:
|
|
90
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
91
|
-
_ = 0;
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
95
|
-
_.label = op[1];
|
|
96
|
-
break;
|
|
97
|
-
}
|
|
98
|
-
if (op[0] === 6 && _.label < t[1]) {
|
|
99
|
-
_.label = t[1];
|
|
100
|
-
t = op;
|
|
101
|
-
break;
|
|
102
|
-
}
|
|
103
|
-
if (t && _.label < t[2]) {
|
|
104
|
-
_.label = t[2];
|
|
105
|
-
_.ops.push(op);
|
|
106
|
-
break;
|
|
107
|
-
}
|
|
108
|
-
if (t[2]) _.ops.pop();
|
|
109
|
-
_.trys.pop();
|
|
110
|
-
continue;
|
|
111
|
-
}
|
|
112
|
-
op = body.call(thisArg, _);
|
|
113
|
-
} catch (e) {
|
|
114
|
-
op = [6, e];
|
|
115
|
-
y = 0;
|
|
116
|
-
} finally {
|
|
117
|
-
f = t = 0;
|
|
118
|
-
}
|
|
119
|
-
if (op[0] & 5) throw op[1];
|
|
120
|
-
return {
|
|
121
|
-
value: op[0] ? op[1] : void 0,
|
|
122
|
-
done: true
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
};
|
|
11
|
+
var broadcastTargets = [];
|
|
126
12
|
var WebViewBridge = /** @class */function () {
|
|
127
|
-
|
|
13
|
+
/**
|
|
14
|
+
*
|
|
15
|
+
* @param webviewId webview 组件的 id,可传字符串,或者 my.createWebViewContext 创建出的实例
|
|
16
|
+
* @param config Config 配置
|
|
17
|
+
* @param context 页面上下文,传 this 即可。传入后,H5 侧可以使用广播、可以监听页面事件
|
|
18
|
+
*/
|
|
19
|
+
function WebViewBridge(webviewId, config, context) {
|
|
128
20
|
if (config === void 0) {
|
|
129
21
|
config = {};
|
|
130
22
|
}
|
|
131
23
|
var _this = this;
|
|
132
24
|
this.config = {};
|
|
25
|
+
this.MESSAGE_HANDLER = [];
|
|
133
26
|
this.listen = function (data) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
27
|
+
if (data.detail.source !== 'hylid') return;
|
|
28
|
+
_this.MESSAGE_HANDLER.forEach(function (fn) {
|
|
29
|
+
return fn(data.detail);
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
this.jsApiHandler = function (command) {
|
|
33
|
+
if (command.type !== 'apiCall') return;
|
|
34
|
+
var getApiResult = function getApiResult(data) {
|
|
35
|
+
var api = data.api,
|
|
36
|
+
_options = data.options,
|
|
37
|
+
config = data.config;
|
|
38
|
+
var _a = (config || {}).type,
|
|
39
|
+
type = _a === void 0 ? 'async' : _a;
|
|
40
|
+
return new Promise(function (resolve) {
|
|
41
|
+
var _a;
|
|
42
|
+
var options = __assign(__assign({}, _options), {
|
|
43
|
+
success: function success(res) {
|
|
44
|
+
resolve({
|
|
45
|
+
_type_: 'success',
|
|
46
|
+
_data_: res
|
|
47
|
+
});
|
|
48
|
+
},
|
|
49
|
+
fail: function fail(res) {
|
|
50
|
+
resolve({
|
|
51
|
+
_type_: 'fail',
|
|
52
|
+
_data_: res
|
|
152
53
|
});
|
|
153
|
-
this.webview.postMessage(notAllowedMessage);
|
|
154
|
-
return [2 /*return*/];
|
|
155
54
|
}
|
|
156
|
-
|
|
55
|
+
});
|
|
56
|
+
// @ts-ignore
|
|
57
|
+
var mpApi = ((_a = _this.config.customApi) === null || _a === void 0 ? void 0 : _a[api]) || my[api];
|
|
58
|
+
if (type === 'sync') {
|
|
59
|
+
var data_1 = mpApi(_options);
|
|
60
|
+
resolve({
|
|
61
|
+
_type_: 'success',
|
|
62
|
+
_data_: data_1
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
if (type === 'async') {
|
|
66
|
+
if (!mpApi) {
|
|
157
67
|
// @ts-ignore
|
|
158
|
-
my
|
|
159
|
-
_this.webview.postMessage(__assign(__assign({}, command), {
|
|
160
|
-
result: result
|
|
161
|
-
}));
|
|
162
|
-
});
|
|
68
|
+
my.call(api, options);
|
|
163
69
|
} else {
|
|
164
|
-
|
|
165
|
-
_this.webview.postMessage(__assign(__assign({}, command), {
|
|
166
|
-
result: result
|
|
167
|
-
}));
|
|
168
|
-
});
|
|
70
|
+
mpApi(options);
|
|
169
71
|
}
|
|
170
|
-
// 表示被 hylid-bridge 消费了
|
|
171
|
-
return [2 /*return*/, true];
|
|
172
72
|
}
|
|
173
|
-
return [2 /*return*/];
|
|
174
73
|
});
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
var
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
_type_: 'success',
|
|
189
|
-
_data_: res
|
|
190
|
-
});
|
|
191
|
-
},
|
|
192
|
-
fail: function fail(res) {
|
|
193
|
-
resolve({
|
|
194
|
-
_type_: 'fail',
|
|
195
|
-
_data_: res
|
|
196
|
-
});
|
|
74
|
+
};
|
|
75
|
+
var config = command.config,
|
|
76
|
+
api = command.api;
|
|
77
|
+
var type = (config || {}).type;
|
|
78
|
+
// 权限管控不允许调用 JSAPI
|
|
79
|
+
if (!_this.isAllowed(api)) {
|
|
80
|
+
var notAllowedMessage = __assign(__assign({}, command), {
|
|
81
|
+
result: {
|
|
82
|
+
_type_: 'fail',
|
|
83
|
+
_data_: {
|
|
84
|
+
errorCode: -2,
|
|
85
|
+
errorMessage: 'api is not allowed'
|
|
86
|
+
}
|
|
197
87
|
}
|
|
198
88
|
});
|
|
89
|
+
_this.webview.postMessage(notAllowedMessage);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (type === 'callback') {
|
|
199
93
|
// @ts-ignore
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
94
|
+
my[api](function (result) {
|
|
95
|
+
_this.webview.postMessage(__assign(__assign({}, command), {
|
|
96
|
+
result: result
|
|
97
|
+
}));
|
|
98
|
+
});
|
|
99
|
+
} else {
|
|
100
|
+
getApiResult(command).then(function (result) {
|
|
101
|
+
_this.webview.postMessage(__assign(__assign({}, command), {
|
|
102
|
+
result: result
|
|
103
|
+
}));
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
this.broadcastHandler = function (data) {
|
|
108
|
+
if (data.type !== 'broadcast') return;
|
|
109
|
+
broadcastTargets.forEach(function (target) {
|
|
110
|
+
if (target === _this) return;
|
|
111
|
+
target.webview.postMessage(data);
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
this.pageEventHandler = function (data) {
|
|
115
|
+
var _a;
|
|
116
|
+
if (data.type !== 'pageEvent') return;
|
|
117
|
+
var event = data.event;
|
|
118
|
+
if (!event) return;
|
|
119
|
+
if (!_this.context) {
|
|
120
|
+
console.info("[hylid-call]: context \u5728\u5C0F\u7A0B\u5E8F\u4FA7\u672A\u4F20\u5165\uFF0C".concat(event, " \u76D1\u542C\u5931\u8D25"));
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
// 已经监听过了
|
|
124
|
+
if ((_a = _this.context[event]) === null || _a === void 0 ? void 0 : _a._hy_listener_) return;
|
|
125
|
+
_this.bindCtxEvent(event, function (payload) {
|
|
126
|
+
_this.webview.postMessage(__assign(__assign({}, data), {
|
|
127
|
+
result: payload
|
|
128
|
+
}));
|
|
216
129
|
});
|
|
130
|
+
_this.context[event]._hy_listener_ = true;
|
|
217
131
|
};
|
|
218
|
-
|
|
132
|
+
if (typeof webviewId === 'string') {
|
|
133
|
+
this.webview = my.createWebViewContext(webviewId);
|
|
134
|
+
} else {
|
|
135
|
+
this.webview = webviewId;
|
|
136
|
+
}
|
|
219
137
|
this.config = config;
|
|
138
|
+
this.context = context;
|
|
139
|
+
this.MESSAGE_HANDLER.push(this.jsApiHandler);
|
|
140
|
+
this.MESSAGE_HANDLER.push(this.broadcastHandler);
|
|
141
|
+
this.MESSAGE_HANDLER.push(this.pageEventHandler);
|
|
142
|
+
if (context) {
|
|
143
|
+
broadcastTargets.push(this);
|
|
144
|
+
this.bindCtxEvent('onUnload', function () {
|
|
145
|
+
broadcastTargets = broadcastTargets.filter(function (target) {
|
|
146
|
+
return target !== _this;
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
} else {
|
|
150
|
+
console.info("[hylid-call]: context \u5728\u5C0F\u7A0B\u5E8F\u4FA7\u672A\u4F20\u5165\uFF0Cbroadcast \u529F\u80FD\u3001\u9875\u9762\u76D1\u542C\u5C06\u65E0\u6CD5\u4F7F\u7528");
|
|
151
|
+
}
|
|
220
152
|
}
|
|
221
153
|
WebViewBridge.prototype.isAllowed = function (api) {
|
|
154
|
+
var _a = this.config || {},
|
|
155
|
+
whitelist = _a.whitelist,
|
|
156
|
+
blacklist = _a.blacklist;
|
|
222
157
|
// 黑白名单校验
|
|
223
|
-
if (
|
|
224
|
-
if (
|
|
158
|
+
if (whitelist && !whitelist.includes(api)) return false;
|
|
159
|
+
if (blacklist && blacklist.includes(api)) return false;
|
|
225
160
|
return true;
|
|
226
161
|
};
|
|
162
|
+
WebViewBridge.prototype.bindCtxEvent = function (event, callback) {
|
|
163
|
+
if (!this.context) return;
|
|
164
|
+
var userEvent = this.context[event];
|
|
165
|
+
this.context[event] = function () {
|
|
166
|
+
var rest = [];
|
|
167
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
168
|
+
rest[_i] = arguments[_i];
|
|
169
|
+
}
|
|
170
|
+
if (userEvent) userEvent.apply(void 0, rest);
|
|
171
|
+
callback.apply(void 0, rest);
|
|
172
|
+
}.bind(this.context);
|
|
173
|
+
};
|
|
227
174
|
return WebViewBridge;
|
|
228
175
|
}();
|
|
229
176
|
export { WebViewBridge };
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hylid/call",
|
|
3
|
-
"version": "3.2.0-alpha.
|
|
3
|
+
"version": "3.2.0-alpha.1",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"files": [
|
|
6
6
|
"lib"
|
|
7
7
|
],
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@hylid/env": "^3.2.0-alpha.
|
|
10
|
-
"@hylid/types": "^3.2.0-alpha.
|
|
9
|
+
"@hylid/env": "^3.2.0-alpha.1",
|
|
10
|
+
"@hylid/types": "^3.2.0-alpha.1"
|
|
11
11
|
},
|
|
12
12
|
"publishConfig": {
|
|
13
13
|
"access": "public"
|