@lambo-design-mobile/lambo-js-bridge 1.0.0-beta.4 → 1.0.0-beta.41

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,67 +1,124 @@
1
- import ajax from "@lambo-design-mobile/shared/utils/ajax";
2
- import config from "@lambo-design-mobile/shared/config/config";
3
- import coordtransform from "coordtransform";
1
+ import './yuntu';
2
+
3
+ export const PRINTER_PLUGIN = "printerPlugin"
4
4
 
5
5
  class YunTuAdapter {
6
- constructor() {
7
- this.isInitialized = false; // 标志插件是否已初始化
8
-
9
- // yuntu.js 的代码直接放在这里
10
- window.yuntu = {
11
- config: function (options) {
12
- window.flutter_inappwebview.callHandler(
13
- "yuntu",
14
- "init",
15
- options
16
- )
17
- },
18
- exec: function(plugin, action, successCallback, errorCallback, options) {
19
- window.flutter_inappwebview.callHandler(
20
- plugin,
21
- action,
22
- options
23
- ).then((result) => {
24
- successCallback(result);
25
- }).catch((error) => {
26
- errorCallback(error);
27
- });
28
- }
29
- };
6
+ constructor(options) {
7
+ // 标志插件是否已初始化
8
+ this.isInitialized = false;
9
+ this.audioUrl = null; // 录音文件的 URL
10
+ this.fileName = ''; // 录音文件名称
11
+ this.fileType = ''; // 录音文件类型
12
+ this.initializePlugin(options.pluginConfig);
13
+ }
30
14
 
31
- // 等待一个全局函数或变量加载完成
32
- function waitForGlobalVariableOrFunction(globalName, callHandler, callback) {
33
- // 设置轮询的时间间隔(毫秒)
34
- var interval = 100; // 例如,每100毫秒检查一次
35
- var t = 0;
36
- // 定义一个定时器函数
37
- var timer = setInterval(function () {
38
- console.log(t++);
39
- if (window[globalName][callHandler] !== undefined) {
40
- // 全局变量或函数已加载
41
- clearInterval(timer); // 停止定时器
42
- callback(); // 执行回调函数
43
- }
44
- }, interval);
15
+ async initializePlugin(pluginConfig) {
16
+ try {
17
+ const configToUse = Array.isArray(pluginConfig) && pluginConfig.length > 0
18
+ ? JSON.stringify(pluginConfig)
19
+ : '["localAuthPlugin","amapPlugin","tabBarPlugin","scanCodePlugin","filePreviewPlugin","imagePickerPlugin","' + PRINTER_PLUGIN +'"]';
20
+ // eslint-disable-next-line no-undef
21
+ await yuntuConfig(configToUse); // 确保等待 yuntuConfig 执行完成
22
+ this.isInitialized = true;
23
+ this.plugins = JSON.parse(configToUse);
24
+ console.log("Initialized plugins:", this.plugins);
25
+ return this.plugins;
26
+ } catch (error) {
27
+ console.error("Plugin initialization failed:", error);
28
+ this.isInitialized = false;
29
+ return "Plugin initialization failed:" + error;
45
30
  }
31
+ }
46
32
 
47
- function yuntuConfig(options) {
48
- waitForGlobalVariableOrFunction('flutter_inappwebview', 'callHandler', function () {
49
- // 在这里执行你的代码,myFunction 已加载
50
- window.yuntu.config(options);
51
- });
52
- }
33
+ // 录音:开始录音(支持自动和手动模式)
34
+ async startRecording(options = { auto: false }) {
35
+ return new Promise((resolve, reject) => {
36
+ if (window.autoRecord && typeof window.autoRecord.startRecording === 'function') {
37
+ window.autoRecord.startRecording(
38
+ (result) => {
39
+ console.log("录音开始:", result);
40
+ resolve(result);
41
+ },
42
+ (error) => {
43
+ console.error("录音错误:", error);
44
+ reject(error);
45
+ },
46
+ JSON.stringify(options) // 传入参数:{auto: false} 表示手动录音,{auto: true} 表示自动录音
47
+ );
48
+ } else {
49
+ reject(new Error("autoRecord 接口不可用"));
50
+ }
51
+ });
52
+ }
53
53
 
54
- // 直接调用初始化函数
55
- this.initializePlugin();
54
+ // 录音:结束录音,并返回录音结果
55
+ async stopRecording() {
56
+ return new Promise((resolve, reject) => {
57
+ if (window.autoRecord && typeof window.autoRecord.stopRecording === 'function') {
58
+ window.autoRecord.stopRecording(
59
+ (result) => {
60
+ console.log("录音停止,返回结果:", result);
61
+ // 如果返回数据为字符串,则尝试解析为对象
62
+ try {
63
+ if (typeof result === "string") {
64
+ result = JSON.parse(result);
65
+ }
66
+ } catch (e) {
67
+ console.error("JSON.parse error:", e);
68
+ }
69
+ resolve(result);
70
+ },
71
+ (error) => {
72
+ console.error("录音结束错误:", error);
73
+ // 同样尝试解析错误信息
74
+ try {
75
+ if (typeof error === "string") {
76
+ error = JSON.parse(error);
77
+ }
78
+ } catch (e) {
79
+ console.error("JSON.parse error:", e);
80
+ }
81
+ reject(error);
82
+ }
83
+ );
84
+ } else {
85
+ reject(new Error("autoRecord 接口不可用"));
86
+ }
87
+ });
56
88
  }
57
89
 
58
- async initializePlugin() {
59
- window.yuntu.config('["myPlugin","amapPlugin","tabBarPlugin","scanCodePlugin","filePreviewPlugin","imagePickerPlugin","appUpdatePlugin"]');
60
- this.isInitialized = true;
61
- console.log("this.isInitialized:", this.isInitialized);
90
+ // 新增下载文件接口,options 作为参数传入
91
+ async downloadFile(options) {
92
+ return new Promise((resolve, reject) => {
93
+ if (window.fileDownload && typeof window.fileDownload.downloadFile === 'function') {
94
+ window.fileDownload.downloadFile(
95
+ // 成功回调
96
+ (result) => {
97
+ console.log("文件下载成功:", result);
98
+ try {
99
+ if (typeof result === "string") {
100
+ result = JSON.parse(result);
101
+ }
102
+ } catch (e) {
103
+ console.error("JSON.parse error:", e);
104
+ }
105
+ resolve(result);
106
+ },
107
+ // 失败回调
108
+ (error) => {
109
+ console.error("文件下载失败:", error);
110
+ reject(error);
111
+ },
112
+ // 直接将传入的 options 对象转为字符串
113
+ JSON.stringify(options)
114
+ );
115
+ } else {
116
+ reject(new Error("downloadFile 接口不可用"));
117
+ }
118
+ });
62
119
  }
63
120
 
64
- // 其余代码保持不变
121
+
65
122
  async getPlatform() {
66
123
  return {
67
124
  platform: 'Yuntu'
@@ -69,14 +126,10 @@ class YunTuAdapter {
69
126
  }
70
127
 
71
128
  async getLocation(options) {
72
- if (!this.isInitialized) {
73
- await this.initializePlugin(); // 确保插件初始化
74
- }
75
-
76
129
  return new Promise((resolve, reject) => {
77
130
 
78
- if (window.geolocation && typeof window.geolocation.getCurrentPosition === 'function') {
79
- window.geolocation.getCurrentPosition(
131
+ if (window.top.geolocation && typeof window.top.geolocation.getCurrentPosition === 'function') {
132
+ window.top.geolocation.getCurrentPosition(
80
133
  (res) => {
81
134
  console.log('YunTu: Location obtained', JSON.stringify(res));
82
135
  resolve(res); // 成功获取位置
@@ -96,27 +149,20 @@ class YunTuAdapter {
96
149
  }
97
150
 
98
151
  async openLocation(options) {
99
- if (!this.isInitialized) {
100
- await this.initializePlugin(); // 确保插件初始化
101
- }
102
-
103
152
  return new Promise((resolve, reject) => {
104
- if (window.geolocation && typeof window.geolocation.openLocation === 'function') {
153
+ if (window.top.geolocation && typeof window.top.geolocation.openLocation === 'function') {
105
154
  console.log("1")
106
- window.geolocation.openLocation(
107
- (res) => {
108
- console.log('YunTu: Open location success', res);
109
- resolve(res); // 成功打开地图
110
- },
155
+ window.top.geolocation.openLocation(
111
156
  (err) => {
112
157
  console.error('YunTu: Failed to open location', err);
113
- reject(err); // 打开地图失败
158
+ err.message=err.msg
159
+ reject(err); // 打开地图失败
114
160
  },
115
- {
161
+ JSON.stringify( {
116
162
  latitude: options.latitude,
117
163
  longitude: options.longitude,
118
164
  type: options.type || 'amap' // 默认为高德地图
119
- }
165
+ })
120
166
  );
121
167
  } else {
122
168
  const errorMessage = 'Geolocation is not initialized or not available';
@@ -126,14 +172,25 @@ class YunTuAdapter {
126
172
  });
127
173
  }
128
174
 
129
- async scanCode(options) {
130
- if (!this.isInitialized) {
131
- await this.initializePlugin(); // 确保插件初始化
132
- }
175
+ async initKey(options) {
176
+ return new Promise((resolve, reject) => {
177
+ if (window.top.geolocation && typeof window.top.geolocation.initKey === 'function') {
178
+ window.top.geolocation.initKey(function (msg) {
179
+ console.log('YunTu: initKey success', msg);
180
+ resolve(msg); //
181
+ }, JSON.stringify(options) );
182
+ } else {
183
+ const errorMessage = 'Geolocation is not initialized or not available';
184
+ console.error('YunTu:', errorMessage);
185
+ reject(new Error(errorMessage)); // Geolocation 未初始化或不可用
186
+ }
187
+ });
188
+ }
133
189
 
190
+ async scanCode(options) {
134
191
  return new Promise((resolve, reject) => {
135
- if (window.scanCode && typeof window.scanCode.startScan === 'function') {
136
- window.scanCode.startScan(
192
+ if (window.top.scanCode && typeof window.top.scanCode.startScan === 'function') {
193
+ window.top.scanCode.startScan(
137
194
  (res) => {
138
195
  console.log('YunTu: Scan code success', res);
139
196
  resolve(res); // 成功获取扫码结果
@@ -152,28 +209,68 @@ class YunTuAdapter {
152
209
  });
153
210
  }
154
211
 
212
+ async localAuthPlugin(options) {
213
+ return new Promise((resolve, reject) => {
214
+ if (window.top.localAuthPlugin && typeof window.top.localAuthPlugin.auth === 'function') {
215
+ window.top.localAuthPlugin.auth(
216
+ (res) => {
217
+ console.log('YunTu: local auth success', res);
218
+ resolve(res); // 成功获取认证结果
219
+ },
220
+ (err) => {
221
+ console.error('YunTu: Failed to local auth', err);
222
+ reject(err); // 认证失败
223
+ },
224
+ options // 传递给认证方法的选项
225
+ );
226
+ } else {
227
+ const errorMessage = 'local auth is not initialized or not available';
228
+ console.error('YunTu:', errorMessage);
229
+ reject(new Error(errorMessage)); // ScanCode 未初始化或不可用
230
+ }
231
+ });
232
+ }
233
+
234
+ async getAvailableBiometrics(options) {
235
+ return new Promise((resolve, reject) => {
236
+ if (window.top.localAuthPlugin && typeof window.top.localAuthPlugin.getAvailableBiometrics === 'function') {
237
+ window.top.localAuthPlugin.getAvailableBiometrics(
238
+ (res) => {
239
+ console.log('YunTu: Get Available Biometrics', res);
240
+ resolve(res); // 成功获取生物认证列表
241
+ },
242
+ (err) => {
243
+ console.error('YunTu: Failed Get Available Biometrics', err);
244
+ reject(err); // 获取生物认证列表失败
245
+ },
246
+ options // 传递给认证方法的选项
247
+ );
248
+ } else {
249
+ const errorMessage = 'local auth is not initialized or not available';
250
+ console.error('YunTu:', errorMessage);
251
+ reject(new Error(errorMessage));
252
+ }
253
+ });
254
+ }
255
+
155
256
 
156
257
  async takePhoto(options) {
157
258
  console.log('YunTu: Taking photo with options', options);
158
259
 
159
- if (!this.isInitialized) {
160
- await this.initializePlugin(); // 确保插件初始化
161
- }
162
-
163
260
  return new Promise((resolve, reject) => {
164
- if (window.imagePicker) {
261
+ if (window.top.imagePicker) {
165
262
  const sourceType = options && options.sourceType === 'gallery' ? 'gallery' : 'camera';
166
- const pickerFunction = window.imagePicker[sourceType];
167
-
263
+ const pickerFunction = window.top.imagePicker[sourceType];
264
+ options.type= options.outputType.includes("data")?1:0;
168
265
  if (typeof pickerFunction === 'function') {
169
266
  pickerFunction(
170
267
  (res) => {
171
268
  console.log('YunTu: Photo selection success', res);
172
- resolve(JSON.parse(res)); // 成功获取照片
269
+ resolve(res); // 成功获取照片
173
270
  },
174
271
  (err) => {
175
272
  console.error('YunTu: Failed to select photo', err);
176
- reject(JSON.parse(err)); // 选择照片失败
273
+ reject(err); // 选择照片失败
177
274
  },
178
275
  options // 传递给选取照片方法的选项(如果有需要的话)
179
276
  );
@@ -188,8 +285,31 @@ class YunTuAdapter {
188
285
  reject(new Error(errorMessage)); // ImagePicker 未初始化或不可用
189
286
  }
190
287
  });
191
- }
288
+ };
289
+
290
+ async filePreview(options) {
291
+ console.log('YunTu: File preview with options', options);
192
292
 
293
+ return new Promise((resolve, reject) => {
294
+ if (window.top.filePreview && typeof window.top.filePreview.openFile === 'function') {
295
+ window.top.filePreview.openFile(
296
+ (res) => {
297
+ console.log('YunTu: File preview success', res);
298
+ resolve(res); // Successfully previewed the file
299
+ },
300
+ (err) => {
301
+ console.error('YunTu: File preview failed', err);
302
+ reject(err); // Failed to preview the file
303
+ },
304
+ JSON.stringify(options) // Pass the options as a string
305
+ );
306
+ } else {
307
+ const errorMessage = 'FilePreview is not initialized or not available';
308
+ console.error('YunTu:', errorMessage);
309
+ reject(new Error(errorMessage)); // FilePreview interface is not available
310
+ }
311
+ });
312
+ }
193
313
 
194
314
  }
195
315
 
@@ -0,0 +1,55 @@
1
+ (function(global) {
2
+ global.yuntu = {
3
+ config: function (options) {
4
+ global.flutter_inappwebview.callHandler(
5
+ "yuntu",
6
+ "init",
7
+ options
8
+ );
9
+ },
10
+ exec: function(plugin, action, successCallback, errorCallback, options) {
11
+ global.flutter_inappwebview.callHandler(
12
+ plugin,
13
+ action,
14
+ options
15
+ ).then((result) => {
16
+ successCallback(result);
17
+ }).catch((error) => {
18
+ errorCallback(error);
19
+ });
20
+ }
21
+ };
22
+
23
+ // 等待一个全局函数或变量加载完成
24
+ function waitForGlobalVariableOrFunction(globalName, callHandler) {
25
+ return new Promise((resolve, reject) => {
26
+ var interval = 1000; // 每1000毫秒检查一次
27
+ var timeout = 10000; // 设定最大等待时间 10 秒
28
+ var elapsedTime = 0;
29
+
30
+ var timer = setInterval(function () {
31
+ elapsedTime += interval;
32
+ if (global[globalName] && global[globalName][callHandler] !== undefined) {
33
+ clearInterval(timer); // 停止定时器
34
+ resolve(); // 执行回调函数,表示成功
35
+ console.log("yuntu 插件加载成功");
36
+ } else if (elapsedTime >= timeout) {
37
+ clearInterval(timer); // 停止定时器
38
+ console.log("yuntu 插件加载失败");
39
+ reject(new Error(`Timeout: ${globalName}.${callHandler} not found within ${timeout}ms`));
40
+ }
41
+ }, interval);
42
+ });
43
+ }
44
+
45
+ global.yuntuConfig = async function(options) {
46
+ try {
47
+ await waitForGlobalVariableOrFunction('flutter_inappwebview', 'callHandler');
48
+ global.yuntu.config(options);
49
+ } catch (error) {
50
+ console.error("yuntuConfig 初始化失败:", error);
51
+ throw error; // 如果失败则抛出错误
52
+ }
53
+ };
54
+
55
+ })(window);
package/yuntu.js DELETED
@@ -1,42 +0,0 @@
1
- window.yuntu = {
2
- config: function (options) {
3
- window.flutter_inappwebview.callHandler(
4
- "yuntu",
5
- "init",
6
- options
7
- )
8
- },
9
- exec: function(plugin, action, successCallback, errorCallback, options) {
10
- window.flutter_inappwebview.callHandler(
11
- plugin,
12
- action,
13
- options
14
- ).then((result) => {
15
- successCallback(result);
16
- }).catch((error) => {
17
- errorCallback(error);
18
- });
19
- }
20
- }
21
- // 等待一个全局函数或变量加载完成
22
- function waitForGlobalVariableOrFunction(globalName, callHandler, callback) {
23
- // 设置轮询的时间间隔(毫秒)
24
- var interval = 100; // 例如,每100毫秒检查一次
25
- var t = 0
26
- // 定义一个定时器函数
27
- var timer = setInterval(function () {
28
- console.log(t++)
29
- if (window[globalName][callHandler] !== undefined) {
30
- // 全局变量或函数已加载
31
- clearInterval(timer); // 停止定时器
32
- callback(); // 执行回调函数
33
- }
34
- }, interval);
35
- }
36
-
37
- function yuntuConfig(options){
38
- waitForGlobalVariableOrFunction('flutter_inappwebview', 'callHandler', function () {
39
- // 在这里执行你的代码,myFunction 已加载
40
- window.yuntu.config(options);
41
- });
42
- }