@hylid/call 3.3.0-alpha.11 → 3.3.0-alpha.13

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/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@hylid/call",
3
- "version": "3.3.0-alpha.11",
3
+ "version": "3.3.0-alpha.13",
4
4
  "main": "lib/index.js",
5
5
  "files": [
6
6
  "lib"
7
7
  ],
8
8
  "dependencies": {
9
- "@hylid/env": "^3.3.0-alpha.11",
10
- "@hylid/types": "^3.3.0-alpha.11"
9
+ "@hylid/env": "^3.3.0-alpha.13",
10
+ "@hylid/types": "^3.3.0-alpha.13"
11
11
  },
12
12
  "publishConfig": {
13
13
  "access": "public"
package/lib/index.d.ts DELETED
@@ -1,12 +0,0 @@
1
- /// <reference types="miniprogram" />
2
- import { MPApi, PickMPArgs, PickMpReturns, Callback } from '@hylid/types';
3
- import { mpCall } from './mpCall';
4
- import { mpWebCall } from './mpWebCall';
5
- import { webCall } from './webCall';
6
- import { notFound } from './notFound';
7
- import { Config } from './types';
8
- export declare function call<T extends keyof MPApi>(name: T | ({} & string), options?: PickMPArgs<T> | Common | Callback<PickMpReturns<T>>, config?: Config): any;
9
- export declare function createWebviewSrc(url: string): string;
10
- export { WebViewBridge } from './webviewBridge';
11
- export { mpCall, mpWebCall, webCall, notFound };
12
- export { promisify } from './promisify';
package/lib/index.js DELETED
@@ -1,30 +0,0 @@
1
- var _a;
2
- import { platform, PLATFORM } from '@hylid/env';
3
- import { mpCall } from "./mpCall";
4
- import { mpWebCall } from "./mpWebCall";
5
- import { webCall } from "./webCall";
6
- import { notFound } from "./notFound";
7
- var callMap = (_a = {}, _a[PLATFORM.MP] = mpCall, _a[PLATFORM.MPWEB] = mpWebCall, _a[PLATFORM.WEB] = webCall, _a);
8
- export function call(name, options, config) {
9
- if (platform) {
10
- return callMap[platform](name, options, config);
11
- } else {
12
- return notFound(options, config);
13
- }
14
- }
15
- export function createWebviewSrc(url) {
16
- var app = my.getSystemInfoSync().app;
17
- var hashIndex = url.indexOf('#');
18
- var hash = '';
19
- if (hashIndex !== -1) {
20
- hash = url.slice(hashIndex);
21
- url = url.slice(0, hashIndex);
22
- }
23
- var separator = url.includes('?') ? '&' : '?';
24
- var newUrl = url + separator + '__app__=' + app + hash;
25
- return newUrl;
26
- }
27
- ;
28
- export { WebViewBridge } from "./webviewBridge";
29
- export { mpCall, mpWebCall, webCall, notFound };
30
- export { promisify } from "./promisify";
package/lib/mpCall.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import { MPApi, PickMPArgs, Callback, PickMpReturns } from '@hylid/types';
2
- import { JsApiConfig } from './types';
3
- export declare function mpCall<T extends keyof MPApi>(apiName: T | ({} & string), options?: PickMPArgs<T> | Callback<PickMpReturns<T>>, config?: JsApiConfig): any;
package/lib/mpCall.js DELETED
@@ -1,26 +0,0 @@
1
- export function mpCall(apiName, options, config) {
2
- var _a = (config || {}).type,
3
- type = _a === void 0 ? 'async' : _a;
4
- if (type === 'sync' || (config === null || config === void 0 ? void 0 : config.isSync)) {
5
- // @ts-ignore
6
- return my[apiName](options);
7
- }
8
- if (type === 'async') {
9
- // @ts-ignore
10
- if (my[apiName]) {
11
- // @ts-ignore
12
- my[apiName](options);
13
- } else {
14
- // @ts-ignore
15
- my.call(apiName, options);
16
- }
17
- }
18
- if (type === 'callback') {
19
- // @ts-ignore
20
- my[apiName](options);
21
- }
22
- if (type === 'attribute') {
23
- // @ts-ignore
24
- return my[apiName];
25
- }
26
- }
@@ -1,3 +0,0 @@
1
- import { Message, MessageHandler } from './types';
2
- export declare function mpWebOnMessage(cb: MessageHandler): () => void;
3
- export declare function mpWebPostMessage(message: Message): any;
@@ -1,30 +0,0 @@
1
- import { alipayJSBridge } from "./webCall";
2
- var isListening = false;
3
- var MESSAGE_HANDLER = [];
4
- export function mpWebOnMessage(cb) {
5
- MESSAGE_HANDLER.push(cb);
6
- return function () {
7
- MESSAGE_HANDLER = MESSAGE_HANDLER.filter(function (o) {
8
- return o !== cb;
9
- });
10
- };
11
- }
12
- export function mpWebPostMessage(message) {
13
- if (!isListening) {
14
- document.addEventListener('onToWebViewMessage', function (e) {
15
- var res = e.data.res;
16
- if (!res || res.type !== 'message') return;
17
- var data = JSON.parse(res.data);
18
- MESSAGE_HANDLER.forEach(function (fn) {
19
- return fn(data);
20
- });
21
- });
22
- isListening = true;
23
- }
24
- return alipayJSBridge(function () {
25
- window.AlipayJSBridge.call('postWebViewMessage', {
26
- type: 'message',
27
- detail: message
28
- });
29
- });
30
- }
@@ -1,11 +0,0 @@
1
- import { MPApi } from '@hylid/types';
2
- import { JsApiConfig, 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?: JsApiConfig): void;
5
- export declare namespace mpWebCall {
6
- var onMessage: typeof mpWebOnMessage;
7
- var postMessage: typeof mpWebPostMessage;
8
- var broadcastGlobalData: (key: string, result: any) => void;
9
- var onReceiveGlobalData: (key: string, callback: (data: any, error: any, form: string) => void) => () => void;
10
- var onPageEvent: (event: ({} & string) | "onShow" | "onHide" | "onTitleClick" | "onOptionMenuClick" | "onTabItemTap", callback: (data: any, err?: any) => void) => () => void;
11
- }
package/lib/mpWebCall.js DELETED
@@ -1,111 +0,0 @@
1
- import { mpWebOnMessage, mpWebPostMessage } from "./mpWebBridge";
2
- // 关联 postMessage 和 onMessage,根据 serialId 执行回调
3
- var callMap = {};
4
- // 注册 jsapi 的 message 处理器
5
- mpWebOnMessage(function (msg) {
6
- var _a;
7
- var message = msg;
8
- var serialId = message.serialId;
9
- // 不是 jsapi 的调用的消息
10
- if (!serialId) return;
11
- if (!callMap[serialId]) return;
12
- var callback = callMap[serialId];
13
- var data = message.result;
14
- var type = (_a = message.config) === null || _a === void 0 ? void 0 : _a.type; // JsApi 的类型
15
- if (type === 'callback') {
16
- var _b = data || {},
17
- _data_1 = _b._data_,
18
- _type_1 = _b._type_;
19
- if (_type_1 === 'success') return callback(_data_1);
20
- return callback(undefined, _data_1);
21
- }
22
- var _c = callback || {},
23
- success = _c.success,
24
- fail = _c.fail,
25
- complete = _c.complete;
26
- var _d = data || {},
27
- _data_ = _d._data_,
28
- _type_ = _d._type_;
29
- // 新版本协议
30
- if (_type_) {
31
- if (_type_ === 'success') success === null || success === void 0 ? void 0 : success(_data_);
32
- if (_type_ === 'fail') fail === null || fail === void 0 ? void 0 : fail(_data_);
33
- complete === null || complete === void 0 ? void 0 : complete(_data_);
34
- } else {
35
- // 下面兼容老版本(老版本消息识别不明确,导致 H5 中走 success/fail 逻辑可能岔了)
36
- // @ts-ignore
37
- if (data === null || data === void 0 ? void 0 : data.error) {
38
- fail === null || fail === void 0 ? void 0 : fail(data);
39
- } else {
40
- success === null || success === void 0 ? void 0 : success(data);
41
- }
42
- complete === null || complete === void 0 ? void 0 : complete(data);
43
- }
44
- delete callMap[serialId];
45
- });
46
- export function mpWebCall(api, options, config) {
47
- var random = Math.floor(Math.random() * 1000000);
48
- var serialId = api + '_' + random;
49
- // 只调用,没有参数,不需要接收 jsapi 执行结果
50
- if (options) {
51
- callMap[serialId] = options;
52
- }
53
- var message = {
54
- source: 'hylid',
55
- type: 'apiCall',
56
- api: api,
57
- serialId: serialId,
58
- options: options,
59
- config: config,
60
- isSync: config === null || config === void 0 ? void 0 : config.isSync
61
- };
62
- mpWebPostMessage(message);
63
- }
64
- function broadcastGlobalData(key, result) {
65
- var message = {
66
- source: 'hylid',
67
- type: 'broadcast',
68
- from: window.location.href,
69
- key: key,
70
- result: result
71
- };
72
- mpWebPostMessage(message);
73
- }
74
- function onReceiveGlobalData(key, callback) {
75
- return mpWebOnMessage(function (msg) {
76
- var message = msg;
77
- if (message.source !== 'hylid') return;
78
- if (message.type !== 'broadcast') return;
79
- // 不是接收的目标消息
80
- if (message.key !== key) return;
81
- var _a = message.result || {},
82
- _data_ = _a._data_,
83
- _type_ = _a._type_;
84
- if (_type_ === 'success') return callback(_data_, undefined, message.from);
85
- return callback(undefined, _data_, message.from);
86
- });
87
- }
88
- function onPageEvent(event, callback) {
89
- // 告诉小程序侧,监听 event 事件
90
- mpWebPostMessage({
91
- source: 'hylid',
92
- type: 'pageEvent',
93
- event: event
94
- });
95
- return mpWebOnMessage(function (msg) {
96
- var message = msg;
97
- if (message.source !== 'hylid') return;
98
- if (message.type !== 'pageEvent') return;
99
- if (message.event !== event) return;
100
- var _a = message.result || {},
101
- _data_ = _a._data_,
102
- _type_ = _a._type_;
103
- if (_type_ === 'success') return callback(_data_);
104
- return callback(undefined, _data_);
105
- });
106
- }
107
- mpWebCall.onMessage = mpWebOnMessage;
108
- mpWebCall.postMessage = mpWebPostMessage;
109
- mpWebCall.broadcastGlobalData = broadcastGlobalData;
110
- mpWebCall.onReceiveGlobalData = onReceiveGlobalData;
111
- mpWebCall.onPageEvent = onPageEvent;
package/lib/notFound.d.ts DELETED
@@ -1 +0,0 @@
1
- export declare const notFound: any;
package/lib/notFound.js DELETED
@@ -1,17 +0,0 @@
1
- import { createError } from "./utils";
2
- export var notFound = function notFound(name, opt, config) {
3
- var _a;
4
- if (config === void 0) {
5
- config = {};
6
- }
7
- var _b = config.type,
8
- type = _b === void 0 ? 'async' : _b;
9
- var response = createError('NOTFOUND', name);
10
- if (type === 'sync') return response;
11
- if (type === 'callback') {
12
- opt === null || opt === void 0 ? void 0 : opt(response);
13
- }
14
- if (type === 'async') {
15
- (_a = opt === null || opt === void 0 ? void 0 : opt.fail) === null || _a === void 0 ? void 0 : _a.call(opt, response);
16
- }
17
- };
@@ -1,4 +0,0 @@
1
- export interface Options {
2
- success?: (res: any) => void;
3
- }
4
- export declare const promisify: <T extends Options, P = Parameters<Required<Options> & T["success"]>[0]>(fn: (opt: T) => void) => (args?: Omit<T, "success" | "fail" | "complete"> | undefined) => Promise<P>;
package/lib/promisify.js DELETED
@@ -1,21 +0,0 @@
1
- var __assign = this && this.__assign || function () {
2
- __assign = Object.assign || function (t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
6
- }
7
- return t;
8
- };
9
- return __assign.apply(this, arguments);
10
- };
11
- export var promisify = function promisify(fn) {
12
- return function (args) {
13
- return new Promise(function (resolve, reject) {
14
- var options = __assign(__assign({}, args), {
15
- success: resolve,
16
- fail: reject
17
- });
18
- fn(options);
19
- });
20
- };
21
- };
package/lib/types.d.ts DELETED
@@ -1,59 +0,0 @@
1
- import { JsApiType, MPApi, PickMPArgs, PickMpReturns } from '@hylid/types';
2
- export declare type JsApi = keyof MPApi;
3
- export interface JsApiConfig {
4
- type?: JsApiType;
5
- /**
6
- * @deprecated 优先使用 type = "sync" 配置
7
- */
8
- isSync?: boolean;
9
- }
10
- export declare type Config = JsApiConfig;
11
- export interface MpWebMessageResult<T = any> {
12
- _type_: 'success' | 'fail';
13
- _data_: T;
14
- }
15
- export interface MpWebJsApiMessage {
16
- source: 'hylid';
17
- type: 'apiCall';
18
- api: string;
19
- serialId: string;
20
- options: any;
21
- config?: JsApiConfig;
22
- result?: MpWebMessageResult;
23
- /**
24
- * @deprecated 优先使用 config.type = "sync" 配置
25
- */
26
- isSync?: boolean;
27
- }
28
- export interface MpWebBroadcastMessage {
29
- source: 'hylid';
30
- type: 'broadcast';
31
- from: string;
32
- key: string;
33
- result?: MpWebMessageResult;
34
- }
35
- export interface MpWebPageEventMessage {
36
- source: 'hylid';
37
- type: 'pageEvent';
38
- event: 'onShow' | 'onHide' | 'onTitleClick' | 'onOptionMenuClick' | 'onTabItemTap' | ({} & string);
39
- result?: MpWebMessageResult;
40
- }
41
- export declare type Message = MpWebJsApiMessage | MpWebBroadcastMessage | MpWebPageEventMessage;
42
- export declare type MessageHandler = (message: Message) => void;
43
- declare type Callback<T> = (result?: T) => void;
44
- export declare type MpWebJsApiOptions<T extends keyof MPApi> = PickMPArgs<T> | Callback<PickMpReturns<T>>;
45
- export interface WebviewBridgeConfig {
46
- /** 允许调用 jsApi 的白名单 */
47
- whitelist?: string[];
48
- /** 禁止调用 jsApi 的黑名单 */
49
- blacklist?: string[];
50
- /** 自定义 jsApi [ 优先级比较高,可以覆盖原生的 jsApi ] */
51
- customApi?: CustomApi;
52
- }
53
- export interface CustomApi {
54
- [key: string]: (params: {
55
- success(v: any): void;
56
- fail(v: any): void;
57
- }) => void;
58
- }
59
- export {};
package/lib/types.js DELETED
@@ -1 +0,0 @@
1
- export {};
package/lib/utils.d.ts DELETED
@@ -1,30 +0,0 @@
1
- import { MpWebMessageResult } from './types';
2
- declare const ERROR: {
3
- NOTFOUND: {
4
- code: number;
5
- message: (name: string) => string;
6
- };
7
- NOTALLOWED: {
8
- code: number;
9
- message: (name: string) => string;
10
- };
11
- INVALID: {
12
- code: number;
13
- message: (name: string) => string;
14
- };
15
- NOTBIND: {
16
- code: number;
17
- message: (name: string) => string;
18
- };
19
- PARAMS_ERROR: {
20
- code: number;
21
- message: (name: string) => string;
22
- };
23
- };
24
- export declare const createError: (type: keyof typeof ERROR, name?: string) => {
25
- errorCode: number;
26
- errorMessage: string;
27
- errorSouce: string;
28
- };
29
- export declare function createMessage(message: any, type?: 'success' | 'fail'): MpWebMessageResult;
30
- export {};
package/lib/utils.js DELETED
@@ -1,54 +0,0 @@
1
- var ERROR = {
2
- NOTFOUND: {
3
- code: -1,
4
- message: function message(name) {
5
- return "".concat(name, " is not found");
6
- }
7
- },
8
- NOTALLOWED: {
9
- code: -2,
10
- message: function message(name) {
11
- return "".concat(name, " is not allowed");
12
- }
13
- },
14
- INVALID: {
15
- code: -3,
16
- message: function message(name) {
17
- return "".concat(name, " is invalid");
18
- }
19
- },
20
- NOTBIND: {
21
- code: -4,
22
- message: function message(name) {
23
- return "".concat(name, " exec fail, webviewBridge not bind context");
24
- }
25
- },
26
- PARAMS_ERROR: {
27
- code: -5,
28
- message: function message(name) {
29
- return "".concat(name, " exec fail, params error");
30
- }
31
- }
32
- };
33
- export var createError = function createError(type, name) {
34
- if (name === void 0) {
35
- name = '';
36
- }
37
- var _a = ERROR[type],
38
- code = _a.code,
39
- message = _a.message;
40
- return {
41
- errorCode: code,
42
- errorMessage: message(name),
43
- errorSouce: 'hylid'
44
- };
45
- };
46
- export function createMessage(message, type) {
47
- if (type === void 0) {
48
- type = 'success';
49
- }
50
- return {
51
- _type_: type,
52
- _data_: message
53
- };
54
- }
package/lib/webCall.d.ts DELETED
@@ -1,4 +0,0 @@
1
- import { AliJsApi } from '@hylid/types';
2
- import { JsApiConfig } from './types';
3
- export declare const alipayJSBridge: (cb: Function) => any;
4
- export declare function webCall<T extends keyof AliJsApi.Api>(apiName: T | ({} & string), options?: any, config?: JsApiConfig): void;
package/lib/webCall.js DELETED
@@ -1,29 +0,0 @@
1
- export var alipayJSBridge = function alipayJSBridge(cb) {
2
- if (window.AlipayJSBridge) return cb();
3
- document.addEventListener('AlipayJSBridgeReady', function () {
4
- return cb();
5
- }, false);
6
- };
7
- export function webCall(apiName, options, config) {
8
- alipayJSBridge(function () {
9
- var _a = (config || {}).type,
10
- type = _a === void 0 ? 'async' : _a;
11
- if (type === 'async') {
12
- window.AlipayJSBridge.call(apiName, options, function (res) {
13
- var _a = options || {},
14
- success = _a.success,
15
- fail = _a.fail,
16
- complete = _a.complete;
17
- if (res.error) {
18
- fail === null || fail === void 0 ? void 0 : fail(res);
19
- } else {
20
- success === null || success === void 0 ? void 0 : success(res);
21
- }
22
- complete === null || complete === void 0 ? void 0 : complete(res);
23
- });
24
- }
25
- if (type === 'callback') {
26
- window.AlipayJSBridge.call(apiName, options);
27
- }
28
- });
29
- }
@@ -1,26 +0,0 @@
1
- import { Message, WebviewBridgeConfig } from '../types';
2
- interface WebViewContext {
3
- postMessage(message: Message): void;
4
- }
5
- export declare class WebViewBridge {
6
- private webview;
7
- private config;
8
- private context;
9
- private MESSAGE_HANDLER;
10
- /**
11
- *
12
- * @param webviewId webview 组件的 id,可传字符串,或者 my.createWebViewContext 创建出的实例
13
- * @param config Config 配置
14
- * @param context 页面上下文,传 this 即可。传入后,H5 侧可以使用广播、可以监听页面事件
15
- */
16
- constructor(webviewId: string | WebViewContext, config?: WebviewBridgeConfig, context?: any);
17
- listen: (data: {
18
- detail: Message;
19
- }) => void;
20
- private jsApiHandler;
21
- private broadcastHandler;
22
- private pageEventHandler;
23
- private bindCtxEvent;
24
- private sendMessage;
25
- }
26
- export {};
@@ -1,129 +0,0 @@
1
- var __assign = this && this.__assign || function () {
2
- __assign = Object.assign || function (t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
6
- }
7
- return t;
8
- };
9
- return __assign.apply(this, arguments);
10
- };
11
- import { __hy_internal__ } from "./internal";
12
- import { callbackApi, getApiResult, isAllowed } from "./utils";
13
- import { createMessage, createError } from "../utils";
14
- var broadcastTargets = [];
15
- var WebViewBridge = /** @class */function () {
16
- /**
17
- *
18
- * @param webviewId webview 组件的 id,可传字符串,或者 my.createWebViewContext 创建出的实例
19
- * @param config Config 配置
20
- * @param context 页面上下文,传 this 即可。传入后,H5 侧可以使用广播、可以监听页面事件
21
- */
22
- function WebViewBridge(webviewId, config, context) {
23
- if (config === void 0) {
24
- config = {};
25
- }
26
- var _this = this;
27
- this.config = {};
28
- this.MESSAGE_HANDLER = [];
29
- this.listen = function (data) {
30
- if (data.detail.source !== 'hylid') return;
31
- _this.MESSAGE_HANDLER.forEach(function (fn) {
32
- return fn(data.detail);
33
- });
34
- };
35
- this.jsApiHandler = function (data) {
36
- var _a;
37
- if (data.type !== 'apiCall') return;
38
- var config = data.config,
39
- api = data.api;
40
- var type = (config || {}).type;
41
- // 权限管控不允许调用 JSAPI
42
- if (!isAllowed(_this.config, api)) {
43
- return _this.sendMessage(data, createMessage(createError('NOTALLOWED', api), 'fail'));
44
- }
45
- var jsapi = ((_a = _this.config.customApi) === null || _a === void 0 ? void 0 : _a[api]) || {
46
- __hy_internal__: __hy_internal__
47
- }[api] || my[api];
48
- if (type === 'callback') {
49
- callbackApi(jsapi, data, function (result) {
50
- return _this.sendMessage(data, result);
51
- });
52
- } else {
53
- getApiResult(jsapi, data).then(function (result) {
54
- _this.sendMessage(data, result);
55
- });
56
- }
57
- };
58
- this.broadcastHandler = function (data) {
59
- if (data.type !== 'broadcast') return;
60
- if (!_this.context) {
61
- _this.sendMessage(data, createMessage(createError('NOTBIND', 'broadcast'), 'fail'));
62
- return;
63
- }
64
- broadcastTargets.forEach(function (target) {
65
- if (target === _this) return;
66
- _this.sendMessage(data, createMessage(data.result), target);
67
- });
68
- };
69
- this.pageEventHandler = function (data) {
70
- var _a;
71
- if (data.type !== 'pageEvent') return;
72
- var event = data.event;
73
- if (!event) {
74
- _this.sendMessage(data, createMessage(createError('PARAMS_ERROR'), 'fail'));
75
- return;
76
- }
77
- if (!_this.context) {
78
- _this.sendMessage(data, createMessage(createError('NOTBIND', event), 'fail'));
79
- return;
80
- }
81
- // 已经监听过了
82
- if ((_a = _this.context[event]) === null || _a === void 0 ? void 0 : _a._hy_listener_) return;
83
- _this.bindCtxEvent(event, function (payload) {
84
- return _this.sendMessage(data, payload);
85
- });
86
- _this.context[event]._hy_listener_ = true;
87
- };
88
- if (typeof webviewId === 'string') {
89
- this.webview = my.createWebViewContext(webviewId);
90
- } else {
91
- this.webview = webviewId;
92
- }
93
- this.config = config;
94
- this.context = context;
95
- this.MESSAGE_HANDLER.push(this.jsApiHandler);
96
- this.MESSAGE_HANDLER.push(this.broadcastHandler);
97
- this.MESSAGE_HANDLER.push(this.pageEventHandler);
98
- if (context) {
99
- broadcastTargets.push(this);
100
- this.bindCtxEvent('onUnload', function () {
101
- broadcastTargets = broadcastTargets.filter(function (target) {
102
- return target !== _this;
103
- });
104
- });
105
- } else {
106
- console.info("[hylid-call]: context is null, broadcast and pageEvent cannot be used");
107
- }
108
- }
109
- WebViewBridge.prototype.bindCtxEvent = function (event, callback) {
110
- if (!this.context) {
111
- return callback(createMessage(createError('NOTBIND', event), 'fail'));
112
- }
113
- var userEvent = this.context[event];
114
- this.context[event] = function (opts) {
115
- if (userEvent) userEvent.bind(this.context)(opts);
116
- callback(createMessage(opts));
117
- };
118
- };
119
- WebViewBridge.prototype.sendMessage = function (origin, result, context) {
120
- if (context === void 0) {
121
- context = this;
122
- }
123
- context.webview.postMessage(__assign(__assign({}, origin), {
124
- result: result
125
- }));
126
- };
127
- return WebViewBridge;
128
- }();
129
- export { WebViewBridge };
@@ -1,3 +0,0 @@
1
- export declare function canIUse(payload: {
2
- jsapi: string;
3
- }): boolean;
@@ -1,4 +0,0 @@
1
- export function canIUse(payload) {
2
- var jsapi = payload.jsapi;
3
- return my.canIUse(jsapi);
4
- }
@@ -1,3 +0,0 @@
1
- export declare function _getCurrentPages(): {
2
- route: any;
3
- }[];
@@ -1,8 +0,0 @@
1
- export function _getCurrentPages() {
2
- var pages = getCurrentPages();
3
- return pages.map(function (page) {
4
- return {
5
- route: page.route
6
- };
7
- });
8
- }
@@ -1,18 +0,0 @@
1
- import { _getCurrentPages as getCurrentPages } from './getCurrentPages';
2
- import { getMemory, removeMemory, setMemory } from './memory';
3
- import { canIUse } from './canIUse';
4
- declare const jsapis: {
5
- getMemory: typeof getMemory;
6
- setMemory: typeof setMemory;
7
- removeMemory: typeof removeMemory;
8
- getCurrentPages: typeof getCurrentPages;
9
- canIUse: typeof canIUse;
10
- };
11
- interface InternalOptions {
12
- name: keyof typeof jsapis;
13
- payload: any;
14
- success(res: any): void;
15
- fail(e: any): void;
16
- }
17
- export declare function __hy_internal__(options: InternalOptions): Promise<void>;
18
- export {};
@@ -1,156 +0,0 @@
1
- var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) {
3
- return value instanceof P ? value : new P(function (resolve) {
4
- resolve(value);
5
- });
6
- }
7
- return new (P || (P = Promise))(function (resolve, reject) {
8
- function fulfilled(value) {
9
- try {
10
- step(generator.next(value));
11
- } catch (e) {
12
- reject(e);
13
- }
14
- }
15
- function rejected(value) {
16
- try {
17
- step(generator["throw"](value));
18
- } catch (e) {
19
- reject(e);
20
- }
21
- }
22
- function step(result) {
23
- result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
24
- }
25
- step((generator = generator.apply(thisArg, _arguments || [])).next());
26
- });
27
- };
28
- var __generator = this && this.__generator || function (thisArg, body) {
29
- var _ = {
30
- label: 0,
31
- sent: function sent() {
32
- if (t[0] & 1) throw t[1];
33
- return t[1];
34
- },
35
- trys: [],
36
- ops: []
37
- },
38
- f,
39
- y,
40
- t,
41
- g;
42
- return g = {
43
- next: verb(0),
44
- "throw": verb(1),
45
- "return": verb(2)
46
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
47
- return this;
48
- }), g;
49
- function verb(n) {
50
- return function (v) {
51
- return step([n, v]);
52
- };
53
- }
54
- function step(op) {
55
- if (f) throw new TypeError("Generator is already executing.");
56
- while (_) try {
57
- 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;
58
- if (y = 0, t) op = [op[0] & 2, t.value];
59
- switch (op[0]) {
60
- case 0:
61
- case 1:
62
- t = op;
63
- break;
64
- case 4:
65
- _.label++;
66
- return {
67
- value: op[1],
68
- done: false
69
- };
70
- case 5:
71
- _.label++;
72
- y = op[1];
73
- op = [0];
74
- continue;
75
- case 7:
76
- op = _.ops.pop();
77
- _.trys.pop();
78
- continue;
79
- default:
80
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
81
- _ = 0;
82
- continue;
83
- }
84
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
85
- _.label = op[1];
86
- break;
87
- }
88
- if (op[0] === 6 && _.label < t[1]) {
89
- _.label = t[1];
90
- t = op;
91
- break;
92
- }
93
- if (t && _.label < t[2]) {
94
- _.label = t[2];
95
- _.ops.push(op);
96
- break;
97
- }
98
- if (t[2]) _.ops.pop();
99
- _.trys.pop();
100
- continue;
101
- }
102
- op = body.call(thisArg, _);
103
- } catch (e) {
104
- op = [6, e];
105
- y = 0;
106
- } finally {
107
- f = t = 0;
108
- }
109
- if (op[0] & 5) throw op[1];
110
- return {
111
- value: op[0] ? op[1] : void 0,
112
- done: true
113
- };
114
- }
115
- };
116
- import { _getCurrentPages as getCurrentPages } from "./getCurrentPages";
117
- import { getMemory, removeMemory, setMemory } from "./memory";
118
- import { canIUse } from "./canIUse";
119
- import { createError } from "../../utils";
120
- var jsapis = {
121
- getMemory: getMemory,
122
- setMemory: setMemory,
123
- removeMemory: removeMemory,
124
- getCurrentPages: getCurrentPages,
125
- canIUse: canIUse
126
- };
127
- export function __hy_internal__(options) {
128
- return __awaiter(this, void 0, void 0, function () {
129
- var _a, success, fail, name, payload, jsapi, res, e_1;
130
- return __generator(this, function (_b) {
131
- switch (_b.label) {
132
- case 0:
133
- _a = options || {}, success = _a.success, fail = _a.fail, name = _a.name, payload = _a.payload;
134
- jsapi = jsapis[name];
135
- if (!jsapi) {
136
- fail(createError('NOTFOUND', "internal jsapi ".concat(name)));
137
- return [2 /*return*/];
138
- }
139
- _b.label = 1;
140
- case 1:
141
- _b.trys.push([1, 3,, 4]);
142
- return [4 /*yield*/, jsapi(payload)];
143
- case 2:
144
- res = _b.sent();
145
- success(res);
146
- return [3 /*break*/, 4];
147
- case 3:
148
- e_1 = _b.sent();
149
- fail(e_1);
150
- return [3 /*break*/, 4];
151
- case 4:
152
- return [2 /*return*/];
153
- }
154
- });
155
- });
156
- }
@@ -1,10 +0,0 @@
1
- export declare function getMemory(payload: {
2
- key: string;
3
- }): any;
4
- export declare function setMemory(payload: {
5
- key: string;
6
- value: any;
7
- }): void;
8
- export declare function removeMemory(payload: {
9
- key: string;
10
- }): void;
@@ -1,12 +0,0 @@
1
- var data = {};
2
- export function getMemory(payload) {
3
- return data[payload.key];
4
- }
5
- export function setMemory(payload) {
6
- data[payload.key] = {
7
- data: payload.value
8
- };
9
- }
10
- export function removeMemory(payload) {
11
- delete data[payload.key];
12
- }
@@ -1,4 +0,0 @@
1
- import { MpWebJsApiMessage, MpWebMessageResult, WebviewBridgeConfig } from '../types';
2
- export declare function callbackApi(jsapi: any, data: MpWebJsApiMessage, cb: (msg: MpWebMessageResult) => void): void;
3
- export declare const getApiResult: (jsapi: any, data: MpWebJsApiMessage) => Promise<MpWebMessageResult>;
4
- export declare function isAllowed(config: WebviewBridgeConfig, api: string): boolean;
@@ -1,205 +0,0 @@
1
- var __assign = this && this.__assign || function () {
2
- __assign = Object.assign || function (t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
6
- }
7
- return t;
8
- };
9
- return __assign.apply(this, arguments);
10
- };
11
- var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
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
- };
126
- import { createMessage, createError } from "../utils";
127
- function getAttributeApi(apiName, jsapi) {
128
- // my 上该 api 不存在
129
- if (typeof jsapi === 'undefined') {
130
- return createMessage(createError('NOTFOUND', apiName), 'fail');
131
- }
132
- return createMessage(jsapi);
133
- }
134
- function getSyncApi(apiName, jsapi, options) {
135
- // 同步场景无该 API
136
- if (!jsapi) {
137
- return createMessage(createError('NOTFOUND', apiName), 'fail');
138
- }
139
- // 传进来的 API 不是函数
140
- if (typeof jsapi !== 'function') {
141
- return createMessage(createError('INVALID', apiName), 'fail');
142
- }
143
- return createMessage(jsapi(options));
144
- }
145
- function getAsyncApi(apiName, jsapi, _options) {
146
- return new Promise(function (resolve) {
147
- var options = __assign(__assign({}, _options), {
148
- success: function success(res) {
149
- resolve(createMessage(res));
150
- },
151
- fail: function fail(res) {
152
- resolve(createMessage(res, 'fail'));
153
- }
154
- });
155
- // @ts-ignore
156
- if (!jsapi) return my.call(api, options);
157
- if (typeof jsapi !== 'function') {
158
- return options.fail(createError('INVALID', apiName));
159
- }
160
- jsapi(options);
161
- });
162
- }
163
- export function callbackApi(jsapi, data, cb) {
164
- var api = (data || {}).api;
165
- // 无该 API
166
- if (!jsapi) {
167
- return cb(createMessage(createError('NOTFOUND', api), 'fail'));
168
- }
169
- // 传进来的 API 不是函数
170
- if (typeof jsapi !== 'function') {
171
- return cb(createMessage(createError('INVALID', api), 'fail'));
172
- }
173
- jsapi(function (result) {
174
- return cb(createMessage(result));
175
- });
176
- }
177
- export var getApiResult = function getApiResult(jsapi, data) {
178
- return __awaiter(void 0, void 0, void 0, function () {
179
- var api, options, config, _a, type;
180
- return __generator(this, function (_b) {
181
- api = data.api, options = data.options, config = data.config;
182
- _a = (config || {}).type, type = _a === void 0 ? 'async' : _a;
183
- if (type === 'sync' || data.isSync) {
184
- return [2 /*return*/, getSyncApi(api, jsapi, options)];
185
- }
186
- if (type === 'async') {
187
- return [2 /*return*/, getAsyncApi(api, jsapi, options)];
188
- }
189
- if (type === 'attribute') {
190
- return [2 /*return*/, getAttributeApi(api, jsapi)];
191
- }
192
- // 不识别的类型
193
- return [2 /*return*/, createMessage(createError('INVALID', api), 'fail')];
194
- });
195
- });
196
- };
197
- export function isAllowed(config, api) {
198
- var _a = config || {},
199
- whitelist = _a.whitelist,
200
- blacklist = _a.blacklist;
201
- // 黑白名单校验
202
- if (whitelist && !whitelist.includes(api)) return false;
203
- if (blacklist && blacklist.includes(api)) return false;
204
- return true;
205
- }
@@ -1 +0,0 @@
1
- <web-view id="{{id}}" src="{{webviewSrc}}" onMessage="onMessage" onError="onError" onLoad="onLoad" />
@@ -1,40 +0,0 @@
1
- "use strict";
2
-
3
- Component({
4
- data: {
5
- webviewSrc: ''
6
- },
7
- onInit: function onInit() {
8
- this.setData({
9
- webviewSrc: this.createWebviewSrc(this.props.src)
10
- });
11
- },
12
- deriveDataFromProps: function deriveDataFromProps(nextProps) {
13
- this.setData({
14
- webviewSrc: this.createWebviewSrc(nextProps.src)
15
- });
16
- },
17
- methods: {
18
- createWebviewSrc: function createWebviewSrc(url) {
19
- var app = my.getSystemInfoSync().app;
20
- var hashIndex = url.indexOf('#');
21
- var hash = '';
22
- if (hashIndex !== -1) {
23
- hash = url.slice(hashIndex);
24
- url = url.slice(0, hashIndex);
25
- }
26
- var separator = url.includes('?') ? '&' : '?';
27
- var newUrl = url + separator + '__app__=' + app + hash;
28
- return newUrl;
29
- },
30
- onMessage: function onMessage(params) {
31
- this.props.onMessage && this.props.onMessage(params);
32
- },
33
- onLoad: function onLoad(params) {
34
- this.props.onLoad && this.props.onLoad(params);
35
- },
36
- onError: function onError(params) {
37
- this.props.onError && this.props.onError(params);
38
- }
39
- }
40
- });
@@ -1,3 +0,0 @@
1
- {
2
- "component": true
3
- }