@pisell/utils 1.0.40 → 1.0.42
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/es/jsBridge/index.d.ts +1 -1
- package/es/jsBridge/index.js +6 -4
- package/es/jsBridge/regDeviceApi.d.ts +1 -1
- package/es/jsBridge/regDeviceApi.js +30 -5
- package/lib/jsBridge/index.d.ts +1 -1
- package/lib/jsBridge/index.js +6 -4
- package/lib/jsBridge/regDeviceApi.d.ts +1 -1
- package/lib/jsBridge/regDeviceApi.js +27 -5
- package/package.json +1 -1
package/es/jsBridge/index.d.ts
CHANGED
|
@@ -102,7 +102,7 @@ declare type WebPrintParams = WebPrint0 | WebPrint1 | WebPrint2;
|
|
|
102
102
|
* @param params
|
|
103
103
|
* @param callback
|
|
104
104
|
*/
|
|
105
|
-
export declare function webPrint(params: WebPrintParams, callback: (...arg: any) => void): void;
|
|
105
|
+
export declare function webPrint(params: WebPrintParams, callback: (...arg: any) => void, onError: () => void): void;
|
|
106
106
|
declare type DataManagerParams = {
|
|
107
107
|
module: "set" | "get";
|
|
108
108
|
key: string;
|
package/es/jsBridge/index.js
CHANGED
|
@@ -5,14 +5,15 @@ import regDeviceApi from "./regDeviceApi";
|
|
|
5
5
|
* @param params
|
|
6
6
|
* @param callback
|
|
7
7
|
*/
|
|
8
|
-
export function webPrint(params, _callback) {
|
|
9
|
-
regDeviceApi().then(function (jsBridge) {
|
|
8
|
+
export function webPrint(params, _callback, onError) {
|
|
9
|
+
regDeviceApi(true).then(function (jsBridge) {
|
|
10
10
|
jsBridge.callDeviceApi({
|
|
11
11
|
apiName: "webPrint",
|
|
12
12
|
params: params,
|
|
13
13
|
callback: function callback() {
|
|
14
14
|
_callback && _callback.apply(void 0, arguments);
|
|
15
|
-
}
|
|
15
|
+
},
|
|
16
|
+
onError: onError
|
|
16
17
|
});
|
|
17
18
|
});
|
|
18
19
|
}
|
|
@@ -29,7 +30,8 @@ export function dataManager(params, _callback2, onError) {
|
|
|
29
30
|
params: params,
|
|
30
31
|
callback: function callback() {
|
|
31
32
|
_callback2 && _callback2.apply(void 0, arguments);
|
|
32
|
-
}
|
|
33
|
+
},
|
|
34
|
+
onError: onError
|
|
33
35
|
});
|
|
34
36
|
}).catch(onError);
|
|
35
37
|
}
|
|
@@ -10,6 +10,6 @@ type ComposedJSBridge = {
|
|
|
10
10
|
calledByDevice: (a: { apiName: string; callback: Functon }) => void;
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
-
const regDeviceApi: () => Promise<ComposedJSBridge>;
|
|
13
|
+
const regDeviceApi: (isWebPrint?: boolean) => Promise<ComposedJSBridge>;
|
|
14
14
|
|
|
15
15
|
export default regDeviceApi;
|
|
@@ -4,10 +4,12 @@ var iosRegStatus = false;
|
|
|
4
4
|
var androidBridge;
|
|
5
5
|
var iosBridge;
|
|
6
6
|
var composedBridge;
|
|
7
|
-
function regIosJsBridge(callback) {
|
|
7
|
+
function regIosJsBridge(callback, reject) {
|
|
8
8
|
if (window.WebViewJavascriptBridge) {
|
|
9
9
|
callback(window.WebViewJavascriptBridge);
|
|
10
10
|
return;
|
|
11
|
+
} else {
|
|
12
|
+
reject();
|
|
11
13
|
}
|
|
12
14
|
if (window.WVJBCallbacks) {
|
|
13
15
|
window.WVJBCallbacks.push(callback);
|
|
@@ -44,6 +46,10 @@ function regAndroidJsBridge(extraInit) {
|
|
|
44
46
|
extraInit(bridge);
|
|
45
47
|
}, false);
|
|
46
48
|
}
|
|
49
|
+
function checkApiName(apiNames, apiName) {
|
|
50
|
+
var _apiNames = apiNames || ['webPrint'];
|
|
51
|
+
return _apiNames.includes(apiName);
|
|
52
|
+
}
|
|
47
53
|
|
|
48
54
|
/**
|
|
49
55
|
* 通用jsBridge
|
|
@@ -78,20 +84,31 @@ function getComposedBridge(deviceType) {
|
|
|
78
84
|
inCb.apply(void 0, [result].concat(args));
|
|
79
85
|
};
|
|
80
86
|
if (deviceType === 'iphone') {
|
|
87
|
+
var _iosBridge;
|
|
88
|
+
if (!checkApiName((_iosBridge = iosBridge) === null || _iosBridge === void 0 ? void 0 : _iosBridge.apiNames, apiName)) {
|
|
89
|
+
onError === null || onError === void 0 ? void 0 : onError();
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
81
92
|
iosBridge.callHandler(apiName, params, callback, function (e) {
|
|
82
93
|
console.log(e, '--- 调用ios api报错:', apiName);
|
|
83
94
|
if (onError) {
|
|
84
|
-
onError(e);
|
|
95
|
+
onError === null || onError === void 0 ? void 0 : onError(e);
|
|
85
96
|
}
|
|
86
97
|
});
|
|
87
98
|
} else if (deviceType === 'android') {
|
|
99
|
+
var _androidBridge;
|
|
100
|
+
if (!checkApiName((_androidBridge = androidBridge) === null || _androidBridge === void 0 ? void 0 : _androidBridge.apiNames, apiName)) {
|
|
101
|
+
onError === null || onError === void 0 ? void 0 : onError();
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
88
104
|
androidBridge.callHandler(apiName, params, callback, function (e) {
|
|
89
105
|
console.log(e, '--- 调用android api报错:', apiName);
|
|
90
106
|
if (onError) {
|
|
91
|
-
onError(e);
|
|
107
|
+
onError === null || onError === void 0 ? void 0 : onError(e);
|
|
92
108
|
}
|
|
93
109
|
});
|
|
94
110
|
} else {
|
|
111
|
+
onError === null || onError === void 0 ? void 0 : onError();
|
|
95
112
|
console.log('--- 没获取到js bridge ---');
|
|
96
113
|
}
|
|
97
114
|
},
|
|
@@ -108,15 +125,23 @@ function getComposedBridge(deviceType) {
|
|
|
108
125
|
};
|
|
109
126
|
return composedBridge;
|
|
110
127
|
}
|
|
111
|
-
export default function regDeviceApi() {
|
|
128
|
+
export default function regDeviceApi(isWebPrint) {
|
|
112
129
|
return new Promise(function (resolve, reject) {
|
|
130
|
+
// 旧版本只能用WebPrint
|
|
131
|
+
var isOld = !navigator.userAgent.includes('jsBridge');
|
|
132
|
+
if (isOld) {
|
|
133
|
+
if (!isWebPrint) {
|
|
134
|
+
reject();
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
113
138
|
if (isIos() && !iosRegStatus) {
|
|
114
139
|
regIosJsBridge(function (bridge) {
|
|
115
140
|
iosRegStatus = true;
|
|
116
141
|
iosBridge = bridge;
|
|
117
142
|
var realBridge = getComposedBridge('iphone');
|
|
118
143
|
resolve(realBridge);
|
|
119
|
-
});
|
|
144
|
+
}, reject);
|
|
120
145
|
} else if (isIos() && iosRegStatus) {
|
|
121
146
|
var realBridge = getComposedBridge('iphone');
|
|
122
147
|
resolve(realBridge);
|
package/lib/jsBridge/index.d.ts
CHANGED
|
@@ -102,7 +102,7 @@ declare type WebPrintParams = WebPrint0 | WebPrint1 | WebPrint2;
|
|
|
102
102
|
* @param params
|
|
103
103
|
* @param callback
|
|
104
104
|
*/
|
|
105
|
-
export declare function webPrint(params: WebPrintParams, callback: (...arg: any) => void): void;
|
|
105
|
+
export declare function webPrint(params: WebPrintParams, callback: (...arg: any) => void, onError: () => void): void;
|
|
106
106
|
declare type DataManagerParams = {
|
|
107
107
|
module: "set" | "get";
|
|
108
108
|
key: string;
|
package/lib/jsBridge/index.js
CHANGED
|
@@ -34,14 +34,15 @@ __export(jsBridge_exports, {
|
|
|
34
34
|
});
|
|
35
35
|
module.exports = __toCommonJS(jsBridge_exports);
|
|
36
36
|
var import_regDeviceApi = __toESM(require("./regDeviceApi"));
|
|
37
|
-
function webPrint(params, callback) {
|
|
38
|
-
(0, import_regDeviceApi.default)().then((jsBridge) => {
|
|
37
|
+
function webPrint(params, callback, onError) {
|
|
38
|
+
(0, import_regDeviceApi.default)(true).then((jsBridge) => {
|
|
39
39
|
jsBridge.callDeviceApi({
|
|
40
40
|
apiName: "webPrint",
|
|
41
41
|
params,
|
|
42
42
|
callback: (...arg) => {
|
|
43
43
|
callback && callback(...arg);
|
|
44
|
-
}
|
|
44
|
+
},
|
|
45
|
+
onError
|
|
45
46
|
});
|
|
46
47
|
});
|
|
47
48
|
}
|
|
@@ -52,7 +53,8 @@ function dataManager(params, callback, onError) {
|
|
|
52
53
|
params,
|
|
53
54
|
callback: (...arg) => {
|
|
54
55
|
callback && callback(...arg);
|
|
55
|
-
}
|
|
56
|
+
},
|
|
57
|
+
onError
|
|
56
58
|
});
|
|
57
59
|
}).catch(onError);
|
|
58
60
|
}
|
|
@@ -10,6 +10,6 @@ type ComposedJSBridge = {
|
|
|
10
10
|
calledByDevice: (a: { apiName: string; callback: Functon }) => void;
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
-
const regDeviceApi: () => Promise<ComposedJSBridge>;
|
|
13
|
+
const regDeviceApi: (isWebPrint?: boolean) => Promise<ComposedJSBridge>;
|
|
14
14
|
|
|
15
15
|
export default regDeviceApi;
|
|
@@ -28,10 +28,12 @@ var iosRegStatus = false;
|
|
|
28
28
|
var androidBridge;
|
|
29
29
|
var iosBridge;
|
|
30
30
|
var composedBridge;
|
|
31
|
-
function regIosJsBridge(callback) {
|
|
31
|
+
function regIosJsBridge(callback, reject) {
|
|
32
32
|
if (window.WebViewJavascriptBridge) {
|
|
33
33
|
callback(window.WebViewJavascriptBridge);
|
|
34
34
|
return;
|
|
35
|
+
} else {
|
|
36
|
+
reject();
|
|
35
37
|
}
|
|
36
38
|
if (window.WVJBCallbacks) {
|
|
37
39
|
window.WVJBCallbacks.push(callback);
|
|
@@ -72,6 +74,10 @@ function regAndroidJsBridge(extraInit) {
|
|
|
72
74
|
false
|
|
73
75
|
);
|
|
74
76
|
}
|
|
77
|
+
function checkApiName(apiNames, apiName) {
|
|
78
|
+
let _apiNames = apiNames || ["webPrint"];
|
|
79
|
+
return _apiNames.includes(apiName);
|
|
80
|
+
}
|
|
75
81
|
function getComposedBridge(deviceType) {
|
|
76
82
|
if (composedBridge) {
|
|
77
83
|
return composedBridge;
|
|
@@ -92,20 +98,29 @@ function getComposedBridge(deviceType) {
|
|
|
92
98
|
inCb(result, ...args);
|
|
93
99
|
};
|
|
94
100
|
if (deviceType === "iphone") {
|
|
101
|
+
if (!checkApiName(iosBridge == null ? void 0 : iosBridge.apiNames, apiName)) {
|
|
102
|
+
onError == null ? void 0 : onError();
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
95
105
|
iosBridge.callHandler(apiName, params, callback, (e) => {
|
|
96
106
|
console.log(e, "--- \u8C03\u7528ios api\u62A5\u9519\uFF1A", apiName);
|
|
97
107
|
if (onError) {
|
|
98
|
-
onError(e);
|
|
108
|
+
onError == null ? void 0 : onError(e);
|
|
99
109
|
}
|
|
100
110
|
});
|
|
101
111
|
} else if (deviceType === "android") {
|
|
112
|
+
if (!checkApiName(androidBridge == null ? void 0 : androidBridge.apiNames, apiName)) {
|
|
113
|
+
onError == null ? void 0 : onError();
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
102
116
|
androidBridge.callHandler(apiName, params, callback, (e) => {
|
|
103
117
|
console.log(e, "--- \u8C03\u7528android api\u62A5\u9519\uFF1A", apiName);
|
|
104
118
|
if (onError) {
|
|
105
|
-
onError(e);
|
|
119
|
+
onError == null ? void 0 : onError(e);
|
|
106
120
|
}
|
|
107
121
|
});
|
|
108
122
|
} else {
|
|
123
|
+
onError == null ? void 0 : onError();
|
|
109
124
|
console.log("--- \u6CA1\u83B7\u53D6\u5230js bridge ---");
|
|
110
125
|
}
|
|
111
126
|
},
|
|
@@ -120,15 +135,22 @@ function getComposedBridge(deviceType) {
|
|
|
120
135
|
};
|
|
121
136
|
return composedBridge;
|
|
122
137
|
}
|
|
123
|
-
function regDeviceApi() {
|
|
138
|
+
function regDeviceApi(isWebPrint) {
|
|
124
139
|
return new Promise((resolve, reject) => {
|
|
140
|
+
const isOld = !navigator.userAgent.includes("jsBridge");
|
|
141
|
+
if (isOld) {
|
|
142
|
+
if (!isWebPrint) {
|
|
143
|
+
reject();
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
125
147
|
if ((0, import_platform.isIos)() && !iosRegStatus) {
|
|
126
148
|
regIosJsBridge((bridge) => {
|
|
127
149
|
iosRegStatus = true;
|
|
128
150
|
iosBridge = bridge;
|
|
129
151
|
const realBridge = getComposedBridge("iphone");
|
|
130
152
|
resolve(realBridge);
|
|
131
|
-
});
|
|
153
|
+
}, reject);
|
|
132
154
|
} else if ((0, import_platform.isIos)() && iosRegStatus) {
|
|
133
155
|
const realBridge = getComposedBridge("iphone");
|
|
134
156
|
resolve(realBridge);
|