@mpxjs/api-proxy 2.10.16-beta.1 → 2.10.16-beta.10

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/api-proxy",
3
- "version": "2.10.16-beta.1",
3
+ "version": "2.10.16-beta.10",
4
4
  "description": "convert miniprogram API at each end",
5
5
  "module": "src/index.js",
6
6
  "types": "@types/index.d.ts",
@@ -37,7 +37,7 @@
37
37
  },
38
38
  "homepage": "https://github.com/didi/mpx#readme",
39
39
  "dependencies": {
40
- "@mpxjs/utils": "^2.10.16",
40
+ "@mpxjs/utils": "^2.10.16-beta.6",
41
41
  "axios": "^1.7.3"
42
42
  },
43
43
  "peerDependencies": {
@@ -1,5 +1,4 @@
1
- import BleManager from 'react-native-ble-manager'
2
- import { noop } from '@mpxjs/utils'
1
+ import { noop, type } from '@mpxjs/utils'
3
2
  import mpx from '@mpxjs/core'
4
3
  import { Platform, PermissionsAndroid } from 'react-native'
5
4
  import { base64ToArrayBuffer } from '../base/index'
@@ -72,10 +71,11 @@ const commonFailHandler = function (errMsg, fail, complete) {
72
71
  }
73
72
 
74
73
  function openBluetoothAdapter (options = {}) {
74
+ const BleManager = require('react-native-ble-manager').default
75
75
  const { success = noop, fail = noop, complete = noop } = options
76
76
  let bluetoothPermission = requestBluetoothPermission
77
- if (__mpx_env__ === 'android' && mpx.rnConfig?.bluetoothPermission) { // 安卓需要验证权限,开放给用户可以自定义验证权限的方法
78
- bluetoothPermission = mpx.rnConfig.bluetoothPermission
77
+ if (mpx.config?.rnConfig?.bluetoothPermission) { // 安卓需要验证权限,开放给用户可以自定义验证权限的方法
78
+ bluetoothPermission = mpx.config.rnConfig.bluetoothPermission
79
79
  }
80
80
  // 先请求权限,再初始化蓝牙管理器
81
81
  bluetoothPermission().then((hasPermissions) => {
@@ -118,6 +118,7 @@ function openBluetoothAdapter (options = {}) {
118
118
  }
119
119
 
120
120
  function closeBluetoothAdapter (options = {}) {
121
+ const BleManager = require('react-native-ble-manager').default
121
122
  const { success = noop, fail = noop, complete = noop } = options
122
123
  if (!bleManagerInitialized) {
123
124
  const result = {
@@ -183,6 +184,7 @@ function closeBluetoothAdapter (options = {}) {
183
184
  }
184
185
 
185
186
  function startBluetoothDevicesDiscovery (options = {}) {
187
+ const BleManager = require('react-native-ble-manager').default
186
188
  const {
187
189
  services = [],
188
190
  allowDuplicatesKey = false,
@@ -215,19 +217,23 @@ function startBluetoothDevicesDiscovery (options = {}) {
215
217
  }
216
218
  }
217
219
  deviceFoundCallbacks.forEach(cb => {
218
- cb({
219
- devices: [deviceInfo]
220
- })
220
+ if (type(cb) === 'Function') {
221
+ cb({
222
+ devices: [deviceInfo]
223
+ })
224
+ }
221
225
  })
222
226
  getDevices.push(deviceInfo)
223
227
  // 处理设备发现逻辑
224
228
  })
225
229
  BleManager.scan(services, 0, allowDuplicatesKey).then((res) => { // 必须,没有开启扫描,onDiscoverPeripheral回调不会触发
226
230
  onStateChangeCallbacks.forEach(cb => {
227
- cb({
228
- available: true,
229
- discovering: true
230
- })
231
+ if (type(cb) === 'Function') {
232
+ cb({
233
+ available: true,
234
+ discovering: true
235
+ })
236
+ }
231
237
  })
232
238
  discovering = true
233
239
  getDevices = [] // 清空之前的发现设备列表
@@ -243,6 +249,7 @@ function startBluetoothDevicesDiscovery (options = {}) {
243
249
  }
244
250
 
245
251
  function stopBluetoothDevicesDiscovery (options = {}) {
252
+ const BleManager = require('react-native-ble-manager').default
246
253
  const { success = noop, fail = noop, complete = noop } = options
247
254
 
248
255
  if (!bleManagerInitialized) {
@@ -253,10 +260,12 @@ function stopBluetoothDevicesDiscovery (options = {}) {
253
260
  BleManager.stopScan().then(() => {
254
261
  discovering = false
255
262
  onStateChangeCallbacks.forEach(cb => {
256
- cb({
257
- available: true,
258
- discovering: false
259
- })
263
+ if (type(cb) === 'Function') {
264
+ cb({
265
+ available: true,
266
+ discovering: false
267
+ })
268
+ }
260
269
  })
261
270
  const result = {
262
271
  errMsg: 'stopBluetoothDevicesDiscovery:ok'
@@ -282,6 +291,7 @@ function offBluetoothDeviceFound (callback) {
282
291
  }
283
292
 
284
293
  function getConnectedBluetoothDevices (options = {}) {
294
+ const BleManager = require('react-native-ble-manager').default
285
295
  const { services = [], success = noop, fail = noop, complete = noop } = options
286
296
 
287
297
  if (!bleManagerInitialized) {
@@ -306,6 +316,7 @@ function getConnectedBluetoothDevices (options = {}) {
306
316
  }
307
317
 
308
318
  function getBluetoothAdapterState (options = {}) {
319
+ const BleManager = require('react-native-ble-manager').default
309
320
  const { success = noop, fail = noop, complete = noop } = options
310
321
 
311
322
  if (!bleManagerInitialized) {
@@ -326,20 +337,25 @@ function getBluetoothAdapterState (options = {}) {
326
337
  })
327
338
  }
328
339
  function onDidUpdateState () {
340
+ const BleManager = require('react-native-ble-manager').default
329
341
  updateStateSubscription = BleManager.onDidUpdateState((state) => {
330
342
  onStateChangeCallbacks.forEach(cb => {
331
- cb({
332
- available: state.state === 'on',
333
- discovering: state.state === 'on' ? discovering : false
334
- })
343
+ if (type(cb) === 'Function') {
344
+ cb({
345
+ available: state.state === 'on',
346
+ discovering: state.state === 'on' ? discovering : false
347
+ })
348
+ }
335
349
  })
336
350
  if (onBLEConnectionStateCallbacks.length && connectedDeviceId.length && state.state !== 'on') {
337
351
  connectedDeviceId.forEach((id) => {
338
352
  onBLEConnectionStateCallbacks.forEach(cb => {
339
- cb({
340
- deviceId: id,
341
- connected: false
342
- })
353
+ if (type(cb) === 'Function') {
354
+ cb({
355
+ deviceId: id,
356
+ connected: false
357
+ })
358
+ }
343
359
  })
344
360
  })
345
361
  }
@@ -384,6 +400,7 @@ function getBluetoothDevices (options = {}) { // 该能力只是获取应用级
384
400
  }
385
401
 
386
402
  function writeBLECharacteristicValue (options = {}) {
403
+ const BleManager = require('react-native-ble-manager').default
387
404
  const { deviceId, serviceId, characteristicId, value, success = noop, fail = noop, complete = noop } = options
388
405
  if (!deviceId || !serviceId || !characteristicId || !value) {
389
406
  const result = {
@@ -413,6 +430,7 @@ function writeBLECharacteristicValue (options = {}) {
413
430
  }
414
431
 
415
432
  function readBLECharacteristicValue (options = {}) {
433
+ const BleManager = require('react-native-ble-manager').default
416
434
  const { deviceId, serviceId, characteristicId, success = noop, fail = noop, complete = noop } = options
417
435
 
418
436
  if (!deviceId || !serviceId || !characteristicId) {
@@ -449,6 +467,7 @@ function readBLECharacteristicValue (options = {}) {
449
467
  }
450
468
 
451
469
  function notifyBLECharacteristicValueChange (options = {}) {
470
+ const BleManager = require('react-native-ble-manager').default
452
471
  const { deviceId, serviceId, characteristicId, state = true, success = noop, fail = noop, complete = noop } = options
453
472
 
454
473
  if (!deviceId || !serviceId || !characteristicId) {
@@ -502,6 +521,7 @@ function notifyBLECharacteristicValueChange (options = {}) {
502
521
 
503
522
  let valueForCharacteristicSubscriptions = null
504
523
  function onBLECharacteristicValueChange (callback) {
524
+ const BleManager = require('react-native-ble-manager').default
505
525
  if (characteristicCallbacks.length === 0) {
506
526
  valueForCharacteristicSubscriptions = BleManager.onDidUpdateValueForCharacteristic((data) => {
507
527
  // 将byte array转换为ArrayBuffer
@@ -517,7 +537,9 @@ function onBLECharacteristicValueChange (callback) {
517
537
  value: buffer
518
538
  }
519
539
  characteristicCallbacks.forEach(cb => {
520
- cb(result)
540
+ if (type(cb) === 'Function') {
541
+ cb(result)
542
+ }
521
543
  })
522
544
  })
523
545
  }
@@ -538,6 +560,7 @@ function offBLECharacteristicValueChange (callback) {
538
560
  }
539
561
 
540
562
  function setBLEMTU (options = {}) {
563
+ const BleManager = require('react-native-ble-manager').default
541
564
  const { deviceId, mtu, success = noop, fail = noop, complete = noop } = options
542
565
  if (!mtu) {
543
566
  commonFailHandler('setBLEMTU:fail parameter error: parameter.mtu should be Number instead of Undefined;', fail, complete)
@@ -569,6 +592,7 @@ function setBLEMTU (options = {}) {
569
592
  }
570
593
 
571
594
  function getBLEDeviceRSSI (options = {}) {
595
+ const BleManager = require('react-native-ble-manager').default
572
596
  const { deviceId, success = noop, fail = noop, complete = noop } = options
573
597
 
574
598
  if (!deviceId) {
@@ -599,6 +623,7 @@ function getBLEDeviceRSSI (options = {}) {
599
623
  }
600
624
 
601
625
  function getBLEDeviceServices (options = {}) {
626
+ const BleManager = require('react-native-ble-manager').default
602
627
  const { deviceId, success = noop, fail = noop, complete = noop } = options
603
628
 
604
629
  if (!deviceId) {
@@ -671,7 +696,13 @@ function getBLEDeviceCharacteristics (options = {}) {
671
696
  }
672
697
  const characteristics = characteristicsList.map(char => ({
673
698
  uuid: char.characteristic,
674
- properties: char.properties
699
+ properties: {
700
+ read: !!char.properties.Read,
701
+ write: !!char.properties.Write,
702
+ notify: !!char.properties.Notify,
703
+ indicate: !!char.properties.Indicate,
704
+ writeNoResponse: !!char.properties.writeWithoutResponse
705
+ }
675
706
  }))
676
707
 
677
708
  const result = {
@@ -683,6 +714,7 @@ function getBLEDeviceCharacteristics (options = {}) {
683
714
  }
684
715
 
685
716
  function createBLEConnection (options = {}) {
717
+ const BleManager = require('react-native-ble-manager').default
686
718
  const { deviceId, timeout, success = noop, fail = noop, complete = noop } = options
687
719
 
688
720
  if (!deviceId) {
@@ -703,10 +735,12 @@ function createBLEConnection (options = {}) {
703
735
  }
704
736
  clearTimeout(createBLEConnectionTimeout)
705
737
  onBLEConnectionStateCallbacks.forEach(cb => {
706
- cb({
707
- deviceId,
708
- connected: true
709
- })
738
+ if (type(cb) === 'Function') {
739
+ cb({
740
+ deviceId,
741
+ connected: true
742
+ })
743
+ }
710
744
  })
711
745
  connectedDevices.add(deviceId)
712
746
  const result = {
@@ -730,6 +764,7 @@ function createBLEConnection (options = {}) {
730
764
  }
731
765
 
732
766
  function closeBLEConnection (options = {}) {
767
+ const BleManager = require('react-native-ble-manager').default
733
768
  const { deviceId, success = noop, fail = noop, complete = noop } = options
734
769
 
735
770
  if (!deviceId) {
@@ -748,10 +783,12 @@ function closeBLEConnection (options = {}) {
748
783
  connectedDeviceId.splice(index, 1) // 记录一下已连接的设备id
749
784
  }
750
785
  onBLEConnectionStateCallbacks.forEach(cb => {
751
- cb({
752
- deviceId,
753
- connected: false
754
- })
786
+ if (type(cb) === 'Function') {
787
+ cb({
788
+ deviceId,
789
+ connected: false
790
+ })
791
+ }
755
792
  })
756
793
  connectedDevices.delete(deviceId)
757
794
  const result = {
@@ -0,0 +1,9 @@
1
+ import CreateCamera from './rnCamera'
2
+
3
+ function createCameraContext () {
4
+ return new CreateCamera()
5
+ }
6
+
7
+ export {
8
+ createCameraContext
9
+ }
@@ -0,0 +1,7 @@
1
+ import { ENV_OBJ, envError } from '../../../common/js'
2
+
3
+ const createCameraContext = ENV_OBJ.createCameraContext || envError('createCameraContext')
4
+
5
+ export {
6
+ createCameraContext
7
+ }
@@ -0,0 +1,44 @@
1
+ import { noop, getFocusedNavigation } from '@mpxjs/utils'
2
+
3
+ export default class CreateCamera {
4
+ constructor () {
5
+ const navigation = getFocusedNavigation() || {}
6
+ this.camera = navigation.camera || {}
7
+ }
8
+
9
+ setZoom (options = {}) {
10
+ const { zoom, success = noop, fail = noop, complete = noop } = options
11
+ try {
12
+ if (this.camera.setZoom) {
13
+ const result = { errMsg: 'setZoom:ok' }
14
+ success(result)
15
+ complete(result)
16
+ this.camera.setZoom(zoom)
17
+ } else {
18
+ const result = {
19
+ errMsg: 'setZoom:fail camera instance not found'
20
+ }
21
+ fail(result)
22
+ complete(result)
23
+ }
24
+ } catch (error) {
25
+ const result = {
26
+ errMsg: 'setZoom:fail ' + (error?.message || '')
27
+ }
28
+ fail(result)
29
+ complete(result)
30
+ }
31
+ }
32
+
33
+ takePhoto (options) {
34
+ this.camera?.takePhoto(options)
35
+ }
36
+
37
+ startRecord (options) {
38
+ this.camera?.startRecord(options)
39
+ }
40
+
41
+ stopRecord (options) {
42
+ this.camera?.stopRecord(options)
43
+ }
44
+ }
@@ -1,6 +1,5 @@
1
- import WifiManager from 'react-native-wifi-reborn'
2
1
  import { PermissionsAndroid } from 'react-native'
3
- import { noop } from '@mpxjs/utils'
2
+ import { noop, type } from '@mpxjs/utils'
4
3
  import mpx from '@mpxjs/core'
5
4
  let startWifiReady = false
6
5
  const wifiListListeners = []
@@ -22,6 +21,7 @@ async function requestWifiPermission () {
22
21
  }
23
22
 
24
23
  function startWifi (options = {}) {
24
+ const WifiManager = require('react-native-wifi-reborn').default
25
25
  const { success = noop, fail = noop, complete = noop } = options
26
26
  if (__mpx_mode__ === 'ios') {
27
27
  const result = {
@@ -31,10 +31,9 @@ function startWifi (options = {}) {
31
31
  complete(result)
32
32
  return
33
33
  }
34
- startWifiReady = true
35
34
  let wifiPermission = requestWifiPermission
36
- if (mpx.rnConfig?.wifiPermission) {
37
- wifiPermission = mpx.rnConfig.wifiPermission
35
+ if (mpx.config?.rnConfig?.wifiPermission) {
36
+ wifiPermission = mpx.config.rnConfig.wifiPermission
38
37
  }
39
38
  wifiPermission().then(async () => {
40
39
  let enabled
@@ -43,17 +42,21 @@ function startWifi (options = {}) {
43
42
  } catch (e) {
44
43
  enabled = false
45
44
  }
46
- const result = {
47
- errMsg: 'startWifi:success'
48
- }
49
45
  if (!enabled) {
50
- WifiManager.setEnabled(true)
51
- success(result)
52
- complete(result)
53
- } else {
54
- success(result)
46
+ const result = {
47
+ errMsg: 'startWifi:fail wifi not turned on',
48
+ errCode: 12005
49
+ }
50
+ fail(result)
55
51
  complete(result)
52
+ return
56
53
  }
54
+ startWifiReady = true
55
+ const result = {
56
+ errMsg: 'startWifi:success'
57
+ }
58
+ success(result)
59
+ complete(result)
57
60
  }).catch((err) => {
58
61
  const result = {
59
62
  errMsg: 'startWifi:fail ' + (typeof err === 'string' ? err : ''),
@@ -74,7 +77,7 @@ function stopWifi (options = {}) {
74
77
  complete(result)
75
78
  return
76
79
  }
77
- WifiManager.setEnabled(false)
80
+ startWifiReady = false
78
81
  const result = {
79
82
  errMsg: 'stopWifi:success'
80
83
  }
@@ -83,6 +86,7 @@ function stopWifi (options = {}) {
83
86
  }
84
87
 
85
88
  function getWifiList (options = {}) {
89
+ const WifiManager = require('react-native-wifi-reborn').default
86
90
  const { success = noop, fail = noop, complete = noop } = options
87
91
  if (__mpx_mode__ === 'ios') {
88
92
  const result = {
@@ -101,19 +105,20 @@ function getWifiList (options = {}) {
101
105
  complete(result)
102
106
  return
103
107
  }
104
- WifiManager.loadWifiList().then((res) => {
105
- if (wifiListListeners.length) {
106
- const result = res.map(item => {
107
- return {
108
- SSID: item.SSID,
109
- BSSID: item.BSSID,
110
- frequency: item.frequency
111
- }
112
- })
113
- wifiListListeners.forEach(callback => {
114
- callback({ wifiList: result })
115
- })
116
- }
108
+ WifiManager.reScanAndLoadWifiList().then((res) => {
109
+ const wifiList = res.map(item => {
110
+ return {
111
+ SSID: item.SSID,
112
+ BSSID: item.BSSID,
113
+ frequency: item.frequency,
114
+ signalStrength: 100 + (item.level || 0)
115
+ }
116
+ })
117
+ wifiListListeners.forEach(callback => {
118
+ if (type(callback) === 'Function') {
119
+ callback({ wifiList })
120
+ }
121
+ })
117
122
  const result = {
118
123
  errMsg: 'getWifiList:success',
119
124
  errno: 0,
@@ -148,6 +153,7 @@ function offGetWifiList (callback) {
148
153
  }
149
154
 
150
155
  function getConnectedWifi (options = {}) {
156
+ const WifiManager = require('react-native-wifi-reborn').default
151
157
  const { partialInfo = false, success = noop, fail = noop, complete = noop } = options
152
158
 
153
159
  if (!startWifiReady) {
@@ -174,8 +180,7 @@ function getConnectedWifi (options = {}) {
174
180
  }
175
181
  success(result)
176
182
  complete(result)
177
- }).catch((error) => {
178
- console.log(error)
183
+ }).catch(() => {
179
184
  const result = {
180
185
  errMsg: 'getConnectedWifi:fail'
181
186
  }
@@ -122,3 +122,6 @@ export * from './api/keyboard'
122
122
 
123
123
  // getSetting, openSetting, enableAlertBeforeUnload, disableAlertBeforeUnload, getMenuButtonBoundingClientRect
124
124
  export * from './api/setting'
125
+
126
+ // createCameraContext
127
+ export * from './api/camera'