@lambo-design-mobile/lambo-js-bridge 1.0.0-beta.21 → 1.0.0-beta.23

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.
@@ -1,224 +1,309 @@
1
- import './yuntu';
2
-
3
- class YunTuAdapter {
4
- constructor(options) {
5
- // 标志插件是否已初始化
6
- this.isInitialized = false;
7
- this.initializePlugin(options.pluginConfig);
8
- }
9
-
10
- async initializePlugin(pluginConfig) {
11
- try {
12
- const configToUse = Array.isArray(pluginConfig) && pluginConfig.length > 0
13
- ? JSON.stringify(pluginConfig)
14
- : '["localAuthPlugin","amapPlugin","tabBarPlugin","scanCodePlugin","filePreviewPlugin","imagePickerPlugin"]';
15
- // eslint-disable-next-line no-undef
16
- await yuntuConfig(configToUse); // 确保等待 yuntuConfig 执行完成
17
- this.isInitialized = true;
18
- this.plugins = JSON.parse(configToUse);
19
- console.log("Initialized plugins:", this.plugins);
20
- return this.plugins;
21
- } catch (error) {
22
- console.error("Plugin initialization failed:", error);
23
- this.isInitialized = false;
24
- return "Plugin initialization failed:" + error;
25
- }
26
- }
27
-
28
- // 其余代码保持不变
29
- async getPlatform() {
30
- return {
31
- platform: 'Yuntu'
32
- };
33
- }
34
-
35
- async getLocation(options) {
36
- return new Promise((resolve, reject) => {
37
-
38
- if (window.top.geolocation && typeof window.top.geolocation.getCurrentPosition === 'function') {
39
- window.top.geolocation.getCurrentPosition(
40
- (res) => {
41
- console.log('YunTu: Location obtained', JSON.stringify(res));
42
- resolve(res); // 成功获取位置
43
- },
44
- (err) => {
45
- console.error('YunTu: Failed to obtain location', err);
46
- reject(err); // 获取位置失败
47
- },
48
- options
49
- );
50
- } else {
51
- const errorMessage = 'Geolocation is not initialized or not available';
52
- console.error('YunTu:', errorMessage);
53
- reject(new Error(errorMessage)); // Geolocation 未初始化或不可用
54
- }
55
- });
56
- }
57
-
58
- async openLocation(options) {
59
- return new Promise((resolve, reject) => {
60
- if (window.top.geolocation && typeof window.top.geolocation.openLocation === 'function') {
61
- console.log("1")
62
- window.top.geolocation.openLocation(
63
- (err) => {
64
- console.error('YunTu: Failed to open location', err);
65
- err.message=err.msg
66
- reject(err); // 打开地图失败
67
- },
68
- JSON.stringify( {
69
- latitude: options.latitude,
70
- longitude: options.longitude,
71
- type: options.type || 'amap' // 默认为高德地图
72
- })
73
- );
74
- } else {
75
- const errorMessage = 'Geolocation is not initialized or not available';
76
- console.error('YunTu:', errorMessage);
77
- reject(new Error(errorMessage)); // Geolocation 未初始化或不可用
78
- }
79
- });
80
- }
81
-
82
- async initKey(options) {
83
- return new Promise((resolve, reject) => {
84
- if (window.top.geolocation && typeof window.top.geolocation.initKey === 'function') {
85
- window.top.geolocation.initKey(function (msg) {
86
- console.log('YunTu: initKey success', msg);
87
- resolve(msg); //
88
- }, JSON.stringify(options) );
89
- } else {
90
- const errorMessage = 'Geolocation is not initialized or not available';
91
- console.error('YunTu:', errorMessage);
92
- reject(new Error(errorMessage)); // Geolocation 未初始化或不可用
93
- }
94
- });
95
- }
96
-
97
- async scanCode(options) {
98
- return new Promise((resolve, reject) => {
99
- if (window.top.scanCode && typeof window.top.scanCode.startScan === 'function') {
100
- window.top.scanCode.startScan(
101
- (res) => {
102
- console.log('YunTu: Scan code success', res);
103
- resolve(res); // 成功获取扫码结果
104
- },
105
- (err) => {
106
- console.error('YunTu: Failed to scan code', err);
107
- reject(err); // 扫码失败
108
- },
109
- options // 传递给扫码方法的选项
110
- );
111
- } else {
112
- const errorMessage = 'ScanCode is not initialized or not available';
113
- console.error('YunTu:', errorMessage);
114
- reject(new Error(errorMessage)); // ScanCode 未初始化或不可用
115
- }
116
- });
117
- }
118
-
119
- async localAuthPlugin(options) {
120
- return new Promise((resolve, reject) => {
121
- if (window.top.localAuthPlugin && typeof window.top.localAuthPlugin.auth === 'function') {
122
- window.top.localAuthPlugin.auth(
123
- (res) => {
124
- console.log('YunTu: local auth success', res);
125
- resolve(res); // 成功获取认证结果
126
- },
127
- (err) => {
128
- console.error('YunTu: Failed to local auth', err);
129
- reject(err); // 认证失败
130
- },
131
- options // 传递给认证方法的选项
132
- );
133
- } else {
134
- const errorMessage = 'local auth is not initialized or not available';
135
- console.error('YunTu:', errorMessage);
136
- reject(new Error(errorMessage)); // ScanCode 未初始化或不可用
137
- }
138
- });
139
- }
140
-
141
- async getAvailableBiometrics(options) {
142
- return new Promise((resolve, reject) => {
143
- if (window.top.localAuthPlugin && typeof window.top.localAuthPlugin.getAvailableBiometrics === 'function') {
144
- window.top.localAuthPlugin.getAvailableBiometrics(
145
- (res) => {
146
- console.log('YunTu: Get Available Biometrics', res);
147
- resolve(res); // 成功获取生物认证列表
148
- },
149
- (err) => {
150
- console.error('YunTu: Failed Get Available Biometrics', err);
151
- reject(err); // 获取生物认证列表失败
152
- },
153
- options // 传递给认证方法的选项
154
- );
155
- } else {
156
- const errorMessage = 'local auth is not initialized or not available';
157
- console.error('YunTu:', errorMessage);
158
- reject(new Error(errorMessage));
159
- }
160
- });
161
- }
162
-
163
-
164
- async takePhoto(options) {
165
- console.log('YunTu: Taking photo with options', options);
166
-
167
- return new Promise((resolve, reject) => {
168
- if (window.top.imagePicker) {
169
- const sourceType = options && options.sourceType === 'gallery' ? 'gallery' : 'camera';
170
- const pickerFunction = window.top.imagePicker[sourceType];
171
- options.type= options.outputType.includes("data")?1:0;
172
- if (typeof pickerFunction === 'function') {
173
- pickerFunction(
174
- (res) => {
175
- console.log('YunTu: Photo selection success', res);
176
- resolve(res); // 成功获取照片
177
- },
178
- (err) => {
179
- console.error('YunTu: Failed to select photo', err);
180
- reject(err); // 选择照片失败
181
- },
182
- options // 传递给选取照片方法的选项(如果有需要的话)
183
- );
184
- } else {
185
- const errorMessage = `${sourceType} function is not available in ImagePicker`;
186
- console.error('YunTu:', errorMessage);
187
- reject(new Error(errorMessage)); // 相应的接口未初始化或不可用
188
- }
189
- } else {
190
- const errorMessage = 'ImagePicker is not initialized or not available';
191
- console.error('YunTu:', errorMessage);
192
- reject(new Error(errorMessage)); // ImagePicker 未初始化或不可用
193
- }
194
- });
195
- };
196
-
197
- async filePreview(options) {
198
- console.log('YunTu: File preview with options', options);
199
-
200
- return new Promise((resolve, reject) => {
201
- if (window.top.filePreview && typeof window.top.filePreview.openFile === 'function') {
202
- window.top.filePreview.openFile(
203
- (res) => {
204
- console.log('YunTu: File preview success', res);
205
- resolve(res); // Successfully previewed the file
206
- },
207
- (err) => {
208
- console.error('YunTu: File preview failed', err);
209
- reject(err); // Failed to preview the file
210
- },
211
- JSON.stringify(options) // Pass the options as a string
212
- );
213
- } else {
214
- const errorMessage = 'FilePreview is not initialized or not available';
215
- console.error('YunTu:', errorMessage);
216
- reject(new Error(errorMessage)); // FilePreview interface is not available
217
- }
218
- });
219
- }
220
-
221
-
222
- }
223
-
224
- export default YunTuAdapter;
1
+ import './yuntu';
2
+
3
+ class YunTuAdapter {
4
+ constructor(options) {
5
+ // 标志插件是否已初始化
6
+ this.isInitialized = false;
7
+ this.audioUrl = null; // 录音文件的 URL
8
+ this.fileName = ''; // 录音文件名称
9
+ this.fileType = ''; // 录音文件类型
10
+ this.initializePlugin(options.pluginConfig);
11
+ }
12
+
13
+ async initializePlugin(pluginConfig) {
14
+ try {
15
+ const configToUse = Array.isArray(pluginConfig) && pluginConfig.length > 0
16
+ ? JSON.stringify(pluginConfig)
17
+ : '["localAuthPlugin","amapPlugin","tabBarPlugin","scanCodePlugin","filePreviewPlugin","imagePickerPlugin"]';
18
+ // eslint-disable-next-line no-undef
19
+ await yuntuConfig(configToUse); // 确保等待 yuntuConfig 执行完成
20
+ this.isInitialized = true;
21
+ this.plugins = JSON.parse(configToUse);
22
+ console.log("Initialized plugins:", this.plugins);
23
+ return this.plugins;
24
+ } catch (error) {
25
+ console.error("Plugin initialization failed:", error);
26
+ this.isInitialized = false;
27
+ return "Plugin initialization failed:" + error;
28
+ }
29
+ }
30
+
31
+ // 录音:开始录音(支持自动和手动模式)
32
+ async startRecording(options = { auto: false }) {
33
+ return new Promise((resolve, reject) => {
34
+ if (window.autoRecord && typeof window.autoRecord.startRecording === 'function') {
35
+ window.autoRecord.startRecording(
36
+ (result) => {
37
+ console.log("录音开始:", result);
38
+ resolve(result);
39
+ },
40
+ (error) => {
41
+ console.error("录音错误:", error);
42
+ reject(error);
43
+ },
44
+ JSON.stringify(options) // 传入参数:{auto: false} 表示手动录音,{auto: true} 表示自动录音
45
+ );
46
+ } else {
47
+ reject(new Error("autoRecord 接口不可用"));
48
+ }
49
+ });
50
+ }
51
+
52
+ // 录音:结束录音,并返回录音结果
53
+ async stopRecording() {
54
+ return new Promise((resolve, reject) => {
55
+ if (window.autoRecord && typeof window.autoRecord.stopRecording === 'function') {
56
+ window.autoRecord.stopRecording(
57
+ (result) => {
58
+ console.log("录音停止,返回结果:", result);
59
+ // 如果返回数据为字符串,则尝试解析为对象
60
+ try {
61
+ if (typeof result === "string") {
62
+ result = JSON.parse(result);
63
+ }
64
+ } catch (e) {
65
+ console.error("JSON.parse error:", e);
66
+ }
67
+ resolve(result);
68
+ },
69
+ (error) => {
70
+ console.error("录音结束错误:", error);
71
+ // 同样尝试解析错误信息
72
+ try {
73
+ if (typeof error === "string") {
74
+ error = JSON.parse(error);
75
+ }
76
+ } catch (e) {
77
+ console.error("JSON.parse error:", e);
78
+ }
79
+ reject(error);
80
+ }
81
+ );
82
+ } else {
83
+ reject(new Error("autoRecord 接口不可用"));
84
+ }
85
+ });
86
+ }
87
+
88
+ // 新增下载文件接口
89
+ async downloadFile({ fileUrl, fileName = 'downloadedFile' }) {
90
+ try {
91
+ const response = await fetch(fileUrl);
92
+ if (!response.ok) {
93
+ throw new Error(`Failed to fetch file from ${fileUrl}`);
94
+ }
95
+
96
+ const fileBlob = await response.blob();
97
+
98
+ // 创建一个下载的 Blob URL
99
+ const downloadUrl = URL.createObjectURL(fileBlob);
100
+
101
+ // 通过 window.open 来触发文件下载(无 <a> 标签)
102
+ window.open(downloadUrl, '_blank');
103
+
104
+ // 清理临时 URL
105
+ URL.revokeObjectURL(downloadUrl);
106
+
107
+ console.log(`File "${fileName}" downloaded successfully.`);
108
+ } catch (error) {
109
+ console.error('Download failed:', error);
110
+ }
111
+ }
112
+
113
+
114
+ async getPlatform() {
115
+ return {
116
+ platform: 'Yuntu'
117
+ };
118
+ }
119
+
120
+ async getLocation(options) {
121
+ return new Promise((resolve, reject) => {
122
+
123
+ if (window.top.geolocation && typeof window.top.geolocation.getCurrentPosition === 'function') {
124
+ window.top.geolocation.getCurrentPosition(
125
+ (res) => {
126
+ console.log('YunTu: Location obtained', JSON.stringify(res));
127
+ resolve(res); // 成功获取位置
128
+ },
129
+ (err) => {
130
+ console.error('YunTu: Failed to obtain location', err);
131
+ reject(err); // 获取位置失败
132
+ },
133
+ options
134
+ );
135
+ } else {
136
+ const errorMessage = 'Geolocation is not initialized or not available';
137
+ console.error('YunTu:', errorMessage);
138
+ reject(new Error(errorMessage)); // Geolocation 未初始化或不可用
139
+ }
140
+ });
141
+ }
142
+
143
+ async openLocation(options) {
144
+ return new Promise((resolve, reject) => {
145
+ if (window.top.geolocation && typeof window.top.geolocation.openLocation === 'function') {
146
+ console.log("1")
147
+ window.top.geolocation.openLocation(
148
+ (err) => {
149
+ console.error('YunTu: Failed to open location', err);
150
+ err.message=err.msg
151
+ reject(err); // 打开地图失败
152
+ },
153
+ JSON.stringify( {
154
+ latitude: options.latitude,
155
+ longitude: options.longitude,
156
+ type: options.type || 'amap' // 默认为高德地图
157
+ })
158
+ );
159
+ } else {
160
+ const errorMessage = 'Geolocation is not initialized or not available';
161
+ console.error('YunTu:', errorMessage);
162
+ reject(new Error(errorMessage)); // Geolocation 未初始化或不可用
163
+ }
164
+ });
165
+ }
166
+
167
+ async initKey(options) {
168
+ return new Promise((resolve, reject) => {
169
+ if (window.top.geolocation && typeof window.top.geolocation.initKey === 'function') {
170
+ window.top.geolocation.initKey(function (msg) {
171
+ console.log('YunTu: initKey success', msg);
172
+ resolve(msg); //
173
+ }, JSON.stringify(options) );
174
+ } else {
175
+ const errorMessage = 'Geolocation is not initialized or not available';
176
+ console.error('YunTu:', errorMessage);
177
+ reject(new Error(errorMessage)); // Geolocation 未初始化或不可用
178
+ }
179
+ });
180
+ }
181
+
182
+ async scanCode(options) {
183
+ return new Promise((resolve, reject) => {
184
+ if (window.top.scanCode && typeof window.top.scanCode.startScan === 'function') {
185
+ window.top.scanCode.startScan(
186
+ (res) => {
187
+ console.log('YunTu: Scan code success', res);
188
+ resolve(res); // 成功获取扫码结果
189
+ },
190
+ (err) => {
191
+ console.error('YunTu: Failed to scan code', err);
192
+ reject(err); // 扫码失败
193
+ },
194
+ options // 传递给扫码方法的选项
195
+ );
196
+ } else {
197
+ const errorMessage = 'ScanCode is not initialized or not available';
198
+ console.error('YunTu:', errorMessage);
199
+ reject(new Error(errorMessage)); // ScanCode 未初始化或不可用
200
+ }
201
+ });
202
+ }
203
+
204
+ async localAuthPlugin(options) {
205
+ return new Promise((resolve, reject) => {
206
+ if (window.top.localAuthPlugin && typeof window.top.localAuthPlugin.auth === 'function') {
207
+ window.top.localAuthPlugin.auth(
208
+ (res) => {
209
+ console.log('YunTu: local auth success', res);
210
+ resolve(res); // 成功获取认证结果
211
+ },
212
+ (err) => {
213
+ console.error('YunTu: Failed to local auth', err);
214
+ reject(err); // 认证失败
215
+ },
216
+ options // 传递给认证方法的选项
217
+ );
218
+ } else {
219
+ const errorMessage = 'local auth is not initialized or not available';
220
+ console.error('YunTu:', errorMessage);
221
+ reject(new Error(errorMessage)); // ScanCode 未初始化或不可用
222
+ }
223
+ });
224
+ }
225
+
226
+ async getAvailableBiometrics(options) {
227
+ return new Promise((resolve, reject) => {
228
+ if (window.top.localAuthPlugin && typeof window.top.localAuthPlugin.getAvailableBiometrics === 'function') {
229
+ window.top.localAuthPlugin.getAvailableBiometrics(
230
+ (res) => {
231
+ console.log('YunTu: Get Available Biometrics', res);
232
+ resolve(res); // 成功获取生物认证列表
233
+ },
234
+ (err) => {
235
+ console.error('YunTu: Failed Get Available Biometrics', err);
236
+ reject(err); // 获取生物认证列表失败
237
+ },
238
+ options // 传递给认证方法的选项
239
+ );
240
+ } else {
241
+ const errorMessage = 'local auth is not initialized or not available';
242
+ console.error('YunTu:', errorMessage);
243
+ reject(new Error(errorMessage));
244
+ }
245
+ });
246
+ }
247
+
248
+
249
+ async takePhoto(options) {
250
+ console.log('YunTu: Taking photo with options', options);
251
+
252
+ return new Promise((resolve, reject) => {
253
+ if (window.top.imagePicker) {
254
+ const sourceType = options && options.sourceType === 'gallery' ? 'gallery' : 'camera';
255
+ const pickerFunction = window.top.imagePicker[sourceType];
256
+ options.type= options.outputType.includes("data")?1:0;
257
+ if (typeof pickerFunction === 'function') {
258
+ pickerFunction(
259
+ (res) => {
260
+ console.log('YunTu: Photo selection success', res);
261
+ resolve(res); // 成功获取照片
262
+ },
263
+ (err) => {
264
+ console.error('YunTu: Failed to select photo', err);
265
+ reject(err); // 选择照片失败
266
+ },
267
+ options // 传递给选取照片方法的选项(如果有需要的话)
268
+ );
269
+ } else {
270
+ const errorMessage = `${sourceType} function is not available in ImagePicker`;
271
+ console.error('YunTu:', errorMessage);
272
+ reject(new Error(errorMessage)); // 相应的接口未初始化或不可用
273
+ }
274
+ } else {
275
+ const errorMessage = 'ImagePicker is not initialized or not available';
276
+ console.error('YunTu:', errorMessage);
277
+ reject(new Error(errorMessage)); // ImagePicker 未初始化或不可用
278
+ }
279
+ });
280
+ };
281
+
282
+ async filePreview(options) {
283
+ console.log('YunTu: File preview with options', options);
284
+
285
+ return new Promise((resolve, reject) => {
286
+ if (window.top.filePreview && typeof window.top.filePreview.openFile === 'function') {
287
+ window.top.filePreview.openFile(
288
+ (res) => {
289
+ console.log('YunTu: File preview success', res);
290
+ resolve(res); // Successfully previewed the file
291
+ },
292
+ (err) => {
293
+ console.error('YunTu: File preview failed', err);
294
+ reject(err); // Failed to preview the file
295
+ },
296
+ JSON.stringify(options) // Pass the options as a string
297
+ );
298
+ } else {
299
+ const errorMessage = 'FilePreview is not initialized or not available';
300
+ console.error('YunTu:', errorMessage);
301
+ reject(new Error(errorMessage)); // FilePreview interface is not available
302
+ }
303
+ });
304
+ }
305
+
306
+
307
+ }
308
+
309
+ export default YunTuAdapter;