@mpxjs/api-proxy 2.10.21 → 2.11.0

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/@types/index.d.ts CHANGED
@@ -15,17 +15,12 @@ type AddParam<O, V extends (...args: any) => any> =
15
15
 
16
16
  type PickApiValue<T extends keyof WechatMiniprogram.Wx> = Pick<WechatMiniprogram.Wx, T>[T]
17
17
 
18
- interface GetLocationInterface extends WechatMiniprogram.GetLocationOption {
19
- aliType?: number
20
- }
21
-
22
18
  interface RequestPaymentInterface extends WechatMiniprogram.RequestPaymentOption {
23
19
  tradeNO?: string
24
20
  }
25
21
 
26
22
  declare module '@mpxjs/core' {
27
23
  interface Mpx extends AddPromise<WechatMiniprogram.Wx> {
28
- getLocation: AddParam<GetLocationInterface, PickApiValue<'getLocation'>>
29
24
  requestPayment: AddParam<RequestPaymentInterface, PickApiValue<'requestPayment'>>
30
25
  }
31
26
  }
@@ -34,6 +29,7 @@ export const getProxy: (...args: any) => void
34
29
 
35
30
  export const promisify: (listObj: object, whiteList?: string[], customBlackList?: string[]) => Record<string, any>
36
31
 
32
+ export const getLocation: WechatMiniprogram.Wx['getLocation']
37
33
  export const showActionSheet: WechatMiniprogram.Wx['showActionSheet']
38
34
  export const addPhoneContact: WechatMiniprogram.Wx['addPhoneContact']
39
35
  export const onAppShow: WechatMiniprogram.Wx['onAppShow']
@@ -128,6 +124,8 @@ export const clearStorage: WechatMiniprogram.Wx['clearStorage']
128
124
  export const clearStorageSync: WechatMiniprogram.Wx['clearStorageSync']
129
125
  export const getSystemInfo: WechatMiniprogram.Wx['getSystemInfo']
130
126
  export const getSystemInfoSync: WechatMiniprogram.Wx['getSystemInfoSync']
127
+ export const getDeviceInfo: WechatMiniprogram.Wx['getDeviceInfo']
128
+ export const getLaunchOptionsSync: WechatMiniprogram.Wx['getLaunchOptionsSync']
131
129
  export const getWindowInfo: WechatMiniprogram.Wx['getWindowInfo']
132
130
  export const setTabBarItem: WechatMiniprogram.Wx['setTabBarItem']
133
131
  export const setTabBarStyle: WechatMiniprogram.Wx['setTabBarStyle']
@@ -159,6 +157,10 @@ export const onLocationChange: WechatMiniprogram.Wx['onLocationChange']
159
157
  export const offLocationChange: WechatMiniprogram.Wx['offLocationChange']
160
158
  export const startLocationUpdate: WechatMiniprogram.Wx['startLocationUpdate']
161
159
  export const stopLocationUpdate: WechatMiniprogram.Wx['stopLocationUpdate']
160
+ export const onKeyboardHeightChange: WechatMiniprogram.Wx['onKeyboardHeightChange']
161
+ export const offKeyboardHeightChange: WechatMiniprogram.Wx['offKeyboardHeightChange']
162
+ export const hideKeyboard: WechatMiniprogram.Wx['hideKeyboard']
163
+ export const createCameraContext: WechatMiniprogram.Wx['createCameraContext']
162
164
 
163
165
  declare const install: (...args: any) => any
164
166
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/api-proxy",
3
- "version": "2.10.21",
3
+ "version": "2.11.0",
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.21",
40
+ "@mpxjs/utils": "^2.11.0",
41
41
  "axios": "^1.7.3"
42
42
  },
43
43
  "peerDependencies": {
@@ -76,5 +76,5 @@
76
76
  "optional": true
77
77
  }
78
78
  },
79
- "gitHead": "a2cbdad9f8dcc376f2098d217485525953b64cd9"
79
+ "gitHead": "14548ee8e1f00f6be64f848e4fbdab194d642bf6"
80
80
  }
@@ -10,9 +10,9 @@ let DiscoverPeripheralSubscription = null
10
10
  let updateStateSubscription = null
11
11
  let discovering = false
12
12
  let getDevices = [] // 记录已扫描的设备列表
13
- const deviceFoundCallbacks = []
13
+ let deviceFoundCallback = null // 仅允许一个回调,重复添加会被覆盖
14
+ let characteristicCallback = null // 仅允许一个回调,重复添加会被覆盖
14
15
  const onStateChangeCallbacks = []
