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

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.
@@ -3,7 +3,8 @@
3
3
 
4
4
  import ajax from "@lambo-design-mobile/shared/utils/ajax";
5
5
  import config from "@lambo-design-mobile/shared/config/config";
6
- import coordtransform from "coordtransform";
6
+ // import coordtransform from "coordtransform";
7
+ import { getAddress, initBdMap, getAddressByType, getAxisByType } from './BdMapUtils';
7
8
 
8
9
  class WechatAdapter {
9
10
  constructor(_options) {
@@ -87,16 +88,14 @@ class WechatAdapter {
87
88
  if (!unifiedLocation.latitude || !unifiedLocation.longitude) {
88
89
  return reject(new Error('Invalid location data'));
89
90
  }
90
-
91
- const myGeo = new BMap.Geocoder({extensions_town: false});
92
- // 根据坐标得到地址描述
93
- myGeo.getLocation(new BMap.Point(unifiedLocation.longitude, unifiedLocation.latitude), function(result){
94
- if (result){
95
- unifiedLocation.city = result.addressComponents.city
96
- unifiedLocation.province = result.addressComponents.province
97
- unifiedLocation.address = result.address
98
- }
99
- });
91
+ // options.needAddress 是否需要地址信息
92
+ // if(options.needAddress && !unifiedLocation.address) {
93
+ // // 根据坐标得到地址描述
94
+ // const res = getAddress(unifiedLocation.longitude, unifiedLocation.latitude)
95
+ // unifiedLocation.city = res.addressComponents.city
96
+ // unifiedLocation.province = res.addressComponents.province
97
+ // unifiedLocation.address = res.address
98
+ // }
100
99
 
101
100
  resolve(unifiedLocation);
102
101
  },
@@ -108,31 +107,67 @@ class WechatAdapter {
108
107
  });
109
108
  };
110
109
 
111
- if (locationType === 'bd09') {
112
- // 先获取 gcj02 坐标
113
- fetchLocation('gcj02').then(unifiedLocation => {
114
- // gcj02 坐标转换为 bd09
115
- const [bd09Lng, bd09Lat] = coordtransform.gcj02tobd09(unifiedLocation.longitude, unifiedLocation.latitude);
116
- unifiedLocation.longitude = bd09Lng;
117
- unifiedLocation.latitude = bd09Lat;
110
+ // 先判断是否需要地理位置 如不需要 直接返回坐标
111
+ if(options.needAddress) {
112
+ fetchLocation('gcj02'.then(unifiedLocation => {
113
+ const result = getAddressByType(unifiedLocation, locationType)
114
+ if(!result) {
115
+ return reject(new Error('Invalid location data'));
116
+ }
117
+ resolve(unifiedLocation);
118
+ }))
119
+ .catch(err => { reject(err) })
120
+ }
121
+ else {
122
+ resolve(fetchLocation(locationType))
123
+ }
118
124
 
119
- const myGeo = new BMap.Geocoder({extensions_town: false});
120
- // 根据坐标得到地址描述
121
- myGeo.getLocation(new BMap.Point(unifiedLocation.longitude, unifiedLocation.latitude), function(result){
122
- if (result){
123
- unifiedLocation.city = result.addressComponents.city
124
- unifiedLocation.province = result.addressComponents.province
125
- unifiedLocation.address = result.address
126
- }
127
- });
125
+ // // 先取gcj02 再根据type转成不同坐标系
126
+ // fetchLocation('gcj02').then(unifiedLocation => {
127
+ // // options.needAddress 是否需要地址信息
128
+ // if(options.needAddress && !unifiedLocation.address) {
129
+ // const result = getAddressByType(unifiedLocation, locationType)
130
+ // if(!result) {
131
+ // return reject(new Error('Invalid location data'));
132
+ // }
133
+ // resolve(unifiedLocation);
134
+ // }
135
+ // else {
136
+ // const result = getAxisByType(unifiedLocation, locationType)
137
+ // if(!result) {
138
+ // return reject(new Error('Invalid location data'));
139
+ // }
140
+ // resolve(unifiedLocation);
141
+ // }
142
+ // }).catch(err => {
143
+ // reject(err);
144
+ // });
128
145
 
129
- resolve(unifiedLocation);
130
- }).catch(err => {
131
- reject(err);
132
- });
133
- } else {
134
- fetchLocation(locationType).then(resolve).catch(reject);
135
- }
146
+
147
+ // if (locationType === 'bd09') {
148
+ // // 先获取 gcj02 坐标
149
+ // fetchLocation('gcj02').then(unifiedLocation => {
150
+ // // gcj02 坐标转换为 bd09
151
+ // const [bd09Lng, bd09Lat] = coordtransform.gcj02tobd09(unifiedLocation.longitude, unifiedLocation.latitude);
152
+ // unifiedLocation.longitude = bd09Lng;
153
+ // unifiedLocation.latitude = bd09Lat;
154
+ // if (!unifiedLocation.latitude || !unifiedLocation.longitude) {
155
+ // return reject(new Error('Invalid location data'));
156
+ // }
157
+ // // options.needAddress 是否需要地址信息
158
+ // if(options.needAddress && !unifiedLocation.address) {
159
+ // // 根据坐标得到地址描述
160
+ // const res = getAddress(unifiedLocation.longitude, unifiedLocation.latitude)
161
+ // unifiedLocation.city = res.addressComponents.city
162
+ // unifiedLocation.province = res.addressComponents.province
163
+ // unifiedLocation.address = res.address
164
+ // }
165
+
166
+ // resolve(unifiedLocation);
167
+ // }).catch(err => {
168
+ // reject(err);
169
+ // });
170
+ // }
136
171
  });
137
172
  });
138
173
  }
@@ -1,163 +1,224 @@
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";
4
-
5
- class YunTuAdapter {
6
- constructor() {
7
- this.isInitialized = false; // 标志插件是否已初始化
8
- function loadScript(src) {
9
- return new Promise((resolve, reject) => {
10
- const script = document.createElement('script');
11
- script.src = src;
12
- script.onload = () => resolve(script);
13
- script.onerror = () => reject(new Error('Failed to load script'));
14
- document.head.appendChild(script);
15
- });
16
- }
17
- loadScript('/packages/js-bridge/yuntu.js')
18
- .then(script => {
19
- console.log('Script loaded');
20
- this.initializePlugin(); // 确保初始化完成
21
- })
22
- .catch(err => console.error(err));
23
- }
24
-
25
- async initializePlugin() {
26
- window.yuntu.config('["myPlugin","amapPlugin","tabBarPlugin","scanCodePlugin","filePreviewPlugin","imagePickerPlugin","appUpdatePlugin"]');
27
- this.isInitialized = true;
28
- console.log("this.isInitialized:",this.isInitialized);
29
- }
30
-
31
-
32
- async getPlatform() {
33
- return {
34
- platform: 'Yuntu'
35
- };
36
- }
37
-
38
- async getLocation(options) {
39
- if (!this.isInitialized) {
40
- await this.initializePlugin(); // 确保插件初始化
41
- }
42
-
43
- return new Promise((resolve, reject) => {
44
-
45
- if (window.geolocation && typeof window.geolocation.getCurrentPosition === 'function') {
46
- window.geolocation.getCurrentPosition(
47
- (res) => {
48
- console.log('YunTu: Location obtained', JSON.stringify(res));
49
- resolve(res); // 成功获取位置
50
- },
51
- (err) => {
52
- console.error('YunTu: Failed to obtain location', err);
53
- reject(err); // 获取位置失败
54
- },
55
- options
56
- );
57
- } else {
58
- const errorMessage = 'Geolocation is not initialized or not available';
59
- console.error('YunTu:', errorMessage);
60
- reject(new Error(errorMessage)); // Geolocation 未初始化或不可用
61
- }
62
- });
63
- }
64
-
65
- async openLocation(options) {
66
- if (!this.isInitialized) {
67
- await this.initializePlugin(); // 确保插件初始化
68
- }
69
-
70
- return new Promise((resolve, reject) => {
71
- if (window.geolocation && typeof window.geolocation.openLocation === 'function') {
72
- console.log("1")
73
- window.geolocation.openLocation(
74
- (res) => {
75
- console.log('YunTu: Open location success', res);
76
- resolve(res); // 成功打开地图
77
- },
78
- (err) => {
79
- console.error('YunTu: Failed to open location', err);
80
- reject(err); // 打开地图失败
81
- },
82
- {
83
- latitude: options.latitude,
84
- longitude: options.longitude,
85
- type: options.type || 'amap' // 默认为高德地图
86
- }
87
- );
88
- } else {
89
- const errorMessage = 'Geolocation is not initialized or not available';
90
- console.error('YunTu:', errorMessage);
91
- reject(new Error(errorMessage)); // Geolocation 未初始化或不可用
92
- }
93
- });
94
- }
95
-
96
- async scanCode(options) {
97
- if (!this.isInitialized) {
98
- await this.initializePlugin(); // 确保插件初始化
99
- }
100
-
101
- return new Promise((resolve, reject) => {
102
- if (window.scanCode && typeof window.scanCode.startScan === 'function') {
103
- window.scanCode.startScan(
104
- (res) => {
105
- console.log('YunTu: Scan code success', res);
106
- resolve(res); // 成功获取扫码结果
107
- },
108
- (err) => {
109
- console.error('YunTu: Failed to scan code', err);
110
- reject(err); // 扫码失败
111
- },
112
- options // 传递给扫码方法的选项
113
- );
114
- } else {
115
- const errorMessage = 'ScanCode is not initialized or not available';
116
- console.error('YunTu:', errorMessage);
117
- reject(new Error(errorMessage)); // ScanCode 未初始化或不可用
118
- }
119
- });
120
- }
121
-
122
-
123
- async takePhoto(options) {
124
- console.log('YunTu: Taking photo with options', options);
125
-
126
- if (!this.isInitialized) {
127
- await this.initializePlugin(); // 确保插件初始化
128
- }
129
-
130
- return new Promise((resolve, reject) => {
131
- if (window.imagePicker) {
132
- const sourceType = options && options.sourceType === 'gallery' ? 'gallery' : 'camera';
133
- const pickerFunction = window.imagePicker[sourceType];
134
-
135
- if (typeof pickerFunction === 'function') {
136
- pickerFunction(
137
- (res) => {
138
- console.log('YunTu: Photo selection success', res);
139
- resolve(JSON.parse(res)); // 成功获取照片
140
- },
141
- (err) => {
142
- console.error('YunTu: Failed to select photo', err);
143
- reject(JSON.parse(err)); // 选择照片失败
144
- },
145
- options // 传递给选取照片方法的选项(如果有需要的话)
146
- );
147
- } else {
148
- const errorMessage = `${sourceType} function is not available in ImagePicker`;
149
- console.error('YunTu:', errorMessage);
150
- reject(new Error(errorMessage)); // 相应的接口未初始化或不可用
151
- }
152
- } else {
153
- const errorMessage = 'ImagePicker is not initialized or not available';
154
- console.error('YunTu:', errorMessage);
155
- reject(new Error(errorMessage)); // ImagePicker 未初始化或不可用
156
- }
157
- });
158
- }
159
-
160
-
161
- }
162
-
163
- export default YunTuAdapter;
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;
@@ -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
- }