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

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.6",
4
4
  "description": "convert miniprogram API at each end",
5
5
  "module": "src/index.js",
6
6
  "types": "@types/index.d.ts",
@@ -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,114 @@
1
+ import { noop } from '@mpxjs/utils'
2
+
3
+ const qualityValue = {
4
+ high: 90,
5
+ normal: 75,
6
+ low: 50,
7
+ original: 100
8
+ }
9
+ export default class CreateCamera {
10
+ constructor () {
11
+ const navigation = Object.values(global.__mpxPagesMap || {})[0]?.[1]
12
+ this.camera = navigation?.camera || {}
13
+ }
14
+
15
+ setZoom (options = {}) {
16
+ const { zoom } = options
17
+ if (this.camera.setZoom) {
18
+ this.camera.setZoom(zoom)
19
+ }
20
+ }
21
+
22
+ takePhoto (options = {}) {
23
+ const { success = noop, fail = noop, complete = noop } = options
24
+ const takePhoto = this.camera.getTakePhoto?.()
25
+ if (takePhoto) {
26
+ takePhoto({
27
+ quality: qualityValue[options.quality || 'normal']
28
+ }).then((res) => {
29
+ const result = {
30
+ errMsg: 'takePhoto:ok',
31
+ tempImagePath: res.path
32
+ }
33
+ success(result)
34
+ complete(result)
35
+ }).catch(() => {
36
+ const result = {
37
+ errMsg: 'takePhoto:fail'
38
+ }
39
+ fail(result)
40
+ complete(result)
41
+ })
42
+ }
43
+ }
44
+
45
+ startRecord (options = {}) {
46
+ let { timeout = 30, success = noop, fail = noop, complete = noop, timeoutCallback = noop } = options
47
+ timeout = timeout > 300 ? 300 : timeout
48
+ let recordTimer = null
49
+ const isTimeout = false
50
+ const startRecord = this.camera.getStartRecord?.()
51
+ if (startRecord) {
52
+ const result = {
53
+ errMsg: 'startRecord:ok'
54
+ }
55
+ success(result)
56
+ complete(result)
57
+ startRecord({
58
+ onRecordingError: (res) => {
59
+ clearTimeout(recordTimer)
60
+ timeoutCallback()
61
+ },
62
+ onRecordingFinished: (res) => {
63
+ if (isTimeout) {
64
+ console.log('record timeout, ignore', res)
65
+ }
66
+ clearTimeout(recordTimer)
67
+ console.log('record finished', res)
68
+ }
69
+ })
70
+ recordTimer = setTimeout(() => { // 超时自动停止
71
+ if (this.camera.stopRecord) {
72
+ this.camera.stopRecord().catch(() => {})
73
+ }
74
+ }, timeout * 1000)
75
+ } else {
76
+ const result = {
77
+ errMsg: 'startRecord:fail to initialize the camera'
78
+ }
79
+ fail(result)
80
+ complete(result)
81
+ }
82
+ }
83
+
84
+ stopRecord (options = {}) {
85
+ const { success = noop, fail = noop, complete = noop } = options
86
+ const stopRecord = this.camera.getStopRecord?.()
87
+ if (stopRecord) {
88
+ stopRecord().then((res) => {
89
+ console.log('stopRecord res', res)
90
+ const result = {
91
+ errMsg: 'stopRecord:ok',
92
+ tempVideoPath: res.path,
93
+ duration: res.duration * 1000, // 转成ms
94
+ size: res.fileSize
95
+ }
96
+ success(result)
97
+ complete(result)
98
+ }).catch((e) => {
99
+ console.log('stopRecord error', e)
100
+ const result = {
101
+ errMsg: 'stopRecord:fail'
102
+ }
103
+ fail(result)
104
+ complete(result)
105
+ })
106
+ } else {
107
+ const result = {
108
+ errMsg: 'stopRecord:fail to initialize the camera'
109
+ }
110
+ fail(result)
111
+ complete(result)
112
+ }
113
+ }
114
+ }
@@ -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'