Skip to content

Commit cfec2bc

Browse files
authored
Merge pull request #2262 from didi/feat-camera
RN下支持camera
2 parents 77f7cd5 + a17b077 commit cfec2bc

12 files changed

Lines changed: 476 additions & 4 deletions

File tree

packages/api-proxy/src/platform/api/base/rnCanIUseConfig.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,5 +234,13 @@ export const SUPPORTED_OBJECTS = {
234234
'abort',
235235
'onHeadersReceived',
236236
'offHeadersReceived'
237+
],
238+
239+
// camera
240+
CameraContext: [
241+
'setZoom',
242+
'takePhoto',
243+
'startRecord',
244+
'stopRecord'
237245
]
238246
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import CameraContext from './rnCamera'
2+
3+
function createCameraContext () {
4+
return new CameraContext()
5+
}
6+
7+
export {
8+
createCameraContext
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { noop, getFocusedNavigation } from '@mpxjs/utils'
2+
3+
export default class CameraContext {
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+
}

packages/api-proxy/src/platform/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,6 @@ export * from './api/keyboard'
122122

123123
// getSetting, openSetting, enableAlertBeforeUnload, disableAlertBeforeUnload, getMenuButtonBoundingClientRect
124124
export * from './api/setting'
125+
126+
// createCameraContext
127+
export * from './api/camera'

packages/core/@types/index.d.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,37 @@ export interface RnConfig {
387387
* @platform android
388388
* @default true
389389
*/
390-
enableNativeKeyboardAvoiding?: boolean
390+
enableNativeKeyboardAvoiding?: boolean,
391+
392+
/**
393+
* 自定义蓝牙权限检查函数,用于在调用 openBluetoothAdapter 时替代默认的权限检查逻辑。
394+
*
395+
* Mpx 在 iOS 上默认返回 true(假定权限由系统弹窗处理),在 Android 上会请求 ACCESS_FINE_LOCATION 或 BLUETOOTH_SCAN/CONNECT 权限。
396+
* 如果需要自定义权限申请逻辑(例如在某些定制 Android 设备上),可配置此函数。
397+
*
398+
* @returns Promise<boolean> Resolves 为 true 表示权限获取成功,false 表示失败。
399+
*/
400+
bluetoothPermission?: () => Promise<boolean>
401+
402+
/**
403+
* 自定义 Wi-Fi 权限检查函数,用于在调用 startWifi 时替代默认的权限检查逻辑。
404+
*
405+
* Mpx 在 Android 上默认会请求 ACCESS_FINE_LOCATION 权限。
406+
* 如果需要自定义权限申请逻辑,可配置此函数。
407+
*
408+
* @returns Promise<boolean> Resolves 为 true 表示权限获取成功,false 表示失败。
409+
*/
410+
wifiPermission?: () => Promise<boolean>
411+
412+
/**
413+
* 自定义相机权限检查函数,用于在渲染 Camera 组件前进行权限检查。
414+
*
415+
* 默认情况下,Mpx 会直接渲染 Camera 组件。
416+
* 如果配置了此函数,Camera 组件会等待该函数返回 true 后再进行渲染。
417+
*
418+
* @returns Promise<boolean> Resolves 为 true 表示权限获取成功,false 表示失败。
419+
*/
420+
cameraPermission?: () => Promise<boolean>
391421
}
392422

393423
interface MpxConfig {

packages/webpack-plugin/lib/platform/template/wx/component-config/camera.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ module.exports = function ({ print }) {
1818

1919
return {
2020
test: TAG_NAME,
21+
ios (tag, { el }) {
22+
el.isBuiltIn = true
23+
return 'mpx-camera'
24+
},
25+
android (tag, { el }) {
26+
el.isBuiltIn = true
27+
return 'mpx-camera'
28+
},
29+
harmony (tag, { el }) {
30+
el.isBuiltIn = true
31+
return 'mpx-camera'
32+
},
2133
props: [
2234
{
2335
test: 'mode',

packages/webpack-plugin/lib/platform/template/wx/component-config/unsupported.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const JD_UNSUPPORTED_TAG_NAME_ARR = ['functional-page-navigator', 'live-pusher',
1313
// 快应用不支持的标签集合
1414
const QA_UNSUPPORTED_TAG_NAME_ARR = ['movable-view', 'movable-area', 'open-data', 'official-account', 'editor', 'functional-page-navigator', 'live-player', 'live-pusher', 'ad', 'cover-image']
1515
// RN不支持的标签集合
16-
const RN_UNSUPPORTED_TAG_NAME_ARR = ['open-data', 'official-account', 'editor', 'functional-page-navigator', 'live-player', 'live-pusher', 'ad', 'audio', 'camera', 'match-media', 'page-container', 'editor', 'keyboard-accessory', 'map']
16+
const RN_UNSUPPORTED_TAG_NAME_ARR = ['open-data', 'official-account', 'editor', 'functional-page-navigator', 'live-player', 'live-pusher', 'ad', 'audio', 'match-media', 'page-container', 'editor', 'keyboard-accessory', 'map']
1717

1818
/**
1919
* @param {function(object): function} print

0 commit comments

Comments
 (0)