15
- const characteristicCallbacks = []
16
16
  const onBLEConnectionStateCallbacks = []
17
17
  let characteristicSubscriptions = {}
18
18
  const connectedDevices = new Set()
@@ -56,7 +56,10 @@ const removeUpdateStateSubscription = function () {
56
56
  updateStateSubscription = null
57
57
  }
58
58
  }
59
- const commonFailHandler = function (errMsg, fail, complete) {
59
+ const commonFailHandler = function (errMsg, fail, complete, reason) {
60
+ if (reason != null) {
61
+ errMsg = errMsg + ' ' + (reason.message != null ? reason.message : String(reason))
62
+ }
60
63
  const result = {
61
64
  errMsg
62
65
  }
@@ -80,12 +83,12 @@ function openBluetoothAdapter (options = {}) {
80
83
  // 先请求权限,再初始化蓝牙管理器
81
84
  bluetoothPermission().then((hasPermissions) => {
82
85
  if (!hasPermissions) {
83
- commonFailHandler('openBluetoothAdapter:fail no permission', fail, complete)
86
+ commonFailHandler('openBluetoothAdapter:fail', fail, complete, 'no permission')
84
87
  return
85
88
  }
86
89
 
87
90
  if (bleManagerInitialized) {
88
- commonFailHandler('openBluetoothAdapter:fail already opened', fail, complete)
91
+ commonFailHandler('openBluetoothAdapter:fail', fail, complete, 'already opened')
89
92
  return
90
93
  }
91
94
 
@@ -103,17 +106,17 @@ function openBluetoothAdapter (options = {}) {
103
106
  success(result)
104
107
  complete(result)
105
108
  } else {
106
- commonFailHandler('openBluetoothAdapter:fail bluetooth not enabled', fail, complete)
109
+ commonFailHandler('openBluetoothAdapter:fail', fail, complete, 'bluetooth not enabled')
107
110
  }
108
111
  }).catch((error) => {
109
- commonFailHandler('openBluetoothAdapter:fail ' + (typeof error === 'string' ? error : ''), fail, complete)
112
+ commonFailHandler('openBluetoothAdapter:fail', fail, complete, error)
110
113
  })
111
114
  }, 1000)
112
115
  }).catch((error) => {
113
- commonFailHandler('openBluetoothAdapter:fail ' + (typeof error === 'string' ? error : ''), fail, complete)
116
+ commonFailHandler('openBluetoothAdapter:fail', fail, complete, error)
114
117
  })
115
- }).catch(() => {
116
- commonFailHandler('openBluetoothAdapter:fail no permission', fail, complete)
118
+ }).catch((error) => {
119
+ commonFailHandler('openBluetoothAdapter:fail', fail, complete, error)
117
120
  })
118
121
  }
119
122
 
@@ -150,9 +153,9 @@ function closeBluetoothAdapter (options = {}) {
150
153
  BleManager.disconnect(id).catch(() => {})
151
154
  })
152
155
  connectedDevices.clear()
153
- deviceFoundCallbacks.length = 0
156
+ deviceFoundCallback = null
154
157
  onStateChangeCallbacks.length = 0
155
- characteristicCallbacks.length = 0
158
+ characteristicCallback = null
156
159
  onBLEConnectionStateCallbacks.length = 0
157
160
  if (valueForCharacteristicSubscriptions) {
158
161
  valueForCharacteristicSubscriptions.remove()
@@ -194,7 +197,7 @@ function startBluetoothDevicesDiscovery (options = {}) {
194
197
  } = options
195
198
 
196
199
  if (!bleManagerInitialized) {
197
- commonFailHandler('startBluetoothDevicesDiscovery:fail ble adapter hans\'t been opened or ble is unavailable.', fail, complete)
200
+ commonFailHandler('startBluetoothDevicesDiscovery:fail', fail, complete, 'ble adapter hans\'t been opened or ble is unavailable.')
198
201
  return
199
202
  }
200
203
  DiscoverPeripheralSubscription = BleManager.onDiscoverPeripheral((device) => {
@@ -219,13 +222,11 @@ function startBluetoothDevicesDiscovery (options = {}) {
219
222
  return
220
223
  }
221
224
  }
222
- deviceFoundCallbacks.forEach(cb => {
223
- if (type(cb) === 'Function') {
224
- cb({
225
- devices: [deviceInfo]
226
- })
227
- }
228
- })
225
+ if (type(deviceFoundCallback) === 'Function') {
226
+ deviceFoundCallback({
227
+ devices: [deviceInfo]
228
+ })
229
+ }
229
230
  getDevices.push(deviceInfo)
230
231
  // 处理设备发现逻辑
231
232
  })
