@bud-fe/h5-native-bridge 1.0.4
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/README.md +352 -0
- package/dist/examples/index.d.ts +0 -0
- package/dist/index.d.ts +3 -0
- package/dist/native-bridge.es.js +3316 -0
- package/dist/native-bridge.es.js.map +1 -0
- package/dist/native-bridge.umd.js +2 -0
- package/dist/native-bridge.umd.js.map +1 -0
- package/dist/src/core/errors.d.ts +14 -0
- package/dist/src/core/index.d.ts +172 -0
- package/dist/src/index.d.ts +19 -0
- package/dist/src/plugins/authentication.d.ts +30 -0
- package/dist/src/plugins/bluetooth.d.ts +399 -0
- package/dist/src/plugins/device.d.ts +117 -0
- package/dist/src/plugins/location.d.ts +89 -0
- package/dist/src/plugins/media.d.ts +157 -0
- package/dist/src/plugins/navigate.d.ts +84 -0
- package/dist/src/plugins/request.d.ts +89 -0
- package/dist/src/plugins/storage.d.ts +52 -0
- package/dist/src/plugins/toast.d.ts +47 -0
- package/dist/src/plugins/userInfo.d.ts +30 -0
- package/dist/src/plugins/wifi.d.ts +65 -0
- package/package.json +46 -0
|
@@ -0,0 +1,3316 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => {
|
|
4
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
+
return value;
|
|
6
|
+
};
|
|
7
|
+
var BridgeErrorCode = /* @__PURE__ */ ((BridgeErrorCode2) => {
|
|
8
|
+
BridgeErrorCode2[BridgeErrorCode2["SUCCESS"] = 0] = "SUCCESS";
|
|
9
|
+
BridgeErrorCode2[BridgeErrorCode2["JSON_ERROR"] = 1] = "JSON_ERROR";
|
|
10
|
+
BridgeErrorCode2[BridgeErrorCode2["SYSTEM_ERROR"] = 2] = "SYSTEM_ERROR";
|
|
11
|
+
BridgeErrorCode2[BridgeErrorCode2["TIMEOUT"] = 3] = "TIMEOUT";
|
|
12
|
+
BridgeErrorCode2[BridgeErrorCode2["PLUGIN_NOT_INITIALIZED"] = 4] = "PLUGIN_NOT_INITIALIZED";
|
|
13
|
+
BridgeErrorCode2[BridgeErrorCode2["INVALID_ACTION"] = 5] = "INVALID_ACTION";
|
|
14
|
+
return BridgeErrorCode2;
|
|
15
|
+
})(BridgeErrorCode || {});
|
|
16
|
+
const createBridgeError = (code, message, details) => {
|
|
17
|
+
const error = new Error(message);
|
|
18
|
+
error.code = code;
|
|
19
|
+
error.details = details;
|
|
20
|
+
return error;
|
|
21
|
+
};
|
|
22
|
+
const _NativeBridgeCore = class _NativeBridgeCore {
|
|
23
|
+
/**
|
|
24
|
+
* 私有构造函数(单例模式)
|
|
25
|
+
*/
|
|
26
|
+
constructor(options = {}) {
|
|
27
|
+
__publicField(this, "callbackMap");
|
|
28
|
+
__publicField(this, "callbackId");
|
|
29
|
+
__publicField(this, "isReady");
|
|
30
|
+
__publicField(this, "readyCallbacks");
|
|
31
|
+
__publicField(this, "debugMode");
|
|
32
|
+
__publicField(this, "defaultTimeout");
|
|
33
|
+
__publicField(this, "plugins");
|
|
34
|
+
__publicField(this, "pendingCalls");
|
|
35
|
+
__publicField(this, "errorHandler");
|
|
36
|
+
__publicField(this, "eventListeners", /* @__PURE__ */ new Map());
|
|
37
|
+
__publicField(this, "nativeEventHandlers", /* @__PURE__ */ new Map());
|
|
38
|
+
// 版本号管理
|
|
39
|
+
__publicField(this, "VERSION", "1.0.0");
|
|
40
|
+
__publicField(this, "API_VERSION", "1");
|
|
41
|
+
// 新增方法注册机制
|
|
42
|
+
__publicField(this, "methodRegistry", /* @__PURE__ */ new Map());
|
|
43
|
+
this.callbackMap = /* @__PURE__ */ new Map();
|
|
44
|
+
this.callbackId = 1;
|
|
45
|
+
this.isReady = false;
|
|
46
|
+
this.readyCallbacks = [];
|
|
47
|
+
this.debugMode = options.debug || true;
|
|
48
|
+
this.defaultTimeout = options.timeout || 1e7;
|
|
49
|
+
this.plugins = /* @__PURE__ */ new Map();
|
|
50
|
+
this.pendingCalls = /* @__PURE__ */ new Map();
|
|
51
|
+
this.errorHandler = options.errorHandler;
|
|
52
|
+
this.setupBridge();
|
|
53
|
+
if (options.plugins && options.plugins.length > 0) {
|
|
54
|
+
options.plugins.forEach((plugin) => this.use(plugin));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* 获取NativeBridge单例
|
|
59
|
+
*/
|
|
60
|
+
static getInstance(options = {}) {
|
|
61
|
+
if (!_NativeBridgeCore.instance) {
|
|
62
|
+
_NativeBridgeCore.instance = new _NativeBridgeCore(options);
|
|
63
|
+
} else if (options.plugins && options.plugins.length > 0) {
|
|
64
|
+
options.plugins.forEach((plugin) => {
|
|
65
|
+
_NativeBridgeCore.instance.use(plugin);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
return _NativeBridgeCore.instance;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* 注册原生事件监听
|
|
72
|
+
*/
|
|
73
|
+
onNativeEvent(eventType, handler) {
|
|
74
|
+
if (!this.nativeEventHandlers.has(eventType)) {
|
|
75
|
+
this.nativeEventHandlers.set(eventType, /* @__PURE__ */ new Set());
|
|
76
|
+
}
|
|
77
|
+
this.nativeEventHandlers.get(eventType).add(handler);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* 取消原生事件监听
|
|
81
|
+
*/
|
|
82
|
+
offNativeEvent(eventType, handler) {
|
|
83
|
+
var _a;
|
|
84
|
+
(_a = this.nativeEventHandlers.get(eventType)) == null ? void 0 : _a.delete(handler);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* 供原生调用,分发事件
|
|
88
|
+
*/
|
|
89
|
+
dispatchNativeEvent(eventType, data) {
|
|
90
|
+
var _a;
|
|
91
|
+
if (this.debugMode) {
|
|
92
|
+
this.log(`分发原生事件: ${eventType}`, data);
|
|
93
|
+
}
|
|
94
|
+
(_a = this.nativeEventHandlers.get(eventType)) == null ? void 0 : _a.forEach((handler) => {
|
|
95
|
+
try {
|
|
96
|
+
handler({ type: eventType, data });
|
|
97
|
+
} catch (e) {
|
|
98
|
+
this.error(`分发原生事件 ${eventType} 失败`, e);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
registerMethod(name, method) {
|
|
103
|
+
if (this.methodRegistry.has(name)) {
|
|
104
|
+
this.warn(`方法 ${name} 已注册,将被覆盖`);
|
|
105
|
+
}
|
|
106
|
+
this.methodRegistry.set(name, method);
|
|
107
|
+
this[name] = method;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* 安装插件
|
|
111
|
+
*/
|
|
112
|
+
use(plugin) {
|
|
113
|
+
if (!plugin || !plugin.name || !plugin.install || typeof plugin.install !== "function") {
|
|
114
|
+
throw createBridgeError(
|
|
115
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
116
|
+
`无效的插件: ${(plugin == null ? void 0 : plugin.name) || "未命名"}`
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
if (this.plugins.has(plugin.name)) {
|
|
120
|
+
this.warn(`插件 ${plugin.name} 已经安装`);
|
|
121
|
+
return this;
|
|
122
|
+
}
|
|
123
|
+
try {
|
|
124
|
+
const context = { bridge: this };
|
|
125
|
+
plugin.install(context);
|
|
126
|
+
this.plugins.set(plugin.name, plugin);
|
|
127
|
+
this.log(`插件 ${plugin.name}@${plugin.version} 安装成功`);
|
|
128
|
+
} catch (error) {
|
|
129
|
+
throw createBridgeError(
|
|
130
|
+
BridgeErrorCode.SYSTEM_ERROR,
|
|
131
|
+
`安装插件 ${plugin.name} 失败`,
|
|
132
|
+
error
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* 检查插件是否已安装
|
|
139
|
+
*/
|
|
140
|
+
hasPlugin(name) {
|
|
141
|
+
return this.plugins.has(name);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* 获取已安装的插件
|
|
145
|
+
*/
|
|
146
|
+
getPlugin(name) {
|
|
147
|
+
return this.plugins.get(name);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* 设置桥接环境
|
|
151
|
+
*/
|
|
152
|
+
setupBridge() {
|
|
153
|
+
try {
|
|
154
|
+
const isInApp = this.checkIsInApp();
|
|
155
|
+
if (isInApp) {
|
|
156
|
+
window.nativeBridgeCallback = this.handleNativeCallback.bind(this);
|
|
157
|
+
window.nativeBridgeDispatchEvent = (eventType, data) => {
|
|
158
|
+
this.dispatchNativeEvent(eventType, data);
|
|
159
|
+
};
|
|
160
|
+
this.callNative(
|
|
161
|
+
{
|
|
162
|
+
action: "ready",
|
|
163
|
+
params: { version: this.VERSION, apiVersion: this.API_VERSION }
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
success: () => {
|
|
167
|
+
this.log("App已准备就绪=====>");
|
|
168
|
+
this.isReady = true;
|
|
169
|
+
this.readyCallbacks.forEach((callback) => callback());
|
|
170
|
+
this.readyCallbacks = [];
|
|
171
|
+
},
|
|
172
|
+
fail: (error) => {
|
|
173
|
+
this.error("App准备就绪失败", error);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
);
|
|
177
|
+
const readyHandler = () => {
|
|
178
|
+
this.isReady = true;
|
|
179
|
+
this.readyCallbacks.forEach((callback) => callback());
|
|
180
|
+
this.readyCallbacks = [];
|
|
181
|
+
};
|
|
182
|
+
document.addEventListener("NativeBridgeReady", readyHandler);
|
|
183
|
+
this.eventListeners.set("NativeBridgeReady", readyHandler);
|
|
184
|
+
} else {
|
|
185
|
+
setTimeout(() => {
|
|
186
|
+
this.isReady = true;
|
|
187
|
+
this.readyCallbacks.forEach((callback) => callback());
|
|
188
|
+
this.readyCallbacks = [];
|
|
189
|
+
}, 0);
|
|
190
|
+
}
|
|
191
|
+
} catch (error) {
|
|
192
|
+
this.error("设置桥接环境失败", error);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
destroy() {
|
|
196
|
+
this.eventListeners.forEach((listener, event) => {
|
|
197
|
+
document.removeEventListener(event, listener);
|
|
198
|
+
});
|
|
199
|
+
this.eventListeners.clear();
|
|
200
|
+
this.callbackMap.clear();
|
|
201
|
+
this.pendingCalls.forEach((timeout) => clearTimeout(timeout));
|
|
202
|
+
this.pendingCalls.clear();
|
|
203
|
+
window.nativeBridgeCallback = void 0;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* 检测是否在App环境中
|
|
207
|
+
*/
|
|
208
|
+
checkIsInApp() {
|
|
209
|
+
var _a;
|
|
210
|
+
try {
|
|
211
|
+
const userAgent = navigator.userAgent.toLowerCase();
|
|
212
|
+
console.log(`[NativeBridge]--userAgent ${userAgent}`);
|
|
213
|
+
console.log(
|
|
214
|
+
`[NativeBridge]--messageHandlers ${JSON.stringify(
|
|
215
|
+
(_a = window.webkit) == null ? void 0 : _a.messageHandlers
|
|
216
|
+
)}`
|
|
217
|
+
);
|
|
218
|
+
return true;
|
|
219
|
+
} catch (error) {
|
|
220
|
+
this.error("检测App环境失败", error);
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* 等待桥接准备就绪
|
|
226
|
+
*/
|
|
227
|
+
ready(callback) {
|
|
228
|
+
if (this.isReady) {
|
|
229
|
+
callback();
|
|
230
|
+
} else {
|
|
231
|
+
this.readyCallbacks.push(callback);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* 调用原生方法
|
|
236
|
+
*/
|
|
237
|
+
// 调用原生方法
|
|
238
|
+
callNative(options, callbacks) {
|
|
239
|
+
var _a;
|
|
240
|
+
const callbackId = `cb_${this.callbackId++}_${Date.now()}`;
|
|
241
|
+
const {
|
|
242
|
+
action,
|
|
243
|
+
params = {},
|
|
244
|
+
timeout = this.defaultTimeout,
|
|
245
|
+
retryCount = 0,
|
|
246
|
+
retryDelay = 1e3
|
|
247
|
+
} = options;
|
|
248
|
+
if (callbacks) {
|
|
249
|
+
this.callbackMap.set(callbackId, callbacks);
|
|
250
|
+
if (timeout > 0) {
|
|
251
|
+
const controller = new AbortController();
|
|
252
|
+
const timeoutId = setTimeout(() => {
|
|
253
|
+
var _a2, _b;
|
|
254
|
+
if (controller.signal.aborted)
|
|
255
|
+
return;
|
|
256
|
+
if (this.callbackMap.has(callbackId)) {
|
|
257
|
+
this.callbackMap.delete(callbackId);
|
|
258
|
+
this.pendingCalls.delete(callbackId);
|
|
259
|
+
if (retryCount > 0) {
|
|
260
|
+
this.log(
|
|
261
|
+
`调用 ${action} 超时,${retryDelay}ms后重试 (剩余重试次数: ${retryCount})`
|
|
262
|
+
);
|
|
263
|
+
setTimeout(() => {
|
|
264
|
+
if (controller.signal.aborted)
|
|
265
|
+
return;
|
|
266
|
+
this.callNative(
|
|
267
|
+
{
|
|
268
|
+
action,
|
|
269
|
+
params,
|
|
270
|
+
timeout,
|
|
271
|
+
retryCount: retryCount - 1,
|
|
272
|
+
retryDelay
|
|
273
|
+
},
|
|
274
|
+
callbacks
|
|
275
|
+
);
|
|
276
|
+
}, retryDelay);
|
|
277
|
+
} else {
|
|
278
|
+
const timeoutError = createBridgeError(
|
|
279
|
+
BridgeErrorCode.TIMEOUT,
|
|
280
|
+
`调用 ${action} 超时 (${timeout}ms)`
|
|
281
|
+
);
|
|
282
|
+
(_a2 = callbacks.fail) == null ? void 0 : _a2.call(callbacks, timeoutError);
|
|
283
|
+
(_b = callbacks.complete) == null ? void 0 : _b.call(callbacks);
|
|
284
|
+
this.error(`调用超时: ${action}`, timeoutError);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}, timeout);
|
|
288
|
+
this.pendingCalls.set(callbackId, timeoutId);
|
|
289
|
+
controller.signal.addEventListener("abort", () => {
|
|
290
|
+
clearTimeout(timeoutId);
|
|
291
|
+
this.pendingCalls.delete(callbackId);
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
const message = { action, params, timeout, callbackId };
|
|
296
|
+
if (this.debugMode) {
|
|
297
|
+
this.log(`调用原生方法: ${action}`, params);
|
|
298
|
+
}
|
|
299
|
+
try {
|
|
300
|
+
if (this.checkIsInApp()) {
|
|
301
|
+
this.sendToNative(message);
|
|
302
|
+
} else {
|
|
303
|
+
this.mockNativeCall(message);
|
|
304
|
+
}
|
|
305
|
+
} catch (error) {
|
|
306
|
+
this.error(`调用原生方法 ${action} 失败`, error);
|
|
307
|
+
(_a = callbacks == null ? void 0 : callbacks.fail) == null ? void 0 : _a.call(callbacks, error);
|
|
308
|
+
this.releaseCallback(callbackId);
|
|
309
|
+
}
|
|
310
|
+
return callbackId;
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* 同步调用原生方法
|
|
314
|
+
*
|
|
315
|
+
* 该方法通过不同的桥接方式(iOS WKWebView、Android WebView或iframe)与原生应用通信,
|
|
316
|
+
* 发送指定的操作指令和参数,并返回处理结果
|
|
317
|
+
*
|
|
318
|
+
* @param action - 要执行的原生操作名称
|
|
319
|
+
* @param params - 传递给原生方法的参数对象
|
|
320
|
+
* @returns 处理后的原生返回数据
|
|
321
|
+
* @throws 当发送消息到原生App失败时抛出错误
|
|
322
|
+
*/
|
|
323
|
+
callNativeSync(action, params, callbacks) {
|
|
324
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
325
|
+
let data = void 0;
|
|
326
|
+
try {
|
|
327
|
+
const messageString = JSON.stringify({ action, params });
|
|
328
|
+
const win = typeof window !== "undefined" ? window : void 0;
|
|
329
|
+
if ((_c = (_b = (_a = win == null ? void 0 : win.webkit) == null ? void 0 : _a.messageHandlers) == null ? void 0 : _b.nativeBridge) == null ? void 0 : _c.postSyncMessage) {
|
|
330
|
+
this.log(`发送消息到 iOS WKWebView: ${messageString}`);
|
|
331
|
+
data = win.webkit.messageHandlers.nativeBridge.postSyncMessage(messageString);
|
|
332
|
+
} else if ((_d = win == null ? void 0 : win.AndroidBridge) == null ? void 0 : _d.postSyncMessage) {
|
|
333
|
+
this.log(`发送消息到安卓原生App: ${messageString}`);
|
|
334
|
+
data = win.AndroidBridge.postSyncMessage(messageString);
|
|
335
|
+
} else if (win) {
|
|
336
|
+
try {
|
|
337
|
+
const iframe = document.createElement("iframe");
|
|
338
|
+
iframe.style.display = "none";
|
|
339
|
+
iframe.src = `yourapp://bridge?message=${encodeURIComponent(
|
|
340
|
+
messageString
|
|
341
|
+
)}`;
|
|
342
|
+
document.body.appendChild(iframe);
|
|
343
|
+
setTimeout(() => {
|
|
344
|
+
document.body.removeChild(iframe);
|
|
345
|
+
}, 0);
|
|
346
|
+
return void 0;
|
|
347
|
+
} catch (iframeError) {
|
|
348
|
+
(_e = callbacks == null ? void 0 : callbacks.fail) == null ? void 0 : _e.call(callbacks, iframeError);
|
|
349
|
+
throw createBridgeError(
|
|
350
|
+
BridgeErrorCode.SYSTEM_ERROR,
|
|
351
|
+
`创建 iframe 失败: ${iframeError.message}`
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
} catch (error) {
|
|
356
|
+
(_f = callbacks == null ? void 0 : callbacks.fail) == null ? void 0 : _f.call(callbacks, error);
|
|
357
|
+
throw createBridgeError(
|
|
358
|
+
BridgeErrorCode.SYSTEM_ERROR,
|
|
359
|
+
`发送消息到原生App失败: ${error.message}`
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
if (this.debugMode) {
|
|
363
|
+
this.log(`收到原生回调: success`, data);
|
|
364
|
+
}
|
|
365
|
+
(_g = callbacks == null ? void 0 : callbacks.success) == null ? void 0 : _g.call(callbacks, processJsonValue(data));
|
|
366
|
+
return processJsonValue(data);
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* 发送消息到原生
|
|
370
|
+
*/
|
|
371
|
+
sendToNative(message) {
|
|
372
|
+
try {
|
|
373
|
+
const messageString = JSON.stringify(message);
|
|
374
|
+
if (typeof window !== "undefined" && window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.nativeBridge) {
|
|
375
|
+
this.log(`发送消息到 iOS WKWebView: ${messageString}`);
|
|
376
|
+
window.webkit.messageHandlers.nativeBridge.postMessage(
|
|
377
|
+
messageString
|
|
378
|
+
);
|
|
379
|
+
} else if (window.AndroidBridge && window.AndroidBridge.postMessage) {
|
|
380
|
+
this.log(`发送消息到安卓原生App: ${messageString}`);
|
|
381
|
+
window.AndroidBridge.postMessage(messageString);
|
|
382
|
+
} else {
|
|
383
|
+
const iframe = document.createElement("iframe");
|
|
384
|
+
iframe.style.display = "none";
|
|
385
|
+
iframe.src = `yourapp://bridge?message=${encodeURIComponent(
|
|
386
|
+
messageString
|
|
387
|
+
)}`;
|
|
388
|
+
document.body.appendChild(iframe);
|
|
389
|
+
setTimeout(() => {
|
|
390
|
+
document.body.removeChild(iframe);
|
|
391
|
+
}, 0);
|
|
392
|
+
}
|
|
393
|
+
} catch (error) {
|
|
394
|
+
throw createBridgeError(
|
|
395
|
+
BridgeErrorCode.SYSTEM_ERROR,
|
|
396
|
+
`发送消息到原生App失败: ${error.message}`
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* 处理原生回调
|
|
402
|
+
*/
|
|
403
|
+
handleNativeCallback(callbackId, type, data) {
|
|
404
|
+
const callbacks = this.callbackMap.get(callbackId);
|
|
405
|
+
if (!callbacks) {
|
|
406
|
+
this.warn(`未找到回调ID: ${callbackId}`);
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
if (this.pendingCalls.has(callbackId)) {
|
|
410
|
+
clearTimeout(this.pendingCalls.get(callbackId));
|
|
411
|
+
this.pendingCalls.delete(callbackId);
|
|
412
|
+
}
|
|
413
|
+
if (this.debugMode) {
|
|
414
|
+
this.log(`收到原生回调: ${type}`, data);
|
|
415
|
+
}
|
|
416
|
+
try {
|
|
417
|
+
if (type === "success" && callbacks.success) {
|
|
418
|
+
callbacks.success(processJsonValue(data));
|
|
419
|
+
} else if (type === "fail" && callbacks.fail) {
|
|
420
|
+
let error;
|
|
421
|
+
if (data instanceof Error) {
|
|
422
|
+
error = data;
|
|
423
|
+
} else if (typeof data === "string") {
|
|
424
|
+
error = new Error(data);
|
|
425
|
+
} else if (data && data.message) {
|
|
426
|
+
error = new Error(data.message);
|
|
427
|
+
if (data.code) {
|
|
428
|
+
error.code = data.code;
|
|
429
|
+
}
|
|
430
|
+
} else {
|
|
431
|
+
error = new Error("未知错误");
|
|
432
|
+
}
|
|
433
|
+
callbacks.fail(error);
|
|
434
|
+
}
|
|
435
|
+
if (callbacks.complete) {
|
|
436
|
+
callbacks.complete();
|
|
437
|
+
}
|
|
438
|
+
} catch (error) {
|
|
439
|
+
this.error("执行回调函数时出错", error);
|
|
440
|
+
}
|
|
441
|
+
this.callbackMap.delete(callbackId);
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* 模拟原生调用(仅用于开发测试)
|
|
445
|
+
*/
|
|
446
|
+
mockNativeCall(message) {
|
|
447
|
+
if (this.debugMode) {
|
|
448
|
+
this.log("模拟原生调用:", message);
|
|
449
|
+
}
|
|
450
|
+
switch (message.action) {
|
|
451
|
+
case "ready":
|
|
452
|
+
setTimeout(() => {
|
|
453
|
+
const event = new Event("NativeBridgeReady");
|
|
454
|
+
document.dispatchEvent(event);
|
|
455
|
+
}, 100);
|
|
456
|
+
break;
|
|
457
|
+
default:
|
|
458
|
+
for (const [plugin] of this.plugins.entries()) {
|
|
459
|
+
if (plugin.mockAction && typeof plugin.mockAction === "function") {
|
|
460
|
+
if (plugin.mockAction(message)) {
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
this.warn(`未实现的模拟功能: ${message.action}`);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* 释放回调
|
|
470
|
+
*/
|
|
471
|
+
releaseCallback(callbackId) {
|
|
472
|
+
if (this.callbackMap.has(callbackId)) {
|
|
473
|
+
this.callbackMap.delete(callbackId);
|
|
474
|
+
}
|
|
475
|
+
if (this.pendingCalls.has(callbackId)) {
|
|
476
|
+
clearTimeout(this.pendingCalls.get(callbackId));
|
|
477
|
+
this.pendingCalls.delete(callbackId);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* 设置调试模式
|
|
482
|
+
*/
|
|
483
|
+
setDebug(debug) {
|
|
484
|
+
this.debugMode = debug;
|
|
485
|
+
if (debug) {
|
|
486
|
+
console.log("[NativeBridge] 调试模式已开启 ✅");
|
|
487
|
+
} else {
|
|
488
|
+
console.log("[NativeBridge] 调试模式已关闭 ❌");
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* 设置全局错误处理器
|
|
493
|
+
*/
|
|
494
|
+
setErrorHandler(handler) {
|
|
495
|
+
this.errorHandler = handler;
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* 获取已安装插件列表
|
|
499
|
+
*/
|
|
500
|
+
getInstalledPlugins() {
|
|
501
|
+
return Array.from(this.plugins.keys());
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* 获取当前调试模式状态
|
|
505
|
+
* @returns 是否开启调试模式
|
|
506
|
+
*/
|
|
507
|
+
isDebugMode() {
|
|
508
|
+
return this.debugMode;
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* 日志记录 - 信息
|
|
512
|
+
*/
|
|
513
|
+
log(message, ...args) {
|
|
514
|
+
if (message.includes("调试模式已")) {
|
|
515
|
+
console.log(`[NativeBridge] 📢 ${message}`, ...args);
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
if (this.debugMode) {
|
|
519
|
+
console.log(`[NativeBridge] ${message}`, ...args);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* 日志记录 - 警告
|
|
524
|
+
*/
|
|
525
|
+
warn(message, ...args) {
|
|
526
|
+
if (this.debugMode) {
|
|
527
|
+
console.warn(`[NativeBridge] ⚠️ ${message}`, ...args);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* 日志记录 - 错误
|
|
532
|
+
*/
|
|
533
|
+
error(source, error) {
|
|
534
|
+
console.error(`[NativeBridge] 🛑 ${source}:`, error);
|
|
535
|
+
if (this.errorHandler) {
|
|
536
|
+
try {
|
|
537
|
+
this.errorHandler(error, source);
|
|
538
|
+
} catch (handlerError) {
|
|
539
|
+
console.error("[NativeBridge] 错误处理器执行失败:", handlerError);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* 获取回调映射表
|
|
545
|
+
*/
|
|
546
|
+
getCallBackMap() {
|
|
547
|
+
return this.callbackMap;
|
|
548
|
+
}
|
|
549
|
+
};
|
|
550
|
+
__publicField(_NativeBridgeCore, "instance");
|
|
551
|
+
let NativeBridgeCore = _NativeBridgeCore;
|
|
552
|
+
function processJsonValue(value) {
|
|
553
|
+
if (value === void 0 || value === null) {
|
|
554
|
+
return value;
|
|
555
|
+
}
|
|
556
|
+
if (typeof value !== "string") {
|
|
557
|
+
return value;
|
|
558
|
+
}
|
|
559
|
+
if (value === "") {
|
|
560
|
+
return value;
|
|
561
|
+
}
|
|
562
|
+
try {
|
|
563
|
+
const trimmedValue = value.trim();
|
|
564
|
+
if (trimmedValue.startsWith("{") && trimmedValue.endsWith("}") || trimmedValue.startsWith("[") && trimmedValue.endsWith("]")) {
|
|
565
|
+
JSON.parse(value);
|
|
566
|
+
return JSON.parse(value);
|
|
567
|
+
}
|
|
568
|
+
return value;
|
|
569
|
+
} catch (e) {
|
|
570
|
+
return value;
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
const createNativeBridge = (options) => {
|
|
574
|
+
return NativeBridgeCore.getInstance(options);
|
|
575
|
+
};
|
|
576
|
+
NativeBridgeCore.getInstance();
|
|
577
|
+
class LocationPlugin {
|
|
578
|
+
constructor() {
|
|
579
|
+
__publicField(this, "name", "location");
|
|
580
|
+
__publicField(this, "version", "1.0.0");
|
|
581
|
+
__publicField(this, "context");
|
|
582
|
+
}
|
|
583
|
+
install(context) {
|
|
584
|
+
this.context = context;
|
|
585
|
+
context.bridge.registerMethod(
|
|
586
|
+
"getLocationSync",
|
|
587
|
+
this.getLocationSync.bind(this)
|
|
588
|
+
);
|
|
589
|
+
context.bridge.registerMethod("getLocation", this.getLocation.bind(this));
|
|
590
|
+
context.bridge.log(`位置服务插件 v${this.version} 已安装`);
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* 获取当前位置信息
|
|
594
|
+
* @param options - 位置选项,可选参数包括:
|
|
595
|
+
* - targetAccuracy: 目标精度,单位米,默认200
|
|
596
|
+
* - coordinate: 坐标类型,默认1(可能表示WGS84)
|
|
597
|
+
* - withReGeocode: 是否需要逆地理编码,默认true
|
|
598
|
+
* - useCache: 是否使用缓存,默认true
|
|
599
|
+
* @returns 返回一个Promise,解析为LocationInfo对象
|
|
600
|
+
*/
|
|
601
|
+
getLocation(options = {
|
|
602
|
+
targetAccuracy: 200,
|
|
603
|
+
coordinate: 1,
|
|
604
|
+
withReGeocode: true,
|
|
605
|
+
useCache: true
|
|
606
|
+
}) {
|
|
607
|
+
options.useCache = options.useCache ?? true;
|
|
608
|
+
return new Promise((resolve, reject) => {
|
|
609
|
+
this.getLocationSync({
|
|
610
|
+
...options,
|
|
611
|
+
success: (location) => {
|
|
612
|
+
resolve(location);
|
|
613
|
+
},
|
|
614
|
+
fail: (error) => reject(error)
|
|
615
|
+
});
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
getLocationSync(options = {
|
|
619
|
+
targetAccuracy: 200,
|
|
620
|
+
coordinate: 1,
|
|
621
|
+
withReGeocode: true,
|
|
622
|
+
useCache: true
|
|
623
|
+
}) {
|
|
624
|
+
if (!this.context) {
|
|
625
|
+
throw createBridgeError(
|
|
626
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
627
|
+
"插件未正确初始化"
|
|
628
|
+
);
|
|
629
|
+
}
|
|
630
|
+
if (typeof options.targetAccuracy !== "number") {
|
|
631
|
+
throw createBridgeError(
|
|
632
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
633
|
+
"targetAccuracy参数格式错误"
|
|
634
|
+
);
|
|
635
|
+
}
|
|
636
|
+
if (typeof options.coordinate !== "number") {
|
|
637
|
+
throw createBridgeError(
|
|
638
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
639
|
+
"coordinate参数格式错误"
|
|
640
|
+
);
|
|
641
|
+
}
|
|
642
|
+
if (typeof options.withReGeocode !== "boolean") {
|
|
643
|
+
throw createBridgeError(
|
|
644
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
645
|
+
"withReGeocode参数格式错误"
|
|
646
|
+
);
|
|
647
|
+
}
|
|
648
|
+
if (typeof options.useCache !== "boolean") {
|
|
649
|
+
throw createBridgeError(
|
|
650
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
651
|
+
"useCache参数格式错误"
|
|
652
|
+
);
|
|
653
|
+
}
|
|
654
|
+
options.useCache = options.useCache ?? true;
|
|
655
|
+
const callbacks = {
|
|
656
|
+
success: options.success,
|
|
657
|
+
fail: options.fail,
|
|
658
|
+
complete: options.complete
|
|
659
|
+
};
|
|
660
|
+
this.context.bridge.ready(() => {
|
|
661
|
+
var _a;
|
|
662
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
663
|
+
{
|
|
664
|
+
action: "getLocation",
|
|
665
|
+
params: options
|
|
666
|
+
},
|
|
667
|
+
callbacks
|
|
668
|
+
);
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
const locationPlugin = new LocationPlugin();
|
|
673
|
+
class DevicePlugin {
|
|
674
|
+
constructor() {
|
|
675
|
+
__publicField(this, "name", "device");
|
|
676
|
+
__publicField(this, "version", "1.0.0");
|
|
677
|
+
__publicField(this, "context");
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* 安装插件
|
|
681
|
+
*/
|
|
682
|
+
install(context) {
|
|
683
|
+
this.context = context;
|
|
684
|
+
context.bridge.registerMethod("getSystemInfo", this.getSystemInfo.bind(this));
|
|
685
|
+
context.bridge.registerMethod("getSystemInfoSync", this.getSystemInfoSync.bind(this));
|
|
686
|
+
context.bridge.registerMethod("getWifiList", this.getWifiList.bind(this));
|
|
687
|
+
context.bridge.registerMethod("getUUID", this.getUUID.bind(this));
|
|
688
|
+
context.bridge.registerMethod(
|
|
689
|
+
"setKeepScreenOn",
|
|
690
|
+
this.setKeepScreenOn.bind(this)
|
|
691
|
+
);
|
|
692
|
+
context.bridge.log(`设备信息插件 v${this.version} 已安装`);
|
|
693
|
+
}
|
|
694
|
+
/**
|
|
695
|
+
* 设置屏幕常亮状态
|
|
696
|
+
* @param options 配置选项
|
|
697
|
+
* @param options.isKeep 是否保持屏幕常亮,默认为true
|
|
698
|
+
* @param options.success 成功回调
|
|
699
|
+
* @param options.fail 失败回调
|
|
700
|
+
* @param options.complete 完成回调
|
|
701
|
+
* @returns void
|
|
702
|
+
*/
|
|
703
|
+
setKeepScreenOn(options) {
|
|
704
|
+
if (!this.context) {
|
|
705
|
+
throw createBridgeError(
|
|
706
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
707
|
+
"插件未正确初始化"
|
|
708
|
+
);
|
|
709
|
+
}
|
|
710
|
+
if (options.isKeep === void 0 || options.isKeep === null) {
|
|
711
|
+
throw createBridgeError(
|
|
712
|
+
BridgeErrorCode.JSON_ERROR,
|
|
713
|
+
"缺少必需参数:isKeep"
|
|
714
|
+
);
|
|
715
|
+
}
|
|
716
|
+
if (options.isKeep !== void 0 && typeof options.isKeep !== "boolean") {
|
|
717
|
+
throw createBridgeError(
|
|
718
|
+
BridgeErrorCode.JSON_ERROR,
|
|
719
|
+
"isKeep参数格式不正确"
|
|
720
|
+
);
|
|
721
|
+
}
|
|
722
|
+
const callbacks = {
|
|
723
|
+
success: options.success,
|
|
724
|
+
fail: options.fail,
|
|
725
|
+
complete: options.complete
|
|
726
|
+
};
|
|
727
|
+
this.context.bridge.ready(() => {
|
|
728
|
+
var _a;
|
|
729
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
730
|
+
{
|
|
731
|
+
action: "setKeepScreenOn",
|
|
732
|
+
params: { isKeep: options.isKeep }
|
|
733
|
+
},
|
|
734
|
+
callbacks
|
|
735
|
+
);
|
|
736
|
+
});
|
|
737
|
+
}
|
|
738
|
+
/**
|
|
739
|
+
* 同步获取设备系统信息
|
|
740
|
+
*
|
|
741
|
+
* @param options - 获取设备信息的配置选项
|
|
742
|
+
* @returns any - 返回设备信息结果
|
|
743
|
+
*/
|
|
744
|
+
getSystemInfoSync(options = {}) {
|
|
745
|
+
var _a;
|
|
746
|
+
if (!this.context) {
|
|
747
|
+
throw createBridgeError(
|
|
748
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
749
|
+
"插件未正确初始化"
|
|
750
|
+
);
|
|
751
|
+
}
|
|
752
|
+
const callbacks = {
|
|
753
|
+
success: (res) => {
|
|
754
|
+
var _a2;
|
|
755
|
+
(_a2 = options.success) == null ? void 0 : _a2.call(options, res);
|
|
756
|
+
},
|
|
757
|
+
fail: options.fail,
|
|
758
|
+
complete: options.complete
|
|
759
|
+
};
|
|
760
|
+
return (_a = this.context) == null ? void 0 : _a.bridge.callNativeSync("getSystemInfoSync", {}, callbacks);
|
|
761
|
+
}
|
|
762
|
+
/**
|
|
763
|
+
* 获取设备信息(Promise 风格)
|
|
764
|
+
*
|
|
765
|
+
* @description
|
|
766
|
+
* 1. 检查插件是否初始化,未初始化则抛出错误
|
|
767
|
+
* 2. 创建并返回一个Promise,用于异步获取设备信息
|
|
768
|
+
* 3. 通过Native Bridge调用原生能力获取设备信息
|
|
769
|
+
*
|
|
770
|
+
* @throws {BridgeError} 当插件未初始化时抛出错误
|
|
771
|
+
* @returns {Promise<DeviceInfo>} 返回包含设备信息的Promise对象
|
|
772
|
+
*/
|
|
773
|
+
getSystemInfo() {
|
|
774
|
+
return new Promise((resolve, reject) => {
|
|
775
|
+
if (!this.context) {
|
|
776
|
+
throw createBridgeError(
|
|
777
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
778
|
+
"插件未正确初始化"
|
|
779
|
+
);
|
|
780
|
+
}
|
|
781
|
+
const callbacks = {
|
|
782
|
+
success: (res) => {
|
|
783
|
+
resolve(res);
|
|
784
|
+
},
|
|
785
|
+
fail: (err) => {
|
|
786
|
+
reject(err);
|
|
787
|
+
},
|
|
788
|
+
complete: () => {
|
|
789
|
+
}
|
|
790
|
+
};
|
|
791
|
+
this.context.bridge.ready(() => {
|
|
792
|
+
var _a;
|
|
793
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
794
|
+
{
|
|
795
|
+
action: "getSystemInfo",
|
|
796
|
+
params: {}
|
|
797
|
+
},
|
|
798
|
+
callbacks
|
|
799
|
+
);
|
|
800
|
+
});
|
|
801
|
+
});
|
|
802
|
+
}
|
|
803
|
+
/**
|
|
804
|
+
* 获取WiFi列表
|
|
805
|
+
*
|
|
806
|
+
* 该方法通过原生桥接获取当前可用的WiFi列表,返回一个Promise对象
|
|
807
|
+
*
|
|
808
|
+
* @param {GetWifiOptions} [options={}] - 获取WiFi列表的可选参数对象
|
|
809
|
+
* @returns {Promise<WifiListAsyncResult>} 返回一个Promise,解析后包含WiFi列表或错误信息
|
|
810
|
+
* - resultCode: 0表示成功,1表示失败
|
|
811
|
+
* - resultMessage: 结果描述信息
|
|
812
|
+
* - wifiList: WiFi列表数组
|
|
813
|
+
*/
|
|
814
|
+
getWifiList(options = {}) {
|
|
815
|
+
return new Promise((resolve, reject) => {
|
|
816
|
+
if (!this.context) {
|
|
817
|
+
throw createBridgeError(
|
|
818
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
819
|
+
"插件未正确初始化"
|
|
820
|
+
);
|
|
821
|
+
}
|
|
822
|
+
if (options.timeout !== void 0 && (typeof options.timeout !== "number" || options.timeout <= 0 || !Number.isInteger(options.timeout))) {
|
|
823
|
+
throw createBridgeError(
|
|
824
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
825
|
+
"timeout参数格式错误"
|
|
826
|
+
);
|
|
827
|
+
}
|
|
828
|
+
if (options.cacheTime !== void 0 && (typeof options.cacheTime !== "number" || options.cacheTime <= 0 || !Number.isInteger(options.cacheTime))) {
|
|
829
|
+
throw createBridgeError(
|
|
830
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
831
|
+
"cacheTime参数格式错误"
|
|
832
|
+
);
|
|
833
|
+
}
|
|
834
|
+
const callbacks = {
|
|
835
|
+
success: (res) => {
|
|
836
|
+
resolve(res);
|
|
837
|
+
},
|
|
838
|
+
fail: (err) => {
|
|
839
|
+
const resp = {
|
|
840
|
+
resultCode: 1,
|
|
841
|
+
resultMessage: err.message,
|
|
842
|
+
wifiList: []
|
|
843
|
+
};
|
|
844
|
+
console.error("保存失败:", resp);
|
|
845
|
+
reject(resp);
|
|
846
|
+
},
|
|
847
|
+
complete: () => {
|
|
848
|
+
}
|
|
849
|
+
};
|
|
850
|
+
this.context.bridge.ready(() => {
|
|
851
|
+
var _a;
|
|
852
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
853
|
+
{
|
|
854
|
+
action: "getWifiList",
|
|
855
|
+
params: { ...options }
|
|
856
|
+
},
|
|
857
|
+
callbacks
|
|
858
|
+
);
|
|
859
|
+
});
|
|
860
|
+
});
|
|
861
|
+
}
|
|
862
|
+
/**
|
|
863
|
+
* 获取设备的唯一标识符
|
|
864
|
+
* @returns 返回一个Promise,解析后得到设备的UUID信息
|
|
865
|
+
* @throws 如果插件未正确初始化会抛出错误
|
|
866
|
+
*/
|
|
867
|
+
getUUID() {
|
|
868
|
+
return new Promise((resolve, reject) => {
|
|
869
|
+
const callbacks = {
|
|
870
|
+
success: (res) => {
|
|
871
|
+
resolve(res);
|
|
872
|
+
},
|
|
873
|
+
fail: (err) => {
|
|
874
|
+
reject(err);
|
|
875
|
+
}
|
|
876
|
+
};
|
|
877
|
+
if (!this.context) {
|
|
878
|
+
throw createBridgeError(
|
|
879
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
880
|
+
"插件未正确初始化"
|
|
881
|
+
);
|
|
882
|
+
}
|
|
883
|
+
this.context.bridge.ready(() => {
|
|
884
|
+
var _a;
|
|
885
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
886
|
+
{
|
|
887
|
+
action: "getUUID",
|
|
888
|
+
params: {}
|
|
889
|
+
},
|
|
890
|
+
callbacks
|
|
891
|
+
);
|
|
892
|
+
});
|
|
893
|
+
});
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
const devicePlugin = new DevicePlugin();
|
|
897
|
+
const MAX_VIDEO_DURATION = 60;
|
|
898
|
+
class MediaPlugin {
|
|
899
|
+
constructor() {
|
|
900
|
+
__publicField(this, "name", "media");
|
|
901
|
+
__publicField(this, "version", "1.0.0");
|
|
902
|
+
__publicField(this, "context");
|
|
903
|
+
}
|
|
904
|
+
/**
|
|
905
|
+
* 安装插件
|
|
906
|
+
*/
|
|
907
|
+
install(context) {
|
|
908
|
+
this.context = context;
|
|
909
|
+
context.bridge.registerMethod("saveImage", this.saveImage.bind(this));
|
|
910
|
+
context.bridge.registerMethod("scan", this.scan.bind(this));
|
|
911
|
+
context.bridge.registerMethod("chooseImage", this.chooseImage.bind(this));
|
|
912
|
+
context.bridge.registerMethod("previewImage", this.previewImage.bind(this));
|
|
913
|
+
context.bridge.registerMethod("compressImage", this.compressImage.bind(this));
|
|
914
|
+
context.bridge.registerMethod("chooseVideo", this.chooseVideo.bind(this));
|
|
915
|
+
context.bridge.log(`设备信息插件 v${this.version} 已安装`);
|
|
916
|
+
}
|
|
917
|
+
/**
|
|
918
|
+
* 执行扫码功能
|
|
919
|
+
*
|
|
920
|
+
* @description 调用原生扫码功能,返回扫码结果。该方法会等待桥接准备就绪后调用原生扫码API。
|
|
921
|
+
*
|
|
922
|
+
* @param options - 扫码配置选项,排除了success和fail回调
|
|
923
|
+
* @param options.complete - 可选的完成回调函数,无论成功或失败都会执行
|
|
924
|
+
*
|
|
925
|
+
* @returns Promise<ScanRes> 返回包含扫码结果的Promise对象
|
|
926
|
+
*
|
|
927
|
+
* @throws {BridgeError} 当插件未正确初始化时抛出PLUGIN_NOT_INITIALIZED错误
|
|
928
|
+
* @throws {BridgeError} 当扫码失败时抛出SYSTEM_ERROR错误
|
|
929
|
+
*
|
|
930
|
+
* @example
|
|
931
|
+
*/
|
|
932
|
+
scan(options) {
|
|
933
|
+
return new Promise((resolve, reject) => {
|
|
934
|
+
if (!this.context) {
|
|
935
|
+
throw createBridgeError(
|
|
936
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
937
|
+
"插件未正确初始化"
|
|
938
|
+
);
|
|
939
|
+
}
|
|
940
|
+
if (options.key !== void 0 && typeof options.key !== "string" || options.key !== void 0 && !["qr", "bar"].includes(options.key)) {
|
|
941
|
+
throw createBridgeError(
|
|
942
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
943
|
+
"key参数格式不正确"
|
|
944
|
+
);
|
|
945
|
+
}
|
|
946
|
+
if (options.isSupportAlbum !== void 0 && typeof options.isSupportAlbum !== "boolean") {
|
|
947
|
+
throw createBridgeError(
|
|
948
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
949
|
+
"isSupportAlbum参数格式不正确"
|
|
950
|
+
);
|
|
951
|
+
}
|
|
952
|
+
options.key = options.key || "qr";
|
|
953
|
+
options.isSupportAlbum = options.isSupportAlbum ?? true;
|
|
954
|
+
const callbacks = {
|
|
955
|
+
success: (res) => {
|
|
956
|
+
resolve(res);
|
|
957
|
+
},
|
|
958
|
+
fail: (err) => {
|
|
959
|
+
reject(
|
|
960
|
+
createBridgeError(BridgeErrorCode.SYSTEM_ERROR, "扫码失败", err)
|
|
961
|
+
);
|
|
962
|
+
},
|
|
963
|
+
complete: () => {
|
|
964
|
+
var _a;
|
|
965
|
+
(_a = options.complete) == null ? void 0 : _a.call(options);
|
|
966
|
+
}
|
|
967
|
+
};
|
|
968
|
+
this.context.bridge.ready(() => {
|
|
969
|
+
var _a;
|
|
970
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
971
|
+
{
|
|
972
|
+
action: "scan",
|
|
973
|
+
params: { ...options }
|
|
974
|
+
},
|
|
975
|
+
callbacks
|
|
976
|
+
);
|
|
977
|
+
});
|
|
978
|
+
});
|
|
979
|
+
}
|
|
980
|
+
/**
|
|
981
|
+
* 保存图片到相册(Promise 风格)
|
|
982
|
+
*/
|
|
983
|
+
saveImage(options) {
|
|
984
|
+
return new Promise((resolve, reject) => {
|
|
985
|
+
if (!this.context) {
|
|
986
|
+
throw createBridgeError(
|
|
987
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
988
|
+
"插件未正确初始化"
|
|
989
|
+
);
|
|
990
|
+
}
|
|
991
|
+
if (!options.url) {
|
|
992
|
+
throw createBridgeError(
|
|
993
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
994
|
+
"缺少必需参数:url"
|
|
995
|
+
);
|
|
996
|
+
}
|
|
997
|
+
if (typeof options.url !== "string" || !options.url.trim()) {
|
|
998
|
+
throw createBridgeError(
|
|
999
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1000
|
+
"url参数格式不正确"
|
|
1001
|
+
);
|
|
1002
|
+
}
|
|
1003
|
+
const callbacks = {
|
|
1004
|
+
success: (res) => {
|
|
1005
|
+
console.log("保存成功:", res);
|
|
1006
|
+
const resp = {
|
|
1007
|
+
code: 1,
|
|
1008
|
+
message: "保存成功",
|
|
1009
|
+
data: res
|
|
1010
|
+
};
|
|
1011
|
+
resolve(resp);
|
|
1012
|
+
},
|
|
1013
|
+
fail: (err) => {
|
|
1014
|
+
const resp = {
|
|
1015
|
+
code: 2,
|
|
1016
|
+
message: "保存失败",
|
|
1017
|
+
data: err
|
|
1018
|
+
};
|
|
1019
|
+
console.error("保存失败:", resp);
|
|
1020
|
+
reject(resp);
|
|
1021
|
+
},
|
|
1022
|
+
complete: () => {
|
|
1023
|
+
var _a;
|
|
1024
|
+
(_a = options.complete) == null ? void 0 : _a.call(options);
|
|
1025
|
+
}
|
|
1026
|
+
};
|
|
1027
|
+
this.context.bridge.ready(() => {
|
|
1028
|
+
var _a;
|
|
1029
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
1030
|
+
{
|
|
1031
|
+
action: "saveImage",
|
|
1032
|
+
params: { url: options.url }
|
|
1033
|
+
},
|
|
1034
|
+
callbacks
|
|
1035
|
+
);
|
|
1036
|
+
});
|
|
1037
|
+
});
|
|
1038
|
+
}
|
|
1039
|
+
/**
|
|
1040
|
+
* 选择图片功能
|
|
1041
|
+
* @param options 选择图片的配置选项
|
|
1042
|
+
* @returns 返回一个Promise,解析为选择的图片响应数据
|
|
1043
|
+
*/
|
|
1044
|
+
chooseImage(options = {}) {
|
|
1045
|
+
return new Promise((resolve, reject) => {
|
|
1046
|
+
if (!this.context) {
|
|
1047
|
+
throw createBridgeError(
|
|
1048
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1049
|
+
"插件未正确初始化"
|
|
1050
|
+
);
|
|
1051
|
+
}
|
|
1052
|
+
if (options.count !== void 0 && (typeof options.count !== "number" || options.count <= 0 || !Number.isInteger(options.count))) {
|
|
1053
|
+
throw createBridgeError(
|
|
1054
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1055
|
+
"count参数格式不正确"
|
|
1056
|
+
);
|
|
1057
|
+
}
|
|
1058
|
+
const validSourceType = ["camera", "album"];
|
|
1059
|
+
if (options.sourceType && !Array.isArray(options.sourceType) || options.sourceType && options.sourceType.length === 0) {
|
|
1060
|
+
throw createBridgeError(
|
|
1061
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1062
|
+
"sourceType参数格式不正确"
|
|
1063
|
+
);
|
|
1064
|
+
}
|
|
1065
|
+
if (options.sourceType && options.sourceType.some((type) => !validSourceType.includes(type))) {
|
|
1066
|
+
throw createBridgeError(
|
|
1067
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1068
|
+
"sourceType参数格式不正确"
|
|
1069
|
+
);
|
|
1070
|
+
}
|
|
1071
|
+
options.count = options.count || 1;
|
|
1072
|
+
options.sourceType = options.sourceType || ["camera", "album"];
|
|
1073
|
+
const callbacks = {
|
|
1074
|
+
success: (res) => {
|
|
1075
|
+
resolve(res || { filePaths: [], fileRealPaths: [] });
|
|
1076
|
+
},
|
|
1077
|
+
fail: (err) => {
|
|
1078
|
+
console.error("选择图片失败:", err);
|
|
1079
|
+
reject(err);
|
|
1080
|
+
},
|
|
1081
|
+
complete: () => {
|
|
1082
|
+
}
|
|
1083
|
+
};
|
|
1084
|
+
this.context.bridge.ready(() => {
|
|
1085
|
+
var _a;
|
|
1086
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
1087
|
+
{
|
|
1088
|
+
action: "chooseImage",
|
|
1089
|
+
params: options
|
|
1090
|
+
},
|
|
1091
|
+
callbacks
|
|
1092
|
+
);
|
|
1093
|
+
});
|
|
1094
|
+
});
|
|
1095
|
+
}
|
|
1096
|
+
/**
|
|
1097
|
+
* 预览图片功能
|
|
1098
|
+
* @param options 预览图片的配置参数对象,包含以下属性:
|
|
1099
|
+
* - current: 可选,number类型,默认0,表示从第几张图片开始预览
|
|
1100
|
+
* - urls: string[]类型,图片URL数组
|
|
1101
|
+
* - 其他可能的自定义参数
|
|
1102
|
+
* @returns Promise<void>
|
|
1103
|
+
* 成功时:解析为原生返回的结果对象(已JSON.parse处理)
|
|
1104
|
+
* 失败时:拒绝返回错误信息
|
|
1105
|
+
*/
|
|
1106
|
+
previewImage(options) {
|
|
1107
|
+
return new Promise((resolve, reject) => {
|
|
1108
|
+
var _a;
|
|
1109
|
+
if (!this.context) {
|
|
1110
|
+
throw createBridgeError(
|
|
1111
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1112
|
+
"插件未正确初始化"
|
|
1113
|
+
);
|
|
1114
|
+
}
|
|
1115
|
+
if (!options.urls || !Array.isArray(options.urls) || options.urls.length === 0) {
|
|
1116
|
+
throw createBridgeError(
|
|
1117
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1118
|
+
"urls参数格式不正确"
|
|
1119
|
+
);
|
|
1120
|
+
}
|
|
1121
|
+
options.current = options.current || 0;
|
|
1122
|
+
const callbacks = {
|
|
1123
|
+
success: (res) => {
|
|
1124
|
+
resolve(res);
|
|
1125
|
+
},
|
|
1126
|
+
fail: (err) => {
|
|
1127
|
+
reject(err);
|
|
1128
|
+
},
|
|
1129
|
+
complete: () => {
|
|
1130
|
+
}
|
|
1131
|
+
};
|
|
1132
|
+
(_a = this.context) == null ? void 0 : _a.bridge.ready(() => {
|
|
1133
|
+
var _a2;
|
|
1134
|
+
(_a2 = this.context) == null ? void 0 : _a2.bridge.callNative(
|
|
1135
|
+
{
|
|
1136
|
+
action: "previewImage",
|
|
1137
|
+
params: options
|
|
1138
|
+
},
|
|
1139
|
+
callbacks
|
|
1140
|
+
);
|
|
1141
|
+
});
|
|
1142
|
+
});
|
|
1143
|
+
}
|
|
1144
|
+
/**
|
|
1145
|
+
* 压缩图片
|
|
1146
|
+
* @param options 压缩图片的配置选项
|
|
1147
|
+
* @returns 返回一个Promise,解析为压缩后的图片响应数据
|
|
1148
|
+
*/
|
|
1149
|
+
compressImage(options) {
|
|
1150
|
+
return new Promise((resolve, reject) => {
|
|
1151
|
+
if (!this.context) {
|
|
1152
|
+
throw createBridgeError(
|
|
1153
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1154
|
+
"插件未正确初始化"
|
|
1155
|
+
);
|
|
1156
|
+
}
|
|
1157
|
+
if (!options.filePaths || !Array.isArray(options.filePaths) || options.filePaths.length === 0) {
|
|
1158
|
+
throw createBridgeError(
|
|
1159
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1160
|
+
"filePaths参数格式不正确"
|
|
1161
|
+
);
|
|
1162
|
+
}
|
|
1163
|
+
if (options.compressLevel !== void 0 && (typeof options.compressLevel !== "number" || options.compressLevel < 0 || options.compressLevel > 4 || !Number.isInteger(options.compressLevel))) {
|
|
1164
|
+
throw createBridgeError(
|
|
1165
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1166
|
+
"compressLevel参数格式不正确"
|
|
1167
|
+
);
|
|
1168
|
+
}
|
|
1169
|
+
options.compressLevel = options.compressLevel ?? 4;
|
|
1170
|
+
const callbacks = {
|
|
1171
|
+
success: (res) => {
|
|
1172
|
+
resolve(res || { filePaths: "" });
|
|
1173
|
+
},
|
|
1174
|
+
fail: (err) => {
|
|
1175
|
+
console.error("压缩图片失败:", err);
|
|
1176
|
+
reject(err);
|
|
1177
|
+
},
|
|
1178
|
+
complete: () => {
|
|
1179
|
+
}
|
|
1180
|
+
};
|
|
1181
|
+
this.context.bridge.ready(() => {
|
|
1182
|
+
var _a;
|
|
1183
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
1184
|
+
{
|
|
1185
|
+
action: "compressImage",
|
|
1186
|
+
params: options
|
|
1187
|
+
},
|
|
1188
|
+
callbacks
|
|
1189
|
+
);
|
|
1190
|
+
});
|
|
1191
|
+
});
|
|
1192
|
+
}
|
|
1193
|
+
/**
|
|
1194
|
+
* 选择视频文件
|
|
1195
|
+
* @param options 选择视频的配置选项,包含以下属性:
|
|
1196
|
+
* @param options.sourceType 视频来源类型,默认同时支持相册和相机['album','camera']
|
|
1197
|
+
* @param options.maxDuration 视频最大时长(秒),默认60秒
|
|
1198
|
+
* @returns Promise<ChooseVideoResponse> 返回包含视频信息的Promise对象
|
|
1199
|
+
* @returns ChooseVideoResponse.filePath 视频临时文件路径
|
|
1200
|
+
* @returns ChooseVideoResponse.duration 视频时长(秒)
|
|
1201
|
+
* @returns ChooseVideoResponse.size 视频文件大小(字节)
|
|
1202
|
+
* @returns ChooseVideoResponse.height 视频高度
|
|
1203
|
+
* @returns ChooseVideoResponse.width 视频宽度
|
|
1204
|
+
*/
|
|
1205
|
+
chooseVideo(options = {}) {
|
|
1206
|
+
return new Promise((resolve, reject) => {
|
|
1207
|
+
if (!this.context) {
|
|
1208
|
+
throw createBridgeError(
|
|
1209
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1210
|
+
"插件未正确初始化"
|
|
1211
|
+
);
|
|
1212
|
+
}
|
|
1213
|
+
const validSourceType = ["camera", "album"];
|
|
1214
|
+
if (options.sourceType && !Array.isArray(options.sourceType) || options.sourceType && options.sourceType.length === 0) {
|
|
1215
|
+
throw createBridgeError(
|
|
1216
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1217
|
+
"sourceType参数格式不正确"
|
|
1218
|
+
);
|
|
1219
|
+
}
|
|
1220
|
+
if (options.sourceType && options.sourceType.some((type) => !validSourceType.includes(type))) {
|
|
1221
|
+
throw createBridgeError(
|
|
1222
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1223
|
+
"sourceType参数格式不正确"
|
|
1224
|
+
);
|
|
1225
|
+
}
|
|
1226
|
+
const isIOS = /(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent);
|
|
1227
|
+
let MIN_VIDEO_DURATION = 5;
|
|
1228
|
+
if (isIOS) {
|
|
1229
|
+
MIN_VIDEO_DURATION = 15;
|
|
1230
|
+
}
|
|
1231
|
+
if (options.maxDuration !== void 0 && (typeof options.maxDuration !== "number" || options.maxDuration < MIN_VIDEO_DURATION || options.maxDuration > MAX_VIDEO_DURATION || !Number.isInteger(options.maxDuration))) {
|
|
1232
|
+
throw createBridgeError(
|
|
1233
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1234
|
+
"maxDuration参数格式不正确"
|
|
1235
|
+
);
|
|
1236
|
+
}
|
|
1237
|
+
options.sourceType = options.sourceType || ["album", "camera"];
|
|
1238
|
+
options.maxDuration = options.maxDuration ?? MAX_VIDEO_DURATION;
|
|
1239
|
+
const callbacks = {
|
|
1240
|
+
success: (res) => {
|
|
1241
|
+
resolve(res || { filePath: "", duration: 0, size: 0, height: 0, width: 0 });
|
|
1242
|
+
},
|
|
1243
|
+
fail: (err) => {
|
|
1244
|
+
console.error("选择视频失败:", err);
|
|
1245
|
+
reject(err);
|
|
1246
|
+
},
|
|
1247
|
+
complete: () => {
|
|
1248
|
+
}
|
|
1249
|
+
};
|
|
1250
|
+
this.context.bridge.ready(() => {
|
|
1251
|
+
var _a;
|
|
1252
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
1253
|
+
{
|
|
1254
|
+
action: "chooseVideo",
|
|
1255
|
+
params: options
|
|
1256
|
+
},
|
|
1257
|
+
callbacks
|
|
1258
|
+
);
|
|
1259
|
+
});
|
|
1260
|
+
});
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
const mediaPlugin = new MediaPlugin();
|
|
1264
|
+
class NavigatePlugin {
|
|
1265
|
+
constructor() {
|
|
1266
|
+
__publicField(this, "name", "navigate");
|
|
1267
|
+
__publicField(this, "version", "1.0.0");
|
|
1268
|
+
__publicField(this, "context");
|
|
1269
|
+
}
|
|
1270
|
+
install(context) {
|
|
1271
|
+
this.context = context;
|
|
1272
|
+
context.bridge.registerMethod("navigateTo", this.navigateTo.bind(this));
|
|
1273
|
+
context.bridge.registerMethod("redirectTo", this.redirectTo.bind(this));
|
|
1274
|
+
context.bridge.registerMethod("reLaunch", this.reLaunch.bind(this));
|
|
1275
|
+
context.bridge.registerMethod("navigateBack", this.navigateBack.bind(this));
|
|
1276
|
+
context.bridge.registerMethod(
|
|
1277
|
+
"setNavigationBar",
|
|
1278
|
+
this.setNavigationBar.bind(this)
|
|
1279
|
+
);
|
|
1280
|
+
context.bridge.registerMethod("openLink", this.openLink.bind(this));
|
|
1281
|
+
context.bridge.log(`导航插件 v${this.version} 已安装`);
|
|
1282
|
+
}
|
|
1283
|
+
setNavigationBar(options) {
|
|
1284
|
+
return new Promise((resolve, reject) => {
|
|
1285
|
+
if (!this.context) {
|
|
1286
|
+
throw createBridgeError(
|
|
1287
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1288
|
+
"插件未正确初始化"
|
|
1289
|
+
);
|
|
1290
|
+
}
|
|
1291
|
+
if (options.isShowBar !== void 0 && typeof options.isShowBar !== "boolean") {
|
|
1292
|
+
throw createBridgeError(
|
|
1293
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
1294
|
+
"缺少必需的参数: isShowBar"
|
|
1295
|
+
);
|
|
1296
|
+
}
|
|
1297
|
+
if (options.title !== void 0 && typeof options.title !== "string") {
|
|
1298
|
+
throw createBridgeError(
|
|
1299
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
1300
|
+
"title参数格式不正确"
|
|
1301
|
+
);
|
|
1302
|
+
}
|
|
1303
|
+
if (options.backgroundColor !== void 0 && (typeof options.backgroundColor !== "string" || !/^#[0-9A-Fa-f]{6}$/.test(options.backgroundColor))) {
|
|
1304
|
+
throw createBridgeError(
|
|
1305
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
1306
|
+
"backgroundColor参数格式不正确"
|
|
1307
|
+
);
|
|
1308
|
+
}
|
|
1309
|
+
if (options.reset !== void 0 && typeof options.reset !== "boolean") {
|
|
1310
|
+
throw createBridgeError(
|
|
1311
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
1312
|
+
"reset参数格式不正确"
|
|
1313
|
+
);
|
|
1314
|
+
}
|
|
1315
|
+
options.isShowBar = options.isShowBar ?? true;
|
|
1316
|
+
const callbacks = {
|
|
1317
|
+
success: () => {
|
|
1318
|
+
resolve();
|
|
1319
|
+
},
|
|
1320
|
+
fail: (err) => {
|
|
1321
|
+
reject(
|
|
1322
|
+
createBridgeError(
|
|
1323
|
+
BridgeErrorCode.SYSTEM_ERROR,
|
|
1324
|
+
"设置导航栏失败",
|
|
1325
|
+
err
|
|
1326
|
+
)
|
|
1327
|
+
);
|
|
1328
|
+
},
|
|
1329
|
+
complete: () => {
|
|
1330
|
+
}
|
|
1331
|
+
};
|
|
1332
|
+
this.context.bridge.ready(() => {
|
|
1333
|
+
var _a;
|
|
1334
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
1335
|
+
{
|
|
1336
|
+
action: "setNavigationBar",
|
|
1337
|
+
params: { ...options }
|
|
1338
|
+
},
|
|
1339
|
+
callbacks
|
|
1340
|
+
);
|
|
1341
|
+
});
|
|
1342
|
+
});
|
|
1343
|
+
}
|
|
1344
|
+
navigate(options, type) {
|
|
1345
|
+
if (!this.context) {
|
|
1346
|
+
throw createBridgeError(
|
|
1347
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1348
|
+
"插件未正确初始化"
|
|
1349
|
+
);
|
|
1350
|
+
}
|
|
1351
|
+
if (!options || !options.url) {
|
|
1352
|
+
throw createBridgeError(
|
|
1353
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
1354
|
+
"缺少必需的参数: url"
|
|
1355
|
+
);
|
|
1356
|
+
}
|
|
1357
|
+
if (typeof options.url !== "string" || !options.url.trim()) {
|
|
1358
|
+
throw createBridgeError(
|
|
1359
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
1360
|
+
"url参数格式错误"
|
|
1361
|
+
);
|
|
1362
|
+
}
|
|
1363
|
+
const callbacks = {
|
|
1364
|
+
success: (res) => {
|
|
1365
|
+
var _a;
|
|
1366
|
+
const resp = {
|
|
1367
|
+
code: BridgeErrorCode.SUCCESS,
|
|
1368
|
+
message: "跳转成功",
|
|
1369
|
+
data: res
|
|
1370
|
+
};
|
|
1371
|
+
(_a = options.success) == null ? void 0 : _a.call(options, resp);
|
|
1372
|
+
},
|
|
1373
|
+
fail: (err) => {
|
|
1374
|
+
var _a;
|
|
1375
|
+
const resp = {
|
|
1376
|
+
code: BridgeErrorCode.SYSTEM_ERROR,
|
|
1377
|
+
message: "跳转失败",
|
|
1378
|
+
details: err,
|
|
1379
|
+
name: ""
|
|
1380
|
+
};
|
|
1381
|
+
(_a = options.fail) == null ? void 0 : _a.call(options, resp);
|
|
1382
|
+
},
|
|
1383
|
+
complete: () => {
|
|
1384
|
+
var _a;
|
|
1385
|
+
(_a = options.complete) == null ? void 0 : _a.call(options);
|
|
1386
|
+
}
|
|
1387
|
+
};
|
|
1388
|
+
this.context.bridge.ready(() => {
|
|
1389
|
+
var _a;
|
|
1390
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
1391
|
+
{
|
|
1392
|
+
action: type || "navigateTo",
|
|
1393
|
+
params: { ...options }
|
|
1394
|
+
},
|
|
1395
|
+
callbacks
|
|
1396
|
+
);
|
|
1397
|
+
});
|
|
1398
|
+
}
|
|
1399
|
+
navigateTo(options) {
|
|
1400
|
+
this.navigate(options, "navigateTo");
|
|
1401
|
+
}
|
|
1402
|
+
// 根据传入的options参数,执行redirectTo操作
|
|
1403
|
+
redirectTo(options) {
|
|
1404
|
+
this.navigate(options, "redirectTo");
|
|
1405
|
+
}
|
|
1406
|
+
reLaunch(options) {
|
|
1407
|
+
this.navigate(options, "reLaunch");
|
|
1408
|
+
}
|
|
1409
|
+
navigateBack(options) {
|
|
1410
|
+
if (!this.context) {
|
|
1411
|
+
throw createBridgeError(
|
|
1412
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1413
|
+
"插件未正确初始化"
|
|
1414
|
+
);
|
|
1415
|
+
}
|
|
1416
|
+
if (options.delta !== void 0 && (typeof options.delta !== "number" || options.delta <= 0 || !Number.isInteger(options.delta))) {
|
|
1417
|
+
throw createBridgeError(
|
|
1418
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
1419
|
+
"delta参数格式不正确"
|
|
1420
|
+
);
|
|
1421
|
+
}
|
|
1422
|
+
options.delta = options.delta ?? 1;
|
|
1423
|
+
const callbacks = {
|
|
1424
|
+
success: (res) => {
|
|
1425
|
+
var _a;
|
|
1426
|
+
const resp = {
|
|
1427
|
+
code: BridgeErrorCode.SUCCESS,
|
|
1428
|
+
message: "跳转成功",
|
|
1429
|
+
data: res
|
|
1430
|
+
};
|
|
1431
|
+
(_a = options.success) == null ? void 0 : _a.call(options, resp);
|
|
1432
|
+
},
|
|
1433
|
+
fail: (err) => {
|
|
1434
|
+
var _a;
|
|
1435
|
+
const resp = {
|
|
1436
|
+
code: BridgeErrorCode.SYSTEM_ERROR,
|
|
1437
|
+
message: "跳转失败",
|
|
1438
|
+
details: err,
|
|
1439
|
+
name: ""
|
|
1440
|
+
};
|
|
1441
|
+
(_a = options.fail) == null ? void 0 : _a.call(options, resp);
|
|
1442
|
+
},
|
|
1443
|
+
complete: () => {
|
|
1444
|
+
var _a;
|
|
1445
|
+
(_a = options.complete) == null ? void 0 : _a.call(options);
|
|
1446
|
+
}
|
|
1447
|
+
};
|
|
1448
|
+
this.context.bridge.ready(() => {
|
|
1449
|
+
var _a;
|
|
1450
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
1451
|
+
{
|
|
1452
|
+
action: "navigateBack",
|
|
1453
|
+
params: options
|
|
1454
|
+
},
|
|
1455
|
+
callbacks
|
|
1456
|
+
);
|
|
1457
|
+
});
|
|
1458
|
+
}
|
|
1459
|
+
/**
|
|
1460
|
+
* 打开外部链接
|
|
1461
|
+
* @param options 链接选项
|
|
1462
|
+
* @returns Promise对象,解析为打开链接的响应
|
|
1463
|
+
*/
|
|
1464
|
+
async openLink(options) {
|
|
1465
|
+
return new Promise((resolve, reject) => {
|
|
1466
|
+
if (!this.context) {
|
|
1467
|
+
throw createBridgeError(
|
|
1468
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1469
|
+
"插件未正确初始化"
|
|
1470
|
+
);
|
|
1471
|
+
}
|
|
1472
|
+
if (!options.url) {
|
|
1473
|
+
throw createBridgeError(
|
|
1474
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
1475
|
+
"缺少必需的参数: url"
|
|
1476
|
+
);
|
|
1477
|
+
}
|
|
1478
|
+
if (typeof options.url !== "string" || !options.url.trim()) {
|
|
1479
|
+
throw createBridgeError(
|
|
1480
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
1481
|
+
"url参数格式错误"
|
|
1482
|
+
);
|
|
1483
|
+
}
|
|
1484
|
+
if (options.enableShare !== void 0 && typeof options.enableShare !== "boolean") {
|
|
1485
|
+
throw createBridgeError(
|
|
1486
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
1487
|
+
"enableShare参数格式不正确"
|
|
1488
|
+
);
|
|
1489
|
+
}
|
|
1490
|
+
options.enableShare = options.enableShare || false;
|
|
1491
|
+
const callbacks = {
|
|
1492
|
+
success: () => {
|
|
1493
|
+
resolve();
|
|
1494
|
+
},
|
|
1495
|
+
fail: (err) => {
|
|
1496
|
+
reject(
|
|
1497
|
+
createBridgeError(
|
|
1498
|
+
BridgeErrorCode.SYSTEM_ERROR,
|
|
1499
|
+
"打开链接失败",
|
|
1500
|
+
err
|
|
1501
|
+
)
|
|
1502
|
+
);
|
|
1503
|
+
},
|
|
1504
|
+
complete: () => {
|
|
1505
|
+
}
|
|
1506
|
+
};
|
|
1507
|
+
this.context.bridge.ready(() => {
|
|
1508
|
+
var _a;
|
|
1509
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
1510
|
+
{
|
|
1511
|
+
action: "openLink",
|
|
1512
|
+
params: { ...options }
|
|
1513
|
+
},
|
|
1514
|
+
callbacks
|
|
1515
|
+
);
|
|
1516
|
+
});
|
|
1517
|
+
});
|
|
1518
|
+
}
|
|
1519
|
+
}
|
|
1520
|
+
const navigatePlugin = new NavigatePlugin();
|
|
1521
|
+
class ToastPlugin {
|
|
1522
|
+
constructor() {
|
|
1523
|
+
__publicField(this, "name", "toast");
|
|
1524
|
+
__publicField(this, "version", "1.0.0");
|
|
1525
|
+
__publicField(this, "context");
|
|
1526
|
+
}
|
|
1527
|
+
install(context) {
|
|
1528
|
+
this.context = context;
|
|
1529
|
+
context.bridge.registerMethod("showToast", this.showToast.bind(this));
|
|
1530
|
+
context.bridge.registerMethod("hideToast", this.hideToast.bind(this));
|
|
1531
|
+
context.bridge.registerMethod("showLoading", this.showLoading.bind(this));
|
|
1532
|
+
context.bridge.registerMethod("hideLoading", this.hideLoading.bind(this));
|
|
1533
|
+
context.bridge.log(`导航插件 v${this.version} 已安装`);
|
|
1534
|
+
}
|
|
1535
|
+
hideLoading() {
|
|
1536
|
+
return new Promise((resolve, reject) => {
|
|
1537
|
+
if (!this.context) {
|
|
1538
|
+
throw createBridgeError(
|
|
1539
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1540
|
+
"插件未正确初始化"
|
|
1541
|
+
);
|
|
1542
|
+
}
|
|
1543
|
+
const callbacks = {
|
|
1544
|
+
success: (obj) => {
|
|
1545
|
+
resolve(obj);
|
|
1546
|
+
},
|
|
1547
|
+
fail: (err) => {
|
|
1548
|
+
const resp = {
|
|
1549
|
+
code: BridgeErrorCode.SYSTEM_ERROR,
|
|
1550
|
+
message: "显示失败",
|
|
1551
|
+
details: err,
|
|
1552
|
+
name: ""
|
|
1553
|
+
};
|
|
1554
|
+
reject(resp);
|
|
1555
|
+
},
|
|
1556
|
+
complete: () => {
|
|
1557
|
+
}
|
|
1558
|
+
};
|
|
1559
|
+
this.context.bridge.ready(() => {
|
|
1560
|
+
var _a;
|
|
1561
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
1562
|
+
{
|
|
1563
|
+
action: "hideLoading",
|
|
1564
|
+
params: {}
|
|
1565
|
+
},
|
|
1566
|
+
callbacks
|
|
1567
|
+
);
|
|
1568
|
+
});
|
|
1569
|
+
});
|
|
1570
|
+
}
|
|
1571
|
+
showLoading(options) {
|
|
1572
|
+
return new Promise((resolve, reject) => {
|
|
1573
|
+
if (!this.context) {
|
|
1574
|
+
throw createBridgeError(
|
|
1575
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1576
|
+
"插件未正确初始化"
|
|
1577
|
+
);
|
|
1578
|
+
}
|
|
1579
|
+
if (options.content !== void 0 && typeof options.content !== "string") {
|
|
1580
|
+
throw createBridgeError(
|
|
1581
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
1582
|
+
"content参数格式不正确"
|
|
1583
|
+
);
|
|
1584
|
+
}
|
|
1585
|
+
const callbacks = {
|
|
1586
|
+
success: (obj) => {
|
|
1587
|
+
resolve(obj);
|
|
1588
|
+
},
|
|
1589
|
+
fail: (err) => {
|
|
1590
|
+
const resp = {
|
|
1591
|
+
code: BridgeErrorCode.SYSTEM_ERROR,
|
|
1592
|
+
message: "显示失败",
|
|
1593
|
+
details: err,
|
|
1594
|
+
name: ""
|
|
1595
|
+
};
|
|
1596
|
+
reject(resp);
|
|
1597
|
+
},
|
|
1598
|
+
complete: () => {
|
|
1599
|
+
}
|
|
1600
|
+
};
|
|
1601
|
+
this.context.bridge.ready(() => {
|
|
1602
|
+
var _a;
|
|
1603
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
1604
|
+
{
|
|
1605
|
+
action: "showLoading",
|
|
1606
|
+
params: options
|
|
1607
|
+
},
|
|
1608
|
+
callbacks
|
|
1609
|
+
);
|
|
1610
|
+
});
|
|
1611
|
+
});
|
|
1612
|
+
}
|
|
1613
|
+
/**
|
|
1614
|
+
* 显示Toast提示
|
|
1615
|
+
*
|
|
1616
|
+
* @param options Toast参数
|
|
1617
|
+
* @returns Promise<void>
|
|
1618
|
+
*/
|
|
1619
|
+
showToast(options) {
|
|
1620
|
+
return new Promise((resolve, reject) => {
|
|
1621
|
+
if (!this.context) {
|
|
1622
|
+
throw createBridgeError(
|
|
1623
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1624
|
+
"插件未正确初始化"
|
|
1625
|
+
);
|
|
1626
|
+
}
|
|
1627
|
+
if (options.type !== void 0 && typeof options.type !== "string" || options.type !== void 0 && !["none", "success", "fail", "exception"].includes(options.type)) {
|
|
1628
|
+
throw createBridgeError(
|
|
1629
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
1630
|
+
"type参数格式不正确"
|
|
1631
|
+
);
|
|
1632
|
+
}
|
|
1633
|
+
if (!options.content) {
|
|
1634
|
+
throw createBridgeError(
|
|
1635
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
1636
|
+
"缺少必需的参数: content"
|
|
1637
|
+
);
|
|
1638
|
+
}
|
|
1639
|
+
if (typeof options.content !== "string") {
|
|
1640
|
+
throw createBridgeError(
|
|
1641
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
1642
|
+
"content参数格式不正确"
|
|
1643
|
+
);
|
|
1644
|
+
}
|
|
1645
|
+
if (options.duration !== void 0 && (typeof options.duration !== "number" || options.duration <= 0 || !Number.isInteger(options.duration))) {
|
|
1646
|
+
throw createBridgeError(
|
|
1647
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
1648
|
+
"duration参数格式不正确"
|
|
1649
|
+
);
|
|
1650
|
+
}
|
|
1651
|
+
const { type, content, duration } = options;
|
|
1652
|
+
const callbacks = {
|
|
1653
|
+
success: (obj) => {
|
|
1654
|
+
resolve(obj);
|
|
1655
|
+
},
|
|
1656
|
+
fail: (err) => {
|
|
1657
|
+
const resp = {
|
|
1658
|
+
code: BridgeErrorCode.SYSTEM_ERROR,
|
|
1659
|
+
message: "显示失败",
|
|
1660
|
+
details: err,
|
|
1661
|
+
name: ""
|
|
1662
|
+
};
|
|
1663
|
+
reject(resp);
|
|
1664
|
+
},
|
|
1665
|
+
complete: () => {
|
|
1666
|
+
}
|
|
1667
|
+
};
|
|
1668
|
+
this.context.bridge.ready(() => {
|
|
1669
|
+
var _a;
|
|
1670
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
1671
|
+
{
|
|
1672
|
+
action: "showToast",
|
|
1673
|
+
params: {
|
|
1674
|
+
type: type || "none",
|
|
1675
|
+
content,
|
|
1676
|
+
duration: duration || 2e3
|
|
1677
|
+
}
|
|
1678
|
+
},
|
|
1679
|
+
callbacks
|
|
1680
|
+
);
|
|
1681
|
+
});
|
|
1682
|
+
});
|
|
1683
|
+
}
|
|
1684
|
+
/**
|
|
1685
|
+
* 隐藏Toast提示
|
|
1686
|
+
*
|
|
1687
|
+
* @returns Promise<void>
|
|
1688
|
+
*/
|
|
1689
|
+
hideToast() {
|
|
1690
|
+
return new Promise((resolve, reject) => {
|
|
1691
|
+
if (!this.context) {
|
|
1692
|
+
throw createBridgeError(
|
|
1693
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1694
|
+
"插件未正确初始化"
|
|
1695
|
+
);
|
|
1696
|
+
}
|
|
1697
|
+
const callbacks = {
|
|
1698
|
+
success: (obj) => {
|
|
1699
|
+
resolve(obj);
|
|
1700
|
+
},
|
|
1701
|
+
fail: (err) => {
|
|
1702
|
+
const resp = {
|
|
1703
|
+
code: BridgeErrorCode.SYSTEM_ERROR,
|
|
1704
|
+
message: "显示失败",
|
|
1705
|
+
details: err,
|
|
1706
|
+
name: ""
|
|
1707
|
+
};
|
|
1708
|
+
reject(resp);
|
|
1709
|
+
},
|
|
1710
|
+
complete: () => {
|
|
1711
|
+
}
|
|
1712
|
+
};
|
|
1713
|
+
this.context.bridge.ready(() => {
|
|
1714
|
+
var _a;
|
|
1715
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
1716
|
+
{
|
|
1717
|
+
action: "hideToast",
|
|
1718
|
+
params: {}
|
|
1719
|
+
},
|
|
1720
|
+
callbacks
|
|
1721
|
+
);
|
|
1722
|
+
});
|
|
1723
|
+
});
|
|
1724
|
+
}
|
|
1725
|
+
}
|
|
1726
|
+
const toastPlugin = new ToastPlugin();
|
|
1727
|
+
class UserInfoPlugin {
|
|
1728
|
+
constructor() {
|
|
1729
|
+
__publicField(this, "name", "UserInfo");
|
|
1730
|
+
__publicField(this, "version", "1.0.0");
|
|
1731
|
+
__publicField(this, "context");
|
|
1732
|
+
}
|
|
1733
|
+
install(context) {
|
|
1734
|
+
this.context = context;
|
|
1735
|
+
context.bridge.registerMethod("getUserInfo", this.getUserInfo.bind(this));
|
|
1736
|
+
context.bridge.log(`导航插件 v${this.version} 已安装`);
|
|
1737
|
+
}
|
|
1738
|
+
getUserInfo() {
|
|
1739
|
+
return new Promise((resolve, reject) => {
|
|
1740
|
+
if (!this.context) {
|
|
1741
|
+
throw createBridgeError(
|
|
1742
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1743
|
+
"插件未正确初始化"
|
|
1744
|
+
);
|
|
1745
|
+
}
|
|
1746
|
+
const callbacks = {
|
|
1747
|
+
success: (data) => {
|
|
1748
|
+
resolve(data);
|
|
1749
|
+
},
|
|
1750
|
+
fail: (err) => {
|
|
1751
|
+
const resp = {
|
|
1752
|
+
code: BridgeErrorCode.SYSTEM_ERROR,
|
|
1753
|
+
message: "显示失败",
|
|
1754
|
+
details: err,
|
|
1755
|
+
name: ""
|
|
1756
|
+
};
|
|
1757
|
+
reject(resp);
|
|
1758
|
+
},
|
|
1759
|
+
complete: () => {
|
|
1760
|
+
}
|
|
1761
|
+
};
|
|
1762
|
+
this.context.bridge.ready(() => {
|
|
1763
|
+
var _a;
|
|
1764
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
1765
|
+
{
|
|
1766
|
+
action: "getUserInfo",
|
|
1767
|
+
params: {}
|
|
1768
|
+
},
|
|
1769
|
+
callbacks
|
|
1770
|
+
);
|
|
1771
|
+
});
|
|
1772
|
+
});
|
|
1773
|
+
}
|
|
1774
|
+
}
|
|
1775
|
+
const userinfoPlugin = new UserInfoPlugin();
|
|
1776
|
+
class BluetoothPlugin {
|
|
1777
|
+
constructor() {
|
|
1778
|
+
__publicField(this, "name", "bluetooth");
|
|
1779
|
+
__publicField(this, "version", "1.0.0");
|
|
1780
|
+
__publicField(this, "context");
|
|
1781
|
+
}
|
|
1782
|
+
install(context) {
|
|
1783
|
+
this.context = context;
|
|
1784
|
+
context.bridge.registerMethod(
|
|
1785
|
+
"openBluetoothAdapter",
|
|
1786
|
+
this.openBluetoothAdapter.bind(this)
|
|
1787
|
+
);
|
|
1788
|
+
context.bridge.registerMethod(
|
|
1789
|
+
"closeBluetoothAdapter",
|
|
1790
|
+
this.closeBluetoothAdapter.bind(this)
|
|
1791
|
+
);
|
|
1792
|
+
context.bridge.registerMethod(
|
|
1793
|
+
"startBluetoothDevicesDiscovery",
|
|
1794
|
+
this.startBluetoothDevicesDiscovery.bind(this)
|
|
1795
|
+
);
|
|
1796
|
+
context.bridge.registerMethod(
|
|
1797
|
+
"stopBluetoothDevicesDiscovery",
|
|
1798
|
+
this.stopBluetoothDevicesDiscovery.bind(this)
|
|
1799
|
+
);
|
|
1800
|
+
context.bridge.registerMethod(
|
|
1801
|
+
"onBluetoothDeviceFound",
|
|
1802
|
+
this.onBluetoothDeviceFound.bind(this)
|
|
1803
|
+
);
|
|
1804
|
+
context.bridge.registerMethod(
|
|
1805
|
+
"offBluetoothDeviceFound",
|
|
1806
|
+
this.offBluetoothDeviceFound.bind(this)
|
|
1807
|
+
);
|
|
1808
|
+
context.bridge.registerMethod(
|
|
1809
|
+
"writeBLECharacteristicValue",
|
|
1810
|
+
this.writeBLECharacteristicValue.bind(this)
|
|
1811
|
+
);
|
|
1812
|
+
context.bridge.registerMethod(
|
|
1813
|
+
"readBLECharacteristicValue",
|
|
1814
|
+
this.readBLECharacteristicValue.bind(this)
|
|
1815
|
+
);
|
|
1816
|
+
context.bridge.registerMethod(
|
|
1817
|
+
"connectBLEDevice",
|
|
1818
|
+
this.connectBLEDevice.bind(this)
|
|
1819
|
+
);
|
|
1820
|
+
context.bridge.registerMethod(
|
|
1821
|
+
"disconnectBLEDevice",
|
|
1822
|
+
this.disconnectBLEDevice.bind(this)
|
|
1823
|
+
);
|
|
1824
|
+
context.bridge.registerMethod(
|
|
1825
|
+
"getBLEDeviceCharacteristics",
|
|
1826
|
+
this.getBLEDeviceCharacteristics.bind(this)
|
|
1827
|
+
);
|
|
1828
|
+
context.bridge.registerMethod(
|
|
1829
|
+
"getBLEDeviceServices",
|
|
1830
|
+
this.getBLEDeviceServices.bind(this)
|
|
1831
|
+
);
|
|
1832
|
+
context.bridge.registerMethod(
|
|
1833
|
+
"onBLECharacteristicValueChange",
|
|
1834
|
+
this.onBLECharacteristicValueChange.bind(this)
|
|
1835
|
+
);
|
|
1836
|
+
context.bridge.registerMethod(
|
|
1837
|
+
"offBLECharacteristicValueChange",
|
|
1838
|
+
this.offBLECharacteristicValueChange.bind(this)
|
|
1839
|
+
);
|
|
1840
|
+
context.bridge.registerMethod(
|
|
1841
|
+
"onBLEConnectionStateChanged",
|
|
1842
|
+
this.onBLEConnectionStateChanged.bind(this)
|
|
1843
|
+
);
|
|
1844
|
+
context.bridge.registerMethod(
|
|
1845
|
+
"offBLEConnectionStateChanged",
|
|
1846
|
+
this.offBLEConnectionStateChanged.bind(this)
|
|
1847
|
+
);
|
|
1848
|
+
context.bridge.log(`蓝牙插件 v${this.version} 已安装`);
|
|
1849
|
+
}
|
|
1850
|
+
//BLE蓝牙:---------------------------------------------------------------
|
|
1851
|
+
/**
|
|
1852
|
+
* 取消监听蓝牙低功耗连接状态变化事件
|
|
1853
|
+
*
|
|
1854
|
+
* @description 该方法用于移除之前注册的蓝牙低功耗连接状态变化监听器。
|
|
1855
|
+
* 当不再需要监听连接状态变化时,应调用此方法以避免内存泄漏。
|
|
1856
|
+
*
|
|
1857
|
+
* @param callback - 可选的回调函数,当成功取消监听后会被调用
|
|
1858
|
+
* @returns void
|
|
1859
|
+
*
|
|
1860
|
+
* @throws {BridgeError} 当插件未正确初始化时抛出错误
|
|
1861
|
+
*
|
|
1862
|
+
* @example
|
|
1863
|
+
*/
|
|
1864
|
+
offBLEConnectionStateChanged(callback) {
|
|
1865
|
+
if (!this.context) {
|
|
1866
|
+
throw createBridgeError(
|
|
1867
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1868
|
+
"插件未正确初始化"
|
|
1869
|
+
);
|
|
1870
|
+
}
|
|
1871
|
+
this.context.bridge.ready(() => {
|
|
1872
|
+
var _a;
|
|
1873
|
+
(_a = this.context) == null ? void 0 : _a.bridge.offNativeEvent(
|
|
1874
|
+
"offBLEConnectionStateChanged",
|
|
1875
|
+
() => {
|
|
1876
|
+
callback && callback();
|
|
1877
|
+
}
|
|
1878
|
+
);
|
|
1879
|
+
});
|
|
1880
|
+
}
|
|
1881
|
+
/**
|
|
1882
|
+
* 监听BLE设备连接状态变化
|
|
1883
|
+
*
|
|
1884
|
+
* @description 注册一个回调函数来监听蓝牙低功耗(BLE)设备的连接状态变化事件。
|
|
1885
|
+
* 当设备连接或断开时,会触发相应的回调函数。
|
|
1886
|
+
*
|
|
1887
|
+
* @param callback - 连接状态变化时的回调函数
|
|
1888
|
+
* @param callback.data - 回调函数的参数对象
|
|
1889
|
+
* @param callback.data.deviceId - 设备ID,用于标识具体的BLE设备
|
|
1890
|
+
* @param callback.data.connected - 连接状态,true表示已连接,false表示已断开
|
|
1891
|
+
*
|
|
1892
|
+
* @throws {BridgeError} 当插件未正确初始化时抛出错误
|
|
1893
|
+
*
|
|
1894
|
+
* @example
|
|
1895
|
+
*
|
|
1896
|
+
* @since 1.0.0
|
|
1897
|
+
*/
|
|
1898
|
+
onBLEConnectionStateChanged(callback) {
|
|
1899
|
+
if (!this.context) {
|
|
1900
|
+
throw createBridgeError(
|
|
1901
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1902
|
+
"插件未正确初始化"
|
|
1903
|
+
);
|
|
1904
|
+
}
|
|
1905
|
+
this.context.bridge.ready(() => {
|
|
1906
|
+
var _a;
|
|
1907
|
+
(_a = this.context) == null ? void 0 : _a.bridge.onNativeEvent(
|
|
1908
|
+
"onBLEConnectionStateChanged",
|
|
1909
|
+
(event) => {
|
|
1910
|
+
callback(event.data);
|
|
1911
|
+
}
|
|
1912
|
+
);
|
|
1913
|
+
});
|
|
1914
|
+
}
|
|
1915
|
+
/**
|
|
1916
|
+
* 取消监听低功耗蓝牙设备的特征值变化事件
|
|
1917
|
+
*
|
|
1918
|
+
* @description 此方法用于移除之前通过onBLECharacteristicValueChange注册的事件监听器。
|
|
1919
|
+
* 当不再需要监听BLE特征值变化时,应调用此方法以避免内存泄漏和不必要的回调执行。
|
|
1920
|
+
*
|
|
1921
|
+
* @param {Function} [callback] - 可选的回调函数,在成功取消监听后执行
|
|
1922
|
+
* @returns {void} 无返回值
|
|
1923
|
+
*
|
|
1924
|
+
* @throws {BridgeError} 当插件未正确初始化时抛出错误
|
|
1925
|
+
*
|
|
1926
|
+
* @example
|
|
1927
|
+
*/
|
|
1928
|
+
offBLECharacteristicValueChange(callback) {
|
|
1929
|
+
if (!this.context) {
|
|
1930
|
+
throw createBridgeError(
|
|
1931
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1932
|
+
"插件未正确初始化"
|
|
1933
|
+
);
|
|
1934
|
+
}
|
|
1935
|
+
this.context.bridge.ready(() => {
|
|
1936
|
+
var _a;
|
|
1937
|
+
(_a = this.context) == null ? void 0 : _a.bridge.offNativeEvent(
|
|
1938
|
+
"onBLECharacteristicValueChange",
|
|
1939
|
+
() => {
|
|
1940
|
+
callback && callback();
|
|
1941
|
+
}
|
|
1942
|
+
);
|
|
1943
|
+
});
|
|
1944
|
+
}
|
|
1945
|
+
/**
|
|
1946
|
+
* 监听 BLE 特征值变化事件
|
|
1947
|
+
*
|
|
1948
|
+
* 当蓝牙低功耗设备的特征值发生变化时,会触发此回调函数。
|
|
1949
|
+
* 在调用此方法前,需要确保插件已正确初始化。
|
|
1950
|
+
*
|
|
1951
|
+
* @param callback - 特征值变化时的回调函数
|
|
1952
|
+
* @param callback.data - 特征值变化的数据对象
|
|
1953
|
+
* @param callback.data.serviceId - 服务 ID,标识 BLE 服务
|
|
1954
|
+
* @param callback.data.characteristicId - 特征 ID,标识 BLE 特征
|
|
1955
|
+
* @param callback.data.deviceId - 设备 ID,标识 BLE 设备
|
|
1956
|
+
* @param callback.data.value - 特征值,变化后的特征值内容
|
|
1957
|
+
*
|
|
1958
|
+
* @throws {BridgeError} 当插件未正确初始化时抛出错误
|
|
1959
|
+
*
|
|
1960
|
+
* @example
|
|
1961
|
+
*/
|
|
1962
|
+
onBLECharacteristicValueChange(callback) {
|
|
1963
|
+
if (!this.context) {
|
|
1964
|
+
throw createBridgeError(
|
|
1965
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
1966
|
+
"插件未正确初始化"
|
|
1967
|
+
);
|
|
1968
|
+
}
|
|
1969
|
+
this.context.bridge.ready(() => {
|
|
1970
|
+
var _a;
|
|
1971
|
+
(_a = this.context) == null ? void 0 : _a.bridge.onNativeEvent(
|
|
1972
|
+
"onBLECharacteristicValueChange",
|
|
1973
|
+
(event) => {
|
|
1974
|
+
callback(event.data);
|
|
1975
|
+
}
|
|
1976
|
+
);
|
|
1977
|
+
});
|
|
1978
|
+
}
|
|
1979
|
+
/**
|
|
1980
|
+
* 读取低功耗蓝牙设备特征值
|
|
1981
|
+
*
|
|
1982
|
+
* @description 从指定的蓝牙设备特征值中读取数据。该方法需要在插件正确初始化后才能使用。
|
|
1983
|
+
*
|
|
1984
|
+
* @param {ReadBLECharacteristicValueOptions} options - 读取BLE特征值的配置选项
|
|
1985
|
+
* @param {string} options.deviceId - 蓝牙设备ID
|
|
1986
|
+
* @param {string} options.serviceId - 蓝牙服务ID
|
|
1987
|
+
* @param {string} options.characteristicId - 蓝牙特征值ID
|
|
1988
|
+
* @param {function} [options.success] - 成功回调函数,参数为读取到的特征值数据
|
|
1989
|
+
* @param {function} [options.fail] - 失败回调函数
|
|
1990
|
+
* @param {function} [options.complete] - 完成回调函数(无论成功或失败都会执行)
|
|
1991
|
+
*
|
|
1992
|
+
* @throws {BridgeError} 当插件未正确初始化时抛出错误
|
|
1993
|
+
*
|
|
1994
|
+
* @returns {void} 无返回值
|
|
1995
|
+
*
|
|
1996
|
+
* @example
|
|
1997
|
+
*/
|
|
1998
|
+
readBLECharacteristicValue(options) {
|
|
1999
|
+
if (!this.context) {
|
|
2000
|
+
throw createBridgeError(
|
|
2001
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
2002
|
+
"插件未正确初始化"
|
|
2003
|
+
);
|
|
2004
|
+
}
|
|
2005
|
+
if (!options.deviceId || !options.serviceId || !options.characteristicId) {
|
|
2006
|
+
throw createBridgeError(
|
|
2007
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
2008
|
+
"缺少必需的参数"
|
|
2009
|
+
);
|
|
2010
|
+
}
|
|
2011
|
+
const callbacks = {
|
|
2012
|
+
success: (data) => {
|
|
2013
|
+
var _a;
|
|
2014
|
+
(_a = options.success) == null ? void 0 : _a.call(options, data);
|
|
2015
|
+
},
|
|
2016
|
+
fail: options.fail,
|
|
2017
|
+
complete: options.complete
|
|
2018
|
+
};
|
|
2019
|
+
this.context.bridge.ready(() => {
|
|
2020
|
+
var _a;
|
|
2021
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
2022
|
+
{
|
|
2023
|
+
action: "readBLECharacteristicValue",
|
|
2024
|
+
params: options
|
|
2025
|
+
},
|
|
2026
|
+
callbacks
|
|
2027
|
+
);
|
|
2028
|
+
});
|
|
2029
|
+
}
|
|
2030
|
+
/**
|
|
2031
|
+
* 向低功耗蓝牙设备特征值中写入数据
|
|
2032
|
+
*
|
|
2033
|
+
* @param options - 写入BLE特征值的配置选项
|
|
2034
|
+
* @param options.deviceId - 蓝牙设备 id
|
|
2035
|
+
* @param options.serviceId - 蓝牙特征值对应服务的 uuid
|
|
2036
|
+
* @param options.characteristicId - 蓝牙特征值的 uuid
|
|
2037
|
+
* @param options.value - 蓝牙设备特征值对应的二进制值
|
|
2038
|
+
* @param options.success - 接口调用成功的回调函数
|
|
2039
|
+
* @param options.fail - 接口调用失败的回调函数
|
|
2040
|
+
* @param options.complete - 接口调用结束的回调函数(调用成功、失败都会执行)
|
|
2041
|
+
*
|
|
2042
|
+
* @throws {BridgeError} 当插件未正确初始化时抛出错误
|
|
2043
|
+
*
|
|
2044
|
+
* @example
|
|
2045
|
+
*/
|
|
2046
|
+
writeBLECharacteristicValue(options) {
|
|
2047
|
+
var _a;
|
|
2048
|
+
if (!this.context) {
|
|
2049
|
+
throw createBridgeError(
|
|
2050
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
2051
|
+
"插件未正确初始化"
|
|
2052
|
+
);
|
|
2053
|
+
}
|
|
2054
|
+
if (!options.deviceId || !options.serviceId || !options.characteristicId || !options.value) {
|
|
2055
|
+
throw createBridgeError(
|
|
2056
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
2057
|
+
"缺少必需的参数"
|
|
2058
|
+
);
|
|
2059
|
+
}
|
|
2060
|
+
if (typeof options.deviceId !== "string") {
|
|
2061
|
+
throw createBridgeError(
|
|
2062
|
+
BridgeErrorCode.JSON_ERROR,
|
|
2063
|
+
"deviceId参数格式错误"
|
|
2064
|
+
);
|
|
2065
|
+
}
|
|
2066
|
+
if (typeof options.serviceId !== "string") {
|
|
2067
|
+
throw createBridgeError(
|
|
2068
|
+
BridgeErrorCode.JSON_ERROR,
|
|
2069
|
+
"serviceId参数格式错误"
|
|
2070
|
+
);
|
|
2071
|
+
}
|
|
2072
|
+
if (typeof options.characteristicId !== "string") {
|
|
2073
|
+
throw createBridgeError(
|
|
2074
|
+
BridgeErrorCode.JSON_ERROR,
|
|
2075
|
+
"characteristicId参数格式错误"
|
|
2076
|
+
);
|
|
2077
|
+
}
|
|
2078
|
+
if (typeof options.value !== "string" || !/^[0-9A-Fa-f]+$/.test(options.value) || ((_a = options.value) == null ? void 0 : _a.length) > 20) {
|
|
2079
|
+
throw createBridgeError(
|
|
2080
|
+
BridgeErrorCode.JSON_ERROR,
|
|
2081
|
+
"value参数格式错误"
|
|
2082
|
+
);
|
|
2083
|
+
}
|
|
2084
|
+
const callbacks = {
|
|
2085
|
+
success: options.success,
|
|
2086
|
+
fail: options.fail,
|
|
2087
|
+
complete: options.complete
|
|
2088
|
+
};
|
|
2089
|
+
this.context.bridge.ready(() => {
|
|
2090
|
+
var _a2;
|
|
2091
|
+
(_a2 = this.context) == null ? void 0 : _a2.bridge.callNative(
|
|
2092
|
+
{
|
|
2093
|
+
action: "writeBLECharacteristicValue",
|
|
2094
|
+
params: options
|
|
2095
|
+
},
|
|
2096
|
+
callbacks
|
|
2097
|
+
);
|
|
2098
|
+
});
|
|
2099
|
+
}
|
|
2100
|
+
/**
|
|
2101
|
+
* 获取蓝牙低功耗设备的所有服务
|
|
2102
|
+
*
|
|
2103
|
+
* @description 该方法用于获取已连接的BLE设备的所有可用服务列表。
|
|
2104
|
+
* 调用前需要确保设备已经成功连接,否则可能获取失败。
|
|
2105
|
+
*
|
|
2106
|
+
* @param {BLEDeviceServicesOptions} options - 获取BLE设备服务的配置选项
|
|
2107
|
+
* @param {string} options.deviceId - 蓝牙设备标识符,用于指定要获取服务的设备
|
|
2108
|
+
* @param {function} [options.success] - 成功回调函数,参数为BLEDeviceServicesResponse类型
|
|
2109
|
+
* @param {function} [options.fail] - 失败回调函数,参数为错误信息
|
|
2110
|
+
* @param {function} [options.complete] - 完成回调函数,无论成功失败都会调用
|
|
2111
|
+
*
|
|
2112
|
+
* @throws {BridgeError} 当插件未正确初始化时抛出PLUGIN_NOT_INITIALIZED错误
|
|
2113
|
+
*
|
|
2114
|
+
* @returns {void} 无返回值,结果通过回调函数返回
|
|
2115
|
+
*
|
|
2116
|
+
* @example
|
|
2117
|
+
*/
|
|
2118
|
+
getBLEDeviceServices(options) {
|
|
2119
|
+
if (!this.context) {
|
|
2120
|
+
throw createBridgeError(
|
|
2121
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
2122
|
+
"插件未正确初始化"
|
|
2123
|
+
);
|
|
2124
|
+
}
|
|
2125
|
+
if (!options.deviceId) {
|
|
2126
|
+
throw createBridgeError(
|
|
2127
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
2128
|
+
"缺少必需的参数: deviceId"
|
|
2129
|
+
);
|
|
2130
|
+
}
|
|
2131
|
+
if (typeof options.deviceId !== "string") {
|
|
2132
|
+
throw createBridgeError(
|
|
2133
|
+
BridgeErrorCode.JSON_ERROR,
|
|
2134
|
+
"deviceId参数格式错误"
|
|
2135
|
+
);
|
|
2136
|
+
}
|
|
2137
|
+
const callbacks = {
|
|
2138
|
+
success: (data) => {
|
|
2139
|
+
var _a;
|
|
2140
|
+
(_a = options.success) == null ? void 0 : _a.call(options, data);
|
|
2141
|
+
},
|
|
2142
|
+
fail: options.fail,
|
|
2143
|
+
complete: options.complete
|
|
2144
|
+
};
|
|
2145
|
+
this.context.bridge.ready(() => {
|
|
2146
|
+
var _a;
|
|
2147
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
2148
|
+
{
|
|
2149
|
+
action: "getBLEDeviceServices",
|
|
2150
|
+
params: options
|
|
2151
|
+
},
|
|
2152
|
+
callbacks
|
|
2153
|
+
);
|
|
2154
|
+
});
|
|
2155
|
+
}
|
|
2156
|
+
/**
|
|
2157
|
+
* 连接蓝牙低功耗设备
|
|
2158
|
+
*
|
|
2159
|
+
* @description 此方法用于连接指定的蓝牙低功耗(BLE)设备。在调用前需要确保插件已正确初始化,
|
|
2160
|
+
* 否则会抛出插件未初始化的错误。连接过程是异步的,通过回调函数返回结果。
|
|
2161
|
+
*
|
|
2162
|
+
* @param {BLEDeviceOptions} options - 连接BLE设备的配置选项
|
|
2163
|
+
* @param {Function} [options.success] - 连接成功时的回调函数
|
|
2164
|
+
* @param {Function} [options.fail] - 连接失败时的回调函数
|
|
2165
|
+
* @param {Function} [options.complete] - 连接完成时的回调函数(无论成功或失败都会执行)
|
|
2166
|
+
*
|
|
2167
|
+
* @throws {BridgeError} 当插件未正确初始化时抛出 PLUGIN_NOT_INITIALIZED 错误
|
|
2168
|
+
*
|
|
2169
|
+
* @returns {void} 无返回值
|
|
2170
|
+
*
|
|
2171
|
+
* @example
|
|
2172
|
+
*/
|
|
2173
|
+
connectBLEDevice(options) {
|
|
2174
|
+
if (!this.context) {
|
|
2175
|
+
throw createBridgeError(
|
|
2176
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
2177
|
+
"插件未正确初始化"
|
|
2178
|
+
);
|
|
2179
|
+
}
|
|
2180
|
+
if (!options.deviceId) {
|
|
2181
|
+
throw createBridgeError(
|
|
2182
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
2183
|
+
"缺少必需的参数: deviceId"
|
|
2184
|
+
);
|
|
2185
|
+
}
|
|
2186
|
+
const callbacks = {
|
|
2187
|
+
success: options.success,
|
|
2188
|
+
fail: options.fail,
|
|
2189
|
+
complete: options.complete
|
|
2190
|
+
};
|
|
2191
|
+
this.context.bridge.ready(() => {
|
|
2192
|
+
var _a;
|
|
2193
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
2194
|
+
{
|
|
2195
|
+
action: "connectBLEDevice",
|
|
2196
|
+
params: options
|
|
2197
|
+
},
|
|
2198
|
+
callbacks
|
|
2199
|
+
);
|
|
2200
|
+
});
|
|
2201
|
+
}
|
|
2202
|
+
/**
|
|
2203
|
+
* 断开蓝牙低功耗设备连接
|
|
2204
|
+
*
|
|
2205
|
+
* @description 断开与指定蓝牙低功耗设备的连接。此方法会调用原生平台的蓝牙断连功能。
|
|
2206
|
+
*
|
|
2207
|
+
* @param {BLEDeviceOptions} options - 断开连接的配置选项
|
|
2208
|
+
* @param {Function} [options.success] - 成功回调函数
|
|
2209
|
+
* @param {Function} [options.fail] - 失败回调函数
|
|
2210
|
+
* @param {Function} [options.complete] - 完成回调函数(无论成功或失败都会执行)
|
|
2211
|
+
*
|
|
2212
|
+
* @throws {BridgeError} 当插件未正确初始化时抛出错误
|
|
2213
|
+
*
|
|
2214
|
+
* @returns {void}
|
|
2215
|
+
*
|
|
2216
|
+
* @example
|
|
2217
|
+
*/
|
|
2218
|
+
disconnectBLEDevice(options) {
|
|
2219
|
+
if (!this.context) {
|
|
2220
|
+
throw createBridgeError(
|
|
2221
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
2222
|
+
"插件未正确初始化"
|
|
2223
|
+
);
|
|
2224
|
+
}
|
|
2225
|
+
if (!options.deviceId) {
|
|
2226
|
+
throw createBridgeError(
|
|
2227
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
2228
|
+
"缺少必需的参数: deviceId"
|
|
2229
|
+
);
|
|
2230
|
+
}
|
|
2231
|
+
if (typeof options.deviceId !== "string") {
|
|
2232
|
+
throw createBridgeError(
|
|
2233
|
+
BridgeErrorCode.JSON_ERROR,
|
|
2234
|
+
"deviceId参数格式错误"
|
|
2235
|
+
);
|
|
2236
|
+
}
|
|
2237
|
+
const callbacks = {
|
|
2238
|
+
success: options.success,
|
|
2239
|
+
fail: options.fail,
|
|
2240
|
+
complete: options.complete
|
|
2241
|
+
};
|
|
2242
|
+
this.context.bridge.ready(() => {
|
|
2243
|
+
var _a;
|
|
2244
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
2245
|
+
{
|
|
2246
|
+
action: "disconnectBLEDevice",
|
|
2247
|
+
params: options
|
|
2248
|
+
},
|
|
2249
|
+
callbacks
|
|
2250
|
+
);
|
|
2251
|
+
});
|
|
2252
|
+
}
|
|
2253
|
+
/**
|
|
2254
|
+
* 获取蓝牙低功耗设备的特征值列表
|
|
2255
|
+
*
|
|
2256
|
+
* @description 用于获取指定蓝牙低功耗设备的所有特征值信息,包括特征值的UUID、属性等。
|
|
2257
|
+
* 该方法需要在蓝牙设备连接成功后调用。
|
|
2258
|
+
*
|
|
2259
|
+
* @param {GetBLEDeviceOptions} options - 获取设备特征值的配置选项
|
|
2260
|
+
* @param {string} options.deviceId - 蓝牙设备ID
|
|
2261
|
+
* @param {string} options.serviceId - 蓝牙服务UUID
|
|
2262
|
+
* @param {function} [options.success] - 成功回调函数,返回特征值列表数据
|
|
2263
|
+
* @param {function} [options.fail] - 失败回调函数
|
|
2264
|
+
* @param {function} [options.complete] - 完成回调函数(无论成功或失败都会执行)
|
|
2265
|
+
*
|
|
2266
|
+
* @throws {BridgeError} 当插件未正确初始化时抛出错误
|
|
2267
|
+
*
|
|
2268
|
+
* @example
|
|
2269
|
+
*
|
|
2270
|
+
* @since 1.0.0
|
|
2271
|
+
*/
|
|
2272
|
+
getBLEDeviceCharacteristics(options) {
|
|
2273
|
+
if (!this.context) {
|
|
2274
|
+
throw createBridgeError(
|
|
2275
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
2276
|
+
"插件未正确初始化"
|
|
2277
|
+
);
|
|
2278
|
+
}
|
|
2279
|
+
if (!options.deviceId || !options.serviceId) {
|
|
2280
|
+
throw createBridgeError(
|
|
2281
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
2282
|
+
"缺少必需的参数: deviceId 或 serviceId"
|
|
2283
|
+
);
|
|
2284
|
+
}
|
|
2285
|
+
if (typeof options.deviceId !== "string") {
|
|
2286
|
+
throw createBridgeError(
|
|
2287
|
+
BridgeErrorCode.JSON_ERROR,
|
|
2288
|
+
"deviceId参数格式错误"
|
|
2289
|
+
);
|
|
2290
|
+
}
|
|
2291
|
+
if (typeof options.serviceId !== "string") {
|
|
2292
|
+
throw createBridgeError(
|
|
2293
|
+
BridgeErrorCode.JSON_ERROR,
|
|
2294
|
+
"serviceId参数格式错误"
|
|
2295
|
+
);
|
|
2296
|
+
}
|
|
2297
|
+
const callbacks = {
|
|
2298
|
+
success: (data) => {
|
|
2299
|
+
var _a;
|
|
2300
|
+
(_a = options.success) == null ? void 0 : _a.call(options, data);
|
|
2301
|
+
},
|
|
2302
|
+
fail: (err) => {
|
|
2303
|
+
var _a;
|
|
2304
|
+
(_a = options.fail) == null ? void 0 : _a.call(options, err);
|
|
2305
|
+
},
|
|
2306
|
+
complete: options.complete
|
|
2307
|
+
};
|
|
2308
|
+
this.context.bridge.ready(() => {
|
|
2309
|
+
var _a;
|
|
2310
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
2311
|
+
{
|
|
2312
|
+
action: "getBLEDeviceCharacteristics",
|
|
2313
|
+
params: options
|
|
2314
|
+
},
|
|
2315
|
+
callbacks
|
|
2316
|
+
);
|
|
2317
|
+
});
|
|
2318
|
+
}
|
|
2319
|
+
//经典蓝牙:---------------------------------------------------------------
|
|
2320
|
+
/**
|
|
2321
|
+
* 取消监听蓝牙设备发现事件
|
|
2322
|
+
*
|
|
2323
|
+
* @description 移除对蓝牙设备发现事件的监听,当不再需要接收蓝牙设备发现通知时调用此方法
|
|
2324
|
+
*
|
|
2325
|
+
* @param callback - 可选的回调函数,在成功取消监听后执行
|
|
2326
|
+
*
|
|
2327
|
+
* @throws {BridgeError} 当插件未正确初始化时抛出错误
|
|
2328
|
+
*
|
|
2329
|
+
* @example
|
|
2330
|
+
*
|
|
2331
|
+
* @returns {void} 无返回值
|
|
2332
|
+
*/
|
|
2333
|
+
offBluetoothDeviceFound(callback) {
|
|
2334
|
+
if (!this.context) {
|
|
2335
|
+
throw createBridgeError(
|
|
2336
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
2337
|
+
"插件未正确初始化"
|
|
2338
|
+
);
|
|
2339
|
+
}
|
|
2340
|
+
this.context.bridge.ready(() => {
|
|
2341
|
+
var _a;
|
|
2342
|
+
(_a = this.context) == null ? void 0 : _a.bridge.offNativeEvent("offBluetoothDeviceFound", () => {
|
|
2343
|
+
callback && callback();
|
|
2344
|
+
});
|
|
2345
|
+
});
|
|
2346
|
+
}
|
|
2347
|
+
/**
|
|
2348
|
+
* 监听蓝牙设备发现事件
|
|
2349
|
+
*
|
|
2350
|
+
* 当发现新的蓝牙设备时,会触发回调函数并传入设备列表。
|
|
2351
|
+
* 该方法需要在插件正确初始化后调用,否则会抛出异常。
|
|
2352
|
+
*
|
|
2353
|
+
* @param callback - 设备发现回调函数
|
|
2354
|
+
* @param callback.devices - 发现的蓝牙设备列表
|
|
2355
|
+
* @throws {BridgeError} 当插件未正确初始化时抛出 PLUGIN_NOT_INITIALIZED 错误
|
|
2356
|
+
*
|
|
2357
|
+
* @example
|
|
2358
|
+
*/
|
|
2359
|
+
onBluetoothDeviceFound(callback) {
|
|
2360
|
+
if (!this.context) {
|
|
2361
|
+
throw createBridgeError(
|
|
2362
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
2363
|
+
"插件未正确初始化"
|
|
2364
|
+
);
|
|
2365
|
+
}
|
|
2366
|
+
this.context.bridge.ready(() => {
|
|
2367
|
+
var _a;
|
|
2368
|
+
(_a = this.context) == null ? void 0 : _a.bridge.onNativeEvent(
|
|
2369
|
+
"onBluetoothDeviceFound",
|
|
2370
|
+
(event) => {
|
|
2371
|
+
callback(event.data ? JSON.parse(event.data) : []);
|
|
2372
|
+
}
|
|
2373
|
+
);
|
|
2374
|
+
});
|
|
2375
|
+
}
|
|
2376
|
+
/**
|
|
2377
|
+
* 停止搜索蓝牙设备
|
|
2378
|
+
*
|
|
2379
|
+
* @description 停止搜索附近的蓝牙外围设备。若已经找到需要连接的蓝牙设备并不需要继续搜索时,建议调用该接口停止蓝牙搜索。
|
|
2380
|
+
*
|
|
2381
|
+
* @param options - 可选的回调函数配置对象
|
|
2382
|
+
* @param options.success - 接口调用成功的回调函数
|
|
2383
|
+
* @param options.fail - 接口调用失败的回调函数
|
|
2384
|
+
* @param options.complete - 接口调用结束的回调函数(调用成功、失败都会执行)
|
|
2385
|
+
*
|
|
2386
|
+
* @throws {BridgeError} 当插件未正确初始化时抛出异常
|
|
2387
|
+
*
|
|
2388
|
+
* @example
|
|
2389
|
+
*/
|
|
2390
|
+
stopBluetoothDevicesDiscovery(options) {
|
|
2391
|
+
if (!this.context) {
|
|
2392
|
+
throw createBridgeError(
|
|
2393
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
2394
|
+
"插件未正确初始化"
|
|
2395
|
+
);
|
|
2396
|
+
}
|
|
2397
|
+
const callbacks = {
|
|
2398
|
+
success: options == null ? void 0 : options.success,
|
|
2399
|
+
fail: options == null ? void 0 : options.fail,
|
|
2400
|
+
complete: options == null ? void 0 : options.complete
|
|
2401
|
+
};
|
|
2402
|
+
this.context.bridge.ready(() => {
|
|
2403
|
+
var _a;
|
|
2404
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
2405
|
+
{
|
|
2406
|
+
action: "stopBluetoothDevicesDiscovery",
|
|
2407
|
+
params: options
|
|
2408
|
+
},
|
|
2409
|
+
callbacks
|
|
2410
|
+
);
|
|
2411
|
+
});
|
|
2412
|
+
}
|
|
2413
|
+
/**
|
|
2414
|
+
* 开启蓝牙适配器
|
|
2415
|
+
*
|
|
2416
|
+
* @description 开启本机蓝牙适配器模块,允许设备进行蓝牙操作。调用该方法前需要确保插件已正确初始化。
|
|
2417
|
+
*
|
|
2418
|
+
* @param {OpenBluetoothOptions} [options={}] - 开启蓝牙适配器的配置选项
|
|
2419
|
+
* @param {Function} [options.success] - 接口调用成功的回调函数
|
|
2420
|
+
* @param {Function} [options.fail] - 接口调用失败的回调函数
|
|
2421
|
+
* @param {Function} [options.complete] - 接口调用结束的回调函数(调用成功、失败都会执行)
|
|
2422
|
+
*
|
|
2423
|
+
* @throws {BridgeError} 当插件未正确初始化时抛出错误
|
|
2424
|
+
*
|
|
2425
|
+
* @returns {void}
|
|
2426
|
+
*
|
|
2427
|
+
* @example
|
|
2428
|
+
*/
|
|
2429
|
+
startBluetoothDevicesDiscovery(options = {}) {
|
|
2430
|
+
if (!this.context) {
|
|
2431
|
+
throw createBridgeError(
|
|
2432
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
2433
|
+
"插件未正确初始化"
|
|
2434
|
+
);
|
|
2435
|
+
}
|
|
2436
|
+
if (options.services !== void 0 && !Array.isArray(options.services) && typeof options.services !== "string") {
|
|
2437
|
+
throw createBridgeError(
|
|
2438
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
2439
|
+
"services参数格式不正确"
|
|
2440
|
+
);
|
|
2441
|
+
}
|
|
2442
|
+
if (options.allowDuplicatesKey !== void 0 && typeof options.allowDuplicatesKey !== "boolean") {
|
|
2443
|
+
throw createBridgeError(
|
|
2444
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
2445
|
+
"allowDuplicatesKey参数格式不正确"
|
|
2446
|
+
);
|
|
2447
|
+
}
|
|
2448
|
+
if (options.interval !== void 0 && (typeof options.interval !== "number" || !Number.isInteger(options.interval))) {
|
|
2449
|
+
throw createBridgeError(
|
|
2450
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
2451
|
+
"interval参数格式不正确"
|
|
2452
|
+
);
|
|
2453
|
+
}
|
|
2454
|
+
options.interval = (options == null ? void 0 : options.interval) || 0;
|
|
2455
|
+
const callbacks = {
|
|
2456
|
+
success: options == null ? void 0 : options.success,
|
|
2457
|
+
fail: options == null ? void 0 : options.fail,
|
|
2458
|
+
complete: options == null ? void 0 : options.complete
|
|
2459
|
+
};
|
|
2460
|
+
this.context.bridge.ready(() => {
|
|
2461
|
+
var _a;
|
|
2462
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
2463
|
+
{
|
|
2464
|
+
action: "startBluetoothDevicesDiscovery",
|
|
2465
|
+
params: options
|
|
2466
|
+
},
|
|
2467
|
+
callbacks
|
|
2468
|
+
);
|
|
2469
|
+
});
|
|
2470
|
+
}
|
|
2471
|
+
/**
|
|
2472
|
+
* 关闭蓝牙适配器
|
|
2473
|
+
*
|
|
2474
|
+
* @description 关闭本机蓝牙适配器模块,断开所有已建立的连接。调用该方法将会断开所有蓝牙连接,
|
|
2475
|
+
* 释放系统资源。建议在不需要使用蓝牙功能时及时调用此方法。
|
|
2476
|
+
*
|
|
2477
|
+
* @param {CloseBluetoothOptions} [options={}] - 关闭蓝牙适配器的配置选项
|
|
2478
|
+
* @param {Function} [options.success] - 接口调用成功的回调函数
|
|
2479
|
+
* @param {Function} [options.fail] - 接口调用失败的回调函数
|
|
2480
|
+
* @param {Function} [options.complete] - 接口调用结束的回调函数(调用成功、失败都会执行)
|
|
2481
|
+
*
|
|
2482
|
+
* @throws {BridgeError} 当插件未正确初始化时抛出错误
|
|
2483
|
+
*
|
|
2484
|
+
* @returns {void}
|
|
2485
|
+
*
|
|
2486
|
+
* @example
|
|
2487
|
+
*/
|
|
2488
|
+
closeBluetoothAdapter(options = {}) {
|
|
2489
|
+
if (!this.context) {
|
|
2490
|
+
throw createBridgeError(
|
|
2491
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
2492
|
+
"插件未正确初始化"
|
|
2493
|
+
);
|
|
2494
|
+
}
|
|
2495
|
+
const callbacks = {
|
|
2496
|
+
success: options.success,
|
|
2497
|
+
fail: options.fail,
|
|
2498
|
+
complete: options.complete
|
|
2499
|
+
};
|
|
2500
|
+
this.context.bridge.ready(() => {
|
|
2501
|
+
var _a;
|
|
2502
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
2503
|
+
{
|
|
2504
|
+
action: "closeBluetoothAdapter",
|
|
2505
|
+
params: options
|
|
2506
|
+
},
|
|
2507
|
+
callbacks
|
|
2508
|
+
);
|
|
2509
|
+
});
|
|
2510
|
+
}
|
|
2511
|
+
/**
|
|
2512
|
+
* 开启蓝牙功能
|
|
2513
|
+
*
|
|
2514
|
+
* 此方法用于启用设备的蓝牙功能。调用前需要确保插件已正确初始化,
|
|
2515
|
+
* 否则会抛出相应的错误。方法会通过桥接器调用原生蓝牙开启功能。
|
|
2516
|
+
*
|
|
2517
|
+
* @param options - 开启蓝牙的配置选项
|
|
2518
|
+
* @param options.success - 成功回调函数,当蓝牙成功开启时调用
|
|
2519
|
+
* @param options.fail - 失败回调函数,当蓝牙开启失败时调用
|
|
2520
|
+
* @param options.complete - 完成回调函数,无论成功或失败都会调用
|
|
2521
|
+
*
|
|
2522
|
+
* @throws {BridgeError} 当插件未正确初始化时抛出 PLUGIN_NOT_INITIALIZED 错误
|
|
2523
|
+
*
|
|
2524
|
+
* @returns {void} 无返回值
|
|
2525
|
+
*
|
|
2526
|
+
* @example
|
|
2527
|
+
*/
|
|
2528
|
+
openBluetoothAdapter(options = {}) {
|
|
2529
|
+
if (!this.context) {
|
|
2530
|
+
throw createBridgeError(
|
|
2531
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
2532
|
+
"插件未正确初始化"
|
|
2533
|
+
);
|
|
2534
|
+
}
|
|
2535
|
+
if (options.autoClose !== void 0 && typeof options.autoClose !== "boolean") {
|
|
2536
|
+
throw createBridgeError(
|
|
2537
|
+
BridgeErrorCode.JSON_ERROR,
|
|
2538
|
+
"autoClose参数格式不正确"
|
|
2539
|
+
);
|
|
2540
|
+
}
|
|
2541
|
+
options.autoClose = options.autoClose ?? true;
|
|
2542
|
+
const callbacks = {
|
|
2543
|
+
success: options.success,
|
|
2544
|
+
fail: options.fail,
|
|
2545
|
+
complete: options.complete
|
|
2546
|
+
};
|
|
2547
|
+
this.context.bridge.ready(() => {
|
|
2548
|
+
var _a;
|
|
2549
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
2550
|
+
{
|
|
2551
|
+
action: "openBluetoothAdapter",
|
|
2552
|
+
params: options
|
|
2553
|
+
},
|
|
2554
|
+
callbacks
|
|
2555
|
+
);
|
|
2556
|
+
});
|
|
2557
|
+
}
|
|
2558
|
+
/**
|
|
2559
|
+
* 搜索到新蓝牙时触发
|
|
2560
|
+
* 当发现新的蓝牙设备时,会触发回调函数并传入设备列表。
|
|
2561
|
+
* 该方法需要在插件正确初始化后调用,否则会抛出异常。
|
|
2562
|
+
* @param callback - 新蓝牙搜索到时触发的回调函数
|
|
2563
|
+
* @param callback.devices - 发现的蓝牙设备列表
|
|
2564
|
+
* @throws {BridgeError} 当插件未正确初始化时抛出 PLUGIN_NOT_INITIALIZED 错误
|
|
2565
|
+
*/
|
|
2566
|
+
getBluetoothDevices(callback) {
|
|
2567
|
+
if (!this.context) {
|
|
2568
|
+
throw createBridgeError(
|
|
2569
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
2570
|
+
"插件未正确初始化"
|
|
2571
|
+
);
|
|
2572
|
+
}
|
|
2573
|
+
this.context.bridge.ready(() => {
|
|
2574
|
+
var _a;
|
|
2575
|
+
(_a = this.context) == null ? void 0 : _a.bridge.onNativeEvent(
|
|
2576
|
+
"getBluetoothDevices",
|
|
2577
|
+
(event) => {
|
|
2578
|
+
callback(event.data);
|
|
2579
|
+
}
|
|
2580
|
+
);
|
|
2581
|
+
});
|
|
2582
|
+
}
|
|
2583
|
+
}
|
|
2584
|
+
const bluetooth = new BluetoothPlugin();
|
|
2585
|
+
class StoragePlugin {
|
|
2586
|
+
constructor() {
|
|
2587
|
+
__publicField(this, "context");
|
|
2588
|
+
__publicField(this, "name", "storage");
|
|
2589
|
+
__publicField(this, "version", "1.0.0");
|
|
2590
|
+
}
|
|
2591
|
+
install(context) {
|
|
2592
|
+
this.context = context;
|
|
2593
|
+
context.bridge.registerMethod("setStorage", this.setStorage.bind(this));
|
|
2594
|
+
context.bridge.registerMethod("setStorageSync", this.setStorageSync.bind(this));
|
|
2595
|
+
context.bridge.registerMethod("getStorage", this.getStorage.bind(this));
|
|
2596
|
+
context.bridge.registerMethod("getStorageSync", this.getStorageSync.bind(this));
|
|
2597
|
+
context.bridge.registerMethod("removeStorage", this.removeStorage.bind(this));
|
|
2598
|
+
context.bridge.log(`本地存储插件 v${this.version} 已安装`);
|
|
2599
|
+
}
|
|
2600
|
+
_createCallbacks(options) {
|
|
2601
|
+
return {
|
|
2602
|
+
success: options.success,
|
|
2603
|
+
fail: options.fail,
|
|
2604
|
+
complete: options.complete
|
|
2605
|
+
};
|
|
2606
|
+
}
|
|
2607
|
+
async _callNativeMethod(action, params, callbacks) {
|
|
2608
|
+
if (!this.context) {
|
|
2609
|
+
throw createBridgeError(
|
|
2610
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
2611
|
+
"插件未正确初始化"
|
|
2612
|
+
);
|
|
2613
|
+
}
|
|
2614
|
+
return new Promise((resolve, reject) => {
|
|
2615
|
+
var _a;
|
|
2616
|
+
(_a = this.context) == null ? void 0 : _a.bridge.ready(() => {
|
|
2617
|
+
var _a2;
|
|
2618
|
+
(_a2 = this.context) == null ? void 0 : _a2.bridge.callNative(
|
|
2619
|
+
{
|
|
2620
|
+
action,
|
|
2621
|
+
params
|
|
2622
|
+
},
|
|
2623
|
+
{
|
|
2624
|
+
...callbacks,
|
|
2625
|
+
success: (response) => {
|
|
2626
|
+
if (callbacks.success) {
|
|
2627
|
+
callbacks.success(response);
|
|
2628
|
+
}
|
|
2629
|
+
resolve(response);
|
|
2630
|
+
},
|
|
2631
|
+
fail: (error) => {
|
|
2632
|
+
if (callbacks.fail) {
|
|
2633
|
+
callbacks.fail(error);
|
|
2634
|
+
}
|
|
2635
|
+
reject(error);
|
|
2636
|
+
}
|
|
2637
|
+
}
|
|
2638
|
+
);
|
|
2639
|
+
});
|
|
2640
|
+
});
|
|
2641
|
+
}
|
|
2642
|
+
/**
|
|
2643
|
+
* 同步设置存储数据
|
|
2644
|
+
* @param options 存储选项,包含键值和数据
|
|
2645
|
+
* @returns 存储响应
|
|
2646
|
+
* @throws 如果未提供key,则抛出错误
|
|
2647
|
+
*/
|
|
2648
|
+
setStorageSync(options) {
|
|
2649
|
+
var _a;
|
|
2650
|
+
if (!this.context) {
|
|
2651
|
+
throw createBridgeError(
|
|
2652
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
2653
|
+
"插件未正确初始化"
|
|
2654
|
+
);
|
|
2655
|
+
}
|
|
2656
|
+
const key = options.key;
|
|
2657
|
+
if (!key) {
|
|
2658
|
+
throw createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, "缺少必需的参数: key");
|
|
2659
|
+
}
|
|
2660
|
+
if (!options.data) {
|
|
2661
|
+
throw createBridgeError(BridgeErrorCode.INVALID_ACTION, "缺少必需的参数: data");
|
|
2662
|
+
}
|
|
2663
|
+
if (typeof key !== "string") {
|
|
2664
|
+
throw createBridgeError(BridgeErrorCode.INVALID_ACTION, "key参数格式不正确");
|
|
2665
|
+
}
|
|
2666
|
+
if (typeof options.data !== "object" && typeof options.data !== "string") {
|
|
2667
|
+
throw createBridgeError(BridgeErrorCode.INVALID_ACTION, "data参数格式不正确");
|
|
2668
|
+
}
|
|
2669
|
+
if (typeof options.data === "object" && Object.keys(options.data).length === 0) {
|
|
2670
|
+
throw createBridgeError(BridgeErrorCode.INVALID_ACTION, "data参数格式不正确");
|
|
2671
|
+
}
|
|
2672
|
+
const callbacks = this._createCallbacks(options);
|
|
2673
|
+
(_a = this.context) == null ? void 0 : _a.bridge.ready(() => {
|
|
2674
|
+
var _a2;
|
|
2675
|
+
(_a2 = this.context) == null ? void 0 : _a2.bridge.callNative(
|
|
2676
|
+
{
|
|
2677
|
+
action: "setStorageSync",
|
|
2678
|
+
params: options
|
|
2679
|
+
},
|
|
2680
|
+
callbacks
|
|
2681
|
+
);
|
|
2682
|
+
});
|
|
2683
|
+
}
|
|
2684
|
+
async setStorage(options) {
|
|
2685
|
+
const key = options.key;
|
|
2686
|
+
if (!key) {
|
|
2687
|
+
throw createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, "缺少必需的参数: key");
|
|
2688
|
+
}
|
|
2689
|
+
if (!options.data) {
|
|
2690
|
+
throw createBridgeError(BridgeErrorCode.INVALID_ACTION, "缺少必需的参数: data");
|
|
2691
|
+
}
|
|
2692
|
+
if (typeof key !== "string") {
|
|
2693
|
+
throw createBridgeError(BridgeErrorCode.INVALID_ACTION, "key参数格式不正确");
|
|
2694
|
+
}
|
|
2695
|
+
if (typeof options.data !== "object" && typeof options.data !== "string") {
|
|
2696
|
+
throw createBridgeError(BridgeErrorCode.INVALID_ACTION, "data参数格式不正确");
|
|
2697
|
+
}
|
|
2698
|
+
if (typeof options.data === "object" && Object.keys(options.data).length === 0) {
|
|
2699
|
+
throw createBridgeError(BridgeErrorCode.INVALID_ACTION, "data参数格式不正确");
|
|
2700
|
+
}
|
|
2701
|
+
const callbacks = this._createCallbacks(options);
|
|
2702
|
+
return this._callNativeMethod("setStorage", { key: options.key, data: options.data }, callbacks);
|
|
2703
|
+
}
|
|
2704
|
+
getStorageSync(options) {
|
|
2705
|
+
var _a;
|
|
2706
|
+
if (!this.context) {
|
|
2707
|
+
throw createBridgeError(
|
|
2708
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
2709
|
+
"插件未正确初始化"
|
|
2710
|
+
);
|
|
2711
|
+
}
|
|
2712
|
+
const key = options.key;
|
|
2713
|
+
if (!key) {
|
|
2714
|
+
throw createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, "缺少必需的参数: key");
|
|
2715
|
+
}
|
|
2716
|
+
if (typeof key !== "string") {
|
|
2717
|
+
throw createBridgeError(BridgeErrorCode.INVALID_ACTION, "key参数格式不正确");
|
|
2718
|
+
}
|
|
2719
|
+
const callbacks = this._createCallbacks(options);
|
|
2720
|
+
return (_a = this.context) == null ? void 0 : _a.bridge.callNativeSync("getStorageSync", options, callbacks);
|
|
2721
|
+
}
|
|
2722
|
+
async getStorage(options) {
|
|
2723
|
+
const key = options.key;
|
|
2724
|
+
if (!key) {
|
|
2725
|
+
throw createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, "缺少必需的参数: key");
|
|
2726
|
+
}
|
|
2727
|
+
if (typeof key !== "string") {
|
|
2728
|
+
throw createBridgeError(BridgeErrorCode.INVALID_ACTION, "key参数格式不正确");
|
|
2729
|
+
}
|
|
2730
|
+
const callbacks = this._createCallbacks(options);
|
|
2731
|
+
return this._callNativeMethod("getStorage", { key: options.key }, callbacks);
|
|
2732
|
+
}
|
|
2733
|
+
/**
|
|
2734
|
+
* 移除指定的存储数据(异步)
|
|
2735
|
+
* @param options 存储选项,包含键值
|
|
2736
|
+
* @returns 存储响应
|
|
2737
|
+
*/
|
|
2738
|
+
async removeStorage(options) {
|
|
2739
|
+
const key = options.key;
|
|
2740
|
+
if (!key) {
|
|
2741
|
+
throw createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, "缺少必需的参数: key");
|
|
2742
|
+
}
|
|
2743
|
+
if (typeof key !== "string") {
|
|
2744
|
+
throw createBridgeError(BridgeErrorCode.INVALID_ACTION, "key参数格式不正确");
|
|
2745
|
+
}
|
|
2746
|
+
const callbacks = this._createCallbacks(options);
|
|
2747
|
+
return this._callNativeMethod("removeStorage", { key }, callbacks);
|
|
2748
|
+
}
|
|
2749
|
+
}
|
|
2750
|
+
const storagePlugin = new StoragePlugin();
|
|
2751
|
+
class AuthenticationPlugin {
|
|
2752
|
+
constructor() {
|
|
2753
|
+
__publicField(this, "context");
|
|
2754
|
+
__publicField(this, "name", "authentication");
|
|
2755
|
+
__publicField(this, "version", "1.0.0");
|
|
2756
|
+
}
|
|
2757
|
+
install(context) {
|
|
2758
|
+
this.context = context;
|
|
2759
|
+
context.bridge.log(`实人认证插件 v${this.version} 已安装`);
|
|
2760
|
+
context.bridge.registerMethod(
|
|
2761
|
+
"exclusiveLiveCheck",
|
|
2762
|
+
this.exclusiveLiveCheck.bind(this)
|
|
2763
|
+
);
|
|
2764
|
+
}
|
|
2765
|
+
/**
|
|
2766
|
+
* 执行实人认证
|
|
2767
|
+
* @param options - 认证选项,包含成功、失败和完成的回调函数
|
|
2768
|
+
* @property options.success - 认证成功时的回调函数,接收认证结果
|
|
2769
|
+
* @property options.fail - 认证失败时的回调函数,接收错误信息
|
|
2770
|
+
* @property options.complete - 认证完成时的回调函数(无论成功或失败)
|
|
2771
|
+
* @returns Promise<AuthenticationResult> - 返回认证结果的Promise对象
|
|
2772
|
+
* @throws {BridgeError} - 如果插件未正确初始化,抛出BridgeError异常
|
|
2773
|
+
*/
|
|
2774
|
+
exclusiveLiveCheck(options = {}) {
|
|
2775
|
+
return new Promise((resolve, reject) => {
|
|
2776
|
+
if (!this.context) {
|
|
2777
|
+
throw createBridgeError(
|
|
2778
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
2779
|
+
"插件未正确初始化"
|
|
2780
|
+
);
|
|
2781
|
+
}
|
|
2782
|
+
const callbacks = {
|
|
2783
|
+
success: (res) => {
|
|
2784
|
+
resolve(res);
|
|
2785
|
+
},
|
|
2786
|
+
fail: (err) => {
|
|
2787
|
+
reject(err);
|
|
2788
|
+
},
|
|
2789
|
+
complete: options.complete
|
|
2790
|
+
};
|
|
2791
|
+
this.context.bridge.ready(() => {
|
|
2792
|
+
var _a;
|
|
2793
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
2794
|
+
{
|
|
2795
|
+
action: "exclusiveLiveCheck",
|
|
2796
|
+
params: {}
|
|
2797
|
+
},
|
|
2798
|
+
callbacks
|
|
2799
|
+
);
|
|
2800
|
+
});
|
|
2801
|
+
});
|
|
2802
|
+
}
|
|
2803
|
+
}
|
|
2804
|
+
const authenticationPlugin = new AuthenticationPlugin();
|
|
2805
|
+
class RequestPlugin {
|
|
2806
|
+
constructor() {
|
|
2807
|
+
__publicField(this, "name", "request");
|
|
2808
|
+
__publicField(this, "version", "1.0.0");
|
|
2809
|
+
__publicField(this, "context");
|
|
2810
|
+
}
|
|
2811
|
+
/**
|
|
2812
|
+
* 安装插件
|
|
2813
|
+
*/
|
|
2814
|
+
install(context) {
|
|
2815
|
+
this.context = context;
|
|
2816
|
+
context.bridge.registerMethod("httpRequest", this.httpRequest.bind(this));
|
|
2817
|
+
context.bridge.registerMethod("httpRequestSync", this.httpRequestSync.bind(this));
|
|
2818
|
+
context.bridge.registerMethod("uploadFile", this.uploadFile.bind(this));
|
|
2819
|
+
context.bridge.registerMethod("downloadFile", this.downloadFile.bind(this));
|
|
2820
|
+
context.bridge.log(`网络请求插件 v${this.version} 已安装`);
|
|
2821
|
+
}
|
|
2822
|
+
/**
|
|
2823
|
+
* 执行HTTP请求
|
|
2824
|
+
* @param options 请求配置选项
|
|
2825
|
+
* @property {string} url 请求URL(必需)
|
|
2826
|
+
* @property {string} [method] HTTP方法,默认为'GET'
|
|
2827
|
+
* @property {Object} [headers] 请求头,默认为{'Content-Type': 'application/x-www-form-urlencoded'}
|
|
2828
|
+
* @property {Object} [data] 请求数据,默认为{}
|
|
2829
|
+
* @property {string} [dataType] 响应数据类型,默认为'json'
|
|
2830
|
+
* @property {number} [timeout] 超时时间(毫秒),默认为30000
|
|
2831
|
+
* @property {string} [cacheModel] 缓存模式,默认为'noStore'
|
|
2832
|
+
* @returns {Promise<RequestResponse>} 返回一个Promise,解析为请求响应结果
|
|
2833
|
+
*/
|
|
2834
|
+
httpRequest(options) {
|
|
2835
|
+
return new Promise((resolve, reject) => {
|
|
2836
|
+
if (!this.context) {
|
|
2837
|
+
throw createBridgeError(
|
|
2838
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
2839
|
+
"插件未正确初始化"
|
|
2840
|
+
);
|
|
2841
|
+
}
|
|
2842
|
+
if (!options.url) {
|
|
2843
|
+
throw createBridgeError(
|
|
2844
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
2845
|
+
"缺少必需的参数: url"
|
|
2846
|
+
);
|
|
2847
|
+
}
|
|
2848
|
+
if (typeof options.url !== "string" || !options.url.trim()) {
|
|
2849
|
+
throw createBridgeError(
|
|
2850
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
2851
|
+
"url参数格式不正确"
|
|
2852
|
+
);
|
|
2853
|
+
}
|
|
2854
|
+
const validMethods = ["GET", "POST"];
|
|
2855
|
+
if (options.method !== void 0 && !validMethods.includes(options.method.toUpperCase())) {
|
|
2856
|
+
throw createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, `method参数格式不正确`);
|
|
2857
|
+
}
|
|
2858
|
+
if (options.headers !== void 0 && typeof options.headers !== "object") {
|
|
2859
|
+
throw createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, "headers参数格式不正确");
|
|
2860
|
+
}
|
|
2861
|
+
if (options.data !== void 0 && typeof options.data !== "object") {
|
|
2862
|
+
throw createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, "data参数格式不正确");
|
|
2863
|
+
}
|
|
2864
|
+
const validDataTypes = ["json", "text", "base64"];
|
|
2865
|
+
if (options.dataType !== void 0 && !validDataTypes.includes(options.dataType.toLowerCase())) {
|
|
2866
|
+
throw createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, `dataType参数格式不正确`);
|
|
2867
|
+
}
|
|
2868
|
+
if (options.timeout !== void 0 && (typeof options.timeout !== "number" || options.timeout <= 0)) {
|
|
2869
|
+
throw createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, "timeout参数格式不正确");
|
|
2870
|
+
}
|
|
2871
|
+
const validCacheModels = ["noStore", "noCache", "smartCache", "firstCache"];
|
|
2872
|
+
if (options.cacheModel !== void 0 && typeof options.cacheModel !== "string" || options.cacheModel !== void 0 && !validCacheModels.includes(options.cacheModel)) {
|
|
2873
|
+
throw createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, `cacheModel参数格式不正确`);
|
|
2874
|
+
}
|
|
2875
|
+
options.method = options.method || "GET";
|
|
2876
|
+
options.headers = options.headers && Object.keys(options.headers).length > 0 ? options.headers : { "Content-Type": "application/x-www-form-urlencoded" };
|
|
2877
|
+
options.data = options.data || {};
|
|
2878
|
+
options.dataType = options.dataType || "json";
|
|
2879
|
+
options.timeout = options.timeout || 3e4;
|
|
2880
|
+
options.cacheModel = options.cacheModel || "noStore";
|
|
2881
|
+
const callbacks = {
|
|
2882
|
+
success: (res) => {
|
|
2883
|
+
var _a;
|
|
2884
|
+
(_a = options == null ? void 0 : options.success) == null ? void 0 : _a.call(options, res);
|
|
2885
|
+
resolve(res);
|
|
2886
|
+
},
|
|
2887
|
+
fail: (err) => {
|
|
2888
|
+
var _a;
|
|
2889
|
+
(_a = options.fail) == null ? void 0 : _a.call(options, err);
|
|
2890
|
+
reject(err);
|
|
2891
|
+
},
|
|
2892
|
+
complete: () => {
|
|
2893
|
+
var _a;
|
|
2894
|
+
(_a = options.complete) == null ? void 0 : _a.call(options);
|
|
2895
|
+
}
|
|
2896
|
+
};
|
|
2897
|
+
this.context.bridge.ready(() => {
|
|
2898
|
+
var _a;
|
|
2899
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
2900
|
+
{
|
|
2901
|
+
action: "httpRequest",
|
|
2902
|
+
params: options,
|
|
2903
|
+
timeout: options.timeout
|
|
2904
|
+
},
|
|
2905
|
+
callbacks
|
|
2906
|
+
);
|
|
2907
|
+
});
|
|
2908
|
+
});
|
|
2909
|
+
}
|
|
2910
|
+
/**
|
|
2911
|
+
*
|
|
2912
|
+
*/
|
|
2913
|
+
httpRequestSync(options) {
|
|
2914
|
+
if (!this.context) {
|
|
2915
|
+
throw createBridgeError(
|
|
2916
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
2917
|
+
"插件未正确初始化"
|
|
2918
|
+
);
|
|
2919
|
+
}
|
|
2920
|
+
if (!options.url) {
|
|
2921
|
+
throw createBridgeError(
|
|
2922
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
2923
|
+
"缺少必需的参数: url"
|
|
2924
|
+
);
|
|
2925
|
+
}
|
|
2926
|
+
if (typeof options.url !== "string" || !options.url.trim()) {
|
|
2927
|
+
throw createBridgeError(
|
|
2928
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
2929
|
+
"url参数格式不正确"
|
|
2930
|
+
);
|
|
2931
|
+
}
|
|
2932
|
+
const validMethods = ["GET", "POST"];
|
|
2933
|
+
if (options.method !== void 0 && !validMethods.includes(options.method.toUpperCase())) {
|
|
2934
|
+
throw createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, `method参数格式不正确`);
|
|
2935
|
+
}
|
|
2936
|
+
if (options.headers !== void 0 && typeof options.headers !== "object") {
|
|
2937
|
+
throw createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, "headers参数格式不正确");
|
|
2938
|
+
}
|
|
2939
|
+
if (options.data !== void 0 && typeof options.data !== "object") {
|
|
2940
|
+
throw createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, "data参数格式不正确");
|
|
2941
|
+
}
|
|
2942
|
+
const validDataTypes = ["json", "text", "base64"];
|
|
2943
|
+
if (options.dataType !== void 0 && !validDataTypes.includes(options.dataType.toLowerCase())) {
|
|
2944
|
+
throw createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, `dataType参数格式不正确`);
|
|
2945
|
+
}
|
|
2946
|
+
if (options.timeout !== void 0 && (typeof options.timeout !== "number" || options.timeout <= 0)) {
|
|
2947
|
+
throw createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, "timeout参数格式不正确");
|
|
2948
|
+
}
|
|
2949
|
+
const validCacheModels = ["noStore", "noCache", "smartCache", "firstCache"];
|
|
2950
|
+
if (options.cacheModel !== void 0 && typeof options.cacheModel !== "string" || options.cacheModel !== void 0 && !validCacheModels.includes(options.cacheModel)) {
|
|
2951
|
+
throw createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, `cacheModel参数格式不正确`);
|
|
2952
|
+
}
|
|
2953
|
+
options.method = options.method || "GET";
|
|
2954
|
+
options.headers = options.headers && Object.keys(options.headers).length > 0 ? options.headers : { "Content-Type": "application/x-www-form-urlencoded" };
|
|
2955
|
+
options.data = options.data || {};
|
|
2956
|
+
options.dataType = options.dataType || "json";
|
|
2957
|
+
options.timeout = options.timeout || 3e4;
|
|
2958
|
+
options.cacheModel = options.cacheModel || "noStore";
|
|
2959
|
+
const callbacks = {
|
|
2960
|
+
success: (res) => {
|
|
2961
|
+
var _a;
|
|
2962
|
+
(_a = options == null ? void 0 : options.success) == null ? void 0 : _a.call(options, res);
|
|
2963
|
+
},
|
|
2964
|
+
fail: (err) => {
|
|
2965
|
+
var _a;
|
|
2966
|
+
(_a = options.fail) == null ? void 0 : _a.call(options, err);
|
|
2967
|
+
},
|
|
2968
|
+
complete: () => {
|
|
2969
|
+
var _a;
|
|
2970
|
+
(_a = options.complete) == null ? void 0 : _a.call(options);
|
|
2971
|
+
}
|
|
2972
|
+
};
|
|
2973
|
+
this.context.bridge.ready(() => {
|
|
2974
|
+
var _a;
|
|
2975
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
2976
|
+
{
|
|
2977
|
+
action: "httpRequest",
|
|
2978
|
+
params: options,
|
|
2979
|
+
timeout: options.timeout
|
|
2980
|
+
},
|
|
2981
|
+
callbacks
|
|
2982
|
+
);
|
|
2983
|
+
});
|
|
2984
|
+
}
|
|
2985
|
+
/**
|
|
2986
|
+
* 执行文件上传
|
|
2987
|
+
* @param options 上传文件选项
|
|
2988
|
+
* @returns Promise<UploadFileResponse> 返回上传结果
|
|
2989
|
+
*/
|
|
2990
|
+
uploadFile(options) {
|
|
2991
|
+
return new Promise((resolve, reject) => {
|
|
2992
|
+
if (!this.context) {
|
|
2993
|
+
throw createBridgeError(
|
|
2994
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
2995
|
+
"插件未正确初始化"
|
|
2996
|
+
);
|
|
2997
|
+
}
|
|
2998
|
+
if (!options.url || !options.filePath || !options.fileName || !options.fileType) {
|
|
2999
|
+
return reject(createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, "缺少必需的参数: url, filePath, fileName, fileType"));
|
|
3000
|
+
}
|
|
3001
|
+
if (typeof options.url !== "string" || !options.url.trim()) {
|
|
3002
|
+
return reject(createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, "url参数格式不正确"));
|
|
3003
|
+
}
|
|
3004
|
+
if (typeof options.filePath !== "string" || !options.filePath.trim()) {
|
|
3005
|
+
return reject(createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, "filePath参数格式不正确"));
|
|
3006
|
+
}
|
|
3007
|
+
if (typeof options.fileName !== "string" || !options.fileName.trim()) {
|
|
3008
|
+
return reject(createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, "fileName参数格式不正确"));
|
|
3009
|
+
}
|
|
3010
|
+
const validFileTypes = ["image", "video", "audio"];
|
|
3011
|
+
if (!validFileTypes.includes(options.fileType)) {
|
|
3012
|
+
return reject(createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, `不支持的文件类型: ${options.fileType}`));
|
|
3013
|
+
}
|
|
3014
|
+
if (options.header !== void 0 && typeof options.header !== "object") {
|
|
3015
|
+
return reject(createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, "header参数格式不正确"));
|
|
3016
|
+
}
|
|
3017
|
+
if (options.formData !== void 0 && typeof options.formData !== "object") {
|
|
3018
|
+
return reject(createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, "formData参数格式不正确"));
|
|
3019
|
+
}
|
|
3020
|
+
const callbacks = {
|
|
3021
|
+
success: (res) => {
|
|
3022
|
+
resolve(res);
|
|
3023
|
+
},
|
|
3024
|
+
fail: (err) => {
|
|
3025
|
+
reject(err);
|
|
3026
|
+
},
|
|
3027
|
+
complete: () => {
|
|
3028
|
+
}
|
|
3029
|
+
};
|
|
3030
|
+
this.context.bridge.ready(() => {
|
|
3031
|
+
var _a;
|
|
3032
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
3033
|
+
{
|
|
3034
|
+
action: "uploadFile",
|
|
3035
|
+
params: options
|
|
3036
|
+
},
|
|
3037
|
+
callbacks
|
|
3038
|
+
);
|
|
3039
|
+
});
|
|
3040
|
+
});
|
|
3041
|
+
}
|
|
3042
|
+
/**
|
|
3043
|
+
* 执行文件下载
|
|
3044
|
+
* @param options 下载文件选项
|
|
3045
|
+
* @returns Promise<DownloadFileResponse> 返回下载结果
|
|
3046
|
+
*/
|
|
3047
|
+
downloadFile(options) {
|
|
3048
|
+
return new Promise((resolve, reject) => {
|
|
3049
|
+
if (!this.context) {
|
|
3050
|
+
throw createBridgeError(
|
|
3051
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
3052
|
+
"插件未正确初始化"
|
|
3053
|
+
);
|
|
3054
|
+
}
|
|
3055
|
+
if (!options.url) {
|
|
3056
|
+
return reject(createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, "缺少必需的参数: url"));
|
|
3057
|
+
}
|
|
3058
|
+
if (typeof options.url !== "string" || !options.url.trim()) {
|
|
3059
|
+
return reject(createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, "url参数格式不正确"));
|
|
3060
|
+
}
|
|
3061
|
+
if (options.header !== void 0 && typeof options.header !== "object") {
|
|
3062
|
+
return reject(createBridgeError(BridgeErrorCode.PLUGIN_NOT_INITIALIZED, "header参数格式不正确"));
|
|
3063
|
+
}
|
|
3064
|
+
const callbacks = {
|
|
3065
|
+
success: (res) => {
|
|
3066
|
+
resolve(res);
|
|
3067
|
+
},
|
|
3068
|
+
fail: (err) => {
|
|
3069
|
+
reject(err);
|
|
3070
|
+
},
|
|
3071
|
+
complete: () => {
|
|
3072
|
+
}
|
|
3073
|
+
};
|
|
3074
|
+
this.context.bridge.ready(() => {
|
|
3075
|
+
var _a;
|
|
3076
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
3077
|
+
{
|
|
3078
|
+
action: "downloadFile",
|
|
3079
|
+
params: options
|
|
3080
|
+
},
|
|
3081
|
+
callbacks
|
|
3082
|
+
);
|
|
3083
|
+
});
|
|
3084
|
+
});
|
|
3085
|
+
}
|
|
3086
|
+
}
|
|
3087
|
+
const requestPlugin = new RequestPlugin();
|
|
3088
|
+
class WifiPlugin {
|
|
3089
|
+
constructor() {
|
|
3090
|
+
__publicField(this, "context");
|
|
3091
|
+
__publicField(this, "name", "wifi");
|
|
3092
|
+
__publicField(this, "version", "1.0.0");
|
|
3093
|
+
}
|
|
3094
|
+
install(context) {
|
|
3095
|
+
this.context = context;
|
|
3096
|
+
context.bridge.registerMethod("startWifi", this.startWifi.bind(this));
|
|
3097
|
+
context.bridge.registerMethod("stopWifi", this.stopWifi.bind(this));
|
|
3098
|
+
context.bridge.registerMethod("connectWifi", this.connectWifi.bind(this));
|
|
3099
|
+
context.bridge.registerMethod("getScanWifiListAsync", this.getScanWifiListAsync.bind(this));
|
|
3100
|
+
context.bridge.log(`Wi-Fi 插件 v${this.version} 已安装`);
|
|
3101
|
+
}
|
|
3102
|
+
/**
|
|
3103
|
+
* 初始化 Wi-Fi 模块
|
|
3104
|
+
* @returns
|
|
3105
|
+
*/
|
|
3106
|
+
startWifi() {
|
|
3107
|
+
return new Promise((resolve, reject) => {
|
|
3108
|
+
if (!this.context) {
|
|
3109
|
+
throw createBridgeError(
|
|
3110
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
3111
|
+
"插件未正确初始化"
|
|
3112
|
+
);
|
|
3113
|
+
}
|
|
3114
|
+
const callbacks = {
|
|
3115
|
+
success: (obj) => {
|
|
3116
|
+
resolve(obj);
|
|
3117
|
+
},
|
|
3118
|
+
fail: (err) => {
|
|
3119
|
+
reject(err);
|
|
3120
|
+
}
|
|
3121
|
+
};
|
|
3122
|
+
this.context.bridge.ready(() => {
|
|
3123
|
+
var _a;
|
|
3124
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
3125
|
+
{
|
|
3126
|
+
action: "startWifi",
|
|
3127
|
+
params: {}
|
|
3128
|
+
},
|
|
3129
|
+
callbacks
|
|
3130
|
+
);
|
|
3131
|
+
});
|
|
3132
|
+
});
|
|
3133
|
+
}
|
|
3134
|
+
/**
|
|
3135
|
+
* 关闭 Wi-Fi 模块
|
|
3136
|
+
* @returns
|
|
3137
|
+
*/
|
|
3138
|
+
stopWifi() {
|
|
3139
|
+
return new Promise((resolve, reject) => {
|
|
3140
|
+
if (!this.context) {
|
|
3141
|
+
throw createBridgeError(
|
|
3142
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
3143
|
+
"插件未正确初始化"
|
|
3144
|
+
);
|
|
3145
|
+
}
|
|
3146
|
+
const callbacks = {
|
|
3147
|
+
success: (obj) => {
|
|
3148
|
+
resolve(obj);
|
|
3149
|
+
},
|
|
3150
|
+
fail: (err) => {
|
|
3151
|
+
reject(err);
|
|
3152
|
+
}
|
|
3153
|
+
};
|
|
3154
|
+
this.context.bridge.ready(() => {
|
|
3155
|
+
var _a;
|
|
3156
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
3157
|
+
{
|
|
3158
|
+
action: "stopWifi",
|
|
3159
|
+
params: {}
|
|
3160
|
+
},
|
|
3161
|
+
callbacks
|
|
3162
|
+
);
|
|
3163
|
+
});
|
|
3164
|
+
});
|
|
3165
|
+
}
|
|
3166
|
+
/**
|
|
3167
|
+
* 连接 Wi-Fi
|
|
3168
|
+
* @param ssid Wi-Fi 名称
|
|
3169
|
+
* @param password Wi-Fi 密码
|
|
3170
|
+
* @returns
|
|
3171
|
+
*/
|
|
3172
|
+
connectWifi(options) {
|
|
3173
|
+
return new Promise((resolve, reject) => {
|
|
3174
|
+
if (!this.context) {
|
|
3175
|
+
throw createBridgeError(
|
|
3176
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
3177
|
+
"插件未正确初始化"
|
|
3178
|
+
);
|
|
3179
|
+
}
|
|
3180
|
+
if (!options.SSID) {
|
|
3181
|
+
throw createBridgeError(
|
|
3182
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
3183
|
+
"缺少必需的参数: SSID"
|
|
3184
|
+
);
|
|
3185
|
+
}
|
|
3186
|
+
if (typeof options.SSID !== "string") {
|
|
3187
|
+
throw createBridgeError(
|
|
3188
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
3189
|
+
"SSID参数格式错误"
|
|
3190
|
+
);
|
|
3191
|
+
}
|
|
3192
|
+
if (options.BSSID !== void 0 && typeof options.BSSID !== "string") {
|
|
3193
|
+
throw createBridgeError(
|
|
3194
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
3195
|
+
"BSSID参数格式错误"
|
|
3196
|
+
);
|
|
3197
|
+
}
|
|
3198
|
+
if (options.password !== void 0 && typeof options.password !== "string") {
|
|
3199
|
+
throw createBridgeError(
|
|
3200
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
3201
|
+
"password参数格式错误"
|
|
3202
|
+
);
|
|
3203
|
+
}
|
|
3204
|
+
if (options.isWEP !== void 0 && typeof options.isWEP !== "boolean") {
|
|
3205
|
+
throw createBridgeError(
|
|
3206
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
3207
|
+
"isWEP参数格式错误"
|
|
3208
|
+
);
|
|
3209
|
+
}
|
|
3210
|
+
options.isWEP = options.isWEP ?? false;
|
|
3211
|
+
const callbacks = {
|
|
3212
|
+
success: (res) => {
|
|
3213
|
+
resolve(res);
|
|
3214
|
+
},
|
|
3215
|
+
fail: (err) => {
|
|
3216
|
+
reject(err);
|
|
3217
|
+
},
|
|
3218
|
+
complete: options.complete
|
|
3219
|
+
};
|
|
3220
|
+
this.context.bridge.ready(() => {
|
|
3221
|
+
var _a;
|
|
3222
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
3223
|
+
{
|
|
3224
|
+
action: "connectWifi",
|
|
3225
|
+
params: options
|
|
3226
|
+
},
|
|
3227
|
+
callbacks
|
|
3228
|
+
);
|
|
3229
|
+
});
|
|
3230
|
+
});
|
|
3231
|
+
}
|
|
3232
|
+
/**
|
|
3233
|
+
* 获取 Wi-Fi 列表
|
|
3234
|
+
* @param options 获取 Wi-Fi 列表的选项
|
|
3235
|
+
* @returns 返回一个 Promise,解析为 Wi-Fi 列表响应数据
|
|
3236
|
+
*/
|
|
3237
|
+
async getScanWifiListAsync(options) {
|
|
3238
|
+
return new Promise((resolve, reject) => {
|
|
3239
|
+
if (!this.context) {
|
|
3240
|
+
throw createBridgeError(
|
|
3241
|
+
BridgeErrorCode.PLUGIN_NOT_INITIALIZED,
|
|
3242
|
+
"插件未正确初始化"
|
|
3243
|
+
);
|
|
3244
|
+
}
|
|
3245
|
+
if (typeof options.timeout !== "number") {
|
|
3246
|
+
throw createBridgeError(
|
|
3247
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
3248
|
+
"timeout参数格式错误"
|
|
3249
|
+
);
|
|
3250
|
+
}
|
|
3251
|
+
if (typeof options.cacheTime !== "number") {
|
|
3252
|
+
throw createBridgeError(
|
|
3253
|
+
BridgeErrorCode.INVALID_ACTION,
|
|
3254
|
+
"cacheTime参数格式错误"
|
|
3255
|
+
);
|
|
3256
|
+
}
|
|
3257
|
+
const callbacks = {
|
|
3258
|
+
success: (res) => {
|
|
3259
|
+
resolve(res);
|
|
3260
|
+
},
|
|
3261
|
+
fail: (err) => {
|
|
3262
|
+
reject(err);
|
|
3263
|
+
},
|
|
3264
|
+
complete: options.complete
|
|
3265
|
+
};
|
|
3266
|
+
this.context.bridge.ready(() => {
|
|
3267
|
+
var _a;
|
|
3268
|
+
(_a = this.context) == null ? void 0 : _a.bridge.callNative(
|
|
3269
|
+
{
|
|
3270
|
+
action: "getScanWifiListAsync",
|
|
3271
|
+
params: options,
|
|
3272
|
+
timeout: options.timeout
|
|
3273
|
+
},
|
|
3274
|
+
callbacks
|
|
3275
|
+
);
|
|
3276
|
+
});
|
|
3277
|
+
});
|
|
3278
|
+
}
|
|
3279
|
+
}
|
|
3280
|
+
const wifiPlugin = new WifiPlugin();
|
|
3281
|
+
const nativeBridge = createNativeBridge({
|
|
3282
|
+
plugins: [
|
|
3283
|
+
locationPlugin,
|
|
3284
|
+
devicePlugin,
|
|
3285
|
+
mediaPlugin,
|
|
3286
|
+
navigatePlugin,
|
|
3287
|
+
toastPlugin,
|
|
3288
|
+
userinfoPlugin,
|
|
3289
|
+
bluetooth,
|
|
3290
|
+
storagePlugin,
|
|
3291
|
+
authenticationPlugin,
|
|
3292
|
+
requestPlugin,
|
|
3293
|
+
wifiPlugin
|
|
3294
|
+
],
|
|
3295
|
+
debug: true,
|
|
3296
|
+
errorHandler: (error, source) => {
|
|
3297
|
+
console.error(`[NativeBridge Error] ${source}:`, error);
|
|
3298
|
+
}
|
|
3299
|
+
});
|
|
3300
|
+
export {
|
|
3301
|
+
NativeBridgeCore,
|
|
3302
|
+
authenticationPlugin,
|
|
3303
|
+
bluetooth as bluetoothPlugin,
|
|
3304
|
+
createNativeBridge,
|
|
3305
|
+
nativeBridge as default,
|
|
3306
|
+
devicePlugin,
|
|
3307
|
+
locationPlugin,
|
|
3308
|
+
mediaPlugin,
|
|
3309
|
+
navigatePlugin,
|
|
3310
|
+
requestPlugin,
|
|
3311
|
+
storagePlugin,
|
|
3312
|
+
toastPlugin,
|
|
3313
|
+
userinfoPlugin,
|
|
3314
|
+
wifiPlugin
|
|
3315
|
+
};
|
|
3316
|
+
//# sourceMappingURL=native-bridge.es.js.map
|