@lambo-design-mobile/lambo-js-bridge 1.0.0-beta.1

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.
@@ -0,0 +1,21 @@
1
+ class DingTalkAdapter {
2
+ async getLocation(options) {
3
+ // 实现钉钉获取位置逻辑
4
+ console.log('DingTalk: Getting location with options', options);
5
+ // 这里应替换为钉钉API的实际调用
6
+ }
7
+
8
+ async scanCode(options) {
9
+ // 实现钉钉扫码逻辑
10
+ console.log('DingTalk: Scanning code with options', options);
11
+ // 这里应替换为钉钉API的实际调用
12
+ }
13
+
14
+ async takePhoto(options) {
15
+ // 实现钉钉拍照或选择图片逻辑
16
+ console.log('DingTalk: Taking photo with options', options);
17
+ // 这里应替换为钉钉API的实际调用
18
+ }
19
+ }
20
+
21
+ export default DingTalkAdapter;
@@ -0,0 +1,73 @@
1
+ import WechatAdapter from './WechatAdapter';
2
+ import DingTalkAdapter from './DingTalkAdapter';
3
+ import CordovaAdapter from './CordovaAdapter';
4
+ import YunTuAdapter from './YunTuAdapter';
5
+ import WeComAdapter from './WeComAdapter';
6
+ import BrowserAdapter from './BrowserAdapter';
7
+
8
+ class LamboJsBridge {
9
+ constructor(param) {
10
+ // param.isBdNiJieXi 需要百度地图逆地址解析
11
+ if(param.isBdNiJieXi) {
12
+ const addScript = function (src, cb) {
13
+ const script = document.createElement('script')
14
+ script.src = src
15
+ document.getElementsByTagName('head')[0].appendChild(script)
16
+ script.onload = function () {
17
+ if (!!cb) {
18
+ cb()
19
+ }
20
+ }
21
+ }
22
+ addScript('https://api.map.baidu.com/api?v=1.0&&type=webgl&ak=AWZBggDSk8Zsv2STX7S2SBYO6wsH7BVT')
23
+ addScript(
24
+ 'https://api.map.baidu.com/getscript?type=webgl&v=1.0&ak=AWZBggDSk8Zsv2STX7S2SBYO6wsH7BVT',
25
+ )
26
+ }
27
+ this._detectPlatform(param);
28
+ }
29
+
30
+ _detectPlatform(param) {
31
+ const userAgent = navigator.userAgent.toLowerCase();
32
+ if (/wxwork/.test(userAgent)) {
33
+ this.platform = new WeComAdapter(param);
34
+ } else if (/micromessenger/.test(userAgent)) {
35
+ this.platform = new WechatAdapter(param);
36
+ }else if (/dingtalk/.test(userAgent)) {
37
+ this.platform = new DingTalkAdapter(param);
38
+ } else if (/cordova/.test(userAgent)) {
39
+ this.platform = new CordovaAdapter(param);
40
+ } else if (/yuntu/.test(userAgent)) {
41
+ this.platform = new YunTuAdapter(param);
42
+ } else {
43
+ this.platform = new BrowserAdapter();
44
+ }
45
+
46
+ }
47
+
48
+ //通用接口:获取初始化信息
49
+ async getPlatform(options = {}) {
50
+ return this.platform.getPlatform(options);
51
+ }
52
+
53
+ // 通用接口:获取位置信息
54
+ async getLocation(options = {}) {
55
+ // options = {type:'wgs84'};
56
+ return this.platform.getLocation(options);
57
+ }
58
+
59
+ // 通用接口:扫码
60
+ async scanCode(options = {}) {
61
+ return this.platform.scanCode(options);
62
+ }
63
+
64
+ // 通用接口:拍照或选择图片
65
+ async takePhoto(options = {}) {
66
+ return this.platform.takePhoto(options);
67
+ }
68
+
69
+ async openLocation(options = {}){
70
+ return this.platform.openLocation(options);
71
+ }
72
+ }
73
+ export default LamboJsBridge;
@@ -0,0 +1,301 @@
1
+ // import * as ww from '@wecom/jssdk'
2
+ import ajax from "@lambo-design-mobile/shared/utils/ajax";
3
+ import config from "@lambo-design-mobile/shared/config/config";
4
+
5
+ class WeComAdapter {
6
+ constructor(_options) {
7
+ this.corpId = _options.weComId; // 保存 appId 以便在 getInitInfo 中使用
8
+ // if(!window.wx){
9
+ // // const script = document.createElement('script')
10
+ // // script.src = "//wwcdn.weixin.qq.com/node/open/js/wecom-jssdk-1.3.1.js"
11
+ // // document.head.appendChild(script)
12
+ // const script = document.createElement('script');
13
+ // script.src = "//res.wx.qq.com/open/js/jweixin-1.2.0.js";
14
+ // document.head.appendChild(script);
15
+ // const script1 = document.createElement('script');
16
+ // script1.src = "https://open.work.weixin.qq.com/wwopen/js/jwxwork-1.0.0.js";
17
+ // document.head.appendChild(script1);
18
+ // }
19
+ // this.init( _options)
20
+ function loadScript(src) {
21
+ return new Promise((resolve, reject) => {
22
+ const script = document.createElement('script');
23
+ script.src = src;
24
+ script.onload = () => resolve(script);
25
+ script.onerror = () => reject(new Error('Failed to load script'));
26
+ document.head.appendChild(script);
27
+ });
28
+ }
29
+ loadScript('//res.wx.qq.com/open/js/jweixin-1.2.0.js')
30
+ .then(script => {
31
+ console.log('Script loaded');
32
+ this.init(_options);
33
+ })
34
+ .catch(err => console.error(err));
35
+ }
36
+
37
+
38
+
39
+ async init(options) {
40
+ const currentUrl = encodeURIComponent(window.location.href.split('#')[0]);
41
+ ajax.request({
42
+ url: config.upmsServerContext + '/manage/ibpWxCpBaseinfo/gtJsTicket?corpId='+options.weComId+"&url="+currentUrl,
43
+ method: 'get'
44
+ }).then((resp) => {
45
+ if(resp.data.code===1){
46
+ const {data} = resp.data
47
+ wx.config({
48
+ beta:true,
49
+ // debug: true,
50
+ appId: data.corpId, // 必填,企业微信的corpid,必须与当前登录的企业一致
51
+ // agentid: '1000065', // 必填,企业微信的应用id (e.g. 1000247)
52
+ timestamp: data.timestamp, // 必填,生成签名的时间戳
53
+ nonceStr: data.noncestr, // 必填,生成签名的随机串
54
+ signature: data.signature,// 必填,签名,见附录-JS-SDK使用权限签名算法
55
+ jsApiList: ['getLocation','scanQRCode','chooseImage','openLocation','getLocalImgData'], //必填,传入需要使用的接口名称
56
+ success: function(res) {
57
+ console.log("ready...")
58
+ },
59
+ fail: function(res) {
60
+ if(res.errMsg.indexOf('function not exist') > -1){
61
+ alert('版本过低请升级')
62
+ }
63
+ }
64
+ });
65
+
66
+ wx.error(function (res) {
67
+ console.error(res)
68
+ });
69
+ wx.ready(function(){
70
+ console.log("ready.....")
71
+ // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
72
+ });
73
+ wx.checkJsApi({
74
+ jsApiList: ['getLocation','scanQRCode','chooseImage','openLocation'], // 需要检测的JS接口列表
75
+ success: function(res) {
76
+ // 以键值对的形式返回,可用的api值true,不可用为false
77
+ // 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
78
+ }
79
+ });
80
+
81
+
82
+ }
83
+
84
+ }).catch((err) => {
85
+ console.error(err)
86
+ })
87
+ }
88
+
89
+ async getLocation(options) {
90
+ return new Promise((resolve, reject) => {
91
+ wx.ready(() => {
92
+ // 检查 options.type,如果为空或者不含有 'wgs84' 或 'bd09',则设置为 'gcj02'
93
+ let locationType = options.type;
94
+ if (!options.type || (options.type !== 'wgs84' && options.type !== 'bd09')) {
95
+ locationType = 'gcj02';
96
+ }
97
+
98
+ const fetchLocation = (type) => {
99
+ return new Promise((resolve, reject) => {
100
+ wx.getLocation({
101
+ ...options,
102
+ type: type,
103
+ success: location => {
104
+ const unifiedLocation = {
105
+ latitude: location.latitude || location.lat || null, // 统一纬度属性
106
+ longitude: location.longitude || location.lng || null, // 统一经度属性
107
+ accuracy: location.accuracy || null // 精确度,如果有的话
108
+ };
109
+
110
+ if (!unifiedLocation.latitude || !unifiedLocation.longitude) {
111
+ return reject(new Error('Invalid location data'));
112
+ }
113
+
114
+ resolve(unifiedLocation);
115
+ },
116
+ fail: err => {
117
+ console.error("getLocation failed:", err);
118
+ reject(err);
119
+ }
120
+ });
121
+ });
122
+ };
123
+
124
+ if (locationType === 'bd09') {
125
+ // 先获取 gcj02 坐标
126
+ fetchLocation('gcj02').then(unifiedLocation => {
127
+ // 将 gcj02 坐标转换为 bd09
128
+ const [bd09Lng, bd09Lat] = coordtransform.gcj02tobd09(unifiedLocation.longitude, unifiedLocation.latitude);
129
+ unifiedLocation.longitude = bd09Lng;
130
+ unifiedLocation.latitude = bd09Lat;
131
+ resolve(unifiedLocation);
132
+ }).catch(err => {
133
+ reject(err);
134
+ });
135
+ } else {
136
+ fetchLocation(locationType).then(resolve).catch(reject);
137
+ }
138
+ });
139
+ });
140
+ }
141
+
142
+ async openLocation(options) {
143
+ return new Promise((resolve, reject) => {
144
+ wx.ready(() => {
145
+ wx.openLocation({
146
+ latitude: options.latitude,
147
+ longitude: options.longitude,
148
+ name: options.name || '',
149
+ address: options.address || '',
150
+ scale: options.scale || 1,
151
+ infoUrl: options.infoUrl || '',
152
+ success: () => {
153
+ console.log("openLocation success");
154
+ resolve();
155
+ },
156
+ fail: err => {
157
+ console.error("openLocation failed:", err);
158
+ reject(err);
159
+ }
160
+ });
161
+ });
162
+ });
163
+ }
164
+
165
+ async scanCode(options) {
166
+ return new Promise((resolve, reject) => {
167
+ wx.ready(() => {
168
+ wx.scanQRCode({
169
+ ...options,
170
+ needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果
171
+ success: (res) => {
172
+ console.log("scanQRCode success:", res);
173
+ resolve(res); // 当needResult 为 1 时,扫码返回的结果
174
+ },
175
+ fail: (err) => {
176
+ console.error("scanQRCode failed:", err);
177
+ reject(err);
178
+ }
179
+ });
180
+ });
181
+ });
182
+ }
183
+
184
+ async takePhoto(options) {
185
+ return new Promise((resolve, reject) => {
186
+ wx.ready(() => {
187
+ wx.chooseImage({
188
+ ...options,
189
+ // count: 1, // 默认9,设置为1表示只选择一张图片
190
+ // sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
191
+ // sourceType: ['camera','album'], // 可以指定来源是相册还是相机,默认二者都有
192
+ success: async (res) => {
193
+ const imageInfo = res.localIds[0]; // 返回选定照片的第一个本地ID
194
+ const results = {};
195
+
196
+ if (!options.outputType) {
197
+ // outputType为空,返回所有结果
198
+ results.imageInfo = imageInfo;
199
+ wx.getLocalImgData({
200
+ localId: imageInfo,
201
+ success: (dataRes) => {
202
+ results.imageData = dataRes.localData;
203
+ resolve(results);
204
+ },
205
+ fail: (err) => {
206
+ console.error("getLocalImgData failed:", err);
207
+ reject(err);
208
+ }
209
+ });
210
+ } else {
211
+ if (options.outputType.includes('info')) {
212
+ results.imageInfo = imageInfo;
213
+ }
214
+ if (options.outputType.includes('data')) {
215
+ await new Promise((resolve, reject) => {
216
+ wx.getLocalImgData({
217
+ localId: imageInfo, // 这里使用返回的第一个localId
218
+ success: (dataRes) => {
219
+ results.imageData = dataRes.localData;
220
+ resolve();
221
+ },
222
+ fail: (err) => {
223
+ console.error("getLocalImgData failed:", err);
224
+ reject(err);
225
+ }
226
+ });
227
+ });
228
+ }
229
+ if (options.outputType.includes('oss')) {
230
+ await new Promise((resolve, reject) => {
231
+ wx.getLocalImgData({
232
+ localId: imageInfo, // 获取base64数据
233
+ success: (dataRes) => {
234
+ const file = this.dataURLtoFile(dataRes.localData, 'image.png');
235
+ this.uploadToOSS(file, options).then((ossResult) => {
236
+ results.imageOss = ossResult;
237
+ resolve();
238
+ }).catch((err) => {
239
+ console.error("uploadToOSS failed:", err);
240
+ reject(err);
241
+ });
242
+ },
243
+ fail: (err) => {
244
+ console.error("getLocalImgData failed:", err);
245
+ reject(err);
246
+ }
247
+ });
248
+ });
249
+ }
250
+ resolve(results);
251
+ }
252
+ },
253
+ fail: (err) => {
254
+ console.error("chooseImage failed:", err);
255
+ reject(err);
256
+ }
257
+ });
258
+ });
259
+ });
260
+ }
261
+
262
+ dataURLtoFile(dataurl, filename) {
263
+ let arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
264
+ bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
265
+ while (n--) {
266
+ u8arr[n] = bstr.charCodeAt(n);
267
+ }
268
+ return new File([u8arr], filename, {type: mime});
269
+ }
270
+
271
+ uploadToOSS(file, options) {
272
+ return new Promise((resolve, reject) => {
273
+ let formData = new FormData();
274
+ formData.append('file', file);
275
+ ajax.post(options.ossServerContext + options.ossImgPutUrl, formData, { payload : true }).then(response => {
276
+ if (response.data.code === 1) {
277
+ const result = response.data.data;
278
+ if (result.length > 0) {
279
+ resolve(result[0]);
280
+ } else {
281
+ reject(new Error('No fileId returned'));
282
+ }
283
+ } else {
284
+ reject(new Error('Upload failed'));
285
+ }
286
+ }).catch(error =>{
287
+ reject(error);
288
+ });
289
+ });
290
+ }
291
+
292
+ async getPlatform(options = {}) {
293
+ // 获取初始化信息
294
+ return {
295
+ appId: this.corpId,
296
+ platform: 'WeCom'
297
+ };
298
+ }
299
+ }
300
+
301
+ export default WeComAdapter;