@@ -247,7 +248,7 @@ function startBluetoothDevicesDiscovery (options = {}) {
247
248
  success(result)
248
249
  complete(result)
249
250
  }).catch((error) => {
250
- commonFailHandler('startBluetoothDevicesDiscovery:fail ' + (typeof error === 'string' ? error : ''), fail, complete)
251
+ commonFailHandler('startBluetoothDevicesDiscovery:fail', fail, complete, error)
251
252
  })
252
253
  }
253
254
 
@@ -256,7 +257,7 @@ function stopBluetoothDevicesDiscovery (options = {}) {
256
257
  const { success = noop, fail = noop, complete = noop } = options
257
258
 
258
259
  if (!bleManagerInitialized) {
259
- commonFailHandler('stopBluetoothDevicesDiscovery:fail ble adapter hans\'t been opened or ble is unavailable.', fail, complete)
260
+ commonFailHandler('stopBluetoothDevicesDiscovery:fail', fail, complete, 'ble adapter hans\'t been opened or ble is unavailable.')
260
261
  return
261
262
  }
262
263
  removeBluetoothDevicesDiscovery()
@@ -276,21 +277,20 @@ function stopBluetoothDevicesDiscovery (options = {}) {
276
277
  success(result)
277
278
  complete(result)
278
279
  }).catch((error) => {
279
- commonFailHandler('stopBluetoothDevicesDiscovery:fail ' + (typeof error === 'string' ? error : ''), fail, complete)
280
+ commonFailHandler('stopBluetoothDevicesDiscovery:fail', fail, complete, error)
280
281
  })
281
282
  }
282
283
 
