syttunnel 0.1.5-3 → 0.1.5-5

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.
@@ -66,6 +66,9 @@ interface ISaveImageToLocalResponse {
66
66
  export interface ISFAddressParams {
67
67
  encryptKey: string;
68
68
  }
69
+ interface IPasteStringParams {
70
+ content: string;
71
+ }
69
72
  export declare class AppCommon {
70
73
  openScan(params: IOpenCodeScanParams): Promise<string>;
71
74
  shareMsgToWXAPI(params: IShareMsgToWXParams): Promise<IShareMsgToWXResponse>;
@@ -79,5 +82,9 @@ export declare class AppCommon {
79
82
  * @returns 返回为json string加密的字符串,需密钥解密
80
83
  */
81
84
  getSFAddress(params: ISFAddressParams): Promise<string>;
85
+ /**
86
+ * 复制字符串
87
+ */
88
+ pasteString(params: IPasteStringParams): Promise<boolean>;
82
89
  }
83
90
  export {};
@@ -95,5 +95,10 @@ export declare class AppNavigator {
95
95
  * 返回上一页
96
96
  */
97
97
  onGoBack(): void;
98
+ /**
99
+ * 监听返回事件
100
+ * @param callback
101
+ */
102
+ onBackPressedListener(callback: (data: any) => boolean): void;
98
103
  }
99
104
  export {};
@@ -10,19 +10,21 @@ export interface IRequestCompletion<T> {
10
10
  success?: (res: T) => void;
11
11
  failure?: (err: IError) => void;
12
12
  }
13
- declare class FuncCallBack {
14
- success?: (res: any) => void;
15
- failure?: (err: IError) => void;
16
- uId: string;
17
- constructor(uId: string, suc?: (res: any) => void, fal?: (err: IError) => void);
18
- }
13
+ declare type SubscribeEventFunction = (params?: any, callback?: (data: any) => void) => void;
19
14
  declare class Bridge {
20
- static cbArr: FuncCallBack[];
21
- static funCallCount: number;
15
+ messageHandlers: {
16
+ [key: string]: any;
17
+ };
18
+ subscribers: {
19
+ [key: string]: SubscribeEventFunction[];
20
+ };
21
+ uniqueId: number;
22
22
  constructor();
23
- private onMessage;
24
- postMessage<T>(params: IPostParams): Promise<T>;
23
+ onMessage: (msg: any) => void;
24
+ postMessage: <T>(params: IPostParams) => Promise<T>;
25
25
  private dealPostMessage;
26
+ subscribeEvent: (handlerName: string, handler: any) => void;
27
+ unsubscribeEvent: (handlerName: string, handler: any) => void;
26
28
  }
27
29
  declare const bridge: Bridge;
28
30
  export default bridge;
@@ -2,7 +2,7 @@
2
2
  * 基本的调用事件
3
3
  */
4
4
  import { IPostParams } from "./bridge";
5
- export declare type CommonEvents = "getUserInfo" | "sytFetch" | "authorize" | "backToBefore" | "setTitle" | "setStyle" | "getNavigationFrame" | "loadUrl" | "payByWeChat" | "payByAli" | "startScanCode" | "shareMessageToWXReq" | "currentLocation" | "appVersion" | "selectContacts" | "saveImageToLocal" | "getEnvironment" | "goBack" | "selectAddress";
5
+ export declare type CommonEvents = "getUserInfo" | "sytFetch" | "authorize" | "backToBefore" | "setTitle" | "setStyle" | "getNavigationFrame" | "loadUrl" | "payByWeChat" | "payByAli" | "startScanCode" | "shareMessageToWXReq" | "currentLocation" | "appVersion" | "selectContacts" | "saveImageToLocal" | "getEnvironment" | "goBack" | "selectAddress" | "pasteString";
6
6
  export interface IPostBasicParams extends IPostParams {
7
7
  method: CommonEvents;
8
8
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "syttunnel",
3
- "version": "0.1.5-3",
3
+ "version": "0.1.5-5",
4
4
  "description": "",
5
5
  "main": "syttunnel.js",
6
6
  "types": "index.d.ts",
package/syttunnel.js CHANGED
@@ -358,14 +358,14 @@ var sytFetch = function (config) {
358
358
  };
359
359
  var appFunc = {
360
360
  method: "sytFetch",
361
- params: data,
361
+ params: request,
362
362
  };
363
363
  return new Promise(function (resolve, reject) {
364
364
  bridge_1.default
365
365
  .postMessage(appFunc)
366
366
  .then(function (result) {
367
367
  var resData = result.data;
368
- if (!data) {
368
+ if (!resData) {
369
369
  reject({
370
370
  errCode: "-1",
371
371
  errMessage: "没有返回data数据",
@@ -402,94 +402,156 @@ exports.sytFetch = sytFetch;
402
402
 
403
403
  /***/ }),
404
404
  /* 6 */
405
- /***/ ((__unused_webpack_module, exports, __webpack_require__) => {
405
+ /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
406
406
 
407
407
 
408
+ var __assign = (this && this.__assign) || function () {
409
+ __assign = Object.assign || function(t) {
410
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
411
+ s = arguments[i];
412
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
413
+ t[p] = s[p];
414
+ }
415
+ return t;
416
+ };
417
+ return __assign.apply(this, arguments);
418
+ };
408
419
  Object.defineProperty(exports, "__esModule", ({ value: true }));
409
420
  var tools_1 = __webpack_require__(7);
410
- var FuncCallBack = /** @class */ (function () {
411
- function FuncCallBack(uId, suc, fal) {
412
- this.uId = uId;
413
- this.success = suc;
414
- this.failure = fal;
415
- }
416
- return FuncCallBack;
417
- }());
421
+ var EventType;
422
+ (function (EventType) {
423
+ EventType["CALL"] = "call";
424
+ EventType["REGISTER"] = "register";
425
+ EventType["UNREGISTER"] = "unregister";
426
+ EventType["CALLBACK"] = "callback";
427
+ })(EventType || (EventType = {}));
418
428
  var Bridge = /** @class */ (function () {
419
429
  function Bridge() {
430
+ var _this = this;
431
+ this.onMessage = function (msg) {
432
+ var message = typeof msg.data === "string"
433
+ ? JSON.parse(msg.data || "")
434
+ : msg.data || {};
435
+ var method = message.method;
436
+ var value = message.value;
437
+ var uniqueId = message.uniqueId;
438
+ var errCode = message.errCode;
439
+ var errMessage = message.errMessage;
440
+ var callbackId = message.callbackId;
441
+ if (!method) {
442
+ return;
443
+ }
444
+ if (uniqueId) {
445
+ // js call native callback
446
+ var callback_1 = _this.messageHandlers[uniqueId];
447
+ if (callback_1) {
448
+ // 成功的回调
449
+ if (value != null && value != undefined) {
450
+ callback_1(null, value);
451
+ }
452
+ else {
453
+ var error = {
454
+ errCode: errCode,
455
+ errMessage: errMessage,
456
+ };
457
+ callback_1(error, null);
458
+ }
459
+ delete _this.messageHandlers[uniqueId];
460
+ }
461
+ }
462
+ else {
463
+ // native call js
464
+ if (!method) {
465
+ return;
466
+ }
467
+ var callback;
468
+ if (callbackId) {
469
+ callback = function (response) {
470
+ _this.dealPostMessage({
471
+ responseId: callbackId,
472
+ method: method,
473
+ params: {
474
+ result: response,
475
+ },
476
+ type: EventType.CALLBACK,
477
+ });
478
+ };
479
+ }
480
+ var subscribers = _this.subscribers[method];
481
+ if (subscribers && subscribers.length > 0) {
482
+ subscribers.forEach(function (handler) {
483
+ handler(value, callback);
484
+ });
485
+ }
486
+ }
487
+ };
488
+ this.postMessage = function (params) {
489
+ return new Promise(function (resolve, reject) {
490
+ _this.dealPostMessage(__assign({ type: EventType.CALL }, params), function (error, result) {
491
+ if (error) {
492
+ reject(error);
493
+ }
494
+ else {
495
+ if (result !== null && result !== undefined) {
496
+ resolve(result);
497
+ }
498
+ else {
499
+ reject(new Error("返回的数据错误"));
500
+ }
501
+ }
502
+ });
503
+ });
504
+ };
505
+ this.dealPostMessage = function (params, callback) {
506
+ if (window.ReactNativeWebView) {
507
+ var postParams = params;
508
+ if (callback) {
509
+ var uniqueId = "cb_" + (_this.uniqueId++).toString() + Date.now().toString();
510
+ _this.messageHandlers[uniqueId] = callback;
511
+ postParams.uniqueId = uniqueId;
512
+ }
513
+ window.ReactNativeWebView.postMessage(JSON.stringify(postParams));
514
+ }
515
+ };
516
+ // 注册方法
517
+ this.subscribeEvent = function (handlerName, handler) {
518
+ var subscribers = _this.subscribers[handlerName] || [];
519
+ subscribers.push(handler);
520
+ _this.subscribers[handlerName] = subscribers;
521
+ // 监听者数量大于1,发送注册事件,通知native
522
+ if (subscribers.length > 0) {
523
+ _this.dealPostMessage({
524
+ method: handlerName,
525
+ type: EventType.REGISTER,
526
+ });
527
+ }
528
+ };
529
+ // 移除注册方法
530
+ this.unsubscribeEvent = function (handlerName, handler) {
531
+ var subscribers = _this.subscribers[handlerName];
532
+ var index = subscribers.indexOf(handler);
533
+ if (index > -1) {
534
+ subscribers.splice(index, 1);
535
+ _this.subscribers[handlerName] = subscribers;
536
+ }
537
+ if (subscribers.length <= 0) {
538
+ // 监听者数量小于等于0,发送移除注册事件,通知native
539
+ _this.dealPostMessage({
540
+ method: handlerName,
541
+ type: EventType.UNREGISTER,
542
+ });
543
+ }
544
+ };
420
545
  if (tools_1.isSfAppIOS()) {
421
546
  window.addEventListener("message", this.onMessage);
422
547
  }
423
548
  else if (tools_1.isSfAppAndroid()) {
424
549
  document.addEventListener("message", this.onMessage);
425
550
  }
426
- this.onMessage = this.onMessage.bind(this);
551
+ this.messageHandlers = {};
552
+ this.subscribers = {};
553
+ this.uniqueId = 0;
427
554
  }
428
- Bridge.prototype.onMessage = function (msg) {
429
- var message = typeof msg.data === "string"
430
- ? JSON.parse(msg.data || "")
431
- : msg.data || {};
432
- var method = message.method;
433
- var value = message.value;
434
- var uniqueId = message.uniqueId;
435
- var errCode = message.errCode;
436
- var errMessage = message.errMessage;
437
- if (!method || !uniqueId) {
438
- return;
439
- }
440
- var obj = Bridge.cbArr.find(function (item) { return item.uId === uniqueId; });
441
- var success = obj === null || obj === void 0 ? void 0 : obj.success;
442
- var failure = obj === null || obj === void 0 ? void 0 : obj.failure;
443
- if ((errCode || errMessage) && failure) {
444
- var error = {
445
- errCode: errCode,
446
- errMessage: errMessage,
447
- };
448
- failure(error);
449
- var delIndex_1 = Bridge.cbArr.findIndex(function (item) { return item.uId === uniqueId; });
450
- if (delIndex_1 > -1) {
451
- Bridge.cbArr.splice(delIndex_1, 1);
452
- }
453
- return;
454
- }
455
- if (value != null && success) {
456
- success(value);
457
- }
458
- var delIndex = Bridge.cbArr.findIndex(function (item) { return item.uId === uniqueId; });
459
- if (delIndex > -1) {
460
- Bridge.cbArr.splice(delIndex, 1);
461
- }
462
- };
463
- Bridge.prototype.postMessage = function (params) {
464
- return new Promise(function (resolve, reject) {
465
- bridge.dealPostMessage(params, function (result) {
466
- if (result !== null && result !== undefined) {
467
- resolve(result);
468
- }
469
- else {
470
- reject(new Error("返回的数据错误"));
471
- }
472
- }, function (err) {
473
- reject(err);
474
- });
475
- });
476
- };
477
- Bridge.prototype.dealPostMessage = function (params, success, failure) {
478
- if (window.ReactNativeWebView) {
479
- var uniqueId = params.method + Bridge.funCallCount.toString() + Date.now().toString();
480
- var funcCbObj = new FuncCallBack(uniqueId, success, failure);
481
- Bridge.cbArr.push(funcCbObj);
482
- Bridge.funCallCount++;
483
- var postParams = {
484
- method: params.method,
485
- params: params.params,
486
- uniqueId: uniqueId,
487
- };
488
- window.ReactNativeWebView.postMessage(JSON.stringify(postParams));
489
- }
490
- };
491
- Bridge.cbArr = [];
492
- Bridge.funCallCount = 0;
493
555
  return Bridge;
494
556
  }());
495
557
  var bridge = new Bridge();
@@ -676,6 +738,16 @@ var AppCommon = /** @class */ (function () {
676
738
  };
677
739
  return bridge_1.default.postMessage(appFunc);
678
740
  };
741
+ /**
742
+ * 复制字符串
743
+ */
744
+ AppCommon.prototype.pasteString = function (params) {
745
+ var appFunc = {
746
+ method: "pasteString",
747
+ params: params,
748
+ };
749
+ return bridge_1.default.postMessage(appFunc);
750
+ };
679
751
  return AppCommon;
680
752
  }());
681
753
  exports.AppCommon = AppCommon;
@@ -752,6 +824,16 @@ var AppNavigator = /** @class */ (function () {
752
824
  };
753
825
  bridge_1.default.postMessage(appFunc).then().catch();
754
826
  };
827
+ /**
828
+ * 监听返回事件
829
+ * @param callback
830
+ */
831
+ AppNavigator.prototype.onBackPressedListener = function (callback) {
832
+ bridge_1.default.subscribeEvent("onBackPressed", function (params, responseCallback) {
833
+ var res = callback(params);
834
+ responseCallback(res);
835
+ });
836
+ };
755
837
  return AppNavigator;
756
838
  }());
757
839
  exports.AppNavigator = AppNavigator;