@lambo-design-mobile/lambo-js-bridge 1.0.0-beta.30 → 1.0.0-beta.32
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/.versionrc +23 -23
- package/CHANGELOG.md +351 -342
- package/README.md +523 -523
- package/demo/index.vue +454 -454
- package/index.js +3 -3
- package/package.json +1 -1
- package/src/sdk/BrowserAdapter.js +63 -63
- package/src/sdk/CordovaAdapter.js +20 -20
- package/src/sdk/DingTalkAdapter.js +20 -20
- package/src/sdk/LPAPI.js +1119 -1117
- package/src/sdk/LamboJsBridge.js +102 -102
- package/src/sdk/{MobileIMAdaoter.js → MobileIMAdapter.js} +42 -9
- package/src/sdk/WeComAdapter.js +367 -367
- package/src/sdk/YunTuAdapter.js +316 -316
- package/src/sdk/yuntu.js +55 -55
package/src/sdk/WeComAdapter.js
CHANGED
|
@@ -1,367 +1,367 @@
|
|
|
1
|
-
/* eslint-disable no-undef */
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
3
|
-
// import * as ww from '@wecom/jssdk'
|
|
4
|
-
import ajax from "@lambo-design-mobile/shared/utils/ajax";
|
|
5
|
-
import config from "@lambo-design-mobile/shared/config/config";
|
|
6
|
-
|
|
7
|
-
class WeComAdapter {
|
|
8
|
-
constructor(_options) {
|
|
9
|
-
this.corpId = _options.weComId; // 保存 appId 以便在 getInitInfo 中使用
|
|
10
|
-
// if(!window.wx){
|
|
11
|
-
// // const script = document.createElement('script')
|
|
12
|
-
// // script.src = "//wwcdn.weixin.qq.com/node/open/js/wecom-jssdk-1.3.1.js"
|
|
13
|
-
// // document.head.appendChild(script)
|
|
14
|
-
// const script = document.createElement('script');
|
|
15
|
-
// script.src = "//res.wx.qq.com/open/js/jweixin-1.2.0.js";
|
|
16
|
-
// document.head.appendChild(script);
|
|
17
|
-
// const script1 = document.createElement('script');
|
|
18
|
-
// script1.src = "https://open.work.weixin.qq.com/wwopen/js/jwxwork-1.0.0.js";
|
|
19
|
-
// document.head.appendChild(script1);
|
|
20
|
-
// }
|
|
21
|
-
// this.init( _options)
|
|
22
|
-
function loadScript(src) {
|
|
23
|
-
return new Promise((resolve, reject) => {
|
|
24
|
-
const script = document.createElement('script');
|
|
25
|
-
script.src = src;
|
|
26
|
-
script.onload = () => resolve(script);
|
|
27
|
-
script.onerror = () => reject(new Error('Failed to load script'));
|
|
28
|
-
document.head.appendChild(script);
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
loadScript('//res.wx.qq.com/open/js/jweixin-1.2.0.js')
|
|
33
|
-
.then(script => {
|
|
34
|
-
console.log('Script loaded');
|
|
35
|
-
this.init(_options);
|
|
36
|
-
})
|
|
37
|
-
.catch(err => console.error(err));
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
async init(options) {
|
|
42
|
-
const currentUrl = encodeURIComponent(window.location.href.split('#')[0]);
|
|
43
|
-
ajax.request({
|
|
44
|
-
url: config.upmsServerContext + '/manage/ibpWxCpBaseinfo/gtJsTicket?corpId=' + options.weComId + "&agentId=" + options.agentId + "&url=" + currentUrl,
|
|
45
|
-
method: 'get'
|
|
46
|
-
}).then((resp) => {
|
|
47
|
-
if (resp.data.code === 1) {
|
|
48
|
-
const {data} = resp.data
|
|
49
|
-
wx.config({
|
|
50
|
-
beta: true,
|
|
51
|
-
// debug: true,
|
|
52
|
-
appId: data.corpId, // 必填,企业微信的corpid,必须与当前登录的企业一致
|
|
53
|
-
// agentid: '1000065', // 必填,企业微信的应用id (e.g. 1000247)
|
|
54
|
-
timestamp: data.timestamp, // 必填,生成签名的时间戳
|
|
55
|
-
nonceStr: data.noncestr, // 必填,生成签名的随机串
|
|
56
|
-
signature: data.signature,// 必填,签名,见附录-JS-SDK使用权限签名算法
|
|
57
|
-
jsApiList: ['getLocation', 'scanQRCode', 'chooseImage', 'openLocation', 'getLocalImgData'], // 必填,传入需要使用的接口名称
|
|
58
|
-
success(res) {
|
|
59
|
-
console.log("ready...")
|
|
60
|
-
},
|
|
61
|
-
fail(res) {
|
|
62
|
-
if (res.errMsg.indexOf('function not exist') > -1) {
|
|
63
|
-
alert('版本过低请升级')
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
wx.error(function (res) {
|
|
69
|
-
console.error(res)
|
|
70
|
-
});
|
|
71
|
-
wx.ready(function () {
|
|
72
|
-
console.log("ready.....")
|
|
73
|
-
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
|
|
74
|
-
});
|
|
75
|
-
wx.checkJsApi({
|
|
76
|
-
jsApiList: ['getLocation', 'scanQRCode', 'chooseImage', 'openLocation', 'getLocalImgData'], // 需要检测的JS接口列表
|
|
77
|
-
success(res) {
|
|
78
|
-
// 以键值对的形式返回,可用的api值true,不可用为false
|
|
79
|
-
// 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
}).catch((err) => {
|
|
87
|
-
console.error(err)
|
|
88
|
-
})
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
async getLocation(options) {
|
|
92
|
-
return new Promise((resolve, reject) => {
|
|
93
|
-
wx.ready(() => {
|
|
94
|
-
// 检查 options.type,如果为空或者不含有 'wgs84' 或 'bd09',则设置为 'gcj02'
|
|
95
|
-
let locationType = options.type;
|
|
96
|
-
if (!options.type || (options.type !== 'wgs84' && options.type !== 'bd09')) {
|
|
97
|
-
locationType = 'gcj02';
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const fetchLocation = (type) => {
|
|
101
|
-
return new Promise((resolve, reject) => {
|
|
102
|
-
wx.getLocation({
|
|
103
|
-
...options,
|
|
104
|
-
type,
|
|
105
|
-
success: location => {
|
|
106
|
-
const unifiedLocation = {
|
|
107
|
-
latitude: location.latitude || location.lat || null, // 统一纬度属性
|
|
108
|
-
longitude: location.longitude || location.lng || null, // 统一经度属性
|
|
109
|
-
accuracy: location.accuracy || null // 精确度,如果有的话
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
if (!unifiedLocation.latitude || !unifiedLocation.longitude) {
|
|
113
|
-
return reject(new Error('Invalid location data'));
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
resolve(unifiedLocation);
|
|
117
|
-
},
|
|
118
|
-
fail: err => {
|
|
119
|
-
console.error("getLocation failed:", err);
|
|
120
|
-
reject(err);
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
});
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
if (locationType === 'bd09') {
|
|
127
|
-
// 先获取 gcj02 坐标
|
|
128
|
-
fetchLocation('gcj02').then(unifiedLocation => {
|
|
129
|
-
// 将 gcj02 坐标转换为 bd09
|
|
130
|
-
const [bd09Lng, bd09Lat] = coordtransform.gcj02tobd09(unifiedLocation.longitude, unifiedLocation.latitude);
|
|
131
|
-
unifiedLocation.longitude = bd09Lng;
|
|
132
|
-
unifiedLocation.latitude = bd09Lat;
|
|
133
|
-
resolve(unifiedLocation);
|
|
134
|
-
}).catch(err => {
|
|
135
|
-
reject(err);
|
|
136
|
-
});
|
|
137
|
-
} else {
|
|
138
|
-
fetchLocation(locationType).then(resolve).catch(reject);
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
async openLocation(options) {
|
|
145
|
-
return new Promise((resolve, reject) => {
|
|
146
|
-
wx.ready(() => {
|
|
147
|
-
wx.openLocation({
|
|
148
|
-
latitude: options.latitude,
|
|
149
|
-
longitude: options.longitude,
|
|
150
|
-
name: options.name || '',
|
|
151
|
-
address: options.address || '',
|
|
152
|
-
scale: options.scale || 1,
|
|
153
|
-
infoUrl: options.infoUrl || '',
|
|
154
|
-
success: () => {
|
|
155
|
-
console.log("openLocation success");
|
|
156
|
-
resolve();
|
|
157
|
-
},
|
|
158
|
-
fail: err => {
|
|
159
|
-
console.error("openLocation failed:", err);
|
|
160
|
-
reject(err);
|
|
161
|
-
}
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
async scanCode(options) {
|
|
168
|
-
return new Promise((resolve, reject) => {
|
|
169
|
-
wx.ready(() => {
|
|
170
|
-
wx.scanQRCode({
|
|
171
|
-
...options,
|
|
172
|
-
needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果
|
|
173
|
-
success: (res) => {
|
|
174
|
-
console.log("scanQRCode success:", res);
|
|
175
|
-
resolve(res); // 当needResult 为 1 时,扫码返回的结果
|
|
176
|
-
},
|
|
177
|
-
fail: (err) => {
|
|
178
|
-
console.error("scanQRCode failed:", err);
|
|
179
|
-
reject(err);
|
|
180
|
-
}
|
|
181
|
-
});
|
|
182
|
-
});
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
async takePhoto(options) {
|
|
187
|
-
return new Promise((resolve, reject) => {
|
|
188
|
-
wx.ready(() => {
|
|
189
|
-
wx.chooseImage({
|
|
190
|
-
...options,
|
|
191
|
-
// count: 1, // 默认9,设置为1表示只选择一张图片
|
|
192
|
-
// sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
|
|
193
|
-
// sourceType: ['camera','album'], // 可以指定来源是相册还是相机,默认二者都有
|
|
194
|
-
success: async (res) => {
|
|
195
|
-
const imageInfo = res.localIds[0]; // 返回选定照片的第一个本地ID
|
|
196
|
-
const results = {};
|
|
197
|
-
|
|
198
|
-
// 检查 outputType 是否为空或不包含 'info', 'data', 'oss' 中的任意一个值
|
|
199
|
-
if (!options.outputType || !['info', 'data', 'oss'].some(type => options.outputType.includes(type))) {
|
|
200
|
-
options.outputType = ['info', 'data', 'oss'];
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
if (options.outputType.includes('info')) {
|
|
204
|
-
results.imageInfo = imageInfo;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
if (options.outputType.includes('data')) {
|
|
208
|
-
await new Promise((resolve, reject) => {
|
|
209
|
-
wx.getLocalImgData({
|
|
210
|
-
localId: imageInfo,
|
|
211
|
-
success: (dataRes) => {
|
|
212
|
-
results.imageData = dataRes.localData;
|
|
213
|
-
resolve();
|
|
214
|
-
},
|
|
215
|
-
fail: (err) => {
|
|
216
|
-
console.error("getLocalImgData failed:", err);
|
|
217
|
-
reject(err);
|
|
218
|
-
}
|
|
219
|
-
});
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
if (options.outputType.includes('oss')) {
|
|
224
|
-
await new Promise((resolve, reject) => {
|
|
225
|
-
wx.getLocalImgData({
|
|
226
|
-
localId: imageInfo,
|
|
227
|
-
success: (dataRes) => {
|
|
228
|
-
const fileName = new Date().getTime() + '.png';
|
|
229
|
-
let file;
|
|
230
|
-
if (options.method == 'file') {
|
|
231
|
-
file = this.dataURLtoFile(dataRes.localData, fileName);
|
|
232
|
-
} else {
|
|
233
|
-
file = this.dataUrltoBlob(dataRes.localData, fileName);
|
|
234
|
-
}
|
|
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
|
-
|
|
251
|
-
resolve(results);
|
|
252
|
-
},
|
|
253
|
-
fail: (err) => {
|
|
254
|
-
console.error("chooseImage failed:", err);
|
|
255
|
-
reject(err);
|
|
256
|
-
}
|
|
257
|
-
});
|
|
258
|
-
});
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
dataURLtoFile(dataurl, filename) {
|
|
265
|
-
const arr = dataurl.split(',');
|
|
266
|
-
const mime = arr[0].match(/:(.*?);/)[1];
|
|
267
|
-
const bstr = atob(arr[1]);
|
|
268
|
-
let n = bstr.length;
|
|
269
|
-
const u8arr = new Uint8Array(n);
|
|
270
|
-
while (n--) {
|
|
271
|
-
u8arr[n] = bstr.charCodeAt(n);
|
|
272
|
-
}
|
|
273
|
-
return new File([u8arr], filename, {type: mime});
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
dataUrltoBlob(dataurl, filename) {
|
|
277
|
-
const arr = dataurl.split(',');
|
|
278
|
-
const mime = arr[0].match(/:(.*?);/)[1];
|
|
279
|
-
console.log('base64^^^^^', arr[1])
|
|
280
|
-
console.log('mime is ^^^^^^^', mime)
|
|
281
|
-
return this.base64ToBlob(arr[1], mime)
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
base64ToBlob(base64Data, contentType) {
|
|
285
|
-
contentType = contentType || ''
|
|
286
|
-
const sliceSize = 1024
|
|
287
|
-
const byteCharacters = atob(base64Data)
|
|
288
|
-
const bytesLength = byteCharacters.length
|
|
289
|
-
const slicesCount = Math.ceil(bytesLength / sliceSize)
|
|
290
|
-
const byteArrays = new Array(slicesCount)
|
|
291
|
-
|
|
292
|
-
for (let sliceIndex = 0; sliceIndex < slicesCount; ++sliceIndex) {
|
|
293
|
-
const begin = sliceIndex * sliceSize
|
|
294
|
-
const end = Math.min(begin + sliceSize, bytesLength)
|
|
295
|
-
|
|
296
|
-
const bytes = new Array(end - begin)
|
|
297
|
-
for (let offset = begin, i = 0; offset < end; ++i, ++offset) {
|
|
298
|
-
bytes[i] = byteCharacters[offset].charCodeAt(0)
|
|
299
|
-
}
|
|
300
|
-
byteArrays[sliceIndex] = new Uint8Array(bytes)
|
|
301
|
-
}
|
|
302
|
-
return new Blob(byteArrays, {type: contentType})
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
uploadToOSS(file, options) {
|
|
306
|
-
if (options.method == "file") {
|
|
307
|
-
return new Promise((resolve, reject) => {
|
|
308
|
-
const formData = new FormData();
|
|
309
|
-
formData.append('file', file);
|
|
310
|
-
ajax.post(options.ossServerContext + options.ossImgPutUrl, formData, {payload: true}).then(response => {
|
|
311
|
-
if (response.data.code === 1) {
|
|
312
|
-
const result = response.data.data;
|
|
313
|
-
if (result.length > 0) {
|
|
314
|
-
resolve(result[0]);
|
|
315
|
-
} else {
|
|
316
|
-
reject(new Error('No fileId returned'));
|
|
317
|
-
}
|
|
318
|
-
} else {
|
|
319
|
-
reject(new Error('Upload failed'));
|
|
320
|
-
}
|
|
321
|
-
}).catch(error => {
|
|
322
|
-
reject(error);
|
|
323
|
-
});
|
|
324
|
-
});
|
|
325
|
-
}
|
|
326
|
-
else{
|
|
327
|
-
return new Promise((resolve, reject) => {
|
|
328
|
-
let formData = new FormData()
|
|
329
|
-
console.log('upload to oss file is ^^^^', file)
|
|
330
|
-
formData.append('file', file, file.name || (new Date().getTime() + '.png'))
|
|
331
|
-
for (var value of formData.values()) {
|
|
332
|
-
console.log('upload to oss data is ^^^^', value)
|
|
333
|
-
}
|
|
334
|
-
ajax({
|
|
335
|
-
method: 'post',
|
|
336
|
-
url: options.ossServerContext + options.ossImgPutUrl,
|
|
337
|
-
headers: {'Content-Type': 'multipart/form-data'},
|
|
338
|
-
data: formData,
|
|
339
|
-
payload: true,
|
|
340
|
-
}).then(response => {
|
|
341
|
-
if (response.data.code == 1) {
|
|
342
|
-
const result = response.data.data
|
|
343
|
-
if (result.length > 0) {
|
|
344
|
-
resolve(result[0])
|
|
345
|
-
} else {
|
|
346
|
-
reject(new Error('No fileId returned'))
|
|
347
|
-
}
|
|
348
|
-
} else {
|
|
349
|
-
reject(new Error('Upload failed'))
|
|
350
|
-
}
|
|
351
|
-
}).catch(err => {
|
|
352
|
-
reject(err)
|
|
353
|
-
})
|
|
354
|
-
})
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
async getPlatform(options = {}) {
|
|
359
|
-
// 获取初始化信息
|
|
360
|
-
return {
|
|
361
|
-
appId: this.corpId,
|
|
362
|
-
platform: 'WeCom'
|
|
363
|
-
};
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
export default WeComAdapter;
|
|
1
|
+
/* eslint-disable no-undef */
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
3
|
+
// import * as ww from '@wecom/jssdk'
|
|
4
|
+
import ajax from "@lambo-design-mobile/shared/utils/ajax";
|
|
5
|
+
import config from "@lambo-design-mobile/shared/config/config";
|
|
6
|
+
|
|
7
|
+
class WeComAdapter {
|
|
8
|
+
constructor(_options) {
|
|
9
|
+
this.corpId = _options.weComId; // 保存 appId 以便在 getInitInfo 中使用
|
|
10
|
+
// if(!window.wx){
|
|
11
|
+
// // const script = document.createElement('script')
|
|
12
|
+
// // script.src = "//wwcdn.weixin.qq.com/node/open/js/wecom-jssdk-1.3.1.js"
|
|
13
|
+
// // document.head.appendChild(script)
|
|
14
|
+
// const script = document.createElement('script');
|
|
15
|
+
// script.src = "//res.wx.qq.com/open/js/jweixin-1.2.0.js";
|
|
16
|
+
// document.head.appendChild(script);
|
|
17
|
+
// const script1 = document.createElement('script');
|
|
18
|
+
// script1.src = "https://open.work.weixin.qq.com/wwopen/js/jwxwork-1.0.0.js";
|
|
19
|
+
// document.head.appendChild(script1);
|
|
20
|
+
// }
|
|
21
|
+
// this.init( _options)
|
|
22
|
+
function loadScript(src) {
|
|
23
|
+
return new Promise((resolve, reject) => {
|
|
24
|
+
const script = document.createElement('script');
|
|
25
|
+
script.src = src;
|
|
26
|
+
script.onload = () => resolve(script);
|
|
27
|
+
script.onerror = () => reject(new Error('Failed to load script'));
|
|
28
|
+
document.head.appendChild(script);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
loadScript('//res.wx.qq.com/open/js/jweixin-1.2.0.js')
|
|
33
|
+
.then(script => {
|
|
34
|
+
console.log('Script loaded');
|
|
35
|
+
this.init(_options);
|
|
36
|
+
})
|
|
37
|
+
.catch(err => console.error(err));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
async init(options) {
|
|
42
|
+
const currentUrl = encodeURIComponent(window.location.href.split('#')[0]);
|
|
43
|
+
ajax.request({
|
|
44
|
+
url: config.upmsServerContext + '/manage/ibpWxCpBaseinfo/gtJsTicket?corpId=' + options.weComId + "&agentId=" + options.agentId + "&url=" + currentUrl,
|
|
45
|
+
method: 'get'
|
|
46
|
+
}).then((resp) => {
|
|
47
|
+
if (resp.data.code === 1) {
|
|
48
|
+
const {data} = resp.data
|
|
49
|
+
wx.config({
|
|
50
|
+
beta: true,
|
|
51
|
+
// debug: true,
|
|
52
|
+
appId: data.corpId, // 必填,企业微信的corpid,必须与当前登录的企业一致
|
|
53
|
+
// agentid: '1000065', // 必填,企业微信的应用id (e.g. 1000247)
|
|
54
|
+
timestamp: data.timestamp, // 必填,生成签名的时间戳
|
|
55
|
+
nonceStr: data.noncestr, // 必填,生成签名的随机串
|
|
56
|
+
signature: data.signature,// 必填,签名,见附录-JS-SDK使用权限签名算法
|
|
57
|
+
jsApiList: ['getLocation', 'scanQRCode', 'chooseImage', 'openLocation', 'getLocalImgData'], // 必填,传入需要使用的接口名称
|
|
58
|
+
success(res) {
|
|
59
|
+
console.log("ready...")
|
|
60
|
+
},
|
|
61
|
+
fail(res) {
|
|
62
|
+
if (res.errMsg.indexOf('function not exist') > -1) {
|
|
63
|
+
alert('版本过低请升级')
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
wx.error(function (res) {
|
|
69
|
+
console.error(res)
|
|
70
|
+
});
|
|
71
|
+
wx.ready(function () {
|
|
72
|
+
console.log("ready.....")
|
|
73
|
+
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
|
|
74
|
+
});
|
|
75
|
+
wx.checkJsApi({
|
|
76
|
+
jsApiList: ['getLocation', 'scanQRCode', 'chooseImage', 'openLocation', 'getLocalImgData'], // 需要检测的JS接口列表
|
|
77
|
+
success(res) {
|
|
78
|
+
// 以键值对的形式返回,可用的api值true,不可用为false
|
|
79
|
+
// 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
}).catch((err) => {
|
|
87
|
+
console.error(err)
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async getLocation(options) {
|
|
92
|
+
return new Promise((resolve, reject) => {
|
|
93
|
+
wx.ready(() => {
|
|
94
|
+
// 检查 options.type,如果为空或者不含有 'wgs84' 或 'bd09',则设置为 'gcj02'
|
|
95
|
+
let locationType = options.type;
|
|
96
|
+
if (!options.type || (options.type !== 'wgs84' && options.type !== 'bd09')) {
|
|
97
|
+
locationType = 'gcj02';
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const fetchLocation = (type) => {
|
|
101
|
+
return new Promise((resolve, reject) => {
|
|
102
|
+
wx.getLocation({
|
|
103
|
+
...options,
|
|
104
|
+
type,
|
|
105
|
+
success: location => {
|
|
106
|
+
const unifiedLocation = {
|
|
107
|
+
latitude: location.latitude || location.lat || null, // 统一纬度属性
|
|
108
|
+
longitude: location.longitude || location.lng || null, // 统一经度属性
|
|
109
|
+
accuracy: location.accuracy || null // 精确度,如果有的话
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
if (!unifiedLocation.latitude || !unifiedLocation.longitude) {
|
|
113
|
+
return reject(new Error('Invalid location data'));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
resolve(unifiedLocation);
|
|
117
|
+
},
|
|
118
|
+
fail: err => {
|
|
119
|
+
console.error("getLocation failed:", err);
|
|
120
|
+
reject(err);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
if (locationType === 'bd09') {
|
|
127
|
+
// 先获取 gcj02 坐标
|
|
128
|
+
fetchLocation('gcj02').then(unifiedLocation => {
|
|
129
|
+
// 将 gcj02 坐标转换为 bd09
|
|
130
|
+
const [bd09Lng, bd09Lat] = coordtransform.gcj02tobd09(unifiedLocation.longitude, unifiedLocation.latitude);
|
|
131
|
+
unifiedLocation.longitude = bd09Lng;
|
|
132
|
+
unifiedLocation.latitude = bd09Lat;
|
|
133
|
+
resolve(unifiedLocation);
|
|
134
|
+
}).catch(err => {
|
|
135
|
+
reject(err);
|
|
136
|
+
});
|
|
137
|
+
} else {
|
|
138
|
+
fetchLocation(locationType).then(resolve).catch(reject);
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async openLocation(options) {
|
|
145
|
+
return new Promise((resolve, reject) => {
|
|
146
|
+
wx.ready(() => {
|
|
147
|
+
wx.openLocation({
|
|
148
|
+
latitude: options.latitude,
|
|
149
|
+
longitude: options.longitude,
|
|
150
|
+
name: options.name || '',
|
|
151
|
+
address: options.address || '',
|
|
152
|
+
scale: options.scale || 1,
|
|
153
|
+
infoUrl: options.infoUrl || '',
|
|
154
|
+
success: () => {
|
|
155
|
+
console.log("openLocation success");
|
|
156
|
+
resolve();
|
|
157
|
+
},
|
|
158
|
+
fail: err => {
|
|
159
|
+
console.error("openLocation failed:", err);
|
|
160
|
+
reject(err);
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async scanCode(options) {
|
|
168
|
+
return new Promise((resolve, reject) => {
|
|
169
|
+
wx.ready(() => {
|
|
170
|
+
wx.scanQRCode({
|
|
171
|
+
...options,
|
|
172
|
+
needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果
|
|
173
|
+
success: (res) => {
|
|
174
|
+
console.log("scanQRCode success:", res);
|
|
175
|
+
resolve(res); // 当needResult 为 1 时,扫码返回的结果
|
|
176
|
+
},
|
|
177
|
+
fail: (err) => {
|
|
178
|
+
console.error("scanQRCode failed:", err);
|
|
179
|
+
reject(err);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async takePhoto(options) {
|
|
187
|
+
return new Promise((resolve, reject) => {
|
|
188
|
+
wx.ready(() => {
|
|
189
|
+
wx.chooseImage({
|
|
190
|
+
...options,
|
|
191
|
+
// count: 1, // 默认9,设置为1表示只选择一张图片
|
|
192
|
+
// sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
|
|
193
|
+
// sourceType: ['camera','album'], // 可以指定来源是相册还是相机,默认二者都有
|
|
194
|
+
success: async (res) => {
|
|
195
|
+
const imageInfo = res.localIds[0]; // 返回选定照片的第一个本地ID
|
|
196
|
+
const results = {};
|
|
197
|
+
|
|
198
|
+
// 检查 outputType 是否为空或不包含 'info', 'data', 'oss' 中的任意一个值
|
|
199
|
+
if (!options.outputType || !['info', 'data', 'oss'].some(type => options.outputType.includes(type))) {
|
|
200
|
+
options.outputType = ['info', 'data', 'oss'];
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (options.outputType.includes('info')) {
|
|
204
|
+
results.imageInfo = imageInfo;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (options.outputType.includes('data')) {
|
|
208
|
+
await new Promise((resolve, reject) => {
|
|
209
|
+
wx.getLocalImgData({
|
|
210
|
+
localId: imageInfo,
|
|
211
|
+
success: (dataRes) => {
|
|
212
|
+
results.imageData = dataRes.localData;
|
|
213
|
+
resolve();
|
|
214
|
+
},
|
|
215
|
+
fail: (err) => {
|
|
216
|
+
console.error("getLocalImgData failed:", err);
|
|
217
|
+
reject(err);
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (options.outputType.includes('oss')) {
|
|
224
|
+
await new Promise((resolve, reject) => {
|
|
225
|
+
wx.getLocalImgData({
|
|
226
|
+
localId: imageInfo,
|
|
227
|
+
success: (dataRes) => {
|
|
228
|
+
const fileName = new Date().getTime() + '.png';
|
|
229
|
+
let file;
|
|
230
|
+
if (options.method == 'file') {
|
|
231
|
+
file = this.dataURLtoFile(dataRes.localData, fileName);
|
|
232
|
+
} else {
|
|
233
|
+
file = this.dataUrltoBlob(dataRes.localData, fileName);
|
|
234
|
+
}
|
|
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
|
+
|
|
251
|
+
resolve(results);
|
|
252
|
+
},
|
|
253
|
+
fail: (err) => {
|
|
254
|
+
console.error("chooseImage failed:", err);
|
|
255
|
+
reject(err);
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
dataURLtoFile(dataurl, filename) {
|
|
265
|
+
const arr = dataurl.split(',');
|
|
266
|
+
const mime = arr[0].match(/:(.*?);/)[1];
|
|
267
|
+
const bstr = atob(arr[1]);
|
|
268
|
+
let n = bstr.length;
|
|
269
|
+
const u8arr = new Uint8Array(n);
|
|
270
|
+
while (n--) {
|
|
271
|
+
u8arr[n] = bstr.charCodeAt(n);
|
|
272
|
+
}
|
|
273
|
+
return new File([u8arr], filename, {type: mime});
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
dataUrltoBlob(dataurl, filename) {
|
|
277
|
+
const arr = dataurl.split(',');
|
|
278
|
+
const mime = arr[0].match(/:(.*?);/)[1];
|
|
279
|
+
console.log('base64^^^^^', arr[1])
|
|
280
|
+
console.log('mime is ^^^^^^^', mime)
|
|
281
|
+
return this.base64ToBlob(arr[1], mime)
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
base64ToBlob(base64Data, contentType) {
|
|
285
|
+
contentType = contentType || ''
|
|
286
|
+
const sliceSize = 1024
|
|
287
|
+
const byteCharacters = atob(base64Data)
|
|
288
|
+
const bytesLength = byteCharacters.length
|
|
289
|
+
const slicesCount = Math.ceil(bytesLength / sliceSize)
|
|
290
|
+
const byteArrays = new Array(slicesCount)
|
|
291
|
+
|
|
292
|
+
for (let sliceIndex = 0; sliceIndex < slicesCount; ++sliceIndex) {
|
|
293
|
+
const begin = sliceIndex * sliceSize
|
|
294
|
+
const end = Math.min(begin + sliceSize, bytesLength)
|
|
295
|
+
|
|
296
|
+
const bytes = new Array(end - begin)
|
|
297
|
+
for (let offset = begin, i = 0; offset < end; ++i, ++offset) {
|
|
298
|
+
bytes[i] = byteCharacters[offset].charCodeAt(0)
|
|
299
|
+
}
|
|
300
|
+
byteArrays[sliceIndex] = new Uint8Array(bytes)
|
|
301
|
+
}
|
|
302
|
+
return new Blob(byteArrays, {type: contentType})
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
uploadToOSS(file, options) {
|
|
306
|
+
if (options.method == "file") {
|
|
307
|
+
return new Promise((resolve, reject) => {
|
|
308
|
+
const formData = new FormData();
|
|
309
|
+
formData.append('file', file);
|
|
310
|
+
ajax.post(options.ossServerContext + options.ossImgPutUrl, formData, {payload: true}).then(response => {
|
|
311
|
+
if (response.data.code === 1) {
|
|
312
|
+
const result = response.data.data;
|
|
313
|
+
if (result.length > 0) {
|
|
314
|
+
resolve(result[0]);
|
|
315
|
+
} else {
|
|
316
|
+
reject(new Error('No fileId returned'));
|
|
317
|
+
}
|
|
318
|
+
} else {
|
|
319
|
+
reject(new Error('Upload failed'));
|
|
320
|
+
}
|
|
321
|
+
}).catch(error => {
|
|
322
|
+
reject(error);
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
else{
|
|
327
|
+
return new Promise((resolve, reject) => {
|
|
328
|
+
let formData = new FormData()
|
|
329
|
+
console.log('upload to oss file is ^^^^', file)
|
|
330
|
+
formData.append('file', file, file.name || (new Date().getTime() + '.png'))
|
|
331
|
+
for (var value of formData.values()) {
|
|
332
|
+
console.log('upload to oss data is ^^^^', value)
|
|
333
|
+
}
|
|
334
|
+
ajax({
|
|
335
|
+
method: 'post',
|
|
336
|
+
url: options.ossServerContext + options.ossImgPutUrl,
|
|
337
|
+
headers: {'Content-Type': 'multipart/form-data'},
|
|
338
|
+
data: formData,
|
|
339
|
+
payload: true,
|
|
340
|
+
}).then(response => {
|
|
341
|
+
if (response.data.code == 1) {
|
|
342
|
+
const result = response.data.data
|
|
343
|
+
if (result.length > 0) {
|
|
344
|
+
resolve(result[0])
|
|
345
|
+
} else {
|
|
346
|
+
reject(new Error('No fileId returned'))
|
|
347
|
+
}
|
|
348
|
+
} else {
|
|
349
|
+
reject(new Error('Upload failed'))
|
|
350
|
+
}
|
|
351
|
+
}).catch(err => {
|
|
352
|
+
reject(err)
|
|
353
|
+
})
|
|
354
|
+
})
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
async getPlatform(options = {}) {
|
|
359
|
+
// 获取初始化信息
|
|
360
|
+
return {
|
|
361
|
+
appId: this.corpId,
|
|
362
|
+
platform: 'WeCom'
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export default WeComAdapter;
|