283
284
  function onBluetoothDeviceFound (callback) {
284
- if (deviceFoundCallbacks.indexOf(callback) === -1) {
285
- deviceFoundCallbacks.push(callback)
285
+ if (type(callback) !== 'Function') {
286
+ console.warn('onBluetoothDeviceFound: callback 应为函数,已忽略')
287
+ return
286
288
  }
289
+ deviceFoundCallback = callback // 只允许一个回调,重复添加被最新覆盖
287
290
  }
288
291
 
289
- function offBluetoothDeviceFound (callback) {
290
- const index = deviceFoundCallbacks.indexOf(callback)
291
- if (index > -1) {
292
- deviceFoundCallbacks.splice(index, 1)
293
- }
292
+ function offBluetoothDeviceFound () {
293
+ deviceFoundCallback = null // 移除所有回调(当前仅有一个)
294
294
  }
295
295
 
296
296
  function getConnectedBluetoothDevices (options = {}) {
@@ -298,7 +298,7 @@ function getConnectedBluetoothDevices (options = {}) {
298
298
  const { services = [], success = noop, fail = noop, complete = noop } = options
299
299
 
300
300
  if (!bleManagerInitialized) {
301
- commonFailHandler('getConnectedBluetoothDevices:fail 请先调用 wx.openBluetoothAdapter 接口进行初始化操作', fail, complete)
301
+ commonFailHandler('getConnectedBluetoothDevices:fail', fail, complete, '请先调用 wx.openBluetoothAdapter 接口进行初始化操作')
302
302
  return
303
303
  }
304
304
 
@@ -314,7 +314,7 @@ function getConnectedBluetoothDevices (options = {}) {
314
314
  success(result)
315
315
  complete(result)
316
316
  }).catch((error) => {
317
- commonFailHandler('getConnectedBluetoothDevices:fail ' + (typeof error === 'string' ? error : ''), fail, complete)
317
+ commonFailHandler('getConnectedBluetoothDevices:fail', fail, complete, error)
318
318
  })
319
319
  }
320
320
 
@@ -323,7 +323,7 @@ function getBluetoothAdapterState (options = {}) {
323
323
  const { success = noop, fail = noop, complete = noop } = options
324
324
 
325
325
  if (!bleManagerInitialized) {
326
- commonFailHandler('getBluetoothAdapterState:fail ble adapter need open first.', fail, complete)
326
+ commonFailHandler('getBluetoothAdapterState:fail', fail, complete, 'ble adapter need open first.')
327
327
  return
328
328
  }
329
329
 
@@ -336,7 +336,7 @@ function getBluetoothAdapterState (options = {}) {
336
336
  success(result)
337
337
  complete(result)
338
338
  }).catch((error) => {
339
- commonFailHandler('getBluetoothAdapterState:fail ' + (typeof error === 'string' ? error : ''), fail, complete)
339
+ commonFailHandler('getBluetoothAdapterState:fail', fail, complete, error)
340
340
  })
341
341
  }
342
342
  function onDidUpdateState () {
@@ -366,6 +366,10 @@ function onDidUpdateState () {
366
366
  }
367
367
 
368
368
  function onBluetoothAdapterStateChange (callback) {
369
+ if (type(callback) !== 'Function') {
370
+ console.warn('onBluetoothAdapterStateChange: callback 应为函数,已忽略')
371
+ return
372
+ }
369
373
  if (!updateStateSubscription) {
370
374
  onDidUpdateState()
371
375
  }
@@ -374,12 +378,17 @@ function onBluetoothAdapterStateChange (callback) {
374
378
  }
375
379
  }
376
380
 
381
+ // 注意:微信官方文档标识没有标识 API 不支持传入 callback 参数,但实际测试发现支持传入 callback 进行移除指定回调
377
382
  function offBluetoothAdapterStateChange (callback) {
378
- const index = onStateChangeCallbacks.indexOf(callback)
379
- if (index > -1) {
380
- onStateChangeCallbacks.splice(index, 1)
383
+ if (callback == null) {
384
+ onStateChangeCallbacks.length = 0
385
+ } else {
386
+ const index = onStateChangeCallbacks.indexOf(callback)
387
+ if (index !== -1) {
388
+ onStateChangeCallbacks.splice(index, 1)
389
+ }
381
390
  }
382
- if (deviceFoundCallbacks.length === 0) {
391
+ if (onStateChangeCallbacks.length === 0) {
383
392
  removeUpdateStateSubscription()
384
393
  }
385
394
  }
@@ -416,8 +425,8 @@ function writeBLECharacteristicValue (options = {}) {
416
425
  }
417
426
  let writeTypeValue = writeType
418
427
  if (!writeType) {
419
- // 与小程序拉齐 iOS 未传值的情况优先 write,安卓优先 writeNoResponse 。
420
- writeTypeValue = __mpx_mode__ === 'ios' ? 'write' : 'writeNoResponse'
428
+ // 与小程序拉齐:iOS / 鸿蒙未传值优先 write,安卓优先 writeNoResponse 。
429
+ writeTypeValue = (__mpx_mode__ === 'ios' || __mpx_mode__ === 'harmony') ? 'write' : 'writeNoResponse'
421
430
  }
422
431
  // 将ArrayBuffer转换为byte array
423
432
  const bytes = Array.from(new Uint8Array(value))
@@ -531,10 +540,15 @@ function notifyBLECharacteristicValueChange (options = {}) {
531
540
 
532
541
  let valueForCharacteristicSubscriptions = null
533
542
  function onBLECharacteristicValueChange (callback) {
543
+ if (type(callback) !== 'Function') {
544
+ console.warn('onBLECharacteristicValueChange: callback 应为函数,已忽略')
545
+ return
546
+ }
547
+ characteristicCallback = callback // 只允许一个回调,重复添加被最新覆盖
534
548
  const BleManager = require('react-native-ble-manager').default
535
- if (characteristicCallbacks.length === 0) {
549
+ if (!valueForCharacteristicSubscriptions) {
536
550
  valueForCharacteristicSubscriptions = BleManager.onDidUpdateValueForCharacteristic((data) => {
537
- // 将byte array转换为ArrayBuffer
551
+ if (type(characteristicCallback) !== 'Function') return
538
552
  const buffer = new ArrayBuffer(data.value.length)
539
553
  const view = new Uint8Array(buffer)
540
554
  data.value.forEach((byte, index) => {
@@ -546,24 +560,14 @@ function onBLECharacteristicValueChange (callback) {
546
560
  characteristicId: data.characteristic,
547
561
  value: buffer
548
562
  }
549
- characteristicCallbacks.forEach(cb => {
550
- if (type(cb) === 'Function') {
551
- cb(result)
552
- }
553
- })
563
+ characteristicCallback(result)
554
564
  })
555
565
  }
556
- if (characteristicCallbacks.indexOf(callback) === -1) {
557
- characteristicCallbacks.push(callback)
558
- }
559
566
  }
560
567
 
561
- function offBLECharacteristicValueChange (callback) {
562
- const index = characteristicCallbacks.indexOf(callback)
563
- if (index > -1) {
564
- characteristicCallbacks.splice(index, 1)
565
- }
566
- if (characteristicCallbacks.length === 0 && valueForCharacteristicSubscriptions) {
568
+ function offBLECharacteristicValueChange () {
569
+ characteristicCallback = null
570
+ if (valueForCharacteristicSubscriptions) {
567
571
  valueForCharacteristicSubscriptions.remove()
568
572
  valueForCharacteristicSubscriptions = null
569
573
  }
@@ -572,16 +576,16 @@ function offBLECharacteristicValueChange (callback) {
572
576
  function setBLEMTU (options = {}) {
573
577
  const BleManager = require('react-native-ble-manager').default
574
578
  const { deviceId, mtu, success = noop, fail = noop, complete = noop } = options
575
- if (!mtu) {
576
- commonFailHandler('setBLEMTU:fail parameter error: parameter.mtu should be Number instead of Undefined;', fail, complete)
579
+ if (!deviceId && !mtu) {
580
+ commonFailHandler('setBLEMTU:fail', fail, complete, 'parameter error: parameter.deviceId should be String instead of Undefined;parameter.mtu should be Number instead of Undefined;')
577
581
  return
578
582
  }
579
- if (!deviceId) {
580
- commonFailHandler('setBLEMTU:fail parameter error: parameter.deviceId should be String instead of Undefined;', fail, complete)
583
+ if (!mtu) {
584
+ commonFailHandler('setBLEMTU:fail', fail, complete, 'parameter error: parameter.mtu should be Number instead of Undefined;')
581
585
  return
582
586
  }
583
- if (!deviceId && !mtu) {
584
- commonFailHandler('setBLEMTU:fail parameter error: parameter.deviceId should be String instead of Undefined;parameter.mtu should be Number instead of Undefined;', fail, complete)
587
+ if (!deviceId) {
588
+ commonFailHandler('setBLEMTU:fail', fail, complete, 'parameter error: parameter.deviceId should be String instead of Undefined;')
585
589
  return
586
590
  }
587
591
 
@@ -815,6 +819,10 @@ function closeBLEConnection (options = {}) {
815
819
  }
816
820
 
817
821
  function onBLEConnectionStateChange (callback) {
822
+ if (type(callback) !== 'Function') {
823
+ console.warn('onBLEConnectionStateChange: callback 应为函数,已忽略')
824
+ return
825
+ }
818
826
  if (!updateStateSubscription) {
819
827
  onDidUpdateState()
820
828
  }
@@ -824,9 +832,13 @@ function onBLEConnectionStateChange (callback) {
824
832
  }
825
833
 
826
834
  function offBLEConnectionStateChange (callback) {
827
- const index = onBLEConnectionStateCallbacks.indexOf(callback)
828
- if (index !== -1) {
829
- onBLEConnectionStateCallbacks.splice(index, 1)
835
+ if (callback == null) {
836
+ onBLEConnectionStateCallbacks.length = 0
837
+ } else {
838
+ const index = onBLEConnectionStateCallbacks.indexOf(callback)
839
+ if (index !== -1) {
840
+ onBLEConnectionStateCallbacks.splice(index, 1)
841
+ }
830
842
  }
831
843
  if (onBLEConnectionStateCallbacks.length === 0) {
832
844
  removeUpdateStateSubscription()
@@ -1,6 +1,14 @@
1
1
  import NodeRef from './rnNodesRef'
2
2
  import { warn, noop } from '@mpxjs/utils'
3
3
 
4
+ const HOST_REF = '__mpxHost'
5
+
6
+ function normalizeNodeRef (ref) {
7
+ if (!ref) return ref
8
+ if (ref.__selectRef) return ref.__selectRef(HOST_REF, 'node')
9
+ if (ref.getNodeInstance) return ref
10
+ }
11
+
4
12
  export default class SelectorQuery {
5
13
  constructor () {
6
14
  this._component = null
@@ -34,7 +42,12 @@ export default class SelectorQuery {
34
42
  warn('SelectQuery.select don\'t support combinator selector, it only supports selector like #a or .a or .a.b now.')
35
43
  return new NodeRef([], this, !all)
36
44
  }
37
- const refs = this._component && this._component.__selectRef(selector, 'node', all)
45
+ let refs = this._component && this._component.__selectRef(selector, 'all', all)
46
+ if (Array.isArray(refs)) {
47
+ refs = refs.map(normalizeNodeRef).filter(Boolean)
48
+ } else {
49
+ refs = normalizeNodeRef(refs)
50
+ }
38
51
  return new NodeRef(refs, this, !all)
39
52
  }
